maitai-python 1.2.13__tar.gz → 1.2.14b0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/PKG-INFO +1 -1
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_inference.py +38 -17
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_maitai.py +6 -6
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_maitai_async.py +7 -7
- maitai_python-1.2.14b0/maitai/exceptions.py +23 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/models/application.py +2 -2
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/models/chat.py +1 -1
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/models/config.py +1 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/models/key.py +2 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_common/processes/websocket_listener_thread.py +64 -23
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_common/version.py +1 -1
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_python.egg-info/PKG-INFO +1 -1
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_python.egg-info/SOURCES.txt +1 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/README.md +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/__init__.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_config.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_config_listener_thread.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_context.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_evaluator.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_maitai_client.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_pydantic.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_types.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/_utils.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/models/__init__.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/models/event.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/models/inference.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/models/metric.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/models/sentinel.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai/tools/__init__.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_common/__init__.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_common/config/__init__.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_common/config/config_service.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_common/processes/__init__.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_common/processes/io_thread.py +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_python.egg-info/dependency_links.txt +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_python.egg-info/requires.txt +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_python.egg-info/top_level.txt +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/setup.cfg +0 -0
- {maitai_python-1.2.13 → maitai_python-1.2.14b0}/setup.py +0 -0
|
@@ -3,31 +3,19 @@ import json
|
|
|
3
3
|
import traceback
|
|
4
4
|
from typing import AsyncIterable, Iterable, Optional
|
|
5
5
|
|
|
6
|
+
import httpx
|
|
7
|
+
|
|
6
8
|
from maitai._config import config
|
|
7
9
|
from maitai._maitai_client import MaitaiClient
|
|
8
10
|
from maitai._types import AsyncChunkQueue, ChunkQueue, EvaluateCallback, QueueIterable
|
|
9
11
|
from maitai._utils import __version__ as version
|
|
10
12
|
from maitai._utils import chat_completion_chunk_to_response
|
|
13
|
+
from maitai.exceptions import BadRequestError, MaitaiConnectionError, NotFoundError
|
|
11
14
|
from maitai.models.chat import ChatCompletionChunk, ChatCompletionResponse
|
|
12
15
|
from maitai.models.inference import InferenceStreamResponse
|
|
13
16
|
from maitai.models.metric import RequestTimingMetric
|
|
14
17
|
|
|
15
18
|
|
|
16
|
-
class InferenceException(Exception):
|
|
17
|
-
def __init__(self, msg: str):
|
|
18
|
-
super().__init__(msg)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class MaitaiConnectionError(ConnectionError):
|
|
22
|
-
def __init__(self, msg: str):
|
|
23
|
-
super().__init__(msg)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
class InferenceWarning(Warning):
|
|
27
|
-
def __init__(self, *args, **kwargs):
|
|
28
|
-
pass
|
|
29
|
-
|
|
30
|
-
|
|
31
19
|
class Inference(MaitaiClient):
|
|
32
20
|
|
|
33
21
|
def __init__(self):
|
|
@@ -107,9 +95,9 @@ class Inference(MaitaiClient):
|
|
|
107
95
|
content=json.dumps(request_dict),
|
|
108
96
|
timeout=timeout,
|
|
109
97
|
) as response:
|
|
110
|
-
response.raise_for_status()
|
|
111
98
|
for line in response.iter_lines():
|
|
112
99
|
if line:
|
|
100
|
+
build_exception(response, line)
|
|
113
101
|
inference_response: InferenceStreamResponse = (
|
|
114
102
|
InferenceStreamResponse.model_validate_json(line)
|
|
115
103
|
)
|
|
@@ -119,7 +107,14 @@ class Inference(MaitaiClient):
|
|
|
119
107
|
|
|
120
108
|
except TimeoutError as e:
|
|
121
109
|
raise
|
|
110
|
+
except BadRequestError as e:
|
|
111
|
+
raise
|
|
112
|
+
except NotFoundError as e:
|
|
113
|
+
raise
|
|
114
|
+
except MaitaiConnectionError as e:
|
|
115
|
+
raise
|
|
122
116
|
except Exception as e:
|
|
117
|
+
print(e)
|
|
123
118
|
exception = MaitaiConnectionError(
|
|
124
119
|
f"Failed to send inference request. Error: {e}"
|
|
125
120
|
)
|
|
@@ -162,9 +157,9 @@ class Inference(MaitaiClient):
|
|
|
162
157
|
async with client.stream(
|
|
163
158
|
"POST", url, headers=headers, json=request_dict, timeout=timeout
|
|
164
159
|
) as response:
|
|
165
|
-
response.raise_for_status()
|
|
166
160
|
async for line in response.aiter_lines():
|
|
167
161
|
if line:
|
|
162
|
+
build_exception(response, line)
|
|
168
163
|
inference_response: InferenceStreamResponse = (
|
|
169
164
|
InferenceStreamResponse.model_validate_json(line)
|
|
170
165
|
)
|
|
@@ -173,6 +168,12 @@ class Inference(MaitaiClient):
|
|
|
173
168
|
yield inference_response
|
|
174
169
|
except TimeoutError as e:
|
|
175
170
|
raise
|
|
171
|
+
except BadRequestError as e:
|
|
172
|
+
raise
|
|
173
|
+
except NotFoundError as e:
|
|
174
|
+
raise
|
|
175
|
+
except MaitaiConnectionError as e:
|
|
176
|
+
raise
|
|
176
177
|
except Exception as e:
|
|
177
178
|
exception = MaitaiConnectionError(
|
|
178
179
|
f"Failed to send inference request: {str(e)}"
|
|
@@ -326,3 +327,23 @@ class Inference(MaitaiClient):
|
|
|
326
327
|
return await client.put(url, headers=headers, json=metric.model_dump())
|
|
327
328
|
except:
|
|
328
329
|
pass
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def build_exception(response: httpx.Response, line: str):
|
|
333
|
+
if response.status_code >= 400 and response.status_code < 500:
|
|
334
|
+
if response.status_code == 400:
|
|
335
|
+
raise BadRequestError(
|
|
336
|
+
f"Error code: {response.status_code} - {line}"
|
|
337
|
+
) from None
|
|
338
|
+
elif response.status_code == 404:
|
|
339
|
+
raise NotFoundError(
|
|
340
|
+
f"Error code: {response.status_code} - {line}"
|
|
341
|
+
) from None
|
|
342
|
+
else:
|
|
343
|
+
raise MaitaiConnectionError(
|
|
344
|
+
f"Error code: {response.status_code} - {line}"
|
|
345
|
+
) from None
|
|
346
|
+
elif response.status_code >= 500:
|
|
347
|
+
raise MaitaiConnectionError(
|
|
348
|
+
f"Error code: {response.status_code} - {line}"
|
|
349
|
+
) from None
|
|
@@ -12,12 +12,7 @@ import openai.types.chat as openai_chat_types
|
|
|
12
12
|
|
|
13
13
|
from maitai._config import config
|
|
14
14
|
from maitai._evaluator import Evaluator
|
|
15
|
-
from maitai._inference import
|
|
16
|
-
Inference,
|
|
17
|
-
InferenceException,
|
|
18
|
-
InferenceWarning,
|
|
19
|
-
MaitaiConnectionError,
|
|
20
|
-
)
|
|
15
|
+
from maitai._inference import Inference
|
|
21
16
|
from maitai._types import (
|
|
22
17
|
Body,
|
|
23
18
|
EvaluateCallback,
|
|
@@ -35,6 +30,11 @@ from maitai._utils import (
|
|
|
35
30
|
required_args,
|
|
36
31
|
type_to_response_format_param,
|
|
37
32
|
)
|
|
33
|
+
from maitai.exceptions import (
|
|
34
|
+
InferenceException,
|
|
35
|
+
InferenceWarning,
|
|
36
|
+
MaitaiConnectionError,
|
|
37
|
+
)
|
|
38
38
|
from maitai.models.chat import (
|
|
39
39
|
AzureParams,
|
|
40
40
|
ChatCompletionParams,
|
|
@@ -12,12 +12,7 @@ import openai.types.chat as openai_chat_types
|
|
|
12
12
|
|
|
13
13
|
from maitai._config import config
|
|
14
14
|
from maitai._evaluator import Evaluator
|
|
15
|
-
from maitai._inference import
|
|
16
|
-
Inference,
|
|
17
|
-
InferenceException,
|
|
18
|
-
InferenceWarning,
|
|
19
|
-
MaitaiConnectionError,
|
|
20
|
-
)
|
|
15
|
+
from maitai._inference import Inference
|
|
21
16
|
from maitai._types import (
|
|
22
17
|
Body,
|
|
23
18
|
EvaluateCallback,
|
|
@@ -35,6 +30,11 @@ from maitai._utils import (
|
|
|
35
30
|
required_args,
|
|
36
31
|
type_to_response_format_param,
|
|
37
32
|
)
|
|
33
|
+
from maitai.exceptions import (
|
|
34
|
+
InferenceException,
|
|
35
|
+
InferenceWarning,
|
|
36
|
+
MaitaiConnectionError,
|
|
37
|
+
)
|
|
38
38
|
from maitai.models.chat import (
|
|
39
39
|
AzureParams,
|
|
40
40
|
ChatCompletionParams,
|
|
@@ -72,7 +72,7 @@ class MaitaiAsync:
|
|
|
72
72
|
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
73
73
|
default_headers: Optional[Mapping[str, str]] = None,
|
|
74
74
|
default_query: Optional[Mapping[str, object]] = None,
|
|
75
|
-
http_client: Optional[httpx.
|
|
75
|
+
http_client: Optional[httpx.AsyncClient] = None,
|
|
76
76
|
_strict_response_validation: bool = False,
|
|
77
77
|
):
|
|
78
78
|
if not maitai_api_key:
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
class InferenceException(Exception):
|
|
2
|
+
def __init__(self, msg: str):
|
|
3
|
+
super().__init__(msg)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MaitaiConnectionError(ConnectionError):
|
|
7
|
+
def __init__(self, msg: str):
|
|
8
|
+
super().__init__(msg)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class BadRequestError(Exception):
|
|
12
|
+
def __init__(self, msg: str):
|
|
13
|
+
super().__init__(msg)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class NotFoundError(Exception):
|
|
17
|
+
def __init__(self, msg: str):
|
|
18
|
+
super().__init__(msg)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class InferenceWarning(Warning):
|
|
22
|
+
def __init__(self, *args, **kwargs):
|
|
23
|
+
pass
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# codegen: frontend, sdk
|
|
2
|
-
from typing import Dict, List, Optional, Union
|
|
2
|
+
from typing import Any, Dict, List, Optional, Union
|
|
3
3
|
|
|
4
4
|
from pydantic import BaseModel, Field
|
|
5
5
|
|
|
@@ -45,7 +45,7 @@ class ApplicationContext(BaseModel):
|
|
|
45
45
|
id: Optional[int] = -1
|
|
46
46
|
date_created: Optional[Union[int, float]] = 0
|
|
47
47
|
application_id: Optional[int] = None
|
|
48
|
-
context_body: Optional[str] = None
|
|
48
|
+
context_body: Optional[Union[str, Dict[Any, Any]]] = None
|
|
49
49
|
context_path: Optional[str] = None
|
|
50
50
|
context_type: Optional[str] = "TEXT"
|
|
51
51
|
reference: Optional[str] = None
|
|
@@ -120,7 +120,7 @@ class ChatCompletionParams(BaseModel):
|
|
|
120
120
|
messages: List[ChatMessage]
|
|
121
121
|
model: str
|
|
122
122
|
max_tokens: Optional[int] = None
|
|
123
|
-
response_format: Optional[ResponseFormat] =
|
|
123
|
+
response_format: Optional[ResponseFormat] = ResponseFormat(type="text")
|
|
124
124
|
temperature: Optional[float] = None
|
|
125
125
|
top_p: Optional[float] = None
|
|
126
126
|
stop: Optional[Union[str, List[str]]] = None
|
|
@@ -48,6 +48,7 @@ class Config(BaseModel):
|
|
|
48
48
|
metadata: Dict[str, Any] = Field(default_factory=dict)
|
|
49
49
|
system_prompt: Optional[str] = Field(default=None)
|
|
50
50
|
reasoning_effort: Optional[str] = None
|
|
51
|
+
disable_session_update_events: Optional[bool] = False
|
|
51
52
|
|
|
52
53
|
|
|
53
54
|
class ModelConfig(BaseModel):
|
|
@@ -24,6 +24,7 @@ class KeyIdMap(BaseModel):
|
|
|
24
24
|
azure_api_key: Optional[int] = 0
|
|
25
25
|
sambanova_api_key: Optional[int] = 0
|
|
26
26
|
|
|
27
|
+
|
|
27
28
|
class KeyMap(BaseModel):
|
|
28
29
|
openai_api_key: Optional[Key] = None
|
|
29
30
|
groq_api_key: Optional[Key] = None
|
|
@@ -32,4 +33,5 @@ class KeyMap(BaseModel):
|
|
|
32
33
|
azure_api_key: Optional[Key] = None
|
|
33
34
|
sambanova_api_key: Optional[Key] = None
|
|
34
35
|
|
|
36
|
+
|
|
35
37
|
Key.model_rebuild()
|
{maitai_python-1.2.13 → maitai_python-1.2.14b0}/maitai_common/processes/websocket_listener_thread.py
RENAMED
|
@@ -23,6 +23,7 @@ class WebsocketListenerThread(IOThread):
|
|
|
23
23
|
self.connection_established_event = threading.Event()
|
|
24
24
|
self.retry_backoff = 1
|
|
25
25
|
self.connection_lock = threading.Lock()
|
|
26
|
+
self.last_reconnect_attempt = None
|
|
26
27
|
|
|
27
28
|
def connect_to_websocket(self):
|
|
28
29
|
self.connection_established_event.clear()
|
|
@@ -37,24 +38,36 @@ class WebsocketListenerThread(IOThread):
|
|
|
37
38
|
on_error=self.on_error,
|
|
38
39
|
on_open=self.on_open,
|
|
39
40
|
)
|
|
41
|
+
old_ws_thread = None
|
|
40
42
|
if self.ws_thread and self.ws_thread.is_alive():
|
|
43
|
+
# Store reference to old thread but don't join it here
|
|
41
44
|
old_ws_thread = self.ws_thread
|
|
42
|
-
|
|
43
|
-
old_ws_thread.join(timeout=2.0)
|
|
45
|
+
|
|
44
46
|
# Create and start new thread
|
|
45
47
|
self.ws_thread = threading.Thread(
|
|
46
48
|
target=self.ws.run_forever, name="WebsocketThread", daemon=True
|
|
47
49
|
)
|
|
48
50
|
self.ws_thread.start()
|
|
49
51
|
self.retry_backoff = 1
|
|
52
|
+
|
|
53
|
+
# Close old websocket connection
|
|
50
54
|
if old_ws:
|
|
51
55
|
old_ws.close()
|
|
52
56
|
|
|
57
|
+
# Only join the old thread if it's not the current thread
|
|
58
|
+
if old_ws_thread and old_ws_thread != threading.current_thread():
|
|
59
|
+
old_ws_thread.join(timeout=2.0)
|
|
60
|
+
|
|
53
61
|
def on_message(self, ws, message):
|
|
54
62
|
self.messages.append(message)
|
|
55
63
|
|
|
56
64
|
def on_close(self, ws, _, __):
|
|
57
65
|
if self.run_thread and not self.closing_ws:
|
|
66
|
+
# Add a small delay before reconnecting to prevent rapid reconnection cycles
|
|
67
|
+
time.sleep(self.retry_backoff)
|
|
68
|
+
self.retry_backoff = min(
|
|
69
|
+
self.retry_backoff * 2, 60
|
|
70
|
+
) # Increase max backoff time
|
|
58
71
|
self.connect_to_websocket()
|
|
59
72
|
|
|
60
73
|
def on_open(self, ws):
|
|
@@ -65,10 +78,18 @@ class WebsocketListenerThread(IOThread):
|
|
|
65
78
|
self.ws.close()
|
|
66
79
|
|
|
67
80
|
def on_error(self, ws, error):
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
81
|
+
# Log the error to help with debugging
|
|
82
|
+
logging.error(f"WebSocket error: {error}")
|
|
83
|
+
|
|
84
|
+
# Don't attempt to reconnect from within the websocket thread
|
|
85
|
+
# Instead, let the process method handle reconnection
|
|
86
|
+
if threading.current_thread() != self.ws_thread:
|
|
87
|
+
time.sleep(self.retry_backoff)
|
|
88
|
+
self.retry_backoff *= 2
|
|
89
|
+
self.retry_backoff = min(
|
|
90
|
+
self.retry_backoff, 60
|
|
91
|
+
) # Increase max backoff time
|
|
92
|
+
self.connect_to_websocket()
|
|
72
93
|
|
|
73
94
|
def initialize(self):
|
|
74
95
|
self.connect_to_websocket()
|
|
@@ -93,22 +114,42 @@ class WebsocketListenerThread(IOThread):
|
|
|
93
114
|
def process(self):
|
|
94
115
|
if self.run_thread:
|
|
95
116
|
# Check if websocket is connected and functioning
|
|
96
|
-
if
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
117
|
+
if (
|
|
118
|
+
not self.ws
|
|
119
|
+
or not (self.ws and self.ws.sock)
|
|
120
|
+
or not (self.ws.sock and self.ws.sock.connected)
|
|
121
|
+
):
|
|
122
|
+
# Add connection attempt tracking to prevent excessive reconnects
|
|
123
|
+
if (
|
|
124
|
+
hasattr(self, "last_reconnect_attempt")
|
|
125
|
+
and time.time() - self.last_reconnect_attempt < self.retry_backoff
|
|
126
|
+
):
|
|
127
|
+
# Skip reconnection attempt if we tried recently
|
|
128
|
+
pass
|
|
129
|
+
else:
|
|
130
|
+
self.last_reconnect_attempt = time.time()
|
|
131
|
+
old_ws = self.ws
|
|
132
|
+
# Temporarily override the on_close to prevent reconnection
|
|
133
|
+
old_on_close = None
|
|
134
|
+
if old_ws:
|
|
135
|
+
old_on_close = old_ws.on_close
|
|
136
|
+
old_ws.on_close = lambda *args, **kwargs: None
|
|
137
|
+
|
|
138
|
+
try:
|
|
139
|
+
# Try to establish a new connection
|
|
140
|
+
self.connect_to_websocket()
|
|
141
|
+
# Wait for the on_open callback to set the event with a timeout
|
|
142
|
+
connection_success = self.connection_established_event.wait(
|
|
143
|
+
timeout=10
|
|
144
|
+
)
|
|
145
|
+
# If successful, close the old connection
|
|
146
|
+
if old_ws and connection_success:
|
|
147
|
+
old_ws.close()
|
|
148
|
+
except Exception as e:
|
|
149
|
+
logging.error(f"Error reconnecting to WebSocket: {e}")
|
|
150
|
+
# If new connection fails, keep the old connection
|
|
151
|
+
if old_ws and old_on_close:
|
|
152
|
+
old_ws.on_close = old_on_close
|
|
153
|
+
self.ws = old_ws
|
|
113
154
|
|
|
114
155
|
super(WebsocketListenerThread, self).process()
|
|
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
|