maitai-python 1.2.7__tar.gz → 1.2.8b0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/PKG-INFO +11 -3
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_config.py +15 -12
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_config_listener_thread.py +2 -2
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_context.py +12 -9
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_evaluator.py +15 -15
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_inference.py +16 -15
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_maitai.py +16 -12
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_maitai_async.py +16 -12
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_maitai_client.py +2 -1
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_pydantic.py +2 -2
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_types.py +20 -75
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/_utils.py +16 -15
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/tools/__init__.py +1 -2
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_common/config/config_service.py +5 -5
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_common/version.py +1 -1
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_python.egg-info/PKG-INFO +11 -3
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_python.egg-info/SOURCES.txt +0 -20
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_python.egg-info/requires.txt +0 -1
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_python.egg-info/top_level.txt +0 -1
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/setup.py +2 -3
- maitai_python-1.2.7/maitai_gen/__init__.py +0 -0
- maitai_python-1.2.7/maitai_gen/application/__init__.py +0 -65
- maitai_python-1.2.7/maitai_gen/backtest/__init__.py +0 -16
- maitai_python-1.2.7/maitai_gen/billing/__init__.py +0 -75
- maitai_python-1.2.7/maitai_gen/chat/__init__.py +0 -581
- maitai_python-1.2.7/maitai_gen/company/__init__.py +0 -23
- maitai_python-1.2.7/maitai_gen/config/__init__.py +0 -59
- maitai_python-1.2.7/maitai_gen/embedding/__init__.py +0 -25
- maitai_python-1.2.7/maitai_gen/evaluation/__init__.py +0 -70
- maitai_python-1.2.7/maitai_gen/event/__init__.py +0 -19
- maitai_python-1.2.7/maitai_gen/finetuning/__init__.py +0 -88
- maitai_python-1.2.7/maitai_gen/inference/__init__.py +0 -22
- maitai_python-1.2.7/maitai_gen/key/__init__.py +0 -40
- maitai_python-1.2.7/maitai_gen/metric/__init__.py +0 -18
- maitai_python-1.2.7/maitai_gen/model.py +0 -24
- maitai_python-1.2.7/maitai_gen/notification/__init__.py +0 -36
- maitai_python-1.2.7/maitai_gen/rates/__init__.py +0 -39
- maitai_python-1.2.7/maitai_gen/sandbox/__init__.py +0 -44
- maitai_python-1.2.7/maitai_gen/sentinel/__init__.py +0 -77
- maitai_python-1.2.7/maitai_gen/session/__init__.py +0 -41
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/README.md +0 -0
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai/__init__.py +0 -0
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_common/__init__.py +0 -0
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_common/config/__init__.py +0 -0
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_common/processes/__init__.py +0 -0
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_common/processes/io_thread.py +0 -0
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_common/processes/websocket_listener_thread.py +0 -0
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/maitai_python.egg-info/dependency_links.txt +0 -0
- {maitai_python-1.2.7 → maitai_python-1.2.8b0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
2
|
Name: maitai-python
|
|
3
|
-
Version: 1.2.
|
|
3
|
+
Version: 1.2.8b0
|
|
4
4
|
Summary: Maitai SDK for Python
|
|
5
5
|
Home-page: https://docs.trymaitai.ai
|
|
6
6
|
Author: Maitai
|
|
@@ -13,8 +13,16 @@ Requires-Dist: requests
|
|
|
13
13
|
Requires-Dist: openai>=1.34.0
|
|
14
14
|
Requires-Dist: httpx
|
|
15
15
|
Requires-Dist: aiohttp
|
|
16
|
-
Requires-Dist: betterproto==2.0.0b7
|
|
17
16
|
Requires-Dist: websocket-client
|
|
17
|
+
Dynamic: author
|
|
18
|
+
Dynamic: author-email
|
|
19
|
+
Dynamic: classifier
|
|
20
|
+
Dynamic: description
|
|
21
|
+
Dynamic: description-content-type
|
|
22
|
+
Dynamic: home-page
|
|
23
|
+
Dynamic: license
|
|
24
|
+
Dynamic: requires-dist
|
|
25
|
+
Dynamic: summary
|
|
18
26
|
|
|
19
27
|
# Quickstart
|
|
20
28
|
|
|
@@ -4,16 +4,15 @@ import threading
|
|
|
4
4
|
from typing import List
|
|
5
5
|
|
|
6
6
|
import requests
|
|
7
|
-
from betterproto import Casing
|
|
8
7
|
from requests.adapters import HTTPAdapter
|
|
9
8
|
from urllib3 import Retry
|
|
10
9
|
|
|
11
10
|
from maitai._config_listener_thread import ConfigListenerThread
|
|
12
11
|
from maitai._maitai_client import MaitaiClient
|
|
13
12
|
from maitai_common.config import config_service
|
|
14
|
-
from
|
|
15
|
-
from
|
|
16
|
-
from
|
|
13
|
+
from maitai_models.application import Application
|
|
14
|
+
from maitai_models.config import Config as ActionConfig
|
|
15
|
+
from maitai_models.key import Key, KeyMap
|
|
17
16
|
|
|
18
17
|
|
|
19
18
|
class Config(MaitaiClient):
|
|
@@ -22,6 +21,7 @@ class Config(MaitaiClient):
|
|
|
22
21
|
"MAITAI_WS", "wss://09hidyy627.execute-api.us-west-2.amazonaws.com/production"
|
|
23
22
|
)
|
|
24
23
|
config_dir = os.path.expanduser(os.environ.get("MAITAI_CONFIG_DIR", "~/.maitai"))
|
|
24
|
+
refresh_timer = None
|
|
25
25
|
|
|
26
26
|
def __init__(self):
|
|
27
27
|
retry_strategy = Retry(total=3, backoff_factor=1)
|
|
@@ -44,7 +44,6 @@ class Config(MaitaiClient):
|
|
|
44
44
|
anthropic_api_key=Key(id=-1, key_value=os.environ.get("ANTHROPIC_API_KEY")),
|
|
45
45
|
cerebras_api_key=Key(id=-1, key_value=os.environ.get("CEREBRAS_API_KEY")),
|
|
46
46
|
)
|
|
47
|
-
self.refresh_timer = None
|
|
48
47
|
self.refresh_interval = 30 * 60 # 1 hour in seconds
|
|
49
48
|
|
|
50
49
|
@property
|
|
@@ -70,6 +69,10 @@ class Config(MaitaiClient):
|
|
|
70
69
|
def initialize(self, api_key, retry=0, use_async=False):
|
|
71
70
|
if self.initialized and self.api_key == api_key:
|
|
72
71
|
return
|
|
72
|
+
|
|
73
|
+
# Clean up existing threads before creating new ones
|
|
74
|
+
self.cleanup()
|
|
75
|
+
|
|
73
76
|
self.api_key = api_key
|
|
74
77
|
self.initialized = True
|
|
75
78
|
try:
|
|
@@ -79,7 +82,7 @@ class Config(MaitaiClient):
|
|
|
79
82
|
)
|
|
80
83
|
|
|
81
84
|
applications = [
|
|
82
|
-
Application
|
|
85
|
+
Application.model_validate(app_json)
|
|
83
86
|
for app_json in response_json["applications"]
|
|
84
87
|
]
|
|
85
88
|
self._company_id = response_json.get("company_id")
|
|
@@ -90,6 +93,7 @@ class Config(MaitaiClient):
|
|
|
90
93
|
self._initialize_websocket()
|
|
91
94
|
self._start_refresh_timer(use_async=use_async)
|
|
92
95
|
except Exception as e:
|
|
96
|
+
self.cleanup() # Ensure cleanup on failure
|
|
93
97
|
try:
|
|
94
98
|
self._load_config_from_file()
|
|
95
99
|
except Exception:
|
|
@@ -155,9 +159,7 @@ class Config(MaitaiClient):
|
|
|
155
159
|
for ref_name, intent_map in self._application_action_configs.items():
|
|
156
160
|
config_object[ref_name] = {}
|
|
157
161
|
for intent_name, intent_config in intent_map.items():
|
|
158
|
-
config_object[ref_name][intent_name] = intent_config.
|
|
159
|
-
casing=Casing.SNAKE, include_default_values=False
|
|
160
|
-
)
|
|
162
|
+
config_object[ref_name][intent_name] = intent_config.model_dump()
|
|
161
163
|
f.write(json.dumps(config_object, indent=2))
|
|
162
164
|
|
|
163
165
|
def _load_config_from_file(self):
|
|
@@ -168,9 +170,9 @@ class Config(MaitaiClient):
|
|
|
168
170
|
if ref_name not in self._application_action_configs:
|
|
169
171
|
self._application_action_configs[ref_name] = {}
|
|
170
172
|
for intent_name, intent_config in intent_map.items():
|
|
171
|
-
self._application_action_configs[ref_name][
|
|
172
|
-
|
|
173
|
-
|
|
173
|
+
self._application_action_configs[ref_name][intent_name] = (
|
|
174
|
+
ActionConfig.model_validate(intent_config)
|
|
175
|
+
)
|
|
174
176
|
|
|
175
177
|
def _initialize_websocket(self):
|
|
176
178
|
self.config_listener_thread = ConfigListenerThread(
|
|
@@ -186,6 +188,7 @@ class Config(MaitaiClient):
|
|
|
186
188
|
if self.refresh_timer:
|
|
187
189
|
self.refresh_timer.cancel()
|
|
188
190
|
self.refresh_timer = None
|
|
191
|
+
self.initialized = False # Reset initialization state
|
|
189
192
|
|
|
190
193
|
|
|
191
194
|
config = Config()
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import json
|
|
2
2
|
|
|
3
3
|
from maitai_common.processes.websocket_listener_thread import WebsocketListenerThread
|
|
4
|
-
from
|
|
4
|
+
from maitai_models.application import Application
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
class ConfigListenerThread(WebsocketListenerThread):
|
|
@@ -15,7 +15,7 @@ class ConfigListenerThread(WebsocketListenerThread):
|
|
|
15
15
|
application_json = event.get("event_data")
|
|
16
16
|
if application_json:
|
|
17
17
|
try:
|
|
18
|
-
application = Application
|
|
18
|
+
application = Application.model_validate(application_json)
|
|
19
19
|
self.config.store_application_metadata([application])
|
|
20
20
|
except:
|
|
21
21
|
self.config.refresh_applications()
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import json
|
|
2
2
|
|
|
3
3
|
import requests
|
|
4
|
-
from betterproto import Casing
|
|
5
4
|
|
|
6
5
|
from maitai._config import config
|
|
7
6
|
from maitai._utils import __version__ as version
|
|
8
7
|
from maitai._utils import required_args
|
|
9
|
-
from
|
|
8
|
+
from maitai_models.application import ApplicationContext
|
|
10
9
|
|
|
11
10
|
|
|
12
11
|
class ContextManager:
|
|
@@ -27,17 +26,21 @@ class ContextManager:
|
|
|
27
26
|
if context_body and file_path:
|
|
28
27
|
raise ValueError("Only one of context_body and file can be specified")
|
|
29
28
|
|
|
30
|
-
context = ApplicationContext()
|
|
31
|
-
context.reference = reference
|
|
32
29
|
if file_path:
|
|
33
30
|
s3_path = self.upload_context_file(file_path).get("s3_path")
|
|
34
31
|
if not s3_path:
|
|
35
32
|
raise Exception("Failed to upload context")
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
context_type = "FILE"
|
|
34
|
+
context_path = s3_path
|
|
38
35
|
else:
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
context_type = "TEXT"
|
|
37
|
+
context_path = None
|
|
38
|
+
context = ApplicationContext(
|
|
39
|
+
reference=reference,
|
|
40
|
+
context_type=context_type,
|
|
41
|
+
context_path=context_path,
|
|
42
|
+
context_body=context_body,
|
|
43
|
+
)
|
|
41
44
|
|
|
42
45
|
host = config.maitai_host
|
|
43
46
|
url = f"{host}/context/application"
|
|
@@ -52,7 +55,7 @@ class ContextManager:
|
|
|
52
55
|
data=json.dumps(
|
|
53
56
|
{
|
|
54
57
|
"application_ref_name": application,
|
|
55
|
-
"context": context.
|
|
58
|
+
"context": context.model_dump(),
|
|
56
59
|
}
|
|
57
60
|
),
|
|
58
61
|
)
|
|
@@ -11,7 +11,7 @@ from maitai._maitai_client import MaitaiClient
|
|
|
11
11
|
from maitai._types import EvaluateCallback
|
|
12
12
|
from maitai._utils import __version__ as version
|
|
13
13
|
from maitai._utils import chat_completion_chunk_to_response
|
|
14
|
-
from
|
|
14
|
+
from maitai_models.chat import (
|
|
15
15
|
ChatCompletionChunk,
|
|
16
16
|
ChatCompletionParams,
|
|
17
17
|
ChatCompletionResponse,
|
|
@@ -19,7 +19,7 @@ from maitai_gen.chat import (
|
|
|
19
19
|
EvaluateResponse,
|
|
20
20
|
EvaluationContentType,
|
|
21
21
|
)
|
|
22
|
-
from
|
|
22
|
+
from maitai_models.metric import RequestTimingMetric
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
def _get_content_type(content, partial=None):
|
|
@@ -78,18 +78,19 @@ class Evaluator(MaitaiClient):
|
|
|
78
78
|
"evaluation_enabled": True,
|
|
79
79
|
"params": completion_params,
|
|
80
80
|
"return_evaluation": True if callback else False,
|
|
81
|
-
"auth_keys": config.auth_keys.
|
|
81
|
+
"auth_keys": config.auth_keys.model_dump(),
|
|
82
82
|
"metadata": metadata,
|
|
83
83
|
}
|
|
84
84
|
if chat_completion_chunk is not None:
|
|
85
85
|
chat_completion_response = chat_completion_chunk_to_response(
|
|
86
86
|
chat_completion_chunk, content
|
|
87
87
|
)
|
|
88
|
-
print(chat_completion_response)
|
|
89
88
|
eval_request["chat_completion_response"] = (
|
|
90
|
-
chat_completion_response.
|
|
89
|
+
chat_completion_response.model_dump()
|
|
90
|
+
if chat_completion_response
|
|
91
|
+
else None
|
|
91
92
|
)
|
|
92
|
-
eval_request["timing_metrics"] = timing.
|
|
93
|
+
eval_request["timing_metrics"] = timing.model_dump() if timing else None
|
|
93
94
|
await asyncio.create_task(
|
|
94
95
|
cls.send_evaluation_request_async(eval_request, callback)
|
|
95
96
|
)
|
|
@@ -138,7 +139,7 @@ class Evaluator(MaitaiClient):
|
|
|
138
139
|
"evaluation_enabled": True,
|
|
139
140
|
"params": completion_params,
|
|
140
141
|
"return_evaluation": True if callback else False,
|
|
141
|
-
"auth_keys": config.auth_keys.
|
|
142
|
+
"auth_keys": config.auth_keys.model_dump(),
|
|
142
143
|
"metadata": metadata,
|
|
143
144
|
}
|
|
144
145
|
if chat_completion_chunk is not None:
|
|
@@ -146,9 +147,9 @@ class Evaluator(MaitaiClient):
|
|
|
146
147
|
chat_completion_chunk, content
|
|
147
148
|
)
|
|
148
149
|
eval_request["chat_completion_response"] = (
|
|
149
|
-
chat_completion_response.
|
|
150
|
+
chat_completion_response.model_dump()
|
|
150
151
|
)
|
|
151
|
-
eval_request["timing_metrics"] = timing.
|
|
152
|
+
eval_request["timing_metrics"] = timing.model_dump() if timing else None
|
|
152
153
|
|
|
153
154
|
cls.run_async(cls.send_evaluation_request_async(eval_request, callback))
|
|
154
155
|
|
|
@@ -194,14 +195,13 @@ class Evaluator(MaitaiClient):
|
|
|
194
195
|
chat_completion_response=None,
|
|
195
196
|
):
|
|
196
197
|
eval_request = {
|
|
197
|
-
"evaluation_content_type": content_type,
|
|
198
|
+
"evaluation_content_type": content_type.value,
|
|
198
199
|
"application_id": application_id,
|
|
199
200
|
"application_ref_name": application_ref_name,
|
|
200
201
|
"session_id": session_id,
|
|
201
202
|
"reference_id": reference_id,
|
|
202
203
|
"action_type": intent,
|
|
203
204
|
}
|
|
204
|
-
|
|
205
205
|
if content_type == EvaluationContentType.TEXT:
|
|
206
206
|
if not isinstance(content, str):
|
|
207
207
|
raise Exception("Content must be a string")
|
|
@@ -247,11 +247,11 @@ class Evaluator(MaitaiClient):
|
|
|
247
247
|
return None
|
|
248
248
|
return await response.read()
|
|
249
249
|
except Exception as e:
|
|
250
|
-
await cls.increment_error(config.api_key,
|
|
250
|
+
await cls.increment_error(config.api_key, traceback.format_exc(), url)
|
|
251
251
|
|
|
252
252
|
result = await send_request()
|
|
253
253
|
if result is not None:
|
|
254
|
-
eval_result = EvaluateResponse
|
|
254
|
+
eval_result = EvaluateResponse.model_validate_json(result)
|
|
255
255
|
if callback is not None:
|
|
256
256
|
try:
|
|
257
257
|
callback(eval_result)
|
|
@@ -290,7 +290,7 @@ class Evaluator(MaitaiClient):
|
|
|
290
290
|
response.close()
|
|
291
291
|
|
|
292
292
|
for resp in consume_stream():
|
|
293
|
-
inference_response: ChatCompletionChunk =
|
|
294
|
-
resp
|
|
293
|
+
inference_response: ChatCompletionChunk = (
|
|
294
|
+
ChatCompletionChunk.model_validate_json(resp)
|
|
295
295
|
)
|
|
296
296
|
yield inference_response
|
|
@@ -5,16 +5,15 @@ from typing import AsyncIterable, Iterable, Optional
|
|
|
5
5
|
import aiohttp
|
|
6
6
|
import requests
|
|
7
7
|
from aiohttp import ClientTimeout
|
|
8
|
-
from betterproto import Casing
|
|
9
8
|
|
|
10
9
|
from maitai._config import config
|
|
11
10
|
from maitai._maitai_client import MaitaiClient
|
|
12
11
|
from maitai._types import AsyncChunkQueue, ChunkQueue, EvaluateCallback, QueueIterable
|
|
13
12
|
from maitai._utils import __version__ as version
|
|
14
13
|
from maitai._utils import chat_completion_chunk_to_response
|
|
15
|
-
from
|
|
16
|
-
from
|
|
17
|
-
from
|
|
14
|
+
from maitai_models.chat import ChatCompletionChunk, ChatCompletionResponse
|
|
15
|
+
from maitai_models.inference import InferenceStreamResponse
|
|
16
|
+
from maitai_models.metric import RequestTimingMetric
|
|
18
17
|
|
|
19
18
|
|
|
20
19
|
class InferenceException(Exception):
|
|
@@ -88,7 +87,7 @@ class Inference(MaitaiClient):
|
|
|
88
87
|
"apply_corrections": False,
|
|
89
88
|
"evaluation_enabled": False,
|
|
90
89
|
"params": completion_params,
|
|
91
|
-
"auth_keys": config.auth_keys.
|
|
90
|
+
"auth_keys": config.auth_keys.model_dump(),
|
|
92
91
|
"metadata": metadata,
|
|
93
92
|
}
|
|
94
93
|
|
|
@@ -100,9 +99,11 @@ class Inference(MaitaiClient):
|
|
|
100
99
|
chat_storage_request = {
|
|
101
100
|
"chat_completion_request": inference_request,
|
|
102
101
|
"chat_completion_response": (
|
|
103
|
-
chat_completion_response.
|
|
102
|
+
chat_completion_response.model_dump()
|
|
103
|
+
if chat_completion_response
|
|
104
|
+
else None
|
|
104
105
|
),
|
|
105
|
-
"timing_metrics": timing.
|
|
106
|
+
"timing_metrics": timing.model_dump(),
|
|
106
107
|
}
|
|
107
108
|
|
|
108
109
|
cls.run_async(cls.send_storage_request_async(chat_storage_request))
|
|
@@ -132,12 +133,12 @@ class Inference(MaitaiClient):
|
|
|
132
133
|
for line in response.iter_lines():
|
|
133
134
|
if line:
|
|
134
135
|
inference_response: InferenceStreamResponse = (
|
|
135
|
-
InferenceStreamResponse
|
|
136
|
+
InferenceStreamResponse.model_validate_json(line)
|
|
136
137
|
)
|
|
137
138
|
if not inference_response.keep_alive:
|
|
138
139
|
yield inference_response
|
|
139
140
|
except requests.exceptions.RequestException as e:
|
|
140
|
-
cls.log_error(config.api_key,
|
|
141
|
+
cls.log_error(config.api_key, traceback.format_exc(), url)
|
|
141
142
|
raise MaitaiConnectionError(f"Failed to send inference request. Error: {e}")
|
|
142
143
|
|
|
143
144
|
@classmethod
|
|
@@ -185,7 +186,7 @@ class Inference(MaitaiClient):
|
|
|
185
186
|
async for line in response.content:
|
|
186
187
|
if line:
|
|
187
188
|
inference_response: InferenceStreamResponse = (
|
|
188
|
-
InferenceStreamResponse
|
|
189
|
+
InferenceStreamResponse.model_validate_json(line)
|
|
189
190
|
)
|
|
190
191
|
if inference_response.keep_alive:
|
|
191
192
|
continue
|
|
@@ -208,7 +209,7 @@ class Inference(MaitaiClient):
|
|
|
208
209
|
exception = MaitaiConnectionError(
|
|
209
210
|
f"Failed to send inference request: {str(e)}"
|
|
210
211
|
)
|
|
211
|
-
await cls.increment_error(config.api_key,
|
|
212
|
+
await cls.increment_error(config.api_key, traceback.format_exc(), url)
|
|
212
213
|
if chunk_queue:
|
|
213
214
|
chunk_queue.put(exception)
|
|
214
215
|
if async_chunk_queue:
|
|
@@ -233,7 +234,7 @@ class Inference(MaitaiClient):
|
|
|
233
234
|
data=json.dumps(storage_request),
|
|
234
235
|
)
|
|
235
236
|
except Exception as e:
|
|
236
|
-
await cls.increment_error(config.api_key,
|
|
237
|
+
await cls.increment_error(config.api_key, traceback.format_exc(), url)
|
|
237
238
|
|
|
238
239
|
@classmethod
|
|
239
240
|
def store_request_timing_data(cls, metric: RequestTimingMetric):
|
|
@@ -253,7 +254,7 @@ class Inference(MaitaiClient):
|
|
|
253
254
|
connector=aiohttp.TCPConnector(ssl=False)
|
|
254
255
|
) as session:
|
|
255
256
|
return await session.put(
|
|
256
|
-
url, headers=headers, data=metric.
|
|
257
|
+
url, headers=headers, data=metric.model_dump_json()
|
|
257
258
|
)
|
|
258
259
|
except:
|
|
259
260
|
pass
|
|
@@ -296,7 +297,7 @@ class Inference(MaitaiClient):
|
|
|
296
297
|
async for line in response.content:
|
|
297
298
|
if line:
|
|
298
299
|
inference_response: InferenceStreamResponse = (
|
|
299
|
-
InferenceStreamResponse
|
|
300
|
+
InferenceStreamResponse.model_validate_json(line)
|
|
300
301
|
)
|
|
301
302
|
if inference_response.keep_alive:
|
|
302
303
|
continue
|
|
@@ -322,7 +323,7 @@ class Inference(MaitaiClient):
|
|
|
322
323
|
exception = MaitaiConnectionError(
|
|
323
324
|
f"Failed to send inference request: {str(e)}"
|
|
324
325
|
)
|
|
325
|
-
await cls.increment_error(config.api_key,
|
|
326
|
+
await cls.increment_error(config.api_key, traceback.format_exc(), url)
|
|
326
327
|
if chunk_queue:
|
|
327
328
|
chunk_queue.put(exception)
|
|
328
329
|
if async_chunk_queue:
|
|
@@ -35,15 +35,15 @@ from maitai._utils import (
|
|
|
35
35
|
type_to_response_format_param,
|
|
36
36
|
)
|
|
37
37
|
from maitai.tools import Tools
|
|
38
|
-
from
|
|
38
|
+
from maitai_models.chat import (
|
|
39
39
|
AzureParams,
|
|
40
40
|
ChatCompletionParams,
|
|
41
41
|
ClientParams,
|
|
42
42
|
EvaluationContentType,
|
|
43
43
|
)
|
|
44
|
-
from
|
|
45
|
-
from
|
|
46
|
-
from
|
|
44
|
+
from maitai_models.config import InferenceLocations
|
|
45
|
+
from maitai_models.inference import InferenceStreamResponse
|
|
46
|
+
from maitai_models.metric import RequestTimingMetric
|
|
47
47
|
|
|
48
48
|
DEFAULT_MAX_RETRIES = 2
|
|
49
49
|
|
|
@@ -362,14 +362,14 @@ class Completions:
|
|
|
362
362
|
"fallback_model": fallback_model,
|
|
363
363
|
"user_id": user_id,
|
|
364
364
|
"assistant": assistant,
|
|
365
|
-
"auth_keys": config.auth_keys.
|
|
365
|
+
"auth_keys": config.auth_keys.model_dump(),
|
|
366
366
|
"metadata": metadata,
|
|
367
367
|
"safe_mode": safe_mode,
|
|
368
368
|
"fallback_config": fallback_config,
|
|
369
369
|
}
|
|
370
370
|
|
|
371
371
|
if self._client_params is not None:
|
|
372
|
-
request_dict["client_params"] = self._client_params.
|
|
372
|
+
request_dict["client_params"] = self._client_params.model_dump()
|
|
373
373
|
|
|
374
374
|
try:
|
|
375
375
|
timing.time_pre_request = time.time()
|
|
@@ -404,9 +404,11 @@ class Completions:
|
|
|
404
404
|
timing.request_id = resp.chat_completion_response.request_id
|
|
405
405
|
timing.time_request_end = time.time()
|
|
406
406
|
maitai.Inference.store_request_timing_data(timing)
|
|
407
|
-
|
|
408
|
-
resp.chat_completion_response
|
|
407
|
+
completion = MaitaiCompletion.model_validate(
|
|
408
|
+
resp.chat_completion_response.model_dump()
|
|
409
409
|
)
|
|
410
|
+
completion.set_response_format(response_format)
|
|
411
|
+
return completion
|
|
410
412
|
raise InferenceException("No data received")
|
|
411
413
|
except MaitaiConnectionError as e:
|
|
412
414
|
if "gpt" not in model and fallback_model and "gpt" in fallback_model:
|
|
@@ -507,9 +509,11 @@ class Completions:
|
|
|
507
509
|
timing=timing,
|
|
508
510
|
metadata=metadata,
|
|
509
511
|
)
|
|
510
|
-
|
|
511
|
-
maitai_completion
|
|
512
|
+
completion = MaitaiCompletion.model_validate(
|
|
513
|
+
maitai_completion.model_dump()
|
|
512
514
|
)
|
|
515
|
+
completion.set_response_format(response_format)
|
|
516
|
+
return completion
|
|
513
517
|
|
|
514
518
|
|
|
515
519
|
def _process_openai_stream(
|
|
@@ -538,7 +542,7 @@ def _process_openai_stream(
|
|
|
538
542
|
full_body += content
|
|
539
543
|
if maitai_chunk.usage and last_chunk is not None and not last_chunk.usage:
|
|
540
544
|
last_chunk.usage = maitai_chunk.usage
|
|
541
|
-
yield MaitaiChunk(maitai_chunk)
|
|
545
|
+
yield MaitaiChunk.model_validate(maitai_chunk.model_dump())
|
|
542
546
|
timing.time_request_end = time.time()
|
|
543
547
|
if evaluation_enabled:
|
|
544
548
|
full_conv = messages + [{"role": "assistant", "content": full_body}]
|
|
@@ -590,7 +594,7 @@ def _process_inference_stream(
|
|
|
590
594
|
timing.time_first_chunk = time.time()
|
|
591
595
|
timing.request_id = chunk.request_id
|
|
592
596
|
first = False
|
|
593
|
-
yield MaitaiChunk(chunk)
|
|
597
|
+
yield MaitaiChunk.model_validate(chunk.model_dump())
|
|
594
598
|
if chunk.choices and chunk.choices[0].finish_reason:
|
|
595
599
|
timing.time_request_end = time.time()
|
|
596
600
|
maitai.Inference.store_request_timing_data(timing)
|
|
@@ -35,15 +35,15 @@ from maitai._utils import (
|
|
|
35
35
|
type_to_response_format_param,
|
|
36
36
|
)
|
|
37
37
|
from maitai.tools import Tools
|
|
38
|
-
from
|
|
38
|
+
from maitai_models.chat import (
|
|
39
39
|
AzureParams,
|
|
40
40
|
ChatCompletionParams,
|
|
41
41
|
ClientParams,
|
|
42
42
|
EvaluationContentType,
|
|
43
43
|
)
|
|
44
|
-
from
|
|
45
|
-
from
|
|
46
|
-
from
|
|
44
|
+
from maitai_models.config import InferenceLocations
|
|
45
|
+
from maitai_models.inference import InferenceStreamResponse
|
|
46
|
+
from maitai_models.metric import RequestTimingMetric
|
|
47
47
|
|
|
48
48
|
DEFAULT_MAX_RETRIES = 2
|
|
49
49
|
|
|
@@ -360,13 +360,13 @@ class AsyncCompletions:
|
|
|
360
360
|
"fallback_model": fallback_model,
|
|
361
361
|
"user_id": user_id,
|
|
362
362
|
"assistant": assistant,
|
|
363
|
-
"auth_keys": config.auth_keys.
|
|
363
|
+
"auth_keys": config.auth_keys.model_dump(),
|
|
364
364
|
"metadata": metadata,
|
|
365
365
|
"safe_mode": safe_mode,
|
|
366
366
|
}
|
|
367
367
|
|
|
368
368
|
if self._client_params is not None:
|
|
369
|
-
request_dict["client_params"] = self._client_params.
|
|
369
|
+
request_dict["client_params"] = self._client_params.model_dump()
|
|
370
370
|
|
|
371
371
|
try:
|
|
372
372
|
timing.time_pre_request = time.time()
|
|
@@ -401,9 +401,11 @@ class AsyncCompletions:
|
|
|
401
401
|
timing.request_id = resp.chat_completion_response.request_id
|
|
402
402
|
timing.time_request_end = time.time()
|
|
403
403
|
maitai.Inference.store_request_timing_data(timing)
|
|
404
|
-
|
|
405
|
-
resp.chat_completion_response
|
|
404
|
+
completion = MaitaiCompletion.model_validate(
|
|
405
|
+
resp.chat_completion_response.model_dump()
|
|
406
406
|
)
|
|
407
|
+
completion.set_response_format(response_format)
|
|
408
|
+
return completion
|
|
407
409
|
raise InferenceException("No data received")
|
|
408
410
|
except MaitaiConnectionError as e:
|
|
409
411
|
if "gpt" not in model and fallback_model and "gpt" in fallback_model:
|
|
@@ -504,9 +506,11 @@ class AsyncCompletions:
|
|
|
504
506
|
timing=timing,
|
|
505
507
|
metadata=metadata,
|
|
506
508
|
)
|
|
507
|
-
|
|
508
|
-
maitai_completion
|
|
509
|
+
completion = MaitaiCompletion.model_validate(
|
|
510
|
+
maitai_completion.model_dump()
|
|
509
511
|
)
|
|
512
|
+
completion.set_response_format(response_format)
|
|
513
|
+
return completion
|
|
510
514
|
|
|
511
515
|
|
|
512
516
|
async def _process_async_openai_stream(
|
|
@@ -535,7 +539,7 @@ async def _process_async_openai_stream(
|
|
|
535
539
|
full_body += content
|
|
536
540
|
if maitai_chunk.usage and last_chunk is not None and not last_chunk.usage:
|
|
537
541
|
last_chunk.usage = maitai_chunk.usage
|
|
538
|
-
yield MaitaiChunk(maitai_chunk)
|
|
542
|
+
yield MaitaiChunk.model_validate(maitai_chunk.model_dump())
|
|
539
543
|
timing.time_request_end = time.time()
|
|
540
544
|
if last_chunk is None:
|
|
541
545
|
return
|
|
@@ -589,7 +593,7 @@ async def _process_inference_stream_async(
|
|
|
589
593
|
timing.time_first_chunk = time.time()
|
|
590
594
|
timing.request_id = chunk.request_id
|
|
591
595
|
first = False
|
|
592
|
-
yield MaitaiChunk(chunk)
|
|
596
|
+
yield MaitaiChunk.model_validate(chunk.model_dump())
|
|
593
597
|
if chunk.choices and chunk.choices[0].finish_reason:
|
|
594
598
|
timing.time_request_end = time.time()
|
|
595
599
|
maitai.Inference.store_request_timing_data(timing)
|
|
@@ -96,7 +96,8 @@ class MaitaiClient:
|
|
|
96
96
|
|
|
97
97
|
def cleanup(self):
|
|
98
98
|
try:
|
|
99
|
-
|
|
99
|
+
if self._loop.is_running():
|
|
100
|
+
self._loop.run_until_complete(self.close_async_session())
|
|
100
101
|
except Exception as e:
|
|
101
102
|
pass
|
|
102
103
|
try:
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import inspect
|
|
2
|
-
from typing import Any, TypeVar
|
|
2
|
+
from typing import Any, TypeVar, Union
|
|
3
3
|
|
|
4
4
|
import pydantic
|
|
5
5
|
|
|
@@ -26,7 +26,7 @@ def model_json_schema(model: type[_ModelT]) -> dict[str, Any]:
|
|
|
26
26
|
|
|
27
27
|
|
|
28
28
|
def to_strict_json_schema(
|
|
29
|
-
model: type[pydantic.BaseModel]
|
|
29
|
+
model: Union[type[pydantic.BaseModel], pydantic.TypeAdapter[Any]],
|
|
30
30
|
) -> dict[str, Any]:
|
|
31
31
|
if inspect.isclass(model) and is_basemodel_type(model):
|
|
32
32
|
schema = model_json_schema(model)
|