maitai-python 1.2.28b2__tar.gz → 1.2.29__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.28b2 → maitai_python-1.2.29}/PKG-INFO +1 -1
- maitai_python-1.2.29/maitai/__init__.py +7 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_config.py +10 -10
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_context.py +9 -5
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_evaluator.py +17 -16
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_inference.py +45 -23
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_maitai.py +54 -40
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_maitai_async.py +52 -40
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_maitai_client.py +50 -2
- maitai_python-1.2.29/maitai/agent/__init__.py +4 -0
- maitai_python-1.2.29/maitai/agent/_completions.py +122 -0
- maitai_python-1.2.29/maitai/agent/_completions_async.py +122 -0
- maitai_python-1.2.29/maitai/chat/_completions.py +526 -0
- maitai_python-1.2.29/maitai/chat/_completions_async.py +536 -0
- maitai_python-1.2.29/maitai/models/agent/base.py +174 -0
- maitai_python-1.2.29/maitai/models/agent/enum.py +68 -0
- maitai_python-1.2.29/maitai/models/agent/extended.py +1464 -0
- maitai_python-1.2.29/maitai/models/agent_action.py +145 -0
- maitai_python-1.2.29/maitai/models/agent_routing.py +170 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/models/application.py +2 -10
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/models/chat.py +5 -2
- maitai_python-1.2.29/maitai/transcriptions/__init__.py +0 -0
- maitai_python-1.2.29/maitai/transcriptions/_transcriptions.py +290 -0
- maitai_python-1.2.29/maitai/transcriptions/_transcriptions_async.py +292 -0
- maitai_python-1.2.29/maitai_common/__init__.py +0 -0
- maitai_python-1.2.29/maitai_common/config/__init__.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai_common/version.py +1 -1
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai_python.egg-info/PKG-INFO +1 -1
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai_python.egg-info/SOURCES.txt +15 -1
- maitai_python-1.2.28b2/maitai/__init__.py +0 -16
- maitai_python-1.2.28b2/maitai/models/agent.py +0 -26
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/README.md +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_config_listener.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_pydantic.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_types.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/_utils.py +0 -0
- {maitai_python-1.2.28b2/maitai/models → maitai_python-1.2.29/maitai/chat}/__init__.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/exceptions.py +0 -0
- {maitai_python-1.2.28b2/maitai_common → maitai_python-1.2.29/maitai/models}/__init__.py +0 -0
- {maitai_python-1.2.28b2/maitai_common/config → maitai_python-1.2.29/maitai/models/agent}/__init__.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/models/config.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/models/event.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/models/inference.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/models/key.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/models/metric.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/models/sentinel.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai/tools/__init__.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai_common/config/config_service.py +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai_python.egg-info/dependency_links.txt +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai_python.egg-info/requires.txt +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/maitai_python.egg-info/top_level.txt +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/setup.cfg +0 -0
- {maitai_python-1.2.28b2 → maitai_python-1.2.29}/setup.py +0 -0
|
@@ -17,7 +17,8 @@ class Config(MaitaiClient):
|
|
|
17
17
|
config_dir = os.path.expanduser(os.environ.get("MAITAI_CONFIG_DIR", "~/.maitai"))
|
|
18
18
|
refresh_timer = None
|
|
19
19
|
|
|
20
|
-
def __init__(self):
|
|
20
|
+
def __init__(self, api_key=None):
|
|
21
|
+
super().__init__()
|
|
21
22
|
self._api_key = None
|
|
22
23
|
self._company_id = None
|
|
23
24
|
self.websocket_listener_thread = None
|
|
@@ -35,9 +36,14 @@ class Config(MaitaiClient):
|
|
|
35
36
|
deepseek_api_key=Key(id=-1, key_value=os.environ.get("DEEPSEEK_API_KEY")),
|
|
36
37
|
)
|
|
37
38
|
self.refresh_interval = 30 * 60 # 30 minutes in seconds
|
|
39
|
+
if api_key:
|
|
40
|
+
self.initialize(api_key)
|
|
41
|
+
|
|
42
|
+
def __del__(self):
|
|
43
|
+
self.cleanup()
|
|
38
44
|
|
|
39
45
|
@property
|
|
40
|
-
def api_key(self):
|
|
46
|
+
def api_key(self) -> str:
|
|
41
47
|
if self._api_key is None:
|
|
42
48
|
if self.initialized:
|
|
43
49
|
raise ValueError(
|
|
@@ -50,10 +56,11 @@ class Config(MaitaiClient):
|
|
|
50
56
|
"Maitai API Key has not been set. Either pass it directly into the client, or by setting the environment variable MAITAI_API_KEY."
|
|
51
57
|
)
|
|
52
58
|
self.initialize(api_key=api_key)
|
|
59
|
+
self._api_key = api_key
|
|
53
60
|
return self._api_key
|
|
54
61
|
|
|
55
62
|
@api_key.setter
|
|
56
|
-
def api_key(self, value):
|
|
63
|
+
def api_key(self, value: str):
|
|
57
64
|
self._api_key = value
|
|
58
65
|
|
|
59
66
|
def refresh_applications(self):
|
|
@@ -189,10 +196,3 @@ class Config(MaitaiClient):
|
|
|
189
196
|
self.refresh_timer.cancel()
|
|
190
197
|
self.refresh_timer = None
|
|
191
198
|
self.initialized = False
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
config = Config()
|
|
195
|
-
|
|
196
|
-
import atexit
|
|
197
|
-
|
|
198
|
-
atexit.register(config.cleanup)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from maitai._config import
|
|
1
|
+
from maitai._config import Config
|
|
2
2
|
from maitai._maitai_client import MaitaiClient
|
|
3
3
|
from maitai._utils import __version__ as version
|
|
4
4
|
from maitai._utils import required_args
|
|
@@ -6,6 +6,10 @@ from maitai.models.application import ApplicationContext
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
class ContextManager(MaitaiClient):
|
|
9
|
+
def __init__(self, config: Config):
|
|
10
|
+
super().__init__()
|
|
11
|
+
self.config = config
|
|
12
|
+
|
|
9
13
|
@required_args(
|
|
10
14
|
["application", "reference", "context_body"],
|
|
11
15
|
["application", "reference", "file_path"],
|
|
@@ -39,11 +43,11 @@ class ContextManager(MaitaiClient):
|
|
|
39
43
|
context_body=context_body,
|
|
40
44
|
)
|
|
41
45
|
|
|
42
|
-
host = config.maitai_host
|
|
46
|
+
host = self.config.maitai_host
|
|
43
47
|
url = f"{host}/context/application"
|
|
44
48
|
headers = {
|
|
45
49
|
"Content-Type": "application/json",
|
|
46
|
-
"x-api-key": config.api_key,
|
|
50
|
+
"x-api-key": self.config.api_key,
|
|
47
51
|
"User-Agent": version,
|
|
48
52
|
}
|
|
49
53
|
|
|
@@ -61,10 +65,10 @@ class ContextManager(MaitaiClient):
|
|
|
61
65
|
raise RuntimeError(f"Error updating context: {response.text}")
|
|
62
66
|
|
|
63
67
|
def upload_context_file(self, file_path: str):
|
|
64
|
-
host = config.maitai_host
|
|
68
|
+
host = self.config.maitai_host
|
|
65
69
|
url = f"{host}/context/application/file"
|
|
66
70
|
headers = {
|
|
67
|
-
"x-api-key": config.api_key,
|
|
71
|
+
"x-api-key": self.config.api_key,
|
|
68
72
|
"User-Agent": version,
|
|
69
73
|
}
|
|
70
74
|
|
|
@@ -3,7 +3,7 @@ import logging
|
|
|
3
3
|
import traceback
|
|
4
4
|
from typing import Iterable, Optional
|
|
5
5
|
|
|
6
|
-
from maitai._config import
|
|
6
|
+
from maitai._config import Config
|
|
7
7
|
from maitai._maitai_client import MaitaiClient
|
|
8
8
|
from maitai._types import EvaluateCallback
|
|
9
9
|
from maitai._utils import __version__ as version
|
|
@@ -32,8 +32,9 @@ def _get_content_type(content, partial=None):
|
|
|
32
32
|
|
|
33
33
|
class Evaluator(MaitaiClient):
|
|
34
34
|
|
|
35
|
-
def __init__(self):
|
|
35
|
+
def __init__(self, config: Config):
|
|
36
36
|
super().__init__()
|
|
37
|
+
self.config = config
|
|
37
38
|
|
|
38
39
|
async def evaluate_async(
|
|
39
40
|
self,
|
|
@@ -76,7 +77,7 @@ class Evaluator(MaitaiClient):
|
|
|
76
77
|
"evaluation_enabled": True,
|
|
77
78
|
"params": completion_params,
|
|
78
79
|
"return_evaluation": True if callback else False,
|
|
79
|
-
"auth_keys": config.auth_keys.model_dump(),
|
|
80
|
+
"auth_keys": self.config.auth_keys.model_dump(),
|
|
80
81
|
"metadata": metadata,
|
|
81
82
|
}
|
|
82
83
|
if chat_completion_chunk is not None:
|
|
@@ -139,7 +140,7 @@ class Evaluator(MaitaiClient):
|
|
|
139
140
|
"evaluation_enabled": True,
|
|
140
141
|
"params": completion_params,
|
|
141
142
|
"return_evaluation": True if callback else False,
|
|
142
|
-
"auth_keys": config.auth_keys.model_dump(),
|
|
143
|
+
"auth_keys": self.config.auth_keys.model_dump(),
|
|
143
144
|
"metadata": metadata,
|
|
144
145
|
}
|
|
145
146
|
if chat_completion_chunk is not None:
|
|
@@ -221,11 +222,11 @@ class Evaluator(MaitaiClient):
|
|
|
221
222
|
self, eval_request: dict, callback: Optional[EvaluateCallback] = None
|
|
222
223
|
):
|
|
223
224
|
path = "request" if callback else "submit"
|
|
224
|
-
host = config.maitai_host
|
|
225
|
+
host = self.config.maitai_host
|
|
225
226
|
url = f"{host}/evaluation/{path}"
|
|
226
227
|
headers = {
|
|
227
228
|
"Content-Type": "application/json",
|
|
228
|
-
"x-api-key": config.api_key,
|
|
229
|
+
"x-api-key": self.config.api_key,
|
|
229
230
|
"User-Agent": version,
|
|
230
231
|
}
|
|
231
232
|
try:
|
|
@@ -233,10 +234,10 @@ class Evaluator(MaitaiClient):
|
|
|
233
234
|
response = client.post(url, headers=headers, json=eval_request)
|
|
234
235
|
if response.status_code != 200:
|
|
235
236
|
error_text = response.text
|
|
236
|
-
self.log_error(config.api_key, error_text, url)
|
|
237
|
+
self.log_error(self.config.api_key, error_text, url)
|
|
237
238
|
result = response.content
|
|
238
239
|
except Exception as e:
|
|
239
|
-
self.log_error(config.api_key, traceback.format_exc(), url)
|
|
240
|
+
self.log_error(self.config.api_key, traceback.format_exc(), url)
|
|
240
241
|
return None
|
|
241
242
|
if result is not None:
|
|
242
243
|
eval_result = EvaluateResponse.model_validate_json(result)
|
|
@@ -252,24 +253,24 @@ class Evaluator(MaitaiClient):
|
|
|
252
253
|
self, eval_request: dict, callback: Optional[EvaluateCallback] = None
|
|
253
254
|
):
|
|
254
255
|
path = "request" if callback else "submit"
|
|
255
|
-
host = config.maitai_host
|
|
256
|
+
host = self.config.maitai_host
|
|
256
257
|
url = f"{host}/evaluation/{path}"
|
|
257
258
|
headers = {
|
|
258
259
|
"Content-Type": "application/json",
|
|
259
|
-
"x-api-key": config.api_key,
|
|
260
|
+
"x-api-key": self.config.api_key,
|
|
260
261
|
"User-Agent": version,
|
|
261
262
|
}
|
|
262
263
|
|
|
263
264
|
try:
|
|
264
|
-
client =
|
|
265
|
+
client = self.get_async_client()
|
|
265
266
|
response = await client.post(url, headers=headers, json=eval_request)
|
|
266
267
|
if response.status_code != 200:
|
|
267
268
|
error_text = response.text
|
|
268
|
-
await self.log_error_async(config.api_key, error_text, url)
|
|
269
|
+
await self.log_error_async(self.config.api_key, error_text, url)
|
|
269
270
|
return None
|
|
270
271
|
result = response.content
|
|
271
272
|
except Exception as e:
|
|
272
|
-
await self.log_error_async(config.api_key, traceback.format_exc(), url)
|
|
273
|
+
await self.log_error_async(self.config.api_key, traceback.format_exc(), url)
|
|
273
274
|
return None
|
|
274
275
|
|
|
275
276
|
if result is not None:
|
|
@@ -286,11 +287,11 @@ class Evaluator(MaitaiClient):
|
|
|
286
287
|
self, eval_request: dict
|
|
287
288
|
) -> Iterable[ChatCompletionChunk]:
|
|
288
289
|
def consume_stream():
|
|
289
|
-
host = config.maitai_host
|
|
290
|
+
host = self.config.maitai_host
|
|
290
291
|
url = f"{host}/evaluation/stream_correction"
|
|
291
292
|
headers = {
|
|
292
293
|
"Content-Type": "application/json",
|
|
293
|
-
"x-api-key": config.api_key,
|
|
294
|
+
"x-api-key": self.config.api_key,
|
|
294
295
|
"User-Agent": version,
|
|
295
296
|
}
|
|
296
297
|
|
|
@@ -302,7 +303,7 @@ class Evaluator(MaitaiClient):
|
|
|
302
303
|
logger.error(
|
|
303
304
|
f"Failed to send stream correction request. Status code: {response.status_code}. Error: {response.text}"
|
|
304
305
|
)
|
|
305
|
-
self.log_error(config.api_key, response.text, url)
|
|
306
|
+
self.log_error(self.config.api_key, response.text, url)
|
|
306
307
|
return
|
|
307
308
|
try:
|
|
308
309
|
for line in response.iter_lines():
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
import inspect
|
|
3
3
|
import json
|
|
4
|
+
import logging
|
|
4
5
|
import traceback
|
|
5
6
|
from typing import AsyncIterable, Iterable, Optional
|
|
6
7
|
|
|
7
8
|
import httpx
|
|
8
9
|
|
|
9
|
-
from maitai._config import
|
|
10
|
+
from maitai._config import Config
|
|
10
11
|
from maitai._maitai_client import MaitaiClient
|
|
11
12
|
from maitai._types import AsyncChunkQueue, ChunkQueue, EvaluateCallback, QueueIterable
|
|
12
13
|
from maitai._utils import __version__ as version
|
|
@@ -16,11 +17,14 @@ from maitai.models.chat import ChatCompletionChunk, ChatCompletionResponse
|
|
|
16
17
|
from maitai.models.inference import InferenceStreamResponse
|
|
17
18
|
from maitai.models.metric import RequestTimingMetric
|
|
18
19
|
|
|
20
|
+
logger = logging.getLogger("maitai")
|
|
21
|
+
|
|
19
22
|
|
|
20
23
|
class Inference(MaitaiClient):
|
|
21
24
|
|
|
22
|
-
def __init__(self):
|
|
25
|
+
def __init__(self, config: Config):
|
|
23
26
|
super().__init__()
|
|
27
|
+
self.config = config
|
|
24
28
|
|
|
25
29
|
def infer(
|
|
26
30
|
self, request_dict: dict, evaluate_callback, timeout
|
|
@@ -78,11 +82,11 @@ class Inference(MaitaiClient):
|
|
|
78
82
|
request_dict: dict,
|
|
79
83
|
timeout,
|
|
80
84
|
) -> Iterable[InferenceStreamResponse]:
|
|
81
|
-
host = config.
|
|
85
|
+
host = self.config.maitai_inference_host
|
|
82
86
|
url = f"{host}/chat/completions/serialized"
|
|
83
87
|
headers = {
|
|
84
88
|
"Content-Type": "application/json",
|
|
85
|
-
"x-api-key": config.api_key,
|
|
89
|
+
"x-api-key": self.config.api_key,
|
|
86
90
|
"User-Agent": version,
|
|
87
91
|
}
|
|
88
92
|
|
|
@@ -118,7 +122,7 @@ class Inference(MaitaiClient):
|
|
|
118
122
|
exception = MaitaiConnectionError(
|
|
119
123
|
f"Failed to send inference request. Error: {e}"
|
|
120
124
|
)
|
|
121
|
-
self.log_error(config.api_key, traceback.format_exc(), url)
|
|
125
|
+
self.log_error(self.config.api_key, traceback.format_exc(), url)
|
|
122
126
|
raise exception from None
|
|
123
127
|
|
|
124
128
|
async def send_inference_request_with_callback_async(
|
|
@@ -149,22 +153,32 @@ class Inference(MaitaiClient):
|
|
|
149
153
|
request_dict: dict,
|
|
150
154
|
timeout,
|
|
151
155
|
):
|
|
152
|
-
host = config.
|
|
156
|
+
host = self.config.maitai_inference_host
|
|
153
157
|
url = f"{host}/chat/completions/serialized"
|
|
154
158
|
headers = {
|
|
155
159
|
"Content-Type": "application/json",
|
|
156
|
-
"x-api-key": config.api_key,
|
|
160
|
+
"x-api-key": self.config.api_key,
|
|
157
161
|
"User-Agent": version,
|
|
158
162
|
}
|
|
159
163
|
|
|
164
|
+
# Log the request being made
|
|
165
|
+
logger.info(f"Making inference request to {url} with timeout={timeout}")
|
|
166
|
+
logger.debug(f"Request payload keys: {list(request_dict.keys())}")
|
|
167
|
+
|
|
160
168
|
try:
|
|
161
|
-
client =
|
|
169
|
+
client = self.get_async_client()
|
|
162
170
|
# Using a context manager here but properly handling exit even on exceptions
|
|
163
171
|
async with client.stream(
|
|
164
172
|
"POST", url, headers=headers, json=request_dict, timeout=timeout
|
|
165
173
|
) as response:
|
|
174
|
+
# Log response status immediately
|
|
175
|
+
logger.info(
|
|
176
|
+
f"Inference request response status: {response.status_code}, headers: {dict(response.headers)}"
|
|
177
|
+
)
|
|
166
178
|
try:
|
|
179
|
+
line_count = 0
|
|
167
180
|
async for line in response.aiter_lines():
|
|
181
|
+
line_count += 1
|
|
168
182
|
if line:
|
|
169
183
|
build_exception(response, line)
|
|
170
184
|
inference_response: InferenceStreamResponse = (
|
|
@@ -173,8 +187,16 @@ class Inference(MaitaiClient):
|
|
|
173
187
|
if inference_response.keep_alive:
|
|
174
188
|
continue
|
|
175
189
|
yield inference_response
|
|
190
|
+
if line_count == 0:
|
|
191
|
+
logger.warning(
|
|
192
|
+
f"No lines received in inference response (status: {response.status_code})"
|
|
193
|
+
)
|
|
176
194
|
except Exception as e:
|
|
177
195
|
# Let the context manager properly close the stream before re-raising
|
|
196
|
+
logger.error(
|
|
197
|
+
f"Error processing inference response lines: {type(e).__name__}: {str(e)}",
|
|
198
|
+
exc_info=True,
|
|
199
|
+
)
|
|
178
200
|
raise e
|
|
179
201
|
except TimeoutError as e:
|
|
180
202
|
raise
|
|
@@ -188,7 +210,7 @@ class Inference(MaitaiClient):
|
|
|
188
210
|
exception = MaitaiConnectionError(
|
|
189
211
|
f"Failed to send inference request: {str(e)}"
|
|
190
212
|
)
|
|
191
|
-
await self.log_error_async(config.api_key, traceback.format_exc(), url)
|
|
213
|
+
await self.log_error_async(self.config.api_key, traceback.format_exc(), url)
|
|
192
214
|
raise exception from None
|
|
193
215
|
|
|
194
216
|
def store_chat_response(
|
|
@@ -212,7 +234,7 @@ class Inference(MaitaiClient):
|
|
|
212
234
|
"apply_corrections": False,
|
|
213
235
|
"evaluation_enabled": False,
|
|
214
236
|
"params": completion_params,
|
|
215
|
-
"auth_keys": config.auth_keys.model_dump(),
|
|
237
|
+
"auth_keys": self.config.auth_keys.model_dump(),
|
|
216
238
|
"metadata": metadata,
|
|
217
239
|
}
|
|
218
240
|
|
|
@@ -254,7 +276,7 @@ class Inference(MaitaiClient):
|
|
|
254
276
|
"apply_corrections": False,
|
|
255
277
|
"evaluation_enabled": False,
|
|
256
278
|
"params": completion_params,
|
|
257
|
-
"auth_keys": config.auth_keys.model_dump(),
|
|
279
|
+
"auth_keys": self.config.auth_keys.model_dump(),
|
|
258
280
|
"metadata": metadata,
|
|
259
281
|
}
|
|
260
282
|
|
|
@@ -276,48 +298,48 @@ class Inference(MaitaiClient):
|
|
|
276
298
|
asyncio.create_task(self.send_storage_request_async(chat_storage_request))
|
|
277
299
|
|
|
278
300
|
def send_storage_request(self, storage_request: dict):
|
|
279
|
-
host = config.
|
|
301
|
+
host = self.config.maitai_inference_host
|
|
280
302
|
url = f"{host}/chat/completions/response"
|
|
281
303
|
headers = {
|
|
282
304
|
"Content-Type": "application/json",
|
|
283
|
-
"x-api-key": config.api_key,
|
|
305
|
+
"x-api-key": self.config.api_key,
|
|
284
306
|
"User-Agent": version,
|
|
285
307
|
}
|
|
286
308
|
try:
|
|
287
309
|
client = self.get_client()
|
|
288
310
|
return client.put(url, headers=headers, json=storage_request)
|
|
289
311
|
except:
|
|
290
|
-
self.log_error(config.api_key, traceback.format_exc(), url)
|
|
312
|
+
self.log_error(self.config.api_key, traceback.format_exc(), url)
|
|
291
313
|
|
|
292
314
|
async def send_storage_request_async(self, storage_request: dict):
|
|
293
|
-
host = config.
|
|
315
|
+
host = self.config.maitai_inference_host
|
|
294
316
|
url = f"{host}/chat/completions/response"
|
|
295
317
|
headers = {
|
|
296
318
|
"Content-Type": "application/json",
|
|
297
|
-
"x-api-key": config.api_key,
|
|
319
|
+
"x-api-key": self.config.api_key,
|
|
298
320
|
"User-Agent": version,
|
|
299
321
|
}
|
|
300
322
|
try:
|
|
301
|
-
client =
|
|
323
|
+
client = self.get_async_client()
|
|
302
324
|
return await client.put(
|
|
303
325
|
url,
|
|
304
326
|
headers=headers,
|
|
305
327
|
json=storage_request,
|
|
306
328
|
)
|
|
307
329
|
except Exception as e:
|
|
308
|
-
await self.log_error_async(config.api_key, traceback.format_exc(), url)
|
|
330
|
+
await self.log_error_async(self.config.api_key, traceback.format_exc(), url)
|
|
309
331
|
|
|
310
332
|
def store_request_timing_data(self, metric: RequestTimingMetric):
|
|
311
333
|
self.executor.submit(self._store_request_timing_data, metric)
|
|
312
334
|
|
|
313
335
|
def _store_request_timing_data(self, metric: RequestTimingMetric):
|
|
314
|
-
host = config.maitai_host
|
|
336
|
+
host = self.config.maitai_host
|
|
315
337
|
if "/batch" in host:
|
|
316
338
|
host = host.replace("/batch", "")
|
|
317
339
|
url = f"{host}/metrics/timing"
|
|
318
340
|
headers = {
|
|
319
341
|
"Content-Type": "application/json",
|
|
320
|
-
"x-api-key": config.api_key,
|
|
342
|
+
"x-api-key": self.config.api_key,
|
|
321
343
|
"User-Agent": version,
|
|
322
344
|
}
|
|
323
345
|
try:
|
|
@@ -329,17 +351,17 @@ class Inference(MaitaiClient):
|
|
|
329
351
|
pass
|
|
330
352
|
|
|
331
353
|
async def send_request_timing_data_async(self, metric: RequestTimingMetric):
|
|
332
|
-
host = config.maitai_host
|
|
354
|
+
host = self.config.maitai_host
|
|
333
355
|
if "/batch" in host:
|
|
334
356
|
host = host.replace("/batch", "")
|
|
335
357
|
url = f"{host}/metrics/timing"
|
|
336
358
|
headers = {
|
|
337
359
|
"Content-Type": "application/json",
|
|
338
|
-
"x-api-key": config.api_key,
|
|
360
|
+
"x-api-key": self.config.api_key,
|
|
339
361
|
"User-Agent": version,
|
|
340
362
|
}
|
|
341
363
|
try:
|
|
342
|
-
client =
|
|
364
|
+
client = self.get_async_client()
|
|
343
365
|
return await client.put(
|
|
344
366
|
url, headers=headers, timeout=10, json=metric.model_dump()
|
|
345
367
|
)
|
|
@@ -6,7 +6,9 @@ import groq
|
|
|
6
6
|
import httpx
|
|
7
7
|
import openai
|
|
8
8
|
|
|
9
|
-
from maitai._config import
|
|
9
|
+
from maitai._config import Config
|
|
10
|
+
from maitai._context import ContextManager
|
|
11
|
+
from maitai.agent._completions import Completions as AgentCompletions
|
|
10
12
|
from maitai.chat._completions import Completions
|
|
11
13
|
from maitai.models.chat import AzureParams, ClientParams
|
|
12
14
|
from maitai.models.key import Key
|
|
@@ -46,9 +48,7 @@ class Maitai:
|
|
|
46
48
|
):
|
|
47
49
|
if not maitai_api_key:
|
|
48
50
|
maitai_api_key = os.environ.get("MAITAI_API_KEY")
|
|
49
|
-
if maitai_api_key:
|
|
50
|
-
config.initialize(maitai_api_key)
|
|
51
|
-
else:
|
|
51
|
+
if not maitai_api_key:
|
|
52
52
|
raise ValueError(
|
|
53
53
|
"MAITAI_API_KEY has not been set. Either set via environment variable or pass it directly into the client."
|
|
54
54
|
)
|
|
@@ -56,62 +56,63 @@ class Maitai:
|
|
|
56
56
|
raise ValueError(
|
|
57
57
|
"api_key is deprecated. Use provider specific keys instead. Supported variables: openai_api_key, groq_api_key, anthropic_api_key, cerebras_api_key, azure_api_key, sambanova_api_key, gemini_api_key, deepseek_api_key"
|
|
58
58
|
)
|
|
59
|
+
self.config = Config(maitai_api_key)
|
|
59
60
|
if openai_api_key:
|
|
60
|
-
if not config.auth_keys.openai_api_key:
|
|
61
|
-
config.auth_keys.openai_api_key = Key(
|
|
61
|
+
if not self.config.auth_keys.openai_api_key:
|
|
62
|
+
self.config.auth_keys.openai_api_key = Key(
|
|
62
63
|
key_value=openai_api_key,
|
|
63
64
|
)
|
|
64
65
|
else:
|
|
65
|
-
config.auth_keys.openai_api_key.key_value = openai_api_key
|
|
66
|
+
self.config.auth_keys.openai_api_key.key_value = openai_api_key
|
|
66
67
|
if groq_api_key:
|
|
67
|
-
if not config.auth_keys.groq_api_key:
|
|
68
|
-
config.auth_keys.groq_api_key = Key(
|
|
68
|
+
if not self.config.auth_keys.groq_api_key:
|
|
69
|
+
self.config.auth_keys.groq_api_key = Key(
|
|
69
70
|
key_value=groq_api_key,
|
|
70
71
|
)
|
|
71
72
|
else:
|
|
72
|
-
config.auth_keys.groq_api_key.key_value = groq_api_key
|
|
73
|
+
self.config.auth_keys.groq_api_key.key_value = groq_api_key
|
|
73
74
|
if anthropic_api_key:
|
|
74
|
-
if not config.auth_keys.anthropic_api_key:
|
|
75
|
-
config.auth_keys.anthropic_api_key = Key(
|
|
75
|
+
if not self.config.auth_keys.anthropic_api_key:
|
|
76
|
+
self.config.auth_keys.anthropic_api_key = Key(
|
|
76
77
|
key_value=anthropic_api_key,
|
|
77
78
|
)
|
|
78
79
|
else:
|
|
79
|
-
config.auth_keys.anthropic_api_key.key_value = anthropic_api_key
|
|
80
|
+
self.config.auth_keys.anthropic_api_key.key_value = anthropic_api_key
|
|
80
81
|
if cerebras_api_key:
|
|
81
|
-
if not config.auth_keys.cerebras_api_key:
|
|
82
|
-
config.auth_keys.cerebras_api_key = Key(
|
|
82
|
+
if not self.config.auth_keys.cerebras_api_key:
|
|
83
|
+
self.config.auth_keys.cerebras_api_key = Key(
|
|
83
84
|
key_value=cerebras_api_key,
|
|
84
85
|
)
|
|
85
86
|
else:
|
|
86
|
-
config.auth_keys.cerebras_api_key.key_value = cerebras_api_key
|
|
87
|
+
self.config.auth_keys.cerebras_api_key.key_value = cerebras_api_key
|
|
87
88
|
if azure_api_key:
|
|
88
|
-
if not config.auth_keys.azure_api_key:
|
|
89
|
-
config.auth_keys.azure_api_key = Key(
|
|
89
|
+
if not self.config.auth_keys.azure_api_key:
|
|
90
|
+
self.config.auth_keys.azure_api_key = Key(
|
|
90
91
|
key_value=azure_api_key,
|
|
91
92
|
)
|
|
92
93
|
else:
|
|
93
|
-
config.auth_keys.azure_api_key.key_value = azure_api_key
|
|
94
|
+
self.config.auth_keys.azure_api_key.key_value = azure_api_key
|
|
94
95
|
if sambanova_api_key:
|
|
95
|
-
if not config.auth_keys.sambanova_api_key:
|
|
96
|
-
config.auth_keys.sambanova_api_key = Key(
|
|
96
|
+
if not self.config.auth_keys.sambanova_api_key:
|
|
97
|
+
self.config.auth_keys.sambanova_api_key = Key(
|
|
97
98
|
key_value=sambanova_api_key,
|
|
98
99
|
)
|
|
99
100
|
else:
|
|
100
|
-
config.auth_keys.sambanova_api_key.key_value = sambanova_api_key
|
|
101
|
+
self.config.auth_keys.sambanova_api_key.key_value = sambanova_api_key
|
|
101
102
|
if gemini_api_key:
|
|
102
|
-
if not config.auth_keys.gemini_api_key:
|
|
103
|
-
config.auth_keys.gemini_api_key = Key(
|
|
103
|
+
if not self.config.auth_keys.gemini_api_key:
|
|
104
|
+
self.config.auth_keys.gemini_api_key = Key(
|
|
104
105
|
key_value=gemini_api_key,
|
|
105
106
|
)
|
|
106
107
|
else:
|
|
107
|
-
config.auth_keys.gemini_api_key.key_value = gemini_api_key
|
|
108
|
+
self.config.auth_keys.gemini_api_key.key_value = gemini_api_key
|
|
108
109
|
if deepseek_api_key:
|
|
109
|
-
if not config.auth_keys.deepseek_api_key:
|
|
110
|
-
config.auth_keys.deepseek_api_key = Key(
|
|
110
|
+
if not self.config.auth_keys.deepseek_api_key:
|
|
111
|
+
self.config.auth_keys.deepseek_api_key = Key(
|
|
111
112
|
key_value=deepseek_api_key,
|
|
112
113
|
)
|
|
113
114
|
else:
|
|
114
|
-
config.auth_keys.deepseek_api_key.key_value = deepseek_api_key
|
|
115
|
+
self.config.auth_keys.deepseek_api_key.key_value = deepseek_api_key
|
|
115
116
|
self._openai_client = None
|
|
116
117
|
self._groq_client = None
|
|
117
118
|
azure_params = None
|
|
@@ -149,13 +150,13 @@ class Maitai:
|
|
|
149
150
|
http_client=http_client,
|
|
150
151
|
_strict_response_validation=_strict_response_validation,
|
|
151
152
|
)
|
|
152
|
-
if groq_api_key or config.auth_keys.groq_api_key:
|
|
153
|
+
if groq_api_key or self.config.auth_keys.groq_api_key:
|
|
153
154
|
|
|
154
155
|
self._groq_client = groq.Groq(
|
|
155
156
|
api_key=groq_api_key
|
|
156
157
|
or (
|
|
157
|
-
config.auth_keys.groq_api_key.key_value
|
|
158
|
-
if config.auth_keys.groq_api_key
|
|
158
|
+
self.config.auth_keys.groq_api_key.key_value
|
|
159
|
+
if self.config.auth_keys.groq_api_key
|
|
159
160
|
else None
|
|
160
161
|
),
|
|
161
162
|
)
|
|
@@ -172,25 +173,38 @@ class Maitai:
|
|
|
172
173
|
if azure_params:
|
|
173
174
|
client_param_dict["azure_params"] = azure_params
|
|
174
175
|
client_params = ClientParams(**client_param_dict)
|
|
175
|
-
self.chat = Chat(
|
|
176
|
-
|
|
176
|
+
self.chat = Chat(
|
|
177
|
+
self.config, self._openai_client, self._groq_client, client_params
|
|
178
|
+
)
|
|
179
|
+
self.audio = Audio(self.config)
|
|
180
|
+
self.agent = Agent(self.config)
|
|
181
|
+
self.context = ContextManager(self.config)
|
|
177
182
|
|
|
178
183
|
|
|
179
184
|
class Chat:
|
|
180
185
|
def __init__(
|
|
181
186
|
self,
|
|
182
|
-
|
|
183
|
-
|
|
187
|
+
config: Config,
|
|
188
|
+
openai_client: Optional[openai.Client] = None,
|
|
189
|
+
groq_client: Optional[groq.Groq] = None,
|
|
184
190
|
client_params: Optional[ClientParams] = None,
|
|
185
191
|
):
|
|
186
|
-
self.completions = Completions(
|
|
192
|
+
self.completions = Completions(
|
|
193
|
+
config, openai_client, groq_client, client_params
|
|
194
|
+
)
|
|
187
195
|
|
|
188
196
|
|
|
189
197
|
class Audio:
|
|
190
198
|
def __init__(
|
|
191
199
|
self,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
200
|
+
config: Config,
|
|
201
|
+
):
|
|
202
|
+
self.transcriptions = Transcriptions(config)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
class Agent:
|
|
206
|
+
def __init__(
|
|
207
|
+
self,
|
|
208
|
+
config: Config,
|
|
195
209
|
):
|
|
196
|
-
self.
|
|
210
|
+
self.completions = AgentCompletions(config)
|