forked from mistralai/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_client.py
More file actions
294 lines (252 loc) · 11.1 KB
/
async_client.py
File metadata and controls
294 lines (252 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import os
import posixpath
import time
from json import JSONDecodeError
from typing import Any, AsyncGenerator, Dict, List, Optional, Union
from httpx import (
AsyncClient,
AsyncHTTPTransport,
ConnectError,
Limits,
RequestError,
Response,
)
from mistralai.client_base import ClientBase
from mistralai.constants import ENDPOINT, RETRY_STATUS_CODES
from mistralai.exceptions import (
MistralAPIException,
MistralAPIStatusException,
MistralConnectionException,
MistralException,
)
from mistralai.models.chat_completion import (
ChatCompletionResponse,
ChatCompletionStreamResponse,
ResponseFormat,
ToolChoice,
)
from mistralai.models.embeddings import EmbeddingResponse
from mistralai.models.models import ModelList
class MistralAsyncClient(ClientBase):
def __init__(
self,
api_key: Optional[str] = os.environ.get("MISTRAL_API_KEY", None),
endpoint: str = ENDPOINT,
max_retries: int = 5,
timeout: int = 120,
max_concurrent_requests: int = 64,
):
super().__init__(endpoint, api_key, max_retries, timeout)
self._client = AsyncClient(
follow_redirects=True,
timeout=timeout,
limits=Limits(max_connections=max_concurrent_requests),
transport=AsyncHTTPTransport(retries=max_retries),
)
async def close(self) -> None:
await self._client.aclose()
async def _check_response_status_codes(self, response: Response) -> None:
if response.status_code in RETRY_STATUS_CODES:
raise MistralAPIStatusException.from_response(
response,
message=f"Status: {response.status_code}. Message: {response.text}",
)
elif 400 <= response.status_code < 500:
if response.stream:
await response.aread()
raise MistralAPIException.from_response(
response,
message=f"Status: {response.status_code}. Message: {response.text}",
)
elif response.status_code >= 500:
if response.stream:
await response.aread()
raise MistralException(
message=f"Status: {response.status_code}. Message: {response.text}",
)
async def _check_streaming_response(self, response: Response) -> None:
await self._check_response_status_codes(response)
async def _check_response(self, response: Response) -> Dict[str, Any]:
await self._check_response_status_codes(response)
json_response: Dict[str, Any] = response.json()
if "object" not in json_response:
raise MistralException(message=f"Unexpected response: {json_response}")
if "error" == json_response["object"]: # has errors
raise MistralAPIException.from_response(
response,
message=json_response["message"],
)
return json_response
async def _request(
self,
method: str,
json: Dict[str, Any],
path: str,
stream: bool = False,
attempt: int = 1,
) -> AsyncGenerator[Dict[str, Any], None]:
accept_header = "text/event-stream" if stream else "application/json"
headers = {
"Accept": accept_header,
"User-Agent": f"mistral-client-python/{self._version}",
"Authorization": f"Bearer {self._api_key}",
"Content-Type": "application/json",
}
url = posixpath.join(self._endpoint, path)
self._logger.debug(f"Sending request: {method} {url} {json}")
response: Response
try:
if stream:
async with self._client.stream(
method,
url,
headers=headers,
json=json,
) as response:
await self._check_streaming_response(response)
async for line in response.aiter_lines():
json_streamed_response = self._process_line(line)
if json_streamed_response:
yield json_streamed_response
else:
response = await self._client.request(
method,
url,
headers=headers,
json=json,
)
yield await self._check_response(response)
except ConnectError as e:
raise MistralConnectionException(str(e)) from e
except RequestError as e:
raise MistralException(f"Unexpected exception ({e.__class__.__name__}): {e}") from e
except JSONDecodeError as e:
raise MistralAPIException.from_response(
response,
message=f"Failed to decode json body: {response.text}",
) from e
except MistralAPIStatusException as e:
attempt += 1
if attempt > self._max_retries:
raise MistralAPIStatusException.from_response(response, message=str(e)) from e
backoff = 2.0**attempt # exponential backoff
time.sleep(backoff)
# Retry as a generator
async for r in self._request(method, json, path, stream=stream, attempt=attempt):
yield r
async def chat(
self,
messages: List[Any],
model: Optional[str] = None,
tools: Optional[List[Dict[str, Any]]] = None,
temperature: Optional[float] = None,
max_tokens: Optional[int] = None,
top_p: Optional[float] = None,
random_seed: Optional[int] = None,
safe_mode: bool = False,
safe_prompt: bool = False,
tool_choice: Optional[Union[str, ToolChoice]] = None,
response_format: Optional[Union[Dict[str, str], ResponseFormat]] = None,
) -> ChatCompletionResponse:
"""A asynchronous chat endpoint that returns a single response.
Args:
model (str): model the name of the model to chat with, e.g. mistral-tiny
messages (List[Any]): messages an array of messages to chat with, e.g.
[{role: 'user', content: 'What is the best French cheese?'}]
temperature (Optional[float], optional): temperature the temperature to use for sampling, e.g. 0.5.
max_tokens (Optional[int], optional): the maximum number of tokens to generate, e.g. 100. Defaults to None.
top_p (Optional[float], optional): the cumulative probability of tokens to generate, e.g. 0.9.
Defaults to None.
random_seed (Optional[int], optional): the random seed to use for sampling, e.g. 42. Defaults to None.
safe_mode (bool, optional): deprecated, use safe_prompt instead. Defaults to False.
safe_prompt (bool, optional): whether to use safe prompt, e.g. true. Defaults to False.
Returns:
ChatCompletionResponse: a response object containing the generated text.
"""
request = self._make_chat_request(
messages,
model,
tools=tools,
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
random_seed=random_seed,
stream=False,
safe_prompt=safe_mode or safe_prompt,
tool_choice=tool_choice,
response_format=response_format,
)
single_response = self._request("post", request, "v1/chat/completions")
async for response in single_response:
return ChatCompletionResponse(**response)
raise MistralException("No response received")
async def chat_stream(
self,
messages: List[Any],
model: Optional[str] = None,
tools: Optional[List[Dict[str, Any]]] = None,
temperature: Optional[float] = None,
max_tokens: Optional[int] = None,
top_p: Optional[float] = None,
random_seed: Optional[int] = None,
safe_mode: bool = False,
safe_prompt: bool = False,
tool_choice: Optional[Union[str, ToolChoice]] = None,
response_format: Optional[Union[Dict[str, str], ResponseFormat]] = None,
) -> AsyncGenerator[ChatCompletionStreamResponse, None]:
"""An Asynchronous chat endpoint that streams responses.
Args:
model (str): model the name of the model to chat with, e.g. mistral-tiny
messages (List[Any]): messages an array of messages to chat with, e.g.
[{role: 'user', content: 'What is the best French cheese?'}]
tools (Optional[List[Function]], optional): a list of tools to use.
temperature (Optional[float], optional): temperature the temperature to use for sampling, e.g. 0.5.
max_tokens (Optional[int], optional): the maximum number of tokens to generate, e.g. 100. Defaults to None.
top_p (Optional[float], optional): the cumulative probability of tokens to generate, e.g. 0.9.
Defaults to None.
random_seed (Optional[int], optional): the random seed to use for sampling, e.g. 42. Defaults to None.
safe_mode (bool, optional): deprecated, use safe_prompt instead. Defaults to False.
safe_prompt (bool, optional): whether to use safe prompt, e.g. true. Defaults to False.
Returns:
AsyncGenerator[ChatCompletionStreamResponse, None]:
An async generator that yields ChatCompletionStreamResponse objects.
"""
request = self._make_chat_request(
messages,
model,
tools=tools,
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
random_seed=random_seed,
stream=True,
safe_prompt=safe_mode or safe_prompt,
tool_choice=tool_choice,
response_format=response_format,
)
async_response = self._request("post", request, "v1/chat/completions", stream=True)
async for json_response in async_response:
yield ChatCompletionStreamResponse(**json_response)
async def embeddings(self, model: str, input: Union[str, List[str]]) -> EmbeddingResponse:
"""An asynchronous embeddings endpoint that returns embeddings for a single, or batch of inputs
Args:
model (str): The embedding model to use, e.g. mistral-embed
input (Union[str, List[str]]): The input to embed,
e.g. ['What is the best French cheese?']
Returns:
EmbeddingResponse: A response object containing the embeddings.
"""
request = {"model": model, "input": input}
single_response = self._request("post", request, "v1/embeddings")
async for response in single_response:
return EmbeddingResponse(**response)
raise MistralException("No response received")
async def list_models(self) -> ModelList:
"""Returns a list of the available models
Returns:
ModelList: A response object containing the list of models.
"""
single_response = self._request("get", {}, "v1/models")
async for response in single_response:
return ModelList(**response)
raise MistralException("No response received")