maitai-python 1.0.1b2__tar.gz → 1.0.3__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.0.1b2 → maitai_python-1.0.3}/PKG-INFO +1 -1
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/__init__.py +1 -0
- maitai_python-1.0.3/maitai/_azure.py +223 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_evaluator.py +47 -18
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_inference.py +0 -8
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_openai.py +26 -10
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_openai_async.py +18 -2
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_common/processes/io_thread.py +5 -2
- maitai_python-1.0.3/maitai_common/processes/stream_evaluation_thread.py +23 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_gen/chat/__init__.py +44 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_python.egg-info/PKG-INFO +1 -1
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_python.egg-info/SOURCES.txt +2 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/setup.py +1 -1
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/README.md +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_config.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_config_listener_thread.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_maitai_client.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_openai_types.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_types.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai/_utils.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_common/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_common/processes/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_common/processes/websocket_listener_thread.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_common/utils/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_common/utils/proto_utils.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_gen/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_gen/application/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_gen/company/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_gen/inference/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_gen/notification/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_gen/sandbox/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_gen/sentinel/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_gen/session/__init__.py +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_python.egg-info/dependency_links.txt +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_python.egg-info/requires.txt +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/maitai_python.egg-info/top_level.txt +0 -0
- {maitai_python-1.0.1b2 → maitai_python-1.0.3}/setup.cfg +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import os
|
|
2
2
|
|
|
3
3
|
import maitai._types as types
|
|
4
|
+
from maitai._azure import MaiTaiAsyncAzureOpenAIClient as AsyncAzureOpenAI, MaitaiAzureOpenAIClient as AzureOpenAI
|
|
4
5
|
from maitai._evaluator import Evaluator as Evaluator
|
|
5
6
|
from maitai._inference import Inference as Inference
|
|
6
7
|
from maitai._openai import MaiTaiOpenAIClient as OpenAI
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
from typing import Mapping, Optional, overload, Union
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
import openai
|
|
5
|
+
from openai.lib.azure import AsyncAzureADTokenProvider, AzureADTokenProvider
|
|
6
|
+
|
|
7
|
+
from maitai._config import config
|
|
8
|
+
from maitai._openai import Chat
|
|
9
|
+
from maitai._openai_async import AsyncChat
|
|
10
|
+
|
|
11
|
+
DEFAULT_MAX_RETRIES = 2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MaiTaiAsyncAzureOpenAIClient:
|
|
15
|
+
@overload
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
*,
|
|
19
|
+
azure_endpoint: str,
|
|
20
|
+
maitai_api_key: Optional[str] = None,
|
|
21
|
+
azure_deployment: Optional[str] = None,
|
|
22
|
+
api_version: Optional[str] = None,
|
|
23
|
+
api_key: Optional[str] = None,
|
|
24
|
+
azure_ad_token: Optional[str] = None,
|
|
25
|
+
azure_ad_token_provider: Optional[AsyncAzureADTokenProvider] = None,
|
|
26
|
+
organization: Optional[str] = None,
|
|
27
|
+
project: Optional[str] = None,
|
|
28
|
+
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NotGiven,
|
|
29
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
30
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
31
|
+
default_query: Optional[Mapping[str, object]] = None,
|
|
32
|
+
http_client: Optional[httpx.AsyncClient] = None,
|
|
33
|
+
_strict_response_validation: bool = False,
|
|
34
|
+
) -> None:
|
|
35
|
+
...
|
|
36
|
+
|
|
37
|
+
@overload
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
*,
|
|
41
|
+
maitai_api_key: Optional[str] = None,
|
|
42
|
+
azure_deployment: Optional[str] = None,
|
|
43
|
+
api_version: Optional[str] = None,
|
|
44
|
+
api_key: Optional[str] = None,
|
|
45
|
+
azure_ad_token: Optional[str] = None,
|
|
46
|
+
azure_ad_token_provider: Optional[AsyncAzureADTokenProvider] = None,
|
|
47
|
+
organization: Optional[str] = None,
|
|
48
|
+
project: Optional[str] = None,
|
|
49
|
+
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NotGiven,
|
|
50
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
51
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
52
|
+
default_query: Optional[Mapping[str, object]] = None,
|
|
53
|
+
http_client: Optional[httpx.AsyncClient] = None,
|
|
54
|
+
_strict_response_validation: bool = False,
|
|
55
|
+
) -> None:
|
|
56
|
+
...
|
|
57
|
+
|
|
58
|
+
@overload
|
|
59
|
+
def __init__(
|
|
60
|
+
self,
|
|
61
|
+
*,
|
|
62
|
+
base_url: str,
|
|
63
|
+
maitai_api_key: Optional[str] = None,
|
|
64
|
+
api_version: Optional[str] = None,
|
|
65
|
+
api_key: Optional[str] = None,
|
|
66
|
+
azure_ad_token: Optional[str] = None,
|
|
67
|
+
azure_ad_token_provider: Optional[AsyncAzureADTokenProvider] = None,
|
|
68
|
+
organization: Optional[str] = None,
|
|
69
|
+
project: Optional[str] = None,
|
|
70
|
+
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NotGiven,
|
|
71
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
72
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
73
|
+
default_query: Optional[Mapping[str, object]] = None,
|
|
74
|
+
http_client: Optional[httpx.AsyncClient] = None,
|
|
75
|
+
_strict_response_validation: bool = False,
|
|
76
|
+
) -> None:
|
|
77
|
+
...
|
|
78
|
+
|
|
79
|
+
def __init__(
|
|
80
|
+
self,
|
|
81
|
+
*,
|
|
82
|
+
maitai_api_key: Optional[str] = None,
|
|
83
|
+
azure_endpoint: Optional[str] = None,
|
|
84
|
+
azure_deployment: Optional[str] = None,
|
|
85
|
+
api_version: Optional[str] = None,
|
|
86
|
+
api_key: Optional[str] = None,
|
|
87
|
+
azure_ad_token: Optional[str] = None,
|
|
88
|
+
azure_ad_token_provider: Optional[AsyncAzureADTokenProvider] = None,
|
|
89
|
+
organization: Optional[str] = None,
|
|
90
|
+
project: Optional[str] = None,
|
|
91
|
+
base_url: Optional[str] = None,
|
|
92
|
+
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NotGiven,
|
|
93
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
94
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
95
|
+
default_query: Optional[Mapping[str, object]] = None,
|
|
96
|
+
http_client: Optional[httpx.AsyncClient] = None,
|
|
97
|
+
_strict_response_validation: bool = False,
|
|
98
|
+
):
|
|
99
|
+
if maitai_api_key:
|
|
100
|
+
config.initialize(maitai_api_key)
|
|
101
|
+
self.client = openai.AsyncAzureOpenAI(
|
|
102
|
+
azure_endpoint=azure_endpoint,
|
|
103
|
+
azure_deployment=azure_deployment,
|
|
104
|
+
api_version=api_version,
|
|
105
|
+
api_key=api_key,
|
|
106
|
+
azure_ad_token=azure_ad_token,
|
|
107
|
+
azure_ad_token_provider=azure_ad_token_provider,
|
|
108
|
+
organization=organization,
|
|
109
|
+
project=project,
|
|
110
|
+
base_url=base_url,
|
|
111
|
+
timeout=timeout,
|
|
112
|
+
max_retries=max_retries,
|
|
113
|
+
default_headers=default_headers,
|
|
114
|
+
default_query=default_query,
|
|
115
|
+
http_client=http_client,
|
|
116
|
+
_strict_response_validation=_strict_response_validation,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
self.chat = AsyncChat(self.client)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class MaitaiAzureOpenAIClient:
|
|
123
|
+
@overload
|
|
124
|
+
def __init__(
|
|
125
|
+
self,
|
|
126
|
+
*,
|
|
127
|
+
maitai_api_key: Optional[str] = None,
|
|
128
|
+
azure_endpoint: str,
|
|
129
|
+
azure_deployment: Optional[str] = None,
|
|
130
|
+
api_version: Optional[str] = None,
|
|
131
|
+
api_key: Optional[str] = None,
|
|
132
|
+
azure_ad_token: Optional[str] = None,
|
|
133
|
+
azure_ad_token_provider: Optional[AzureADTokenProvider] = None,
|
|
134
|
+
organization: Optional[str] = None,
|
|
135
|
+
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NotGiven,
|
|
136
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
137
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
138
|
+
default_query: Optional[Mapping[str, object]] = None,
|
|
139
|
+
http_client: Optional[httpx.Client] = None,
|
|
140
|
+
_strict_response_validation: bool = False,
|
|
141
|
+
) -> None:
|
|
142
|
+
...
|
|
143
|
+
|
|
144
|
+
@overload
|
|
145
|
+
def __init__(
|
|
146
|
+
self,
|
|
147
|
+
*,
|
|
148
|
+
maitai_api_key: Optional[str] = None,
|
|
149
|
+
azure_deployment: Optional[str] = None,
|
|
150
|
+
api_version: Optional[str] = None,
|
|
151
|
+
api_key: Optional[str] = None,
|
|
152
|
+
azure_ad_token: Optional[str] = None,
|
|
153
|
+
azure_ad_token_provider: Optional[AzureADTokenProvider] = None,
|
|
154
|
+
organization: Optional[str] = None,
|
|
155
|
+
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NotGiven,
|
|
156
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
157
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
158
|
+
default_query: Optional[Mapping[str, object]] = None,
|
|
159
|
+
http_client: Optional[httpx.Client] = None,
|
|
160
|
+
_strict_response_validation: bool = False,
|
|
161
|
+
) -> None:
|
|
162
|
+
...
|
|
163
|
+
|
|
164
|
+
@overload
|
|
165
|
+
def __init__(
|
|
166
|
+
self,
|
|
167
|
+
*,
|
|
168
|
+
base_url: str,
|
|
169
|
+
maitai_api_key: Optional[str] = None,
|
|
170
|
+
api_version: Optional[str] = None,
|
|
171
|
+
api_key: Optional[str] = None,
|
|
172
|
+
azure_ad_token: Optional[str] = None,
|
|
173
|
+
azure_ad_token_provider: Optional[AzureADTokenProvider] = None,
|
|
174
|
+
organization: Optional[str] = None,
|
|
175
|
+
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NotGiven,
|
|
176
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
177
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
178
|
+
default_query: Optional[Mapping[str, object]] = None,
|
|
179
|
+
http_client: Optional[httpx.Client] = None,
|
|
180
|
+
_strict_response_validation: bool = False,
|
|
181
|
+
) -> None:
|
|
182
|
+
...
|
|
183
|
+
|
|
184
|
+
def __init__(
|
|
185
|
+
self,
|
|
186
|
+
*,
|
|
187
|
+
maitai_api_key: Optional[str] = None,
|
|
188
|
+
api_version: Optional[str] = None,
|
|
189
|
+
azure_endpoint: Optional[str] = None,
|
|
190
|
+
azure_deployment: Optional[str] = None,
|
|
191
|
+
api_key: Optional[str] = None,
|
|
192
|
+
azure_ad_token: Optional[str] = None,
|
|
193
|
+
azure_ad_token_provider: Optional[AzureADTokenProvider] = None,
|
|
194
|
+
organization: Optional[str] = None,
|
|
195
|
+
project: Optional[str] = None,
|
|
196
|
+
base_url: Optional[str] = None,
|
|
197
|
+
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NotGiven,
|
|
198
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
199
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
200
|
+
default_query: Optional[Mapping[str, object]] = None,
|
|
201
|
+
http_client: Optional[httpx.Client] = None,
|
|
202
|
+
_strict_response_validation: bool = False,
|
|
203
|
+
) -> None:
|
|
204
|
+
if maitai_api_key:
|
|
205
|
+
config.initialize(maitai_api_key)
|
|
206
|
+
self.client = openai.AzureOpenAI(
|
|
207
|
+
azure_endpoint=azure_endpoint,
|
|
208
|
+
azure_deployment=azure_deployment,
|
|
209
|
+
api_version=api_version,
|
|
210
|
+
api_key=api_key,
|
|
211
|
+
azure_ad_token=azure_ad_token,
|
|
212
|
+
azure_ad_token_provider=azure_ad_token_provider,
|
|
213
|
+
organization=organization,
|
|
214
|
+
project=project,
|
|
215
|
+
base_url=base_url,
|
|
216
|
+
timeout=timeout,
|
|
217
|
+
max_retries=max_retries,
|
|
218
|
+
default_headers=default_headers,
|
|
219
|
+
default_query=default_query,
|
|
220
|
+
http_client=http_client,
|
|
221
|
+
_strict_response_validation=_strict_response_validation,
|
|
222
|
+
)
|
|
223
|
+
self.chat = Chat(self.client)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import json
|
|
2
|
-
import
|
|
2
|
+
from typing import Iterable
|
|
3
3
|
|
|
4
4
|
import aiohttp
|
|
5
5
|
import requests
|
|
@@ -8,10 +8,12 @@ from betterproto import Casing
|
|
|
8
8
|
from maitai._config import config
|
|
9
9
|
from maitai._maitai_client import MaiTaiClient
|
|
10
10
|
from maitai._types import EvaluateCallback
|
|
11
|
-
from maitai_gen.chat import EvaluateRequest, EvaluateResponse, EvaluationContentType
|
|
11
|
+
from maitai_gen.chat import ChatCompletionChunk, EvaluateRequest, EvaluateResponse, EvaluationContentType
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
def _get_content_type(content):
|
|
14
|
+
def _get_content_type(content, partial=None):
|
|
15
|
+
if partial is not None:
|
|
16
|
+
return EvaluateResponse.PARTIAL_CONTENT
|
|
15
17
|
if isinstance(content, str):
|
|
16
18
|
return EvaluationContentType.TEXT
|
|
17
19
|
elif isinstance(content, list):
|
|
@@ -39,36 +41,48 @@ class Evaluator(MaiTaiClient):
|
|
|
39
41
|
return None
|
|
40
42
|
|
|
41
43
|
@classmethod
|
|
42
|
-
def evaluate(cls, session_id, reference_id, action_type, content, content_type=None, application_id=None, application_ref_name=None, callback=None):
|
|
44
|
+
def evaluate(cls, session_id, reference_id, action_type, content, content_type=None, application_id=None, application_ref_name=None, callback=None, partial=None):
|
|
43
45
|
if content_type is None:
|
|
44
|
-
content_type = _get_content_type(content)
|
|
46
|
+
content_type = _get_content_type(content, partial)
|
|
45
47
|
if content_type is None:
|
|
46
48
|
raise Exception('Unable to automatically determine content_type')
|
|
47
49
|
if application_id is None and application_ref_name is None:
|
|
48
50
|
raise Exception('application_id or application_ref_name must be provided')
|
|
49
|
-
eval_request: EvaluateRequest = cls.create_eval_request(application_id, application_ref_name, session_id, reference_id, action_type, content_type, content)
|
|
51
|
+
eval_request: EvaluateRequest = cls.create_eval_request(application_id, application_ref_name, session_id, reference_id, action_type, content_type, content, partial=partial)
|
|
50
52
|
if callback is None:
|
|
51
53
|
return cls.send_evaluation_request(eval_request)
|
|
52
54
|
else:
|
|
53
55
|
cls.run_async(cls.send_evaluation_request_async(eval_request, callback))
|
|
54
56
|
return None
|
|
55
57
|
|
|
58
|
+
@classmethod
|
|
59
|
+
def evaluate_realtime(cls, session_id, reference_id, action_type, content_type, content, application_ref_name, partial, fault_description, sentinel_id):
|
|
60
|
+
eval_request: EvaluateRequest = cls.create_eval_request(None, application_ref_name, session_id, reference_id, action_type, content_type, content, partial=partial,
|
|
61
|
+
fault_description=fault_description)
|
|
62
|
+
eval_request.sentinel_id = sentinel_id
|
|
63
|
+
return cls.send_realtime_eval_request(eval_request)
|
|
64
|
+
|
|
56
65
|
@classmethod
|
|
57
66
|
def create_eval_request(cls, application_id, application_ref_name, session_id, reference_id, action_type,
|
|
58
|
-
content_type, content):
|
|
67
|
+
content_type, content, partial=None, fault_description=None):
|
|
59
68
|
eval_request: EvaluateRequest = EvaluateRequest()
|
|
60
69
|
eval_request.evaluation_content_type = content_type
|
|
61
70
|
if content_type == EvaluationContentType.TEXT:
|
|
62
71
|
if type(content) != str:
|
|
63
72
|
raise Exception('Content must be a string')
|
|
64
73
|
eval_request.text_content = content
|
|
65
|
-
|
|
74
|
+
elif content_type == EvaluationContentType.MESSAGE:
|
|
75
|
+
eval_request.message_content = content
|
|
76
|
+
elif content_type == EvaluationContentType.PARTIAL:
|
|
66
77
|
eval_request.message_content = content
|
|
78
|
+
eval_request.text_content = partial
|
|
67
79
|
eval_request.application_id = application_id
|
|
68
80
|
eval_request.application_ref_name = application_ref_name
|
|
69
81
|
eval_request.session_id = session_id
|
|
70
82
|
eval_request.reference_id = reference_id
|
|
71
83
|
eval_request.action_type = action_type
|
|
84
|
+
if fault_description:
|
|
85
|
+
eval_request.fault_description = fault_description
|
|
72
86
|
return eval_request
|
|
73
87
|
|
|
74
88
|
@classmethod
|
|
@@ -127,8 +141,6 @@ class Evaluator(MaiTaiClient):
|
|
|
127
141
|
|
|
128
142
|
@classmethod
|
|
129
143
|
async def send_evaluation_request_async(cls, eval_request: EvaluateRequest, callback: EvaluateCallback = None):
|
|
130
|
-
start = time.time()
|
|
131
|
-
|
|
132
144
|
async def send_request():
|
|
133
145
|
host = config.maitai_host
|
|
134
146
|
url = f'{host}/evaluation/request'
|
|
@@ -143,9 +155,6 @@ class Evaluator(MaiTaiClient):
|
|
|
143
155
|
error_text = await response.text()
|
|
144
156
|
print(f"Failed to send evaluation request. Status code: {response.status}. Error: {error_text}")
|
|
145
157
|
return None
|
|
146
|
-
else:
|
|
147
|
-
print(f"Successfully sent evaluation request. Status code: {response.status}")
|
|
148
|
-
print(time.time() - start)
|
|
149
158
|
return await response.read()
|
|
150
159
|
|
|
151
160
|
result = await send_request()
|
|
@@ -158,8 +167,6 @@ class Evaluator(MaiTaiClient):
|
|
|
158
167
|
|
|
159
168
|
@classmethod
|
|
160
169
|
def send_evaluation_request(cls, eval_request: EvaluateRequest) -> EvaluateResponse:
|
|
161
|
-
start = time.time()
|
|
162
|
-
|
|
163
170
|
def send_request():
|
|
164
171
|
host = config.maitai_host
|
|
165
172
|
url = f'{host}/evaluation/request'
|
|
@@ -172,9 +179,6 @@ class Evaluator(MaiTaiClient):
|
|
|
172
179
|
if response.status_code != 200:
|
|
173
180
|
print(f"Failed to send evaluation request. Status code: {response.status_code}. Error: {response.text}")
|
|
174
181
|
return None
|
|
175
|
-
else:
|
|
176
|
-
print(f"Successfully sent evaluation request. Status code: {response.status_code}")
|
|
177
|
-
print(time.time() - start)
|
|
178
182
|
return response.content
|
|
179
183
|
|
|
180
184
|
result = send_request()
|
|
@@ -273,3 +277,28 @@ class Evaluator(MaiTaiClient):
|
|
|
273
277
|
print(f"An error occurred while sending application context for append: {e}")
|
|
274
278
|
|
|
275
279
|
await send_context()
|
|
280
|
+
|
|
281
|
+
@classmethod
|
|
282
|
+
def send_realtime_eval_request(cls, eval_request: EvaluateRequest) -> Iterable[ChatCompletionChunk]:
|
|
283
|
+
def consume_stream():
|
|
284
|
+
host = config.maitai_host
|
|
285
|
+
url = f'{host}/evaluation/realtime'
|
|
286
|
+
headers = {
|
|
287
|
+
'Content-Type': 'application/json',
|
|
288
|
+
'x-api-key': config.api_key
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
response = requests.post(url, headers=headers, data=eval_request.to_json(casing=Casing.SNAKE), verify=False, stream=True)
|
|
292
|
+
if response.status_code != 200:
|
|
293
|
+
print(f"Failed to send realtime evaluation request. Status code: {response.status_code}. Error: {response.text}")
|
|
294
|
+
return
|
|
295
|
+
try:
|
|
296
|
+
for line in response.iter_lines():
|
|
297
|
+
if line:
|
|
298
|
+
yield line
|
|
299
|
+
finally:
|
|
300
|
+
response.close()
|
|
301
|
+
|
|
302
|
+
for resp in consume_stream():
|
|
303
|
+
inference_response: ChatCompletionChunk = ChatCompletionChunk().from_json(resp)
|
|
304
|
+
yield inference_response
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import time
|
|
2
1
|
from typing import AsyncIterable, Iterable
|
|
3
2
|
|
|
4
3
|
import aiohttp
|
|
@@ -51,8 +50,6 @@ class Inference(MaiTaiClient):
|
|
|
51
50
|
|
|
52
51
|
@classmethod
|
|
53
52
|
def send_inference_request(cls, chat_request: ChatCompletionRequest) -> Iterable[InferenceStreamResponse]:
|
|
54
|
-
start = time.time()
|
|
55
|
-
|
|
56
53
|
def consume_stream():
|
|
57
54
|
host = config.maitai_host
|
|
58
55
|
url = f'{host}/chat/completions/serialized'
|
|
@@ -77,12 +74,9 @@ class Inference(MaiTaiClient):
|
|
|
77
74
|
for resp in consume_stream():
|
|
78
75
|
inference_response: InferenceStreamResponse = InferenceStreamResponse().from_json(resp)
|
|
79
76
|
yield inference_response
|
|
80
|
-
print(time.time() - start)
|
|
81
77
|
|
|
82
78
|
@classmethod
|
|
83
79
|
async def send_inference_request_async(cls, chat_request: ChatCompletionRequest, chunk_queue: ChunkQueue = None, async_chunk_queue: AsyncChunkQueue = None, evaluation_callback: EvaluateCallback = None):
|
|
84
|
-
start = time.time()
|
|
85
|
-
|
|
86
80
|
host = config.maitai_host
|
|
87
81
|
url = f'{host}/chat/completions/serialized'
|
|
88
82
|
headers = {
|
|
@@ -99,8 +93,6 @@ class Inference(MaiTaiClient):
|
|
|
99
93
|
if async_chunk_queue:
|
|
100
94
|
await async_chunk_queue.put("done")
|
|
101
95
|
return
|
|
102
|
-
else:
|
|
103
|
-
print(f"Successfully sent evaluation request. Status code: {response.status}")
|
|
104
96
|
last_item = None
|
|
105
97
|
async for line in response.content:
|
|
106
98
|
if line:
|
|
@@ -11,8 +11,9 @@ from maitai._openai_types import Body, Headers, Query
|
|
|
11
11
|
from maitai._types import EvaluateCallback
|
|
12
12
|
from maitai._utils import convert_open_ai_chat_completion_chunk, convert_openai_chat_completion, get_chat_completion_params, process_chunk, process_stream_response, required_args, \
|
|
13
13
|
set_completion_evaluation_response
|
|
14
|
+
from maitai_common.processes.stream_evaluation_thread import StreamEvaluationThread
|
|
14
15
|
from maitai_common.utils.proto_utils import openai_messages_to_proto
|
|
15
|
-
from maitai_gen.chat import ChatCompletionChunk, ChatCompletionResponse, ChatMessage, EvaluationContentType
|
|
16
|
+
from maitai_gen.chat import ChatCompletionChunk, ChatCompletionResponse, ChatMessage, EvaluateResponse, EvaluationContentType
|
|
16
17
|
from maitai_gen.inference import InferenceStreamResponse
|
|
17
18
|
|
|
18
19
|
DEFAULT_MAX_RETRIES = 2
|
|
@@ -46,6 +47,7 @@ class MaiTaiOpenAIClient:
|
|
|
46
47
|
default_headers=default_headers,
|
|
47
48
|
default_query=default_query,
|
|
48
49
|
http_client=http_client,
|
|
50
|
+
_strict_response_validation=_strict_response_validation,
|
|
49
51
|
)
|
|
50
52
|
self.chat = Chat(self.client)
|
|
51
53
|
|
|
@@ -172,7 +174,7 @@ class Completions:
|
|
|
172
174
|
|
|
173
175
|
)
|
|
174
176
|
if stream:
|
|
175
|
-
return
|
|
177
|
+
return _process_openai_stream(session_id, reference_id, action_type, application_ref_name, messages, response, callback)
|
|
176
178
|
else:
|
|
177
179
|
maitai_completion = convert_openai_chat_completion(response)
|
|
178
180
|
proto_messages = openai_messages_to_proto(messages)
|
|
@@ -192,22 +194,36 @@ class Completions:
|
|
|
192
194
|
return maitai_completion
|
|
193
195
|
|
|
194
196
|
|
|
195
|
-
def
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
197
|
+
def _process_openai_stream(session_id: Union[str, int],
|
|
198
|
+
reference_id: Union[str, int, None],
|
|
199
|
+
action_type: str,
|
|
200
|
+
application_ref_name: str,
|
|
201
|
+
messages: Iterable[openai_chat_types.ChatCompletionMessageParam],
|
|
202
|
+
stream: openai.Stream[openai_chat_types.ChatCompletionChunk],
|
|
203
|
+
callback: Optional[EvaluateCallback] = None) -> Iterable[ChatCompletionChunk]:
|
|
202
204
|
full_body = ""
|
|
203
205
|
proto_messages = openai_messages_to_proto(messages)
|
|
206
|
+
stream_eval_thread = StreamEvaluationThread()
|
|
207
|
+
stream_eval_thread.start()
|
|
204
208
|
for chunk in stream:
|
|
209
|
+
if stream_eval_thread.output:
|
|
210
|
+
result: EvaluateResponse = stream_eval_thread.output
|
|
211
|
+
stream_eval_thread.terminate()
|
|
212
|
+
new_stream = maitai.Evaluator.evaluate_realtime(session_id, reference_id, action_type, EvaluationContentType.PARTIAL, proto_messages, application_ref_name, full_body,
|
|
213
|
+
result.evaluation_results[0].description, result.evaluation_results[0].sentinel_id)
|
|
214
|
+
for new_chunk in new_stream:
|
|
215
|
+
yield new_chunk
|
|
216
|
+
return
|
|
205
217
|
maitai_chunk = convert_open_ai_chat_completion_chunk(chunk)
|
|
206
|
-
|
|
218
|
+
new_full_body, result_chunk = process_chunk(session_id, reference_id, action_type, application_ref_name, proto_messages, full_body, maitai_chunk, callback)
|
|
219
|
+
if new_full_body != full_body:
|
|
220
|
+
stream_eval_thread.input.append((session_id, reference_id, action_type, EvaluationContentType.PARTIAL, proto_messages, application_ref_name, new_full_body))
|
|
221
|
+
full_body = new_full_body
|
|
207
222
|
if result_chunk is not None:
|
|
208
223
|
yield result_chunk
|
|
209
224
|
break
|
|
210
225
|
yield maitai_chunk
|
|
226
|
+
stream_eval_thread.terminate()
|
|
211
227
|
|
|
212
228
|
|
|
213
229
|
def _process_inference_stream(stream: Iterable[InferenceStreamResponse], callback: EvaluateCallback) -> Iterable[ChatCompletionChunk]:
|
|
@@ -11,8 +11,9 @@ from maitai._openai_types import Body, Headers, Query
|
|
|
11
11
|
from maitai._types import EvaluateCallback
|
|
12
12
|
from maitai._utils import convert_open_ai_chat_completion_chunk, convert_openai_chat_completion, get_chat_completion_params, process_chunk, process_stream_response, required_args, \
|
|
13
13
|
set_completion_evaluation_response
|
|
14
|
+
from maitai_common.processes.stream_evaluation_thread import StreamEvaluationThread
|
|
14
15
|
from maitai_common.utils.proto_utils import openai_messages_to_proto
|
|
15
|
-
from maitai_gen.chat import ChatCompletionChunk, ChatCompletionResponse, ChatMessage, EvaluationContentType
|
|
16
|
+
from maitai_gen.chat import ChatCompletionChunk, ChatCompletionResponse, ChatMessage, EvaluateResponse, EvaluationContentType
|
|
16
17
|
from maitai_gen.inference import InferenceStreamResponse
|
|
17
18
|
|
|
18
19
|
DEFAULT_MAX_RETRIES = 2
|
|
@@ -47,6 +48,7 @@ class MaiTaiAsyncOpenAIClient:
|
|
|
47
48
|
default_headers=default_headers,
|
|
48
49
|
default_query=default_query,
|
|
49
50
|
http_client=http_client,
|
|
51
|
+
_strict_response_validation=_strict_response_validation,
|
|
50
52
|
)
|
|
51
53
|
|
|
52
54
|
self.chat = AsyncChat(self.client)
|
|
@@ -203,13 +205,27 @@ async def _process_async_openai_stream(session_id: Union[str, int],
|
|
|
203
205
|
callback: Optional[EvaluateCallback] = None) -> AsyncIterable[ChatCompletionChunk]:
|
|
204
206
|
full_body = ""
|
|
205
207
|
proto_messages = openai_messages_to_proto(messages)
|
|
208
|
+
stream_eval_thread = StreamEvaluationThread()
|
|
209
|
+
stream_eval_thread.start()
|
|
206
210
|
async for chunk in stream:
|
|
211
|
+
if stream_eval_thread.output:
|
|
212
|
+
result: EvaluateResponse = stream_eval_thread.output
|
|
213
|
+
stream_eval_thread.terminate()
|
|
214
|
+
new_stream = maitai.Evaluator.evaluate_realtime(session_id, reference_id, action_type, EvaluationContentType.PARTIAL, proto_messages, application_ref_name, full_body,
|
|
215
|
+
result.evaluation_results[0].description, result.evaluation_results[0].sentinel_id)
|
|
216
|
+
for new_chunk in new_stream:
|
|
217
|
+
yield new_chunk
|
|
218
|
+
return
|
|
207
219
|
maitai_chunk = convert_open_ai_chat_completion_chunk(chunk)
|
|
208
|
-
|
|
220
|
+
new_full_body, result_chunk = process_chunk(session_id, reference_id, action_type, application_ref_name, proto_messages, full_body, maitai_chunk, callback)
|
|
221
|
+
if new_full_body != full_body:
|
|
222
|
+
stream_eval_thread.input.append((session_id, reference_id, action_type, EvaluationContentType.PARTIAL, proto_messages, application_ref_name, new_full_body))
|
|
223
|
+
full_body = new_full_body
|
|
209
224
|
if result_chunk is not None:
|
|
210
225
|
yield result_chunk
|
|
211
226
|
break
|
|
212
227
|
yield maitai_chunk
|
|
228
|
+
stream_eval_thread.terminate()
|
|
213
229
|
|
|
214
230
|
|
|
215
231
|
async def _process_inference_stream_async(stream: AsyncIterable[InferenceStreamResponse], callback: EvaluateCallback) -> AsyncIterable[ChatCompletionChunk]:
|
|
@@ -32,7 +32,7 @@ class IOThread(Thread):
|
|
|
32
32
|
try:
|
|
33
33
|
self.process()
|
|
34
34
|
except Exception as e:
|
|
35
|
-
print(e, f'[{self.child_name}] Exception hit processing')
|
|
35
|
+
self.print(e, f'[{self.child_name}] Exception hit processing')
|
|
36
36
|
finally:
|
|
37
37
|
self.is_running = False
|
|
38
38
|
|
|
@@ -53,10 +53,13 @@ class IOThread(Thread):
|
|
|
53
53
|
if self.timer_thread is not None:
|
|
54
54
|
self.timer_thread.cancel()
|
|
55
55
|
self.cleaned = True
|
|
56
|
-
print(f'{self.child_name} cleaned up')
|
|
56
|
+
self.print(f'{self.child_name} cleaned up')
|
|
57
57
|
|
|
58
58
|
def process(self):
|
|
59
59
|
return None
|
|
60
60
|
|
|
61
61
|
def initialize(self):
|
|
62
62
|
self.initialized = True
|
|
63
|
+
|
|
64
|
+
def print(self, *args, **kwargs):
|
|
65
|
+
print(*args, **kwargs)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import maitai
|
|
2
|
+
from maitai_common.processes.io_thread import IOThread
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class StreamEvaluationThread(IOThread):
|
|
6
|
+
def __init__(self):
|
|
7
|
+
super(StreamEvaluationThread, self).__init__(interval=0.01)
|
|
8
|
+
self.input = []
|
|
9
|
+
self.output = []
|
|
10
|
+
|
|
11
|
+
def process(self):
|
|
12
|
+
if len(self.input) == 0:
|
|
13
|
+
return
|
|
14
|
+
input_list = self.input
|
|
15
|
+
self.input = []
|
|
16
|
+
session_id, reference_id, action_type, content_type, proto_messages, application_ref_name, partial = input_list.pop()
|
|
17
|
+
result = maitai.Evaluator.evaluate(session_id, reference_id, action_type, proto_messages, content_type=content_type, application_ref_name=application_ref_name, partial=partial)
|
|
18
|
+
if len(result.evaluation_results) > 0:
|
|
19
|
+
if result.evaluation_results[0].status == "FAULT":
|
|
20
|
+
self.output = result
|
|
21
|
+
|
|
22
|
+
def print(self, *args, **kwargs):
|
|
23
|
+
pass
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
from dataclasses import dataclass
|
|
7
7
|
from typing import (
|
|
8
8
|
TYPE_CHECKING,
|
|
9
|
+
AsyncIterator,
|
|
9
10
|
Dict,
|
|
10
11
|
List,
|
|
11
12
|
Optional,
|
|
@@ -27,6 +28,7 @@ if TYPE_CHECKING:
|
|
|
27
28
|
class EvaluationContentType(betterproto.Enum):
|
|
28
29
|
MESSAGE = 0
|
|
29
30
|
TEXT = 1
|
|
31
|
+
PARTIAL = 2
|
|
30
32
|
|
|
31
33
|
|
|
32
34
|
@dataclass(eq=False, repr=False)
|
|
@@ -202,6 +204,8 @@ class EvaluateRequest(betterproto.Message):
|
|
|
202
204
|
text_content: str = betterproto.string_field(9)
|
|
203
205
|
message_content: List["ChatMessage"] = betterproto.message_field(12)
|
|
204
206
|
request_id: str = betterproto.string_field(14)
|
|
207
|
+
sentinel_id: int = betterproto.int64_field(15)
|
|
208
|
+
fault_description: str = betterproto.string_field(16)
|
|
205
209
|
|
|
206
210
|
|
|
207
211
|
@dataclass(eq=False, repr=False)
|
|
@@ -248,12 +252,36 @@ class EvaluationServiceStub(betterproto.ServiceStub):
|
|
|
248
252
|
metadata=metadata,
|
|
249
253
|
)
|
|
250
254
|
|
|
255
|
+
async def evaluate_realtime(
|
|
256
|
+
self,
|
|
257
|
+
evaluate_request: "EvaluateRequest",
|
|
258
|
+
*,
|
|
259
|
+
timeout: Optional[float] = None,
|
|
260
|
+
deadline: Optional["Deadline"] = None,
|
|
261
|
+
metadata: Optional["MetadataLike"] = None
|
|
262
|
+
) -> AsyncIterator["ChatCompletionChunk"]:
|
|
263
|
+
async for response in self._unary_stream(
|
|
264
|
+
"/chat.EvaluationService/EvaluateRealtime",
|
|
265
|
+
evaluate_request,
|
|
266
|
+
ChatCompletionChunk,
|
|
267
|
+
timeout=timeout,
|
|
268
|
+
deadline=deadline,
|
|
269
|
+
metadata=metadata,
|
|
270
|
+
):
|
|
271
|
+
yield response
|
|
272
|
+
|
|
251
273
|
|
|
252
274
|
class EvaluationServiceBase(ServiceBase):
|
|
253
275
|
|
|
254
276
|
async def evaluate(self, evaluate_request: "EvaluateRequest") -> "EvaluateResponse":
|
|
255
277
|
raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)
|
|
256
278
|
|
|
279
|
+
async def evaluate_realtime(
|
|
280
|
+
self, evaluate_request: "EvaluateRequest"
|
|
281
|
+
) -> AsyncIterator["ChatCompletionChunk"]:
|
|
282
|
+
raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)
|
|
283
|
+
yield ChatCompletionChunk()
|
|
284
|
+
|
|
257
285
|
async def __rpc_evaluate(
|
|
258
286
|
self, stream: "grpclib.server.Stream[EvaluateRequest, EvaluateResponse]"
|
|
259
287
|
) -> None:
|
|
@@ -261,6 +289,16 @@ class EvaluationServiceBase(ServiceBase):
|
|
|
261
289
|
response = await self.evaluate(request)
|
|
262
290
|
await stream.send_message(response)
|
|
263
291
|
|
|
292
|
+
async def __rpc_evaluate_realtime(
|
|
293
|
+
self, stream: "grpclib.server.Stream[EvaluateRequest, ChatCompletionChunk]"
|
|
294
|
+
) -> None:
|
|
295
|
+
request = await stream.recv_message()
|
|
296
|
+
await self._call_rpc_handler_server_stream(
|
|
297
|
+
self.evaluate_realtime,
|
|
298
|
+
stream,
|
|
299
|
+
request,
|
|
300
|
+
)
|
|
301
|
+
|
|
264
302
|
def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
|
|
265
303
|
return {
|
|
266
304
|
"/chat.EvaluationService/Evaluate": grpclib.const.Handler(
|
|
@@ -269,4 +307,10 @@ class EvaluationServiceBase(ServiceBase):
|
|
|
269
307
|
EvaluateRequest,
|
|
270
308
|
EvaluateResponse,
|
|
271
309
|
),
|
|
310
|
+
"/chat.EvaluationService/EvaluateRealtime": grpclib.const.Handler(
|
|
311
|
+
self.__rpc_evaluate_realtime,
|
|
312
|
+
grpclib.const.Cardinality.UNARY_STREAM,
|
|
313
|
+
EvaluateRequest,
|
|
314
|
+
ChatCompletionChunk,
|
|
315
|
+
),
|
|
272
316
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
README.md
|
|
2
2
|
setup.py
|
|
3
3
|
maitai/__init__.py
|
|
4
|
+
maitai/_azure.py
|
|
4
5
|
maitai/_config.py
|
|
5
6
|
maitai/_config_listener_thread.py
|
|
6
7
|
maitai/_evaluator.py
|
|
@@ -14,6 +15,7 @@ maitai/_utils.py
|
|
|
14
15
|
maitai_common/__init__.py
|
|
15
16
|
maitai_common/processes/__init__.py
|
|
16
17
|
maitai_common/processes/io_thread.py
|
|
18
|
+
maitai_common/processes/stream_evaluation_thread.py
|
|
17
19
|
maitai_common/processes/websocket_listener_thread.py
|
|
18
20
|
maitai_common/utils/__init__.py
|
|
19
21
|
maitai_common/utils/proto_utils.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
|
{maitai_python-1.0.1b2 → maitai_python-1.0.3}/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
|