maitai-python 1.1.37b0__tar.gz → 1.1.39b0__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.37b0 → maitai_python-1.1.39b0}/PKG-INFO +2 -2
- maitai_python-1.1.39b0/maitai/_evaluator.py +297 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_inference.py +36 -41
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_maitai.py +34 -41
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_maitai_async.py +33 -40
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_types.py +82 -4
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_utils.py +60 -51
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_common/version.py +1 -1
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/annotation/__init__.py +1 -3
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/chat/__init__.py +24 -38
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/config/__init__.py +4 -10
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/key/__init__.py +1 -3
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/metric/__init__.py +1 -3
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/model/__init__.py +4 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/sandbox/__init__.py +3 -5
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/session/__init__.py +1 -3
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_python.egg-info/PKG-INFO +2 -2
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_python.egg-info/SOURCES.txt +0 -3
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_python.egg-info/requires.txt +1 -1
- maitai_python-1.1.39b0/setup.py +42 -0
- maitai_python-1.1.37b0/maitai/_evaluator.py +0 -183
- maitai_python-1.1.37b0/maitai_common/utils/proto_utils.py +0 -17
- maitai_python-1.1.37b0/maitai_common/utils/types.py +0 -75
- maitai_python-1.1.37b0/maitai_gen/__init__.py +0 -0
- maitai_python-1.1.37b0/setup.py +0 -32
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/README.md +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_azure.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_config.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_config_listener_thread.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_context.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/_maitai_client.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai/tools/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_common/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_common/config/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_common/config/config_service.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_common/processes/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_common/processes/io_thread.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_common/processes/websocket_listener_thread.py +0 -0
- {maitai_python-1.1.37b0/maitai_common/utils → maitai_python-1.1.39b0/maitai_gen}/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/application/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/backtest/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/billing/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/company/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/embedding/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/event/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/inference/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/notification/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/rates/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_gen/sentinel/__init__.py +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_python.egg-info/dependency_links.txt +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/maitai_python.egg-info/top_level.txt +0 -0
- {maitai_python-1.1.37b0 → maitai_python-1.1.39b0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: maitai-python
|
|
3
|
-
Version: 1.1.
|
|
3
|
+
Version: 1.1.39b0
|
|
4
4
|
Summary: Maitai SDK for Python
|
|
5
5
|
Home-page: https://docs.trymaitai.ai
|
|
6
6
|
Author: Maitai
|
|
@@ -14,7 +14,7 @@ Requires-Dist: openai>=1.34.0
|
|
|
14
14
|
Requires-Dist: betterproto
|
|
15
15
|
Requires-Dist: httpx
|
|
16
16
|
Requires-Dist: aiohttp
|
|
17
|
-
Requires-Dist: betterproto==2.0.
|
|
17
|
+
Requires-Dist: betterproto==2.0.0b7
|
|
18
18
|
Requires-Dist: websocket-client
|
|
19
19
|
|
|
20
20
|
# Quickstart
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import json
|
|
3
|
+
import traceback
|
|
4
|
+
from typing import Iterable
|
|
5
|
+
|
|
6
|
+
import aiohttp
|
|
7
|
+
import requests
|
|
8
|
+
|
|
9
|
+
from maitai._config import config
|
|
10
|
+
from maitai._maitai_client import MaitaiClient
|
|
11
|
+
from maitai._types import EvaluateCallback
|
|
12
|
+
from maitai._utils import __version__ as version
|
|
13
|
+
from maitai._utils import chat_completion_chunk_to_response
|
|
14
|
+
from maitai_gen.chat import (
|
|
15
|
+
ChatCompletionChunk,
|
|
16
|
+
ChatCompletionParams,
|
|
17
|
+
ChatCompletionResponse,
|
|
18
|
+
EvaluateRequest,
|
|
19
|
+
EvaluateResponse,
|
|
20
|
+
EvaluationContentType,
|
|
21
|
+
)
|
|
22
|
+
from maitai_gen.metric import RequestTimingMetric
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _get_content_type(content, partial=None):
|
|
26
|
+
if partial is not None:
|
|
27
|
+
return EvaluationContentType.PARTIAL
|
|
28
|
+
if isinstance(content, str):
|
|
29
|
+
return EvaluationContentType.TEXT
|
|
30
|
+
elif isinstance(content, list):
|
|
31
|
+
return EvaluationContentType.MESSAGE
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Evaluator(MaitaiClient):
|
|
35
|
+
|
|
36
|
+
def __init__(self):
|
|
37
|
+
super().__init__()
|
|
38
|
+
|
|
39
|
+
@classmethod
|
|
40
|
+
async def evaluate_async(
|
|
41
|
+
cls,
|
|
42
|
+
session_id,
|
|
43
|
+
reference_id,
|
|
44
|
+
intent,
|
|
45
|
+
content,
|
|
46
|
+
content_type=None,
|
|
47
|
+
application_id=None,
|
|
48
|
+
application_ref_name=None,
|
|
49
|
+
callback=None,
|
|
50
|
+
completion_params: dict = None,
|
|
51
|
+
chat_completion_response: ChatCompletionResponse = None,
|
|
52
|
+
chat_completion_chunk: ChatCompletionChunk = None,
|
|
53
|
+
timing: RequestTimingMetric = None,
|
|
54
|
+
metadata: dict = {},
|
|
55
|
+
):
|
|
56
|
+
if content_type is None:
|
|
57
|
+
content_type = _get_content_type(content)
|
|
58
|
+
if content_type is None:
|
|
59
|
+
raise Exception("Unable to automatically determine content_type")
|
|
60
|
+
if application_id is None and application_ref_name is None:
|
|
61
|
+
raise Exception("application_id or application_ref_name must be provided")
|
|
62
|
+
eval_request = cls.create_eval_request(
|
|
63
|
+
application_id,
|
|
64
|
+
application_ref_name,
|
|
65
|
+
session_id,
|
|
66
|
+
reference_id,
|
|
67
|
+
intent,
|
|
68
|
+
content_type,
|
|
69
|
+
content,
|
|
70
|
+
)
|
|
71
|
+
if completion_params is not None:
|
|
72
|
+
eval_request["chat_completion_request"] = {
|
|
73
|
+
"application_ref_name": application_ref_name,
|
|
74
|
+
"session_id": session_id,
|
|
75
|
+
"reference_id": reference_id,
|
|
76
|
+
"action_type": intent,
|
|
77
|
+
"apply_corrections": False,
|
|
78
|
+
"evaluation_enabled": True,
|
|
79
|
+
"params": completion_params,
|
|
80
|
+
"return_evaluation": True if callback else False,
|
|
81
|
+
"auth_keys": config.auth_keys.to_dict(),
|
|
82
|
+
"metadata": metadata,
|
|
83
|
+
}
|
|
84
|
+
if chat_completion_chunk is not None:
|
|
85
|
+
chat_completion_response = chat_completion_chunk_to_response(
|
|
86
|
+
chat_completion_chunk, content
|
|
87
|
+
)
|
|
88
|
+
print(chat_completion_response)
|
|
89
|
+
eval_request["chat_completion_response"] = (
|
|
90
|
+
chat_completion_response.to_dict() if chat_completion_response else None
|
|
91
|
+
)
|
|
92
|
+
eval_request["timing_metrics"] = timing.to_dict() if timing else None
|
|
93
|
+
await asyncio.create_task(
|
|
94
|
+
cls.send_evaluation_request_async(eval_request, callback)
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
@classmethod
|
|
98
|
+
def evaluate(
|
|
99
|
+
cls,
|
|
100
|
+
session_id,
|
|
101
|
+
reference_id,
|
|
102
|
+
intent,
|
|
103
|
+
content,
|
|
104
|
+
content_type=None,
|
|
105
|
+
application_id=None,
|
|
106
|
+
application_ref_name=None,
|
|
107
|
+
callback=None,
|
|
108
|
+
partial=None,
|
|
109
|
+
completion_params: ChatCompletionParams = None,
|
|
110
|
+
chat_completion_response: ChatCompletionResponse = None,
|
|
111
|
+
chat_completion_chunk: ChatCompletionChunk = None,
|
|
112
|
+
timing: RequestTimingMetric = None,
|
|
113
|
+
metadata: dict = {},
|
|
114
|
+
):
|
|
115
|
+
if content_type is None:
|
|
116
|
+
content_type = _get_content_type(content, partial)
|
|
117
|
+
if content_type is None:
|
|
118
|
+
raise Exception("Unable to automatically determine content_type")
|
|
119
|
+
if application_id is None and application_ref_name is None:
|
|
120
|
+
raise Exception("application_id or application_ref_name must be provided")
|
|
121
|
+
eval_request: EvaluateRequest = cls.create_eval_request(
|
|
122
|
+
application_id,
|
|
123
|
+
application_ref_name,
|
|
124
|
+
session_id,
|
|
125
|
+
reference_id,
|
|
126
|
+
intent,
|
|
127
|
+
content_type,
|
|
128
|
+
content,
|
|
129
|
+
partial=partial,
|
|
130
|
+
)
|
|
131
|
+
if completion_params is not None:
|
|
132
|
+
eval_request["chat_completion_request"] = {
|
|
133
|
+
"application_ref_name": application_ref_name,
|
|
134
|
+
"session_id": session_id,
|
|
135
|
+
"reference_id": reference_id,
|
|
136
|
+
"action_type": intent,
|
|
137
|
+
"apply_corrections": False,
|
|
138
|
+
"evaluation_enabled": True,
|
|
139
|
+
"params": completion_params,
|
|
140
|
+
"return_evaluation": True if callback else False,
|
|
141
|
+
"auth_keys": config.auth_keys.to_dict(),
|
|
142
|
+
"metadata": metadata,
|
|
143
|
+
}
|
|
144
|
+
if chat_completion_chunk is not None:
|
|
145
|
+
chat_completion_response = chat_completion_chunk_to_response(
|
|
146
|
+
chat_completion_chunk, content
|
|
147
|
+
)
|
|
148
|
+
eval_request["chat_completion_response"] = (
|
|
149
|
+
chat_completion_response.to_dict()
|
|
150
|
+
)
|
|
151
|
+
eval_request["timing_metrics"] = timing.to_dict() if timing else None
|
|
152
|
+
|
|
153
|
+
cls.run_async(cls.send_evaluation_request_async(eval_request, callback))
|
|
154
|
+
|
|
155
|
+
@classmethod
|
|
156
|
+
def stream_correction(
|
|
157
|
+
cls,
|
|
158
|
+
session_id,
|
|
159
|
+
reference_id,
|
|
160
|
+
intent,
|
|
161
|
+
content_type,
|
|
162
|
+
content,
|
|
163
|
+
application_ref_name,
|
|
164
|
+
partial,
|
|
165
|
+
fault_description,
|
|
166
|
+
sentinel_id,
|
|
167
|
+
):
|
|
168
|
+
eval_request: EvaluateRequest = cls.create_eval_request(
|
|
169
|
+
None,
|
|
170
|
+
application_ref_name,
|
|
171
|
+
session_id,
|
|
172
|
+
reference_id,
|
|
173
|
+
intent,
|
|
174
|
+
content_type,
|
|
175
|
+
content,
|
|
176
|
+
partial=partial,
|
|
177
|
+
fault_description=fault_description,
|
|
178
|
+
)
|
|
179
|
+
eval_request["sentinel_id"] = sentinel_id
|
|
180
|
+
return cls.send_stream_correction_request(eval_request)
|
|
181
|
+
|
|
182
|
+
@classmethod
|
|
183
|
+
def create_eval_request(
|
|
184
|
+
cls,
|
|
185
|
+
application_id,
|
|
186
|
+
application_ref_name,
|
|
187
|
+
session_id,
|
|
188
|
+
reference_id,
|
|
189
|
+
intent,
|
|
190
|
+
content_type,
|
|
191
|
+
content,
|
|
192
|
+
partial=None,
|
|
193
|
+
fault_description=None,
|
|
194
|
+
chat_completion_response=None,
|
|
195
|
+
):
|
|
196
|
+
eval_request = {
|
|
197
|
+
"evaluation_content_type": content_type,
|
|
198
|
+
"application_id": application_id,
|
|
199
|
+
"application_ref_name": application_ref_name,
|
|
200
|
+
"session_id": session_id,
|
|
201
|
+
"reference_id": reference_id,
|
|
202
|
+
"action_type": intent,
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if content_type == EvaluationContentType.TEXT:
|
|
206
|
+
if not isinstance(content, str):
|
|
207
|
+
raise Exception("Content must be a string")
|
|
208
|
+
eval_request["text_content"] = content
|
|
209
|
+
elif content_type == EvaluationContentType.MESSAGE:
|
|
210
|
+
eval_request["message_content"] = content
|
|
211
|
+
elif content_type == EvaluationContentType.PARTIAL:
|
|
212
|
+
eval_request["message_content"] = content
|
|
213
|
+
eval_request["text_content"] = partial
|
|
214
|
+
|
|
215
|
+
if fault_description:
|
|
216
|
+
eval_request["fault_description"] = fault_description
|
|
217
|
+
|
|
218
|
+
return eval_request
|
|
219
|
+
|
|
220
|
+
@classmethod
|
|
221
|
+
async def send_evaluation_request_async(
|
|
222
|
+
cls, eval_request: dict, callback: EvaluateCallback = None
|
|
223
|
+
):
|
|
224
|
+
path = "request" if callback else "submit"
|
|
225
|
+
host = config.maitai_host
|
|
226
|
+
url = f"{host}/evaluation/{path}"
|
|
227
|
+
|
|
228
|
+
async def send_request():
|
|
229
|
+
headers = {
|
|
230
|
+
"Content-Type": "application/json",
|
|
231
|
+
"x-api-key": config.api_key,
|
|
232
|
+
"x-client-version": version,
|
|
233
|
+
}
|
|
234
|
+
try:
|
|
235
|
+
async with aiohttp.ClientSession(
|
|
236
|
+
connector=aiohttp.TCPConnector(ssl=False)
|
|
237
|
+
) as session:
|
|
238
|
+
async with session.post(
|
|
239
|
+
url, headers=headers, data=json.dumps(eval_request)
|
|
240
|
+
) as response:
|
|
241
|
+
print("received", response.status)
|
|
242
|
+
if response.status != 200:
|
|
243
|
+
error_text = await response.text()
|
|
244
|
+
cls.log_error(error_text, url)
|
|
245
|
+
print(
|
|
246
|
+
f"Failed to send evaluation request. Status code: {response.status}. Error: {error_text}"
|
|
247
|
+
)
|
|
248
|
+
return None
|
|
249
|
+
return await response.read()
|
|
250
|
+
except Exception as e:
|
|
251
|
+
await cls.increment_error(str(e), url)
|
|
252
|
+
|
|
253
|
+
result = await send_request()
|
|
254
|
+
if result is not None:
|
|
255
|
+
eval_result = EvaluateResponse().from_json(result)
|
|
256
|
+
if callback is not None:
|
|
257
|
+
try:
|
|
258
|
+
callback(eval_result)
|
|
259
|
+
except:
|
|
260
|
+
traceback.print_exc()
|
|
261
|
+
else:
|
|
262
|
+
return eval_result
|
|
263
|
+
|
|
264
|
+
@classmethod
|
|
265
|
+
def send_stream_correction_request(
|
|
266
|
+
cls, eval_request: dict
|
|
267
|
+
) -> Iterable[ChatCompletionChunk]:
|
|
268
|
+
def consume_stream():
|
|
269
|
+
host = config.maitai_host
|
|
270
|
+
url = f"{host}/evaluation/stream_correction"
|
|
271
|
+
headers = {
|
|
272
|
+
"Content-Type": "application/json",
|
|
273
|
+
"x-api-key": config.api_key,
|
|
274
|
+
"x-client-version": version,
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
response = requests.post(
|
|
278
|
+
url, headers=headers, data=json.dumps(eval_request), stream=True
|
|
279
|
+
)
|
|
280
|
+
if response.status_code != 200:
|
|
281
|
+
print(
|
|
282
|
+
f"Failed to send stream correction request. Status code: {response.status_code}. Error: {response.text}"
|
|
283
|
+
)
|
|
284
|
+
cls.log_error(response.text, url)
|
|
285
|
+
return
|
|
286
|
+
try:
|
|
287
|
+
for line in response.iter_lines():
|
|
288
|
+
if line:
|
|
289
|
+
yield line
|
|
290
|
+
finally:
|
|
291
|
+
response.close()
|
|
292
|
+
|
|
293
|
+
for resp in consume_stream():
|
|
294
|
+
inference_response: ChatCompletionChunk = ChatCompletionChunk().from_json(
|
|
295
|
+
resp
|
|
296
|
+
)
|
|
297
|
+
yield inference_response
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import json
|
|
1
2
|
import traceback
|
|
2
3
|
from typing import AsyncIterable, Iterable, Optional
|
|
3
4
|
|
|
@@ -10,21 +11,10 @@ from urllib3.util.retry import Retry
|
|
|
10
11
|
|
|
11
12
|
from maitai._config import config
|
|
12
13
|
from maitai._maitai_client import MaitaiClient
|
|
14
|
+
from maitai._types import AsyncChunkQueue, ChunkQueue, EvaluateCallback, QueueIterable
|
|
13
15
|
from maitai._utils import __version__ as version
|
|
14
16
|
from maitai._utils import chat_completion_chunk_to_response
|
|
15
|
-
from
|
|
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
|
-
)
|
|
17
|
+
from maitai_gen.chat import ChatCompletionChunk, ChatCompletionResponse
|
|
28
18
|
from maitai_gen.inference import InferenceStreamResponse
|
|
29
19
|
from maitai_gen.metric import RequestTimingMetric
|
|
30
20
|
|
|
@@ -52,13 +42,13 @@ class Inference(MaitaiClient):
|
|
|
52
42
|
|
|
53
43
|
@classmethod
|
|
54
44
|
def infer(
|
|
55
|
-
cls,
|
|
45
|
+
cls, request_dict: dict, evaluate_callback, timeout
|
|
56
46
|
) -> Iterable[InferenceStreamResponse]:
|
|
57
47
|
if evaluate_callback:
|
|
58
48
|
q = ChunkQueue()
|
|
59
49
|
cls.run_async(
|
|
60
50
|
cls.send_inference_request_async(
|
|
61
|
-
|
|
51
|
+
request_dict,
|
|
62
52
|
chunk_queue=q,
|
|
63
53
|
timeout=timeout,
|
|
64
54
|
evaluation_callback=evaluate_callback,
|
|
@@ -66,16 +56,16 @@ class Inference(MaitaiClient):
|
|
|
66
56
|
)
|
|
67
57
|
return QueueIterable(q, timeout=timeout)
|
|
68
58
|
else:
|
|
69
|
-
return cls.send_inference_request(
|
|
59
|
+
return cls.send_inference_request(request_dict, timeout)
|
|
70
60
|
|
|
71
61
|
@classmethod
|
|
72
62
|
async def infer_async(
|
|
73
|
-
cls,
|
|
63
|
+
cls, request_dict: dict, evaluate_callback, timeout
|
|
74
64
|
) -> AsyncIterable[InferenceStreamResponse]:
|
|
75
65
|
q = AsyncChunkQueue()
|
|
76
66
|
cls.run_async(
|
|
77
67
|
cls.send_inference_request_async(
|
|
78
|
-
|
|
68
|
+
request_dict, async_chunk_queue=q, evaluation_callback=evaluate_callback
|
|
79
69
|
)
|
|
80
70
|
)
|
|
81
71
|
return QueueIterable(q, timeout=timeout)
|
|
@@ -87,38 +77,43 @@ class Inference(MaitaiClient):
|
|
|
87
77
|
reference_id,
|
|
88
78
|
intent,
|
|
89
79
|
application_ref_name,
|
|
90
|
-
completion_params:
|
|
80
|
+
completion_params: dict,
|
|
91
81
|
chat_completion_response: Optional[ChatCompletionResponse],
|
|
92
82
|
final_chunk: Optional[ChatCompletionChunk],
|
|
93
83
|
content: str,
|
|
94
84
|
timing: RequestTimingMetric,
|
|
95
85
|
metadata: dict,
|
|
96
86
|
):
|
|
97
|
-
inference_request =
|
|
98
|
-
application_ref_name
|
|
99
|
-
session_id
|
|
100
|
-
reference_id
|
|
101
|
-
action_type
|
|
102
|
-
apply_corrections
|
|
103
|
-
evaluation_enabled
|
|
104
|
-
params
|
|
105
|
-
auth_keys
|
|
106
|
-
metadata
|
|
107
|
-
|
|
87
|
+
inference_request = {
|
|
88
|
+
"application_ref_name": application_ref_name,
|
|
89
|
+
"session_id": session_id,
|
|
90
|
+
"reference_id": reference_id,
|
|
91
|
+
"action_type": intent,
|
|
92
|
+
"apply_corrections": False,
|
|
93
|
+
"evaluation_enabled": False,
|
|
94
|
+
"params": completion_params,
|
|
95
|
+
"auth_keys": config.auth_keys.to_dict(),
|
|
96
|
+
"metadata": metadata,
|
|
97
|
+
}
|
|
98
|
+
|
|
108
99
|
if final_chunk:
|
|
109
100
|
chat_completion_response = chat_completion_chunk_to_response(
|
|
110
101
|
final_chunk, content
|
|
111
102
|
)
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
103
|
+
|
|
104
|
+
chat_storage_request = {
|
|
105
|
+
"chat_completion_request": inference_request,
|
|
106
|
+
"chat_completion_response": (
|
|
107
|
+
chat_completion_response.to_dict() if chat_completion_response else None
|
|
108
|
+
),
|
|
109
|
+
"timing_metrics": timing.to_dict(),
|
|
110
|
+
}
|
|
111
|
+
|
|
117
112
|
cls.run_async(cls.send_storage_request_async(chat_storage_request))
|
|
118
113
|
|
|
119
114
|
@classmethod
|
|
120
115
|
def send_inference_request(
|
|
121
|
-
cls,
|
|
116
|
+
cls, request_dict: dict, timeout
|
|
122
117
|
) -> Iterable[InferenceStreamResponse]:
|
|
123
118
|
host = config.maitai_host
|
|
124
119
|
url = f"{host}/chat/completions/serialized"
|
|
@@ -133,7 +128,7 @@ class Inference(MaitaiClient):
|
|
|
133
128
|
with session.post(
|
|
134
129
|
url,
|
|
135
130
|
headers=headers,
|
|
136
|
-
data=
|
|
131
|
+
data=json.dumps(request_dict),
|
|
137
132
|
stream=True,
|
|
138
133
|
timeout=timeout,
|
|
139
134
|
) as response:
|
|
@@ -152,7 +147,7 @@ class Inference(MaitaiClient):
|
|
|
152
147
|
@classmethod
|
|
153
148
|
async def send_inference_request_async(
|
|
154
149
|
cls,
|
|
155
|
-
|
|
150
|
+
request_dict: dict,
|
|
156
151
|
chunk_queue: ChunkQueue = None,
|
|
157
152
|
async_chunk_queue: AsyncChunkQueue = None,
|
|
158
153
|
evaluation_callback: EvaluateCallback = None,
|
|
@@ -171,7 +166,7 @@ class Inference(MaitaiClient):
|
|
|
171
166
|
timeout=ClientTimeout(timeout) if timeout else None,
|
|
172
167
|
) as session:
|
|
173
168
|
async with session.post(
|
|
174
|
-
url, headers=headers, data=
|
|
169
|
+
url, headers=headers, data=json.dumps(request_dict)
|
|
175
170
|
) as response:
|
|
176
171
|
if response.status != 200:
|
|
177
172
|
error_text = await response.text()
|
|
@@ -220,7 +215,7 @@ class Inference(MaitaiClient):
|
|
|
220
215
|
await async_chunk_queue.put(exception)
|
|
221
216
|
|
|
222
217
|
@classmethod
|
|
223
|
-
async def send_storage_request_async(cls,
|
|
218
|
+
async def send_storage_request_async(cls, storage_request: dict):
|
|
224
219
|
host = config.maitai_host
|
|
225
220
|
url = f"{host}/chat/completions/response"
|
|
226
221
|
headers = {
|
|
@@ -235,7 +230,7 @@ class Inference(MaitaiClient):
|
|
|
235
230
|
return await session.put(
|
|
236
231
|
url,
|
|
237
232
|
headers=headers,
|
|
238
|
-
data=
|
|
233
|
+
data=json.dumps(storage_request),
|
|
239
234
|
)
|
|
240
235
|
except Exception as e:
|
|
241
236
|
await cls.increment_error(str(e), url)
|
|
@@ -19,6 +19,7 @@ from maitai._inference import (
|
|
|
19
19
|
)
|
|
20
20
|
from maitai._types import (
|
|
21
21
|
Body,
|
|
22
|
+
EvaluateCallback,
|
|
22
23
|
Headers,
|
|
23
24
|
MaitaiChunk,
|
|
24
25
|
MaitaiCompletion,
|
|
@@ -32,15 +33,7 @@ from maitai._utils import (
|
|
|
32
33
|
required_args,
|
|
33
34
|
)
|
|
34
35
|
from maitai.tools import Tools
|
|
35
|
-
from
|
|
36
|
-
from maitai_common.utils.types import EvaluateCallback
|
|
37
|
-
from maitai_gen.chat import (
|
|
38
|
-
ChatCompletionParams,
|
|
39
|
-
ChatCompletionRequest,
|
|
40
|
-
ChatMessage,
|
|
41
|
-
ClientParams,
|
|
42
|
-
EvaluationContentType,
|
|
43
|
-
)
|
|
36
|
+
from maitai_gen.chat import ChatCompletionParams, ClientParams, EvaluationContentType
|
|
44
37
|
from maitai_gen.config import InferenceLocations
|
|
45
38
|
from maitai_gen.inference import InferenceStreamResponse
|
|
46
39
|
from maitai_gen.metric import RequestTimingMetric
|
|
@@ -301,31 +294,33 @@ class Completions:
|
|
|
301
294
|
response_timeout = None
|
|
302
295
|
if isinstance(timeout, float) or isinstance(timeout, int):
|
|
303
296
|
response_timeout = timeout
|
|
304
|
-
|
|
305
|
-
application_ref_name
|
|
306
|
-
session_id
|
|
307
|
-
reference_id
|
|
308
|
-
action_type
|
|
309
|
-
apply_corrections
|
|
310
|
-
evaluation_enabled
|
|
311
|
-
params
|
|
312
|
-
return_evaluation
|
|
313
|
-
context_retrieval_enabled
|
|
314
|
-
context_query
|
|
315
|
-
return_request
|
|
316
|
-
fallback_model
|
|
317
|
-
user_id
|
|
318
|
-
assistant
|
|
319
|
-
auth_keys
|
|
320
|
-
metadata
|
|
321
|
-
safe_mode
|
|
322
|
-
|
|
297
|
+
request_dict = {
|
|
298
|
+
"application_ref_name": application_ref_name,
|
|
299
|
+
"session_id": session_id,
|
|
300
|
+
"reference_id": reference_id,
|
|
301
|
+
"action_type": intent,
|
|
302
|
+
"apply_corrections": apply_corrections,
|
|
303
|
+
"evaluation_enabled": evaluation_enabled,
|
|
304
|
+
"params": completion_params,
|
|
305
|
+
"return_evaluation": True if callback else False,
|
|
306
|
+
"context_retrieval_enabled": context_retrieval_enabled,
|
|
307
|
+
"context_query": context_query,
|
|
308
|
+
"return_request": return_request,
|
|
309
|
+
"fallback_model": fallback_model,
|
|
310
|
+
"user_id": user_id,
|
|
311
|
+
"assistant": assistant,
|
|
312
|
+
"auth_keys": config.auth_keys.to_dict(),
|
|
313
|
+
"metadata": metadata,
|
|
314
|
+
"safe_mode": safe_mode,
|
|
315
|
+
}
|
|
316
|
+
|
|
323
317
|
if self._client_params is not None:
|
|
324
|
-
|
|
318
|
+
request_dict["client_params"] = self._client_params.to_dict()
|
|
319
|
+
|
|
325
320
|
try:
|
|
326
321
|
timing.time_pre_request = time.time()
|
|
327
322
|
response = maitai.Inference.infer(
|
|
328
|
-
|
|
323
|
+
request_dict, callback, response_timeout
|
|
329
324
|
)
|
|
330
325
|
if stream:
|
|
331
326
|
if "self" in retry_args:
|
|
@@ -416,20 +411,19 @@ class Completions:
|
|
|
416
411
|
else:
|
|
417
412
|
timing.time_request_end = time.time()
|
|
418
413
|
maitai_completion = convert_openai_chat_completion(response)
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
)
|
|
414
|
+
full_conv = messages + [
|
|
415
|
+
{
|
|
416
|
+
"role": "assistant",
|
|
417
|
+
"content": maitai_completion.choices[0].message.content,
|
|
418
|
+
}
|
|
419
|
+
]
|
|
426
420
|
if evaluation_enabled:
|
|
427
421
|
maitai.Evaluator.evaluate(
|
|
428
422
|
session_id=session_id,
|
|
429
423
|
reference_id=reference_id,
|
|
430
424
|
intent=intent,
|
|
431
425
|
content_type=EvaluationContentType.MESSAGE,
|
|
432
|
-
content=
|
|
426
|
+
content=full_conv,
|
|
433
427
|
application_ref_name=application_ref_name,
|
|
434
428
|
callback=callback,
|
|
435
429
|
chat_completion_response=maitai_completion,
|
|
@@ -467,7 +461,6 @@ def _process_openai_stream(
|
|
|
467
461
|
metadata: dict = {},
|
|
468
462
|
) -> Iterable[MaitaiChunk]:
|
|
469
463
|
full_body = ""
|
|
470
|
-
proto_messages = openai_messages_to_proto(messages)
|
|
471
464
|
last_chunk = None
|
|
472
465
|
for chunk in stream:
|
|
473
466
|
if last_chunk is None:
|
|
@@ -483,13 +476,13 @@ def _process_openai_stream(
|
|
|
483
476
|
yield MaitaiChunk(maitai_chunk)
|
|
484
477
|
timing.time_request_end = time.time()
|
|
485
478
|
if evaluation_enabled:
|
|
486
|
-
|
|
479
|
+
full_conv = messages + [{"role": "assistant", "content": full_body}]
|
|
487
480
|
Evaluator.evaluate(
|
|
488
481
|
session_id=session_id,
|
|
489
482
|
reference_id=reference_id,
|
|
490
483
|
intent=intent,
|
|
491
484
|
content_type=EvaluationContentType.MESSAGE,
|
|
492
|
-
content=
|
|
485
|
+
content=full_conv,
|
|
493
486
|
application_ref_name=application_ref_name,
|
|
494
487
|
callback=callback,
|
|
495
488
|
chat_completion_chunk=last_chunk,
|