maitai-python 1.1.3b1__tar.gz → 1.1.4b0__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.3b1 → maitai_python-1.1.4b0}/PKG-INFO +1 -1
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_inference.py +21 -1
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_maitai.py +17 -2
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_maitai_async.py +18 -2
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/version.py +1 -1
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/chat/__init__.py +1 -0
- maitai_python-1.1.4b0/maitai_gen/metric/__init__.py +20 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_python.egg-info/PKG-INFO +1 -1
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_python.egg-info/SOURCES.txt +1 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/README.md +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_azure.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_config.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_config_listener_thread.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_context.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_evaluator.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_maitai_client.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_types.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/_utils.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai/tools/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/config/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/config/config_service.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/processes/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/processes/io_thread.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/processes/websocket_listener_thread.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/utils/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/utils/proto_utils.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_common/utils/types.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/application/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/company/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/config/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/embedding/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/event/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/inference/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/key/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/model/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/notification/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/sandbox/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/sentinel/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_gen/session/__init__.py +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_python.egg-info/dependency_links.txt +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_python.egg-info/requires.txt +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/maitai_python.egg-info/top_level.txt +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/setup.cfg +0 -0
- {maitai_python-1.1.3b1 → maitai_python-1.1.4b0}/setup.py +0 -0
|
@@ -11,6 +11,7 @@ from maitai._utils import __version__ as version, chat_completion_chunk_to_respo
|
|
|
11
11
|
from maitai_common.utils.types import AsyncChunkQueue, ChunkQueue, EvaluateCallback, QueueIterable
|
|
12
12
|
from maitai_gen.chat import ChatCompletionChunk, ChatCompletionParams, ChatCompletionRequest, ChatCompletionResponse, ChatStorageRequest
|
|
13
13
|
from maitai_gen.inference import InferenceStreamResponse
|
|
14
|
+
from maitai_gen.metric import RequestTimingMetric
|
|
14
15
|
|
|
15
16
|
|
|
16
17
|
class InferenceException(Exception):
|
|
@@ -136,4 +137,23 @@ class Inference(MaitaiClient):
|
|
|
136
137
|
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
|
|
137
138
|
return await session.put(url, headers=headers, data=chat_storage_request.to_json(casing=Casing.SNAKE))
|
|
138
139
|
except:
|
|
139
|
-
|
|
140
|
+
pass
|
|
141
|
+
|
|
142
|
+
@classmethod
|
|
143
|
+
def store_request_timing_data(cls, metric: RequestTimingMetric):
|
|
144
|
+
cls.run_async(cls.send_request_timing_data(metric))
|
|
145
|
+
|
|
146
|
+
@classmethod
|
|
147
|
+
async def send_request_timing_data(cls, metric: RequestTimingMetric):
|
|
148
|
+
host = config.maitai_host
|
|
149
|
+
url = f'{host}/metrics/timing'
|
|
150
|
+
headers = {
|
|
151
|
+
'Content-Type': 'application/json',
|
|
152
|
+
'x-api-key': config.api_key,
|
|
153
|
+
'x-client-version': version,
|
|
154
|
+
}
|
|
155
|
+
try:
|
|
156
|
+
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
|
|
157
|
+
return await session.put(url, headers=headers, data=metric.to_json(casing=Casing.SNAKE))
|
|
158
|
+
except:
|
|
159
|
+
pass
|
|
@@ -22,6 +22,7 @@ from maitai_common.utils.types import EvaluateCallback
|
|
|
22
22
|
from maitai_gen.chat import ChatCompletionParams, ChatCompletionRequest, ChatMessage, ClientParams, EvaluationContentType
|
|
23
23
|
from maitai_gen.config import InferenceLocations
|
|
24
24
|
from maitai_gen.inference import InferenceStreamResponse
|
|
25
|
+
from maitai_gen.metric import RequestTimingMetric
|
|
25
26
|
|
|
26
27
|
DEFAULT_MAX_RETRIES = 2
|
|
27
28
|
|
|
@@ -149,6 +150,9 @@ class Completions:
|
|
|
149
150
|
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NOT_GIVEN,
|
|
150
151
|
) -> Union[MaitaiCompletion, Iterable[MaitaiChunk]]:
|
|
151
152
|
retry_args = locals()
|
|
153
|
+
timing = RequestTimingMetric(
|
|
154
|
+
time_request_start=time.time(),
|
|
155
|
+
)
|
|
152
156
|
if not config.api_key:
|
|
153
157
|
raise ValueError("Maitai API Key has not been set")
|
|
154
158
|
if server_side_inference is False and apply_corrections is True:
|
|
@@ -237,6 +241,7 @@ class Completions:
|
|
|
237
241
|
client_params=self._client_params,
|
|
238
242
|
)
|
|
239
243
|
try:
|
|
244
|
+
timing.time_pre_request = time.time()
|
|
240
245
|
response = maitai.Inference.infer(chat_request, callback, response_timeout)
|
|
241
246
|
if stream:
|
|
242
247
|
if 'self' in retry_args:
|
|
@@ -244,13 +249,16 @@ class Completions:
|
|
|
244
249
|
retry_args['server_side_inference'] = False
|
|
245
250
|
retry_args['model'] = model
|
|
246
251
|
retry_args['fallback_model'] = fallback_model
|
|
247
|
-
return _process_inference_stream(response, retry_args, self.create)
|
|
252
|
+
return _process_inference_stream(response, retry_args, self.create, timing)
|
|
248
253
|
# ChatCompletion only
|
|
249
254
|
for resp in response:
|
|
250
255
|
if resp.warning:
|
|
251
256
|
warnings.warn(resp.warning, InferenceWarning)
|
|
252
257
|
if resp.error:
|
|
253
258
|
raise InferenceException(resp.error)
|
|
259
|
+
timing.request_id = resp.chat_completion_response.request_id
|
|
260
|
+
timing.time_request_end = time.time()
|
|
261
|
+
maitai.Inference.store_request_timing_data(timing)
|
|
254
262
|
return MaitaiCompletion(resp.chat_completion_response)
|
|
255
263
|
raise InferenceException("No data received")
|
|
256
264
|
except MaitaiConnectionError as e:
|
|
@@ -376,7 +384,8 @@ def _process_openai_stream(session_id: Union[str, int],
|
|
|
376
384
|
)
|
|
377
385
|
|
|
378
386
|
|
|
379
|
-
def _process_inference_stream(stream: Iterable[InferenceStreamResponse], retry_args: dict, retry_func) -> Iterable[MaitaiChunk]:
|
|
387
|
+
def _process_inference_stream(stream: Iterable[InferenceStreamResponse], retry_args: dict, retry_func, timing: RequestTimingMetric) -> Iterable[MaitaiChunk]:
|
|
388
|
+
first = True
|
|
380
389
|
try:
|
|
381
390
|
for infer_resp in stream:
|
|
382
391
|
if infer_resp.warning:
|
|
@@ -385,8 +394,14 @@ def _process_inference_stream(stream: Iterable[InferenceStreamResponse], retry_a
|
|
|
385
394
|
raise InferenceException(infer_resp.error)
|
|
386
395
|
chunk = infer_resp.chat_completion_chunk
|
|
387
396
|
if chunk is not None:
|
|
397
|
+
if first:
|
|
398
|
+
timing.time_first_chunk = time.time()
|
|
399
|
+
timing.request_id = chunk.request_id
|
|
400
|
+
first = False
|
|
388
401
|
yield MaitaiChunk(chunk)
|
|
389
402
|
if chunk.choices and chunk.choices[0].finish_reason:
|
|
403
|
+
timing.time_request_end = time.time()
|
|
404
|
+
maitai.Inference.store_request_timing_data(timing)
|
|
390
405
|
return
|
|
391
406
|
except MaitaiConnectionError as e:
|
|
392
407
|
if 'gpt' not in retry_args.get('model', '') and 'gpt' in retry_args.get('fallback_model', ''):
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import time
|
|
1
2
|
import uuid
|
|
2
3
|
import warnings
|
|
3
4
|
from typing import AsyncIterable, Dict, Iterable, List, Mapping, Optional, Union
|
|
@@ -20,6 +21,7 @@ from maitai_common.utils.types import EvaluateCallback
|
|
|
20
21
|
from maitai_gen.chat import ChatCompletionParams, ChatCompletionRequest, ChatMessage, ClientParams, EvaluationContentType
|
|
21
22
|
from maitai_gen.config import InferenceLocations
|
|
22
23
|
from maitai_gen.inference import InferenceStreamResponse
|
|
24
|
+
from maitai_gen.metric import RequestTimingMetric
|
|
23
25
|
|
|
24
26
|
DEFAULT_MAX_RETRIES = 2
|
|
25
27
|
|
|
@@ -143,6 +145,9 @@ class AsyncCompletions:
|
|
|
143
145
|
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NOT_GIVEN,
|
|
144
146
|
) -> Union[MaitaiCompletion, AsyncIterable[MaitaiChunk]]:
|
|
145
147
|
retry_args = locals()
|
|
148
|
+
timing = RequestTimingMetric(
|
|
149
|
+
time_request_start=time.time(),
|
|
150
|
+
)
|
|
146
151
|
if not config.api_key:
|
|
147
152
|
raise ValueError("Maitai API Key has not been set")
|
|
148
153
|
if server_side_inference is False and apply_corrections is True:
|
|
@@ -231,6 +236,7 @@ class AsyncCompletions:
|
|
|
231
236
|
client_params=self._client_params,
|
|
232
237
|
)
|
|
233
238
|
try:
|
|
239
|
+
timing.time_pre_request = time.time()
|
|
234
240
|
response = await maitai.Inference.infer_async(chat_request, callback, response_timeout)
|
|
235
241
|
if stream:
|
|
236
242
|
if 'self' in retry_args:
|
|
@@ -238,13 +244,16 @@ class AsyncCompletions:
|
|
|
238
244
|
retry_args['server_side_inference'] = False
|
|
239
245
|
retry_args['model'] = model
|
|
240
246
|
retry_args['fallback_model'] = fallback_model
|
|
241
|
-
return _process_inference_stream_async(response, retry_args, self.create)
|
|
247
|
+
return _process_inference_stream_async(response, retry_args, self.create, timing)
|
|
242
248
|
# ChatCompletion only
|
|
243
249
|
async for resp in response:
|
|
244
250
|
if resp.warning:
|
|
245
251
|
warnings.warn(resp.warning, InferenceWarning)
|
|
246
252
|
if resp.error:
|
|
247
253
|
raise InferenceException(resp.error)
|
|
254
|
+
timing.request_id = resp.chat_completion_response.request_id
|
|
255
|
+
timing.time_request_end = time.time()
|
|
256
|
+
maitai.Inference.store_request_timing_data(timing)
|
|
248
257
|
return MaitaiCompletion(resp.chat_completion_response)
|
|
249
258
|
raise InferenceException("No data received")
|
|
250
259
|
except MaitaiConnectionError as e:
|
|
@@ -372,7 +381,8 @@ async def _process_async_openai_stream(session_id: Union[str, int],
|
|
|
372
381
|
)
|
|
373
382
|
|
|
374
383
|
|
|
375
|
-
async def _process_inference_stream_async(stream: AsyncIterable[InferenceStreamResponse], retry_args: dict, retry_func) -> AsyncIterable[MaitaiChunk]:
|
|
384
|
+
async def _process_inference_stream_async(stream: AsyncIterable[InferenceStreamResponse], retry_args: dict, retry_func, timing: RequestTimingMetric) -> AsyncIterable[MaitaiChunk]:
|
|
385
|
+
first = True
|
|
376
386
|
try:
|
|
377
387
|
async for infer_resp in stream:
|
|
378
388
|
if infer_resp.warning:
|
|
@@ -381,8 +391,14 @@ async def _process_inference_stream_async(stream: AsyncIterable[InferenceStreamR
|
|
|
381
391
|
raise InferenceException(infer_resp.error)
|
|
382
392
|
chunk = infer_resp.chat_completion_chunk
|
|
383
393
|
if chunk is not None:
|
|
394
|
+
if first:
|
|
395
|
+
timing.time_first_chunk = time.time()
|
|
396
|
+
timing.request_id = chunk.request_id
|
|
397
|
+
first = False
|
|
384
398
|
yield MaitaiChunk(chunk)
|
|
385
399
|
if chunk.choices and chunk.choices[0].finish_reason:
|
|
400
|
+
timing.time_request_end = time.time()
|
|
401
|
+
maitai.Inference.store_request_timing_data(timing)
|
|
386
402
|
return
|
|
387
403
|
except MaitaiConnectionError as e:
|
|
388
404
|
if 'gpt' not in retry_args.get('model', '') and 'gpt' in retry_args.get('fallback_model', ''):
|
|
@@ -238,6 +238,7 @@ class ChatCompletionChunk(betterproto.Message):
|
|
|
238
238
|
10, optional=True, group="_service_tier"
|
|
239
239
|
)
|
|
240
240
|
input_safety_score: float = betterproto.float_field(11)
|
|
241
|
+
request_id: str = betterproto.string_field(12)
|
|
241
242
|
|
|
242
243
|
|
|
243
244
|
@dataclass(eq=False, repr=False)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
|
2
|
+
# sources: metric.proto
|
|
3
|
+
# plugin: python-betterproto
|
|
4
|
+
# This file has been @generated
|
|
5
|
+
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Optional
|
|
8
|
+
|
|
9
|
+
import betterproto
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(eq=False, repr=False)
|
|
13
|
+
class RequestTimingMetric(betterproto.Message):
|
|
14
|
+
time_request_start: float = betterproto.float_field(1)
|
|
15
|
+
time_pre_request: float = betterproto.float_field(2)
|
|
16
|
+
time_first_chunk: Optional[float] = betterproto.float_field(
|
|
17
|
+
3, optional=True, group="_time_first_chunk"
|
|
18
|
+
)
|
|
19
|
+
time_request_end: float = betterproto.float_field(4)
|
|
20
|
+
request_id: str = betterproto.string_field(5)
|
|
@@ -32,6 +32,7 @@ maitai_gen/embedding/__init__.py
|
|
|
32
32
|
maitai_gen/event/__init__.py
|
|
33
33
|
maitai_gen/inference/__init__.py
|
|
34
34
|
maitai_gen/key/__init__.py
|
|
35
|
+
maitai_gen/metric/__init__.py
|
|
35
36
|
maitai_gen/model/__init__.py
|
|
36
37
|
maitai_gen/notification/__init__.py
|
|
37
38
|
maitai_gen/sandbox/__init__.py
|
|
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.3b1 → maitai_python-1.1.4b0}/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
|