maitai-python 1.1.29b0__tar.gz → 1.1.30__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.1.29b0 → maitai_python-1.1.30}/PKG-INFO +1 -1
- maitai_python-1.1.30/maitai/_inference.py +287 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/version.py +1 -1
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_python.egg-info/PKG-INFO +1 -1
- maitai_python-1.1.29b0/maitai/_inference.py +0 -184
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/README.md +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_azure.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_config.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_config_listener_thread.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_context.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_evaluator.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_maitai.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_maitai_async.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_maitai_client.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_types.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/_utils.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai/tools/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/config/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/config/config_service.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/processes/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/processes/io_thread.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/processes/websocket_listener_thread.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/utils/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/utils/proto_utils.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/utils/types.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/annotation/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/application/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/backtest/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/billing/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/chat/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/company/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/config/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/embedding/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/event/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/inference/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/key/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/metric/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/model/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/notification/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/rates/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/sandbox/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/sentinel/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_gen/session/__init__.py +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_python.egg-info/SOURCES.txt +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_python.egg-info/dependency_links.txt +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_python.egg-info/requires.txt +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_python.egg-info/top_level.txt +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/setup.cfg +0 -0
- {maitai_python-1.1.29b0 → maitai_python-1.1.30}/setup.py +0 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import traceback
|
|
2
|
+
from typing import AsyncIterable, Iterable, Optional
|
|
3
|
+
|
|
4
|
+
import aiohttp
|
|
5
|
+
import requests
|
|
6
|
+
from aiohttp import ClientTimeout
|
|
7
|
+
from betterproto import Casing
|
|
8
|
+
from requests.adapters import HTTPAdapter
|
|
9
|
+
from urllib3.util.retry import Retry
|
|
10
|
+
|
|
11
|
+
from maitai._config import config
|
|
12
|
+
from maitai._maitai_client import MaitaiClient
|
|
13
|
+
from maitai._utils import __version__ as version
|
|
14
|
+
from maitai._utils import chat_completion_chunk_to_response
|
|
15
|
+
from maitai_common.utils.types import (
|
|
16
|
+
AsyncChunkQueue,
|
|
17
|
+
ChunkQueue,
|
|
18
|
+
EvaluateCallback,
|
|
19
|
+
QueueIterable,
|
|
20
|
+
)
|
|
21
|
+
from maitai_gen.chat import (
|
|
22
|
+
ChatCompletionChunk,
|
|
23
|
+
ChatCompletionParams,
|
|
24
|
+
ChatCompletionRequest,
|
|
25
|
+
ChatCompletionResponse,
|
|
26
|
+
ChatStorageRequest,
|
|
27
|
+
)
|
|
28
|
+
from maitai_gen.inference import InferenceStreamResponse
|
|
29
|
+
from maitai_gen.metric import RequestTimingMetric
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class InferenceException(Exception):
|
|
33
|
+
def __init__(self, msg: str):
|
|
34
|
+
super().__init__(msg)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class MaitaiConnectionError(ConnectionError):
|
|
38
|
+
def __init__(self, msg: str):
|
|
39
|
+
super().__init__(msg)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class InferenceWarning(Warning):
|
|
43
|
+
def __init__(self, *args, **kwargs):
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Inference(MaitaiClient):
|
|
48
|
+
_session = None
|
|
49
|
+
|
|
50
|
+
def __init__(self):
|
|
51
|
+
super().__init__()
|
|
52
|
+
|
|
53
|
+
@classmethod
|
|
54
|
+
def infer(
|
|
55
|
+
cls, chat_request: ChatCompletionRequest, evaluate_callback, timeout
|
|
56
|
+
) -> Iterable[InferenceStreamResponse]:
|
|
57
|
+
if evaluate_callback:
|
|
58
|
+
q = ChunkQueue()
|
|
59
|
+
cls.run_async(
|
|
60
|
+
cls.send_inference_request_async(
|
|
61
|
+
chat_request,
|
|
62
|
+
chunk_queue=q,
|
|
63
|
+
timeout=timeout,
|
|
64
|
+
evaluation_callback=evaluate_callback,
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
return QueueIterable(q, timeout=timeout)
|
|
68
|
+
else:
|
|
69
|
+
return cls.send_inference_request(chat_request, timeout)
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
async def infer_async(
|
|
73
|
+
cls, chat_request: ChatCompletionRequest, evaluate_callback, timeout
|
|
74
|
+
) -> AsyncIterable[InferenceStreamResponse]:
|
|
75
|
+
q = AsyncChunkQueue()
|
|
76
|
+
cls.run_async(
|
|
77
|
+
cls.send_inference_request_async(
|
|
78
|
+
chat_request, async_chunk_queue=q, evaluation_callback=evaluate_callback
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
return QueueIterable(q, timeout=timeout)
|
|
82
|
+
|
|
83
|
+
@classmethod
|
|
84
|
+
def store_chat_response(
|
|
85
|
+
cls,
|
|
86
|
+
session_id,
|
|
87
|
+
reference_id,
|
|
88
|
+
intent,
|
|
89
|
+
application_ref_name,
|
|
90
|
+
completion_params: ChatCompletionParams,
|
|
91
|
+
chat_completion_response: Optional[ChatCompletionResponse],
|
|
92
|
+
final_chunk: Optional[ChatCompletionChunk],
|
|
93
|
+
content: str,
|
|
94
|
+
timing: RequestTimingMetric,
|
|
95
|
+
metadata: dict,
|
|
96
|
+
):
|
|
97
|
+
inference_request = ChatCompletionRequest(
|
|
98
|
+
application_ref_name=application_ref_name,
|
|
99
|
+
session_id=session_id,
|
|
100
|
+
reference_id=reference_id,
|
|
101
|
+
action_type=intent,
|
|
102
|
+
apply_corrections=False,
|
|
103
|
+
evaluation_enabled=False,
|
|
104
|
+
params=completion_params,
|
|
105
|
+
auth_keys=config.auth_keys,
|
|
106
|
+
metadata=metadata,
|
|
107
|
+
)
|
|
108
|
+
if final_chunk:
|
|
109
|
+
chat_completion_response = chat_completion_chunk_to_response(
|
|
110
|
+
final_chunk, content
|
|
111
|
+
)
|
|
112
|
+
chat_storage_request = ChatStorageRequest(
|
|
113
|
+
chat_completion_request=inference_request,
|
|
114
|
+
chat_completion_response=chat_completion_response,
|
|
115
|
+
timing_metrics=timing,
|
|
116
|
+
)
|
|
117
|
+
cls.run_async(cls.send_storage_request_async(chat_storage_request))
|
|
118
|
+
|
|
119
|
+
@classmethod
|
|
120
|
+
def send_inference_request(
|
|
121
|
+
cls, chat_request: ChatCompletionRequest, timeout
|
|
122
|
+
) -> Iterable[InferenceStreamResponse]:
|
|
123
|
+
host = config.maitai_host
|
|
124
|
+
url = f"{host}/chat/completions/serialized"
|
|
125
|
+
headers = {
|
|
126
|
+
"Content-Type": "application/json",
|
|
127
|
+
"x-api-key": config.api_key,
|
|
128
|
+
"x-client-version": version,
|
|
129
|
+
}
|
|
130
|
+
session = cls.get_session()
|
|
131
|
+
|
|
132
|
+
try:
|
|
133
|
+
with session.post(
|
|
134
|
+
url,
|
|
135
|
+
headers=headers,
|
|
136
|
+
data=chat_request.to_json(casing=Casing.SNAKE),
|
|
137
|
+
stream=True,
|
|
138
|
+
timeout=timeout or 120,
|
|
139
|
+
) as response:
|
|
140
|
+
response.raise_for_status()
|
|
141
|
+
for line in response.iter_lines():
|
|
142
|
+
if line:
|
|
143
|
+
inference_response: InferenceStreamResponse = (
|
|
144
|
+
InferenceStreamResponse().from_json(line)
|
|
145
|
+
)
|
|
146
|
+
if not inference_response.keep_alive:
|
|
147
|
+
yield inference_response
|
|
148
|
+
except requests.exceptions.RequestException as e:
|
|
149
|
+
cls.log_error(str(e), url)
|
|
150
|
+
raise MaitaiConnectionError(f"Failed to send inference request. Error: {e}")
|
|
151
|
+
|
|
152
|
+
@classmethod
|
|
153
|
+
async def send_inference_request_async(
|
|
154
|
+
cls,
|
|
155
|
+
chat_request: ChatCompletionRequest,
|
|
156
|
+
chunk_queue: ChunkQueue = None,
|
|
157
|
+
async_chunk_queue: AsyncChunkQueue = None,
|
|
158
|
+
evaluation_callback: EvaluateCallback = None,
|
|
159
|
+
timeout=None,
|
|
160
|
+
):
|
|
161
|
+
host = config.maitai_host
|
|
162
|
+
url = f"{host}/chat/completions/serialized"
|
|
163
|
+
headers = {
|
|
164
|
+
"Content-Type": "application/json",
|
|
165
|
+
"x-api-key": config.api_key,
|
|
166
|
+
"x-client-version": version,
|
|
167
|
+
}
|
|
168
|
+
try:
|
|
169
|
+
async with aiohttp.ClientSession(
|
|
170
|
+
connector=aiohttp.TCPConnector(ssl=False),
|
|
171
|
+
timeout=ClientTimeout(timeout or 120),
|
|
172
|
+
) as session:
|
|
173
|
+
async with session.post(
|
|
174
|
+
url, headers=headers, data=chat_request.to_json(casing=Casing.SNAKE)
|
|
175
|
+
) as response:
|
|
176
|
+
if response.status != 200:
|
|
177
|
+
error_text = await response.text()
|
|
178
|
+
cls.log_error(error_text, url)
|
|
179
|
+
exception = MaitaiConnectionError(
|
|
180
|
+
f"Failed to send inference request. Status code: {response.status}. Error: {error_text}"
|
|
181
|
+
)
|
|
182
|
+
if chunk_queue:
|
|
183
|
+
chunk_queue.put(exception)
|
|
184
|
+
if async_chunk_queue:
|
|
185
|
+
await async_chunk_queue.put(exception)
|
|
186
|
+
return
|
|
187
|
+
async for line in response.content:
|
|
188
|
+
if line:
|
|
189
|
+
inference_response: InferenceStreamResponse = (
|
|
190
|
+
InferenceStreamResponse().from_json(line)
|
|
191
|
+
)
|
|
192
|
+
if inference_response.keep_alive:
|
|
193
|
+
continue
|
|
194
|
+
if chunk_queue:
|
|
195
|
+
chunk_queue.put(inference_response)
|
|
196
|
+
if async_chunk_queue:
|
|
197
|
+
await async_chunk_queue.put(inference_response)
|
|
198
|
+
if (
|
|
199
|
+
inference_response.evaluate_response
|
|
200
|
+
and evaluation_callback
|
|
201
|
+
):
|
|
202
|
+
try:
|
|
203
|
+
evaluation_callback(
|
|
204
|
+
inference_response.evaluate_response
|
|
205
|
+
)
|
|
206
|
+
except:
|
|
207
|
+
traceback.print_exc()
|
|
208
|
+
if chunk_queue:
|
|
209
|
+
chunk_queue.put(StopIteration())
|
|
210
|
+
if async_chunk_queue:
|
|
211
|
+
await async_chunk_queue.put(StopIteration())
|
|
212
|
+
except Exception as e:
|
|
213
|
+
exception = MaitaiConnectionError(
|
|
214
|
+
f"Failed to send inference request: {str(e)}"
|
|
215
|
+
)
|
|
216
|
+
await cls.increment_error(str(e), url)
|
|
217
|
+
if chunk_queue:
|
|
218
|
+
chunk_queue.put(exception)
|
|
219
|
+
if async_chunk_queue:
|
|
220
|
+
await async_chunk_queue.put(exception)
|
|
221
|
+
|
|
222
|
+
@classmethod
|
|
223
|
+
async def send_storage_request_async(cls, chat_storage_request: ChatStorageRequest):
|
|
224
|
+
host = config.maitai_host
|
|
225
|
+
url = f"{host}/chat/completions/response"
|
|
226
|
+
headers = {
|
|
227
|
+
"Content-Type": "application/json",
|
|
228
|
+
"x-api-key": config.api_key,
|
|
229
|
+
"x-client-version": version,
|
|
230
|
+
}
|
|
231
|
+
try:
|
|
232
|
+
async with aiohttp.ClientSession(
|
|
233
|
+
connector=aiohttp.TCPConnector(ssl=False)
|
|
234
|
+
) as session:
|
|
235
|
+
return await session.put(
|
|
236
|
+
url,
|
|
237
|
+
headers=headers,
|
|
238
|
+
data=chat_storage_request.to_json(casing=Casing.SNAKE),
|
|
239
|
+
)
|
|
240
|
+
except Exception as e:
|
|
241
|
+
await cls.increment_error(str(e), url)
|
|
242
|
+
|
|
243
|
+
@classmethod
|
|
244
|
+
def store_request_timing_data(cls, metric: RequestTimingMetric):
|
|
245
|
+
cls.run_async(cls.send_request_timing_data(metric))
|
|
246
|
+
|
|
247
|
+
@classmethod
|
|
248
|
+
async def send_request_timing_data(cls, metric: RequestTimingMetric):
|
|
249
|
+
host = config.maitai_host
|
|
250
|
+
url = f"{host}/metrics/timing"
|
|
251
|
+
headers = {
|
|
252
|
+
"Content-Type": "application/json",
|
|
253
|
+
"x-api-key": config.api_key,
|
|
254
|
+
"x-client-version": version,
|
|
255
|
+
}
|
|
256
|
+
try:
|
|
257
|
+
async with aiohttp.ClientSession(
|
|
258
|
+
connector=aiohttp.TCPConnector(ssl=False)
|
|
259
|
+
) as session:
|
|
260
|
+
return await session.put(
|
|
261
|
+
url, headers=headers, data=metric.to_json(casing=Casing.SNAKE)
|
|
262
|
+
)
|
|
263
|
+
except:
|
|
264
|
+
pass
|
|
265
|
+
|
|
266
|
+
@classmethod
|
|
267
|
+
def get_session(cls):
|
|
268
|
+
if cls._session is None:
|
|
269
|
+
cls._session = requests.Session()
|
|
270
|
+
retries = Retry(
|
|
271
|
+
total=3, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504]
|
|
272
|
+
)
|
|
273
|
+
cls._session.mount(
|
|
274
|
+
"http://",
|
|
275
|
+
HTTPAdapter(max_retries=retries, pool_connections=10, pool_maxsize=10),
|
|
276
|
+
)
|
|
277
|
+
cls._session.mount(
|
|
278
|
+
"https://",
|
|
279
|
+
HTTPAdapter(max_retries=retries, pool_connections=10, pool_maxsize=10),
|
|
280
|
+
)
|
|
281
|
+
return cls._session
|
|
282
|
+
|
|
283
|
+
@classmethod
|
|
284
|
+
def close_session(cls):
|
|
285
|
+
if cls._session:
|
|
286
|
+
cls._session.close()
|
|
287
|
+
cls._session = None
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import traceback
|
|
2
|
-
from typing import AsyncIterable, Iterable, Optional
|
|
3
|
-
|
|
4
|
-
import aiohttp
|
|
5
|
-
import requests
|
|
6
|
-
from aiohttp import ClientTimeout
|
|
7
|
-
from betterproto import Casing
|
|
8
|
-
|
|
9
|
-
from maitai._config import config
|
|
10
|
-
from maitai._maitai_client import MaitaiClient
|
|
11
|
-
from maitai._utils import __version__ as version, chat_completion_chunk_to_response
|
|
12
|
-
from maitai_common.utils.types import AsyncChunkQueue, ChunkQueue, EvaluateCallback, QueueIterable
|
|
13
|
-
from maitai_gen.chat import ChatCompletionChunk, ChatCompletionParams, ChatCompletionRequest, ChatCompletionResponse, ChatStorageRequest
|
|
14
|
-
from maitai_gen.inference import InferenceStreamResponse
|
|
15
|
-
from maitai_gen.metric import RequestTimingMetric
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
class InferenceException(Exception):
|
|
19
|
-
def __init__(self, msg: str):
|
|
20
|
-
super().__init__(msg)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
class MaitaiConnectionError(ConnectionError):
|
|
24
|
-
def __init__(self, msg: str):
|
|
25
|
-
super().__init__(msg)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
class InferenceWarning(Warning):
|
|
29
|
-
def __init__(self, *args, **kwargs):
|
|
30
|
-
pass
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class Inference(MaitaiClient):
|
|
34
|
-
|
|
35
|
-
def __init__(self):
|
|
36
|
-
super().__init__()
|
|
37
|
-
|
|
38
|
-
@classmethod
|
|
39
|
-
def infer(cls, chat_request: ChatCompletionRequest, evaluate_callback, timeout) -> Iterable[InferenceStreamResponse]:
|
|
40
|
-
if evaluate_callback:
|
|
41
|
-
q = ChunkQueue()
|
|
42
|
-
cls.run_async(cls.send_inference_request_async(chat_request, chunk_queue=q, timeout=timeout, evaluation_callback=evaluate_callback))
|
|
43
|
-
return QueueIterable(q, timeout=timeout)
|
|
44
|
-
else:
|
|
45
|
-
return cls.send_inference_request(chat_request, timeout)
|
|
46
|
-
|
|
47
|
-
@classmethod
|
|
48
|
-
async def infer_async(cls, chat_request: ChatCompletionRequest, evaluate_callback, timeout) -> AsyncIterable[InferenceStreamResponse]:
|
|
49
|
-
q = AsyncChunkQueue()
|
|
50
|
-
cls.run_async(cls.send_inference_request_async(chat_request, async_chunk_queue=q, evaluation_callback=evaluate_callback))
|
|
51
|
-
return QueueIterable(q, timeout=timeout)
|
|
52
|
-
|
|
53
|
-
@classmethod
|
|
54
|
-
def store_chat_response(cls, session_id, reference_id, intent, application_ref_name, completion_params: ChatCompletionParams, chat_completion_response: Optional[ChatCompletionResponse],
|
|
55
|
-
final_chunk: Optional[ChatCompletionChunk], content: str, timing: RequestTimingMetric, metadata: dict):
|
|
56
|
-
inference_request = ChatCompletionRequest(
|
|
57
|
-
application_ref_name=application_ref_name,
|
|
58
|
-
session_id=session_id,
|
|
59
|
-
reference_id=reference_id,
|
|
60
|
-
action_type=intent,
|
|
61
|
-
apply_corrections=False,
|
|
62
|
-
evaluation_enabled=False,
|
|
63
|
-
params=completion_params,
|
|
64
|
-
auth_keys=config.auth_keys,
|
|
65
|
-
metadata=metadata,
|
|
66
|
-
)
|
|
67
|
-
if final_chunk:
|
|
68
|
-
chat_completion_response = chat_completion_chunk_to_response(final_chunk, content)
|
|
69
|
-
chat_storage_request = ChatStorageRequest(chat_completion_request=inference_request, chat_completion_response=chat_completion_response, timing_metrics=timing)
|
|
70
|
-
cls.run_async(cls.send_storage_request_async(chat_storage_request))
|
|
71
|
-
|
|
72
|
-
@classmethod
|
|
73
|
-
def send_inference_request(cls, chat_request: ChatCompletionRequest, timeout) -> Iterable[InferenceStreamResponse]:
|
|
74
|
-
def consume_stream():
|
|
75
|
-
host = config.maitai_host
|
|
76
|
-
url = f'{host}/chat/completions/serialized'
|
|
77
|
-
headers = {
|
|
78
|
-
'Content-Type': 'application/json',
|
|
79
|
-
'x-api-key': config.api_key,
|
|
80
|
-
'x-client-version': version,
|
|
81
|
-
}
|
|
82
|
-
try:
|
|
83
|
-
response = requests.post(url, headers=headers, data=chat_request.to_json(casing=Casing.SNAKE), stream=True, timeout=timeout or 120)
|
|
84
|
-
if response.status_code != 200:
|
|
85
|
-
cls.log_error(response.text, url)
|
|
86
|
-
raise MaitaiConnectionError(f"Failed to send inference request. Status code: {response.status_code}. Error: {response.text}")
|
|
87
|
-
except Exception as e:
|
|
88
|
-
cls.log_error(str(e), url)
|
|
89
|
-
raise MaitaiConnectionError(f"Failed to send inference request. Error: {e}")
|
|
90
|
-
|
|
91
|
-
try:
|
|
92
|
-
for line in response.iter_lines():
|
|
93
|
-
if line:
|
|
94
|
-
yield line
|
|
95
|
-
finally:
|
|
96
|
-
response.close()
|
|
97
|
-
|
|
98
|
-
for resp in consume_stream():
|
|
99
|
-
inference_response: InferenceStreamResponse = InferenceStreamResponse().from_json(resp)
|
|
100
|
-
if inference_response.keep_alive:
|
|
101
|
-
continue
|
|
102
|
-
yield inference_response
|
|
103
|
-
|
|
104
|
-
@classmethod
|
|
105
|
-
async def send_inference_request_async(cls, chat_request: ChatCompletionRequest, chunk_queue: ChunkQueue = None, async_chunk_queue: AsyncChunkQueue = None, evaluation_callback: EvaluateCallback = None, timeout=None):
|
|
106
|
-
host = config.maitai_host
|
|
107
|
-
url = f'{host}/chat/completions/serialized'
|
|
108
|
-
headers = {
|
|
109
|
-
'Content-Type': 'application/json',
|
|
110
|
-
'x-api-key': config.api_key,
|
|
111
|
-
'x-client-version': version,
|
|
112
|
-
}
|
|
113
|
-
try:
|
|
114
|
-
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False), timeout=ClientTimeout(timeout or 120)) as session:
|
|
115
|
-
async with session.post(url, headers=headers, data=chat_request.to_json(casing=Casing.SNAKE)) as response:
|
|
116
|
-
if response.status != 200:
|
|
117
|
-
error_text = await response.text()
|
|
118
|
-
cls.log_error(error_text, url)
|
|
119
|
-
exception = MaitaiConnectionError(f"Failed to send inference request. Status code: {response.status}. Error: {error_text}")
|
|
120
|
-
if chunk_queue:
|
|
121
|
-
chunk_queue.put(exception)
|
|
122
|
-
if async_chunk_queue:
|
|
123
|
-
await async_chunk_queue.put(exception)
|
|
124
|
-
return
|
|
125
|
-
async for line in response.content:
|
|
126
|
-
if line:
|
|
127
|
-
inference_response: InferenceStreamResponse = InferenceStreamResponse().from_json(line)
|
|
128
|
-
if inference_response.keep_alive:
|
|
129
|
-
continue
|
|
130
|
-
if chunk_queue:
|
|
131
|
-
chunk_queue.put(inference_response)
|
|
132
|
-
if async_chunk_queue:
|
|
133
|
-
await async_chunk_queue.put(inference_response)
|
|
134
|
-
if inference_response.evaluate_response and evaluation_callback:
|
|
135
|
-
try:
|
|
136
|
-
evaluation_callback(inference_response.evaluate_response)
|
|
137
|
-
except:
|
|
138
|
-
traceback.print_exc()
|
|
139
|
-
if chunk_queue:
|
|
140
|
-
chunk_queue.put(StopIteration())
|
|
141
|
-
if async_chunk_queue:
|
|
142
|
-
await async_chunk_queue.put(StopIteration())
|
|
143
|
-
except Exception as e:
|
|
144
|
-
exception = MaitaiConnectionError(f"Failed to send inference request: {str(e)}")
|
|
145
|
-
await cls.increment_error(str(e), url)
|
|
146
|
-
if chunk_queue:
|
|
147
|
-
chunk_queue.put(exception)
|
|
148
|
-
if async_chunk_queue:
|
|
149
|
-
await async_chunk_queue.put(exception)
|
|
150
|
-
|
|
151
|
-
@classmethod
|
|
152
|
-
async def send_storage_request_async(cls, chat_storage_request: ChatStorageRequest):
|
|
153
|
-
host = config.maitai_host
|
|
154
|
-
url = f'{host}/chat/completions/response'
|
|
155
|
-
headers = {
|
|
156
|
-
'Content-Type': 'application/json',
|
|
157
|
-
'x-api-key': config.api_key,
|
|
158
|
-
'x-client-version': version,
|
|
159
|
-
}
|
|
160
|
-
try:
|
|
161
|
-
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
|
|
162
|
-
return await session.put(url, headers=headers, data=chat_storage_request.to_json(casing=Casing.SNAKE))
|
|
163
|
-
except Exception as e:
|
|
164
|
-
await cls.increment_error(str(e), url)
|
|
165
|
-
pass
|
|
166
|
-
|
|
167
|
-
@classmethod
|
|
168
|
-
def store_request_timing_data(cls, metric: RequestTimingMetric):
|
|
169
|
-
cls.run_async(cls.send_request_timing_data(metric))
|
|
170
|
-
|
|
171
|
-
@classmethod
|
|
172
|
-
async def send_request_timing_data(cls, metric: RequestTimingMetric):
|
|
173
|
-
host = config.maitai_host
|
|
174
|
-
url = f'{host}/metrics/timing'
|
|
175
|
-
headers = {
|
|
176
|
-
'Content-Type': 'application/json',
|
|
177
|
-
'x-api-key': config.api_key,
|
|
178
|
-
'x-client-version': version,
|
|
179
|
-
}
|
|
180
|
-
try:
|
|
181
|
-
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
|
|
182
|
-
return await session.put(url, headers=headers, data=metric.to_json(casing=Casing.SNAKE))
|
|
183
|
-
except:
|
|
184
|
-
pass
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{maitai_python-1.1.29b0 → maitai_python-1.1.30}/maitai_common/processes/websocket_listener_thread.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|