mistralai 0.4.1__py3-none-any.whl → 0.5.5a50__py3-none-any.whl
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.
- mistralai/__init__.py +5 -0
- mistralai/_hooks/__init__.py +5 -0
- mistralai/_hooks/custom_user_agent.py +16 -0
- mistralai/_hooks/deprecation_warning.py +26 -0
- mistralai/_hooks/registration.py +17 -0
- mistralai/_hooks/sdkhooks.py +57 -0
- mistralai/_hooks/types.py +76 -0
- mistralai/async_client.py +5 -374
- mistralai/basesdk.py +216 -0
- mistralai/chat.py +475 -0
- mistralai/client.py +5 -372
- mistralai/embeddings.py +182 -0
- mistralai/files.py +600 -84
- mistralai/fim.py +439 -0
- mistralai/fine_tuning.py +855 -0
- mistralai/httpclient.py +78 -0
- mistralai/models/__init__.py +80 -0
- mistralai/models/archiveftmodelout.py +19 -0
- mistralai/models/assistantmessage.py +58 -0
- mistralai/models/chatcompletionchoice.py +33 -0
- mistralai/models/chatcompletionrequest.py +114 -0
- mistralai/models/chatcompletionresponse.py +27 -0
- mistralai/models/chatcompletionstreamrequest.py +112 -0
- mistralai/models/checkpointout.py +25 -0
- mistralai/models/completionchunk.py +27 -0
- mistralai/models/completionevent.py +15 -0
- mistralai/models/completionresponsestreamchoice.py +53 -0
- mistralai/models/contentchunk.py +17 -0
- mistralai/models/delete_model_v1_models_model_id_deleteop.py +16 -0
- mistralai/models/deletefileout.py +24 -0
- mistralai/models/deletemodelout.py +25 -0
- mistralai/models/deltamessage.py +52 -0
- mistralai/models/detailedjobout.py +96 -0
- mistralai/models/embeddingrequest.py +66 -0
- mistralai/models/embeddingresponse.py +24 -0
- mistralai/models/embeddingresponsedata.py +19 -0
- mistralai/models/eventout.py +55 -0
- mistralai/models/files_api_routes_delete_fileop.py +16 -0
- mistralai/models/files_api_routes_retrieve_fileop.py +16 -0
- mistralai/models/files_api_routes_upload_fileop.py +51 -0
- mistralai/models/fileschema.py +76 -0
- mistralai/models/fimcompletionrequest.py +99 -0
- mistralai/models/fimcompletionresponse.py +27 -0
- mistralai/models/fimcompletionstreamrequest.py +97 -0
- mistralai/models/finetuneablemodel.py +8 -0
- mistralai/models/ftmodelcapabilitiesout.py +21 -0
- mistralai/models/ftmodelout.py +70 -0
- mistralai/models/function.py +19 -0
- mistralai/models/functioncall.py +16 -0
- mistralai/models/githubrepositoryin.py +57 -0
- mistralai/models/githubrepositoryout.py +57 -0
- mistralai/models/httpvalidationerror.py +23 -0
- mistralai/models/jobin.py +78 -0
- mistralai/models/jobmetadataout.py +59 -0
- mistralai/models/jobout.py +112 -0
- mistralai/models/jobs_api_routes_fine_tuning_archive_fine_tuned_modelop.py +16 -0
- mistralai/models/jobs_api_routes_fine_tuning_cancel_fine_tuning_jobop.py +18 -0
- mistralai/models/jobs_api_routes_fine_tuning_create_fine_tuning_jobop.py +73 -0
- mistralai/models/jobs_api_routes_fine_tuning_get_fine_tuning_jobop.py +18 -0
- mistralai/models/jobs_api_routes_fine_tuning_get_fine_tuning_jobsop.py +86 -0
- mistralai/models/jobs_api_routes_fine_tuning_start_fine_tuning_jobop.py +16 -0
- mistralai/models/jobs_api_routes_fine_tuning_unarchive_fine_tuned_modelop.py +16 -0
- mistralai/models/jobs_api_routes_fine_tuning_update_fine_tuned_modelop.py +19 -0
- mistralai/models/jobsout.py +20 -0
- mistralai/models/legacyjobmetadataout.py +85 -0
- mistralai/models/listfilesout.py +17 -0
- mistralai/models/metricout.py +55 -0
- mistralai/models/modelcapabilities.py +21 -0
- mistralai/models/modelcard.py +71 -0
- mistralai/models/modellist.py +18 -0
- mistralai/models/responseformat.py +18 -0
- mistralai/models/retrieve_model_v1_models_model_id_getop.py +16 -0
- mistralai/models/retrievefileout.py +76 -0
- mistralai/models/sampletype.py +7 -0
- mistralai/models/sdkerror.py +22 -0
- mistralai/models/security.py +16 -0
- mistralai/models/source.py +7 -0
- mistralai/models/systemmessage.py +26 -0
- mistralai/models/textchunk.py +17 -0
- mistralai/models/tool.py +18 -0
- mistralai/models/toolcall.py +20 -0
- mistralai/models/toolmessage.py +55 -0
- mistralai/models/trainingfile.py +17 -0
- mistralai/models/trainingparameters.py +53 -0
- mistralai/models/trainingparametersin.py +61 -0
- mistralai/models/unarchiveftmodelout.py +19 -0
- mistralai/models/updateftmodelin.py +49 -0
- mistralai/models/uploadfileout.py +76 -0
- mistralai/models/usageinfo.py +18 -0
- mistralai/models/usermessage.py +26 -0
- mistralai/models/validationerror.py +24 -0
- mistralai/models/wandbintegration.py +61 -0
- mistralai/models/wandbintegrationout.py +57 -0
- mistralai/models_.py +928 -0
- mistralai/py.typed +1 -0
- mistralai/sdk.py +111 -0
- mistralai/sdkconfiguration.py +53 -0
- mistralai/types/__init__.py +21 -0
- mistralai/types/basemodel.py +35 -0
- mistralai/utils/__init__.py +82 -0
- mistralai/utils/annotations.py +19 -0
- mistralai/utils/enums.py +34 -0
- mistralai/utils/eventstreaming.py +179 -0
- mistralai/utils/forms.py +207 -0
- mistralai/utils/headers.py +136 -0
- mistralai/utils/metadata.py +118 -0
- mistralai/utils/queryparams.py +203 -0
- mistralai/utils/requestbodies.py +66 -0
- mistralai/utils/retries.py +216 -0
- mistralai/utils/security.py +182 -0
- mistralai/utils/serializers.py +181 -0
- mistralai/utils/url.py +150 -0
- mistralai/utils/values.py +128 -0
- {mistralai-0.4.1.dist-info → mistralai-0.5.5a50.dist-info}/LICENSE +1 -1
- mistralai-0.5.5a50.dist-info/METADATA +626 -0
- mistralai-0.5.5a50.dist-info/RECORD +228 -0
- mistralai_azure/__init__.py +5 -0
- mistralai_azure/_hooks/__init__.py +5 -0
- mistralai_azure/_hooks/custom_user_agent.py +16 -0
- mistralai_azure/_hooks/registration.py +15 -0
- mistralai_azure/_hooks/sdkhooks.py +57 -0
- mistralai_azure/_hooks/types.py +76 -0
- mistralai_azure/basesdk.py +215 -0
- mistralai_azure/chat.py +475 -0
- mistralai_azure/httpclient.py +78 -0
- mistralai_azure/models/__init__.py +28 -0
- mistralai_azure/models/assistantmessage.py +58 -0
- mistralai_azure/models/chatcompletionchoice.py +33 -0
- mistralai_azure/models/chatcompletionrequest.py +114 -0
- mistralai_azure/models/chatcompletionresponse.py +27 -0
- mistralai_azure/models/chatcompletionstreamrequest.py +112 -0
- mistralai_azure/models/completionchunk.py +27 -0
- mistralai_azure/models/completionevent.py +15 -0
- mistralai_azure/models/completionresponsestreamchoice.py +53 -0
- mistralai_azure/models/contentchunk.py +17 -0
- mistralai_azure/models/deltamessage.py +52 -0
- mistralai_azure/models/function.py +19 -0
- mistralai_azure/models/functioncall.py +16 -0
- mistralai_azure/models/httpvalidationerror.py +23 -0
- mistralai_azure/models/responseformat.py +18 -0
- mistralai_azure/models/sdkerror.py +22 -0
- mistralai_azure/models/security.py +16 -0
- mistralai_azure/models/systemmessage.py +26 -0
- mistralai_azure/models/textchunk.py +17 -0
- mistralai_azure/models/tool.py +18 -0
- mistralai_azure/models/toolcall.py +20 -0
- mistralai_azure/models/toolmessage.py +55 -0
- mistralai_azure/models/usageinfo.py +18 -0
- mistralai_azure/models/usermessage.py +26 -0
- mistralai_azure/models/validationerror.py +24 -0
- mistralai_azure/py.typed +1 -0
- mistralai_azure/sdk.py +102 -0
- mistralai_azure/sdkconfiguration.py +53 -0
- mistralai_azure/types/__init__.py +21 -0
- mistralai_azure/types/basemodel.py +35 -0
- mistralai_azure/utils/__init__.py +80 -0
- mistralai_azure/utils/annotations.py +19 -0
- mistralai_azure/utils/enums.py +34 -0
- mistralai_azure/utils/eventstreaming.py +179 -0
- mistralai_azure/utils/forms.py +207 -0
- mistralai_azure/utils/headers.py +136 -0
- mistralai_azure/utils/metadata.py +118 -0
- mistralai_azure/utils/queryparams.py +203 -0
- mistralai_azure/utils/requestbodies.py +66 -0
- mistralai_azure/utils/retries.py +216 -0
- mistralai_azure/utils/security.py +168 -0
- mistralai_azure/utils/serializers.py +181 -0
- mistralai_azure/utils/url.py +150 -0
- mistralai_azure/utils/values.py +128 -0
- mistralai_gcp/__init__.py +5 -0
- mistralai_gcp/_hooks/__init__.py +5 -0
- mistralai_gcp/_hooks/custom_user_agent.py +16 -0
- mistralai_gcp/_hooks/registration.py +15 -0
- mistralai_gcp/_hooks/sdkhooks.py +57 -0
- mistralai_gcp/_hooks/types.py +76 -0
- mistralai_gcp/basesdk.py +215 -0
- mistralai_gcp/chat.py +463 -0
- mistralai_gcp/fim.py +439 -0
- mistralai_gcp/httpclient.py +78 -0
- mistralai_gcp/models/__init__.py +31 -0
- mistralai_gcp/models/assistantmessage.py +58 -0
- mistralai_gcp/models/chatcompletionchoice.py +33 -0
- mistralai_gcp/models/chatcompletionrequest.py +110 -0
- mistralai_gcp/models/chatcompletionresponse.py +27 -0
- mistralai_gcp/models/chatcompletionstreamrequest.py +108 -0
- mistralai_gcp/models/completionchunk.py +27 -0
- mistralai_gcp/models/completionevent.py +15 -0
- mistralai_gcp/models/completionresponsestreamchoice.py +53 -0
- mistralai_gcp/models/contentchunk.py +17 -0
- mistralai_gcp/models/deltamessage.py +52 -0
- mistralai_gcp/models/fimcompletionrequest.py +99 -0
- mistralai_gcp/models/fimcompletionresponse.py +27 -0
- mistralai_gcp/models/fimcompletionstreamrequest.py +97 -0
- mistralai_gcp/models/function.py +19 -0
- mistralai_gcp/models/functioncall.py +16 -0
- mistralai_gcp/models/httpvalidationerror.py +23 -0
- mistralai_gcp/models/responseformat.py +18 -0
- mistralai_gcp/models/sdkerror.py +22 -0
- mistralai_gcp/models/security.py +16 -0
- mistralai_gcp/models/systemmessage.py +26 -0
- mistralai_gcp/models/textchunk.py +17 -0
- mistralai_gcp/models/tool.py +18 -0
- mistralai_gcp/models/toolcall.py +20 -0
- mistralai_gcp/models/toolmessage.py +55 -0
- mistralai_gcp/models/usageinfo.py +18 -0
- mistralai_gcp/models/usermessage.py +26 -0
- mistralai_gcp/models/validationerror.py +24 -0
- mistralai_gcp/py.typed +1 -0
- mistralai_gcp/sdk.py +165 -0
- mistralai_gcp/sdkconfiguration.py +53 -0
- mistralai_gcp/types/__init__.py +21 -0
- mistralai_gcp/types/basemodel.py +35 -0
- mistralai_gcp/utils/__init__.py +80 -0
- mistralai_gcp/utils/annotations.py +19 -0
- mistralai_gcp/utils/enums.py +34 -0
- mistralai_gcp/utils/eventstreaming.py +179 -0
- mistralai_gcp/utils/forms.py +207 -0
- mistralai_gcp/utils/headers.py +136 -0
- mistralai_gcp/utils/metadata.py +118 -0
- mistralai_gcp/utils/queryparams.py +203 -0
- mistralai_gcp/utils/requestbodies.py +66 -0
- mistralai_gcp/utils/retries.py +216 -0
- mistralai_gcp/utils/security.py +168 -0
- mistralai_gcp/utils/serializers.py +181 -0
- mistralai_gcp/utils/url.py +150 -0
- mistralai_gcp/utils/values.py +128 -0
- py.typed +1 -0
- mistralai/client_base.py +0 -186
- mistralai/constants.py +0 -3
- mistralai/exceptions.py +0 -54
- mistralai/jobs.py +0 -172
- mistralai/models/chat_completion.py +0 -93
- mistralai/models/common.py +0 -9
- mistralai/models/embeddings.py +0 -19
- mistralai/models/files.py +0 -23
- mistralai/models/jobs.py +0 -98
- mistralai/models/models.py +0 -39
- mistralai-0.4.1.dist-info/METADATA +0 -80
- mistralai-0.4.1.dist-info/RECORD +0 -20
- {mistralai-0.4.1.dist-info → mistralai-0.5.5a50.dist-info}/WHEEL +0 -0
mistralai/client.py
CHANGED
|
@@ -1,381 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import time
|
|
3
|
-
from json import JSONDecodeError
|
|
4
|
-
from typing import Any, Dict, Iterable, Iterator, List, Optional, Union
|
|
1
|
+
from typing import Optional
|
|
5
2
|
|
|
6
|
-
|
|
3
|
+
MIGRATION_MESSAGE = "This client is deprecated starting v1.0.0, pin your version to 0.4.2. Or migrate by following this documentation: TODO add link"
|
|
7
4
|
|
|
8
|
-
from mistralai.client_base import ClientBase
|
|
9
|
-
from mistralai.constants import ENDPOINT, RETRY_STATUS_CODES
|
|
10
|
-
from mistralai.exceptions import (
|
|
11
|
-
MistralAPIException,
|
|
12
|
-
MistralAPIStatusException,
|
|
13
|
-
MistralConnectionException,
|
|
14
|
-
MistralException,
|
|
15
|
-
)
|
|
16
|
-
from mistralai.files import FilesClient
|
|
17
|
-
from mistralai.jobs import JobsClient
|
|
18
|
-
from mistralai.models.chat_completion import (
|
|
19
|
-
ChatCompletionResponse,
|
|
20
|
-
ChatCompletionStreamResponse,
|
|
21
|
-
ResponseFormat,
|
|
22
|
-
ToolChoice,
|
|
23
|
-
)
|
|
24
|
-
from mistralai.models.embeddings import EmbeddingResponse
|
|
25
|
-
from mistralai.models.models import ModelDeleted, ModelList
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
class MistralClient(ClientBase):
|
|
29
|
-
"""
|
|
30
|
-
Synchronous wrapper around the async client
|
|
31
|
-
"""
|
|
32
5
|
|
|
6
|
+
class MistralClient:
|
|
33
7
|
def __init__(
|
|
34
8
|
self,
|
|
35
9
|
api_key: Optional[str] = None,
|
|
36
|
-
endpoint: str =
|
|
10
|
+
endpoint: str = "",
|
|
37
11
|
max_retries: int = 5,
|
|
38
12
|
timeout: int = 120,
|
|
39
13
|
):
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
self._client = Client(
|
|
43
|
-
follow_redirects=True, timeout=self._timeout, transport=HTTPTransport(retries=self._max_retries)
|
|
44
|
-
)
|
|
45
|
-
self.files = FilesClient(self)
|
|
46
|
-
self.jobs = JobsClient(self)
|
|
47
|
-
|
|
48
|
-
def __del__(self) -> None:
|
|
49
|
-
self._client.close()
|
|
50
|
-
|
|
51
|
-
def _check_response_status_codes(self, response: Response) -> None:
|
|
52
|
-
if response.status_code in RETRY_STATUS_CODES:
|
|
53
|
-
raise MistralAPIStatusException.from_response(
|
|
54
|
-
response,
|
|
55
|
-
message=f"Status: {response.status_code}. Message: {response.text}",
|
|
56
|
-
)
|
|
57
|
-
elif 400 <= response.status_code < 500:
|
|
58
|
-
if response.stream:
|
|
59
|
-
response.read()
|
|
60
|
-
raise MistralAPIException.from_response(
|
|
61
|
-
response,
|
|
62
|
-
message=f"Status: {response.status_code}. Message: {response.text}",
|
|
63
|
-
)
|
|
64
|
-
elif response.status_code >= 500:
|
|
65
|
-
if response.stream:
|
|
66
|
-
response.read()
|
|
67
|
-
raise MistralException(
|
|
68
|
-
message=f"Status: {response.status_code}. Message: {response.text}",
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
def _check_streaming_response(self, response: Response) -> None:
|
|
72
|
-
self._check_response_status_codes(response)
|
|
73
|
-
|
|
74
|
-
def _check_response(self, response: Response) -> Dict[str, Any]:
|
|
75
|
-
self._check_response_status_codes(response)
|
|
76
|
-
|
|
77
|
-
json_response: Dict[str, Any] = response.json()
|
|
78
|
-
|
|
79
|
-
if "object" not in json_response:
|
|
80
|
-
raise MistralException(message=f"Unexpected response: {json_response}")
|
|
81
|
-
if "error" == json_response["object"]: # has errors
|
|
82
|
-
raise MistralAPIException.from_response(
|
|
83
|
-
response,
|
|
84
|
-
message=json_response["message"],
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
return json_response
|
|
88
|
-
|
|
89
|
-
def _request(
|
|
90
|
-
self,
|
|
91
|
-
method: str,
|
|
92
|
-
json: Optional[Dict[str, Any]],
|
|
93
|
-
path: str,
|
|
94
|
-
stream: bool = False,
|
|
95
|
-
attempt: int = 1,
|
|
96
|
-
data: Optional[Dict[str, Any]] = None,
|
|
97
|
-
**kwargs: Any,
|
|
98
|
-
) -> Iterator[Dict[str, Any]]:
|
|
99
|
-
accept_header = "text/event-stream" if stream else "application/json"
|
|
100
|
-
headers = {
|
|
101
|
-
"Accept": accept_header,
|
|
102
|
-
"User-Agent": f"mistral-client-python/{self._version}",
|
|
103
|
-
"Authorization": f"Bearer {self._api_key}",
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if json is not None:
|
|
107
|
-
headers["Content-Type"] = "application/json"
|
|
108
|
-
|
|
109
|
-
url = posixpath.join(self._endpoint, path)
|
|
110
|
-
|
|
111
|
-
self._logger.debug(f"Sending request: {method} {url} {json}")
|
|
112
|
-
|
|
113
|
-
response: Response
|
|
114
|
-
|
|
115
|
-
try:
|
|
116
|
-
if stream:
|
|
117
|
-
with self._client.stream(
|
|
118
|
-
method,
|
|
119
|
-
url,
|
|
120
|
-
headers=headers,
|
|
121
|
-
json=json,
|
|
122
|
-
data=data,
|
|
123
|
-
**kwargs,
|
|
124
|
-
) as response:
|
|
125
|
-
self._check_streaming_response(response)
|
|
126
|
-
|
|
127
|
-
for line in response.iter_lines():
|
|
128
|
-
json_streamed_response = self._process_line(line)
|
|
129
|
-
if json_streamed_response:
|
|
130
|
-
yield json_streamed_response
|
|
131
|
-
|
|
132
|
-
else:
|
|
133
|
-
response = self._client.request(
|
|
134
|
-
method,
|
|
135
|
-
url,
|
|
136
|
-
headers=headers,
|
|
137
|
-
json=json,
|
|
138
|
-
data=data,
|
|
139
|
-
**kwargs,
|
|
140
|
-
)
|
|
141
|
-
|
|
142
|
-
yield self._check_response(response)
|
|
143
|
-
|
|
144
|
-
except ConnectError as e:
|
|
145
|
-
raise MistralConnectionException(str(e)) from e
|
|
146
|
-
except RequestError as e:
|
|
147
|
-
raise MistralException(f"Unexpected exception ({e.__class__.__name__}): {e}") from e
|
|
148
|
-
except JSONDecodeError as e:
|
|
149
|
-
raise MistralAPIException.from_response(
|
|
150
|
-
response,
|
|
151
|
-
message=f"Failed to decode json body: {response.text}",
|
|
152
|
-
) from e
|
|
153
|
-
except MistralAPIStatusException as e:
|
|
154
|
-
attempt += 1
|
|
155
|
-
if attempt > self._max_retries:
|
|
156
|
-
raise MistralAPIStatusException.from_response(response, message=str(e)) from e
|
|
157
|
-
backoff = 2.0**attempt # exponential backoff
|
|
158
|
-
time.sleep(backoff)
|
|
159
|
-
|
|
160
|
-
# Retry as a generator
|
|
161
|
-
for r in self._request(method, json, path, stream=stream, attempt=attempt):
|
|
162
|
-
yield r
|
|
163
|
-
|
|
164
|
-
def chat(
|
|
165
|
-
self,
|
|
166
|
-
messages: List[Any],
|
|
167
|
-
model: Optional[str] = None,
|
|
168
|
-
tools: Optional[List[Dict[str, Any]]] = None,
|
|
169
|
-
temperature: Optional[float] = None,
|
|
170
|
-
max_tokens: Optional[int] = None,
|
|
171
|
-
top_p: Optional[float] = None,
|
|
172
|
-
random_seed: Optional[int] = None,
|
|
173
|
-
safe_mode: bool = False,
|
|
174
|
-
safe_prompt: bool = False,
|
|
175
|
-
tool_choice: Optional[Union[str, ToolChoice]] = None,
|
|
176
|
-
response_format: Optional[Union[Dict[str, str], ResponseFormat]] = None,
|
|
177
|
-
) -> ChatCompletionResponse:
|
|
178
|
-
"""A chat endpoint that returns a single response.
|
|
179
|
-
|
|
180
|
-
Args:
|
|
181
|
-
model (str): model the name of the model to chat with, e.g. mistral-tiny
|
|
182
|
-
messages (List[Any]): messages an array of messages to chat with, e.g.
|
|
183
|
-
[{role: 'user', content: 'What is the best French cheese?'}]
|
|
184
|
-
tools (Optional[List[Function]], optional): a list of tools to use.
|
|
185
|
-
temperature (Optional[float], optional): temperature the temperature to use for sampling, e.g. 0.5.
|
|
186
|
-
max_tokens (Optional[int], optional): the maximum number of tokens to generate, e.g. 100. Defaults to None.
|
|
187
|
-
top_p (Optional[float], optional): the cumulative probability of tokens to generate, e.g. 0.9.
|
|
188
|
-
Defaults to None.
|
|
189
|
-
random_seed (Optional[int], optional): the random seed to use for sampling, e.g. 42. Defaults to None.
|
|
190
|
-
safe_mode (bool, optional): deprecated, use safe_prompt instead. Defaults to False.
|
|
191
|
-
safe_prompt (bool, optional): whether to use safe prompt, e.g. true. Defaults to False.
|
|
192
|
-
|
|
193
|
-
Returns:
|
|
194
|
-
ChatCompletionResponse: a response object containing the generated text.
|
|
195
|
-
"""
|
|
196
|
-
request = self._make_chat_request(
|
|
197
|
-
messages,
|
|
198
|
-
model,
|
|
199
|
-
tools=tools,
|
|
200
|
-
temperature=temperature,
|
|
201
|
-
max_tokens=max_tokens,
|
|
202
|
-
top_p=top_p,
|
|
203
|
-
random_seed=random_seed,
|
|
204
|
-
stream=False,
|
|
205
|
-
safe_prompt=safe_mode or safe_prompt,
|
|
206
|
-
tool_choice=tool_choice,
|
|
207
|
-
response_format=response_format,
|
|
208
|
-
)
|
|
209
|
-
|
|
210
|
-
single_response = self._request("post", request, "v1/chat/completions")
|
|
211
|
-
|
|
212
|
-
for response in single_response:
|
|
213
|
-
return ChatCompletionResponse(**response)
|
|
214
|
-
|
|
215
|
-
raise MistralException("No response received")
|
|
216
|
-
|
|
217
|
-
def chat_stream(
|
|
218
|
-
self,
|
|
219
|
-
messages: List[Any],
|
|
220
|
-
model: Optional[str] = None,
|
|
221
|
-
tools: Optional[List[Dict[str, Any]]] = None,
|
|
222
|
-
temperature: Optional[float] = None,
|
|
223
|
-
max_tokens: Optional[int] = None,
|
|
224
|
-
top_p: Optional[float] = None,
|
|
225
|
-
random_seed: Optional[int] = None,
|
|
226
|
-
safe_mode: bool = False,
|
|
227
|
-
safe_prompt: bool = False,
|
|
228
|
-
tool_choice: Optional[Union[str, ToolChoice]] = None,
|
|
229
|
-
response_format: Optional[Union[Dict[str, str], ResponseFormat]] = None,
|
|
230
|
-
) -> Iterable[ChatCompletionStreamResponse]:
|
|
231
|
-
"""A chat endpoint that streams responses.
|
|
232
|
-
|
|
233
|
-
Args:
|
|
234
|
-
model (str): model the name of the model to chat with, e.g. mistral-tiny
|
|
235
|
-
messages (List[Any]): messages an array of messages to chat with, e.g.
|
|
236
|
-
[{role: 'user', content: 'What is the best French cheese?'}]
|
|
237
|
-
tools (Optional[List[Function]], optional): a list of tools to use.
|
|
238
|
-
temperature (Optional[float], optional): temperature the temperature to use for sampling, e.g. 0.5.
|
|
239
|
-
max_tokens (Optional[int], optional): the maximum number of tokens to generate, e.g. 100. Defaults to None.
|
|
240
|
-
top_p (Optional[float], optional): the cumulative probability of tokens to generate, e.g. 0.9.
|
|
241
|
-
Defaults to None.
|
|
242
|
-
random_seed (Optional[int], optional): the random seed to use for sampling, e.g. 42. Defaults to None.
|
|
243
|
-
safe_mode (bool, optional): deprecated, use safe_prompt instead. Defaults to False.
|
|
244
|
-
safe_prompt (bool, optional): whether to use safe prompt, e.g. true. Defaults to False.
|
|
245
|
-
|
|
246
|
-
Returns:
|
|
247
|
-
Iterable[ChatCompletionStreamResponse]:
|
|
248
|
-
A generator that yields ChatCompletionStreamResponse objects.
|
|
249
|
-
"""
|
|
250
|
-
request = self._make_chat_request(
|
|
251
|
-
messages,
|
|
252
|
-
model,
|
|
253
|
-
tools=tools,
|
|
254
|
-
temperature=temperature,
|
|
255
|
-
max_tokens=max_tokens,
|
|
256
|
-
top_p=top_p,
|
|
257
|
-
random_seed=random_seed,
|
|
258
|
-
stream=True,
|
|
259
|
-
safe_prompt=safe_mode or safe_prompt,
|
|
260
|
-
tool_choice=tool_choice,
|
|
261
|
-
response_format=response_format,
|
|
262
|
-
)
|
|
263
|
-
|
|
264
|
-
response = self._request("post", request, "v1/chat/completions", stream=True)
|
|
265
|
-
|
|
266
|
-
for json_streamed_response in response:
|
|
267
|
-
yield ChatCompletionStreamResponse(**json_streamed_response)
|
|
268
|
-
|
|
269
|
-
def embeddings(self, model: str, input: Union[str, List[str]]) -> EmbeddingResponse:
|
|
270
|
-
"""An embeddings endpoint that returns embeddings for a single, or batch of inputs
|
|
271
|
-
|
|
272
|
-
Args:
|
|
273
|
-
model (str): The embedding model to use, e.g. mistral-embed
|
|
274
|
-
input (Union[str, List[str]]): The input to embed,
|
|
275
|
-
e.g. ['What is the best French cheese?']
|
|
276
|
-
|
|
277
|
-
Returns:
|
|
278
|
-
EmbeddingResponse: A response object containing the embeddings.
|
|
279
|
-
"""
|
|
280
|
-
request = {"model": model, "input": input}
|
|
281
|
-
singleton_response = self._request("post", request, "v1/embeddings")
|
|
282
|
-
|
|
283
|
-
for response in singleton_response:
|
|
284
|
-
return EmbeddingResponse(**response)
|
|
285
|
-
|
|
286
|
-
raise MistralException("No response received")
|
|
287
|
-
|
|
288
|
-
def list_models(self) -> ModelList:
|
|
289
|
-
"""Returns a list of the available models
|
|
290
|
-
|
|
291
|
-
Returns:
|
|
292
|
-
ModelList: A response object containing the list of models.
|
|
293
|
-
"""
|
|
294
|
-
singleton_response = self._request("get", {}, "v1/models")
|
|
295
|
-
|
|
296
|
-
for response in singleton_response:
|
|
297
|
-
return ModelList(**response)
|
|
298
|
-
|
|
299
|
-
raise MistralException("No response received")
|
|
300
|
-
|
|
301
|
-
def delete_model(self, model_id: str) -> ModelDeleted:
|
|
302
|
-
single_response = self._request("delete", {}, f"v1/models/{model_id}")
|
|
303
|
-
|
|
304
|
-
for response in single_response:
|
|
305
|
-
return ModelDeleted(**response)
|
|
306
|
-
|
|
307
|
-
raise MistralException("No response received")
|
|
308
|
-
|
|
309
|
-
def completion(
|
|
310
|
-
self,
|
|
311
|
-
model: str,
|
|
312
|
-
prompt: str,
|
|
313
|
-
suffix: Optional[str] = None,
|
|
314
|
-
temperature: Optional[float] = None,
|
|
315
|
-
max_tokens: Optional[int] = None,
|
|
316
|
-
top_p: Optional[float] = None,
|
|
317
|
-
random_seed: Optional[int] = None,
|
|
318
|
-
stop: Optional[List[str]] = None,
|
|
319
|
-
) -> ChatCompletionResponse:
|
|
320
|
-
"""A completion endpoint that returns a single response.
|
|
321
|
-
|
|
322
|
-
Args:
|
|
323
|
-
model (str): model the name of the model to get completion with, e.g. codestral-latest
|
|
324
|
-
prompt (str): the prompt to complete
|
|
325
|
-
suffix (Optional[str]): the suffix to append to the prompt for fill-in-the-middle completion
|
|
326
|
-
temperature (Optional[float], optional): temperature the temperature to use for sampling, e.g. 0.5.
|
|
327
|
-
max_tokens (Optional[int], optional): the maximum number of tokens to generate, e.g. 100. Defaults to None.
|
|
328
|
-
top_p (Optional[float], optional): the cumulative probability of tokens to generate, e.g. 0.9.
|
|
329
|
-
Defaults to None.
|
|
330
|
-
random_seed (Optional[int], optional): the random seed to use for sampling, e.g. 42. Defaults to None.
|
|
331
|
-
stop (Optional[List[str]], optional): a list of tokens to stop generation at, e.g. ['/n/n']
|
|
332
|
-
|
|
333
|
-
Returns:
|
|
334
|
-
Dict[str, Any]: a response object containing the generated text.
|
|
335
|
-
"""
|
|
336
|
-
request = self._make_completion_request(
|
|
337
|
-
prompt, model, suffix, temperature, max_tokens, top_p, random_seed, stop
|
|
338
|
-
)
|
|
339
|
-
|
|
340
|
-
single_response = self._request("post", request, "v1/fim/completions", stream=False)
|
|
341
|
-
|
|
342
|
-
for response in single_response:
|
|
343
|
-
return ChatCompletionResponse(**response)
|
|
344
|
-
|
|
345
|
-
raise MistralException("No response received")
|
|
346
|
-
|
|
347
|
-
def completion_stream(
|
|
348
|
-
self,
|
|
349
|
-
model: str,
|
|
350
|
-
prompt: str,
|
|
351
|
-
suffix: Optional[str] = None,
|
|
352
|
-
temperature: Optional[float] = None,
|
|
353
|
-
max_tokens: Optional[int] = None,
|
|
354
|
-
top_p: Optional[float] = None,
|
|
355
|
-
random_seed: Optional[int] = None,
|
|
356
|
-
stop: Optional[List[str]] = None,
|
|
357
|
-
) -> Iterable[ChatCompletionStreamResponse]:
|
|
358
|
-
"""An asynchronous completion endpoint that streams responses.
|
|
359
|
-
|
|
360
|
-
Args:
|
|
361
|
-
model (str): model the name of the model to get completions with, e.g. codestral-latest
|
|
362
|
-
prompt (str): the prompt to complete
|
|
363
|
-
suffix (Optional[str]): the suffix to append to the prompt for fill-in-the-middle completion
|
|
364
|
-
temperature (Optional[float], optional): temperature the temperature to use for sampling, e.g. 0.5.
|
|
365
|
-
max_tokens (Optional[int], optional): the maximum number of tokens to generate, e.g. 100. Defaults to None.
|
|
366
|
-
top_p (Optional[float], optional): the cumulative probability of tokens to generate, e.g. 0.9.
|
|
367
|
-
Defaults to None.
|
|
368
|
-
random_seed (Optional[int], optional): the random seed to use for sampling, e.g. 42. Defaults to None.
|
|
369
|
-
stop (Optional[List[str]], optional): a list of tokens to stop generation at, e.g. ['/n/n']
|
|
370
|
-
|
|
371
|
-
Returns:
|
|
372
|
-
Iterable[Dict[str, Any]]: a generator that yields response objects containing the generated text.
|
|
373
|
-
"""
|
|
374
|
-
request = self._make_completion_request(
|
|
375
|
-
prompt, model, suffix, temperature, max_tokens, top_p, random_seed, stop, stream=True
|
|
376
|
-
)
|
|
377
|
-
|
|
378
|
-
response = self._request("post", request, "v1/fim/completions", stream=True)
|
|
379
|
-
|
|
380
|
-
for json_streamed_response in response:
|
|
381
|
-
yield ChatCompletionStreamResponse(**json_streamed_response)
|
|
14
|
+
raise NotImplementedError(MIGRATION_MESSAGE)
|
mistralai/embeddings.py
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
|
2
|
+
|
|
3
|
+
from .basesdk import BaseSDK
|
|
4
|
+
from mistralai import models
|
|
5
|
+
from mistralai._hooks import HookContext
|
|
6
|
+
from mistralai.types import OptionalNullable, UNSET
|
|
7
|
+
import mistralai.utils as utils
|
|
8
|
+
from typing import Any, Optional, Union
|
|
9
|
+
|
|
10
|
+
class Embeddings(BaseSDK):
|
|
11
|
+
r"""Embeddings API."""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def create(
|
|
15
|
+
self, *,
|
|
16
|
+
inputs: Union[models.Inputs, models.InputsTypedDict],
|
|
17
|
+
model: str,
|
|
18
|
+
encoding_format: OptionalNullable[str] = UNSET,
|
|
19
|
+
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
|
20
|
+
server_url: Optional[str] = None,
|
|
21
|
+
timeout_ms: Optional[int] = None,
|
|
22
|
+
) -> Optional[models.EmbeddingResponse]:
|
|
23
|
+
r"""Embeddings
|
|
24
|
+
|
|
25
|
+
Embeddings
|
|
26
|
+
|
|
27
|
+
:param inputs: Text to embed.
|
|
28
|
+
:param model: ID of the model to use.
|
|
29
|
+
:param encoding_format: The format to return the embeddings in.
|
|
30
|
+
:param retries: Override the default retry configuration for this method
|
|
31
|
+
:param server_url: Override the default server URL for this method
|
|
32
|
+
:param timeout_ms: Override the default request timeout configuration for this method in milliseconds
|
|
33
|
+
"""
|
|
34
|
+
base_url = None
|
|
35
|
+
url_variables = None
|
|
36
|
+
if timeout_ms is None:
|
|
37
|
+
timeout_ms = self.sdk_configuration.timeout_ms
|
|
38
|
+
|
|
39
|
+
if server_url is not None:
|
|
40
|
+
base_url = server_url
|
|
41
|
+
|
|
42
|
+
request = models.EmbeddingRequest(
|
|
43
|
+
inputs=inputs,
|
|
44
|
+
model=model,
|
|
45
|
+
encoding_format=encoding_format,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
req = self.build_request(
|
|
49
|
+
method="POST",
|
|
50
|
+
path="/v1/embeddings",
|
|
51
|
+
base_url=base_url,
|
|
52
|
+
url_variables=url_variables,
|
|
53
|
+
request=request,
|
|
54
|
+
request_body_required=True,
|
|
55
|
+
request_has_path_params=False,
|
|
56
|
+
request_has_query_params=True,
|
|
57
|
+
user_agent_header="user-agent",
|
|
58
|
+
accept_header_value="application/json",
|
|
59
|
+
security=self.sdk_configuration.security,
|
|
60
|
+
get_serialized_body=lambda: utils.serialize_request_body(request, False, False, "json", models.EmbeddingRequest),
|
|
61
|
+
timeout_ms=timeout_ms,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
if retries == UNSET:
|
|
65
|
+
if self.sdk_configuration.retry_config is not UNSET:
|
|
66
|
+
retries = self.sdk_configuration.retry_config
|
|
67
|
+
|
|
68
|
+
retry_config = None
|
|
69
|
+
if isinstance(retries, utils.RetryConfig):
|
|
70
|
+
retry_config = (retries, [
|
|
71
|
+
"429",
|
|
72
|
+
"500",
|
|
73
|
+
"502",
|
|
74
|
+
"503",
|
|
75
|
+
"504"
|
|
76
|
+
])
|
|
77
|
+
|
|
78
|
+
http_res = self.do_request(
|
|
79
|
+
hook_ctx=HookContext(operation_id="embeddings_v1_embeddings_post", oauth2_scopes=[], security_source=self.sdk_configuration.security),
|
|
80
|
+
request=req,
|
|
81
|
+
error_status_codes=["422","4XX","5XX"],
|
|
82
|
+
retry_config=retry_config
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
data: Any = None
|
|
86
|
+
if utils.match_response(http_res, "200", "application/json"):
|
|
87
|
+
return utils.unmarshal_json(http_res.text, Optional[models.EmbeddingResponse])
|
|
88
|
+
if utils.match_response(http_res, "422", "application/json"):
|
|
89
|
+
data = utils.unmarshal_json(http_res.text, models.HTTPValidationErrorData)
|
|
90
|
+
raise models.HTTPValidationError(data=data)
|
|
91
|
+
if utils.match_response(http_res, ["4XX","5XX"], "*"):
|
|
92
|
+
raise models.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
|
|
93
|
+
|
|
94
|
+
content_type = http_res.headers.get("Content-Type")
|
|
95
|
+
raise models.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
async def create_async(
|
|
100
|
+
self, *,
|
|
101
|
+
inputs: Union[models.Inputs, models.InputsTypedDict],
|
|
102
|
+
model: str,
|
|
103
|
+
encoding_format: OptionalNullable[str] = UNSET,
|
|
104
|
+
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
|
105
|
+
server_url: Optional[str] = None,
|
|
106
|
+
timeout_ms: Optional[int] = None,
|
|
107
|
+
) -> Optional[models.EmbeddingResponse]:
|
|
108
|
+
r"""Embeddings
|
|
109
|
+
|
|
110
|
+
Embeddings
|
|
111
|
+
|
|
112
|
+
:param inputs: Text to embed.
|
|
113
|
+
:param model: ID of the model to use.
|
|
114
|
+
:param encoding_format: The format to return the embeddings in.
|
|
115
|
+
:param retries: Override the default retry configuration for this method
|
|
116
|
+
:param server_url: Override the default server URL for this method
|
|
117
|
+
:param timeout_ms: Override the default request timeout configuration for this method in milliseconds
|
|
118
|
+
"""
|
|
119
|
+
base_url = None
|
|
120
|
+
url_variables = None
|
|
121
|
+
if timeout_ms is None:
|
|
122
|
+
timeout_ms = self.sdk_configuration.timeout_ms
|
|
123
|
+
|
|
124
|
+
if server_url is not None:
|
|
125
|
+
base_url = server_url
|
|
126
|
+
|
|
127
|
+
request = models.EmbeddingRequest(
|
|
128
|
+
inputs=inputs,
|
|
129
|
+
model=model,
|
|
130
|
+
encoding_format=encoding_format,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
req = self.build_request(
|
|
134
|
+
method="POST",
|
|
135
|
+
path="/v1/embeddings",
|
|
136
|
+
base_url=base_url,
|
|
137
|
+
url_variables=url_variables,
|
|
138
|
+
request=request,
|
|
139
|
+
request_body_required=True,
|
|
140
|
+
request_has_path_params=False,
|
|
141
|
+
request_has_query_params=True,
|
|
142
|
+
user_agent_header="user-agent",
|
|
143
|
+
accept_header_value="application/json",
|
|
144
|
+
security=self.sdk_configuration.security,
|
|
145
|
+
get_serialized_body=lambda: utils.serialize_request_body(request, False, False, "json", models.EmbeddingRequest),
|
|
146
|
+
timeout_ms=timeout_ms,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
if retries == UNSET:
|
|
150
|
+
if self.sdk_configuration.retry_config is not UNSET:
|
|
151
|
+
retries = self.sdk_configuration.retry_config
|
|
152
|
+
|
|
153
|
+
retry_config = None
|
|
154
|
+
if isinstance(retries, utils.RetryConfig):
|
|
155
|
+
retry_config = (retries, [
|
|
156
|
+
"429",
|
|
157
|
+
"500",
|
|
158
|
+
"502",
|
|
159
|
+
"503",
|
|
160
|
+
"504"
|
|
161
|
+
])
|
|
162
|
+
|
|
163
|
+
http_res = await self.do_request_async(
|
|
164
|
+
hook_ctx=HookContext(operation_id="embeddings_v1_embeddings_post", oauth2_scopes=[], security_source=self.sdk_configuration.security),
|
|
165
|
+
request=req,
|
|
166
|
+
error_status_codes=["422","4XX","5XX"],
|
|
167
|
+
retry_config=retry_config
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
data: Any = None
|
|
171
|
+
if utils.match_response(http_res, "200", "application/json"):
|
|
172
|
+
return utils.unmarshal_json(http_res.text, Optional[models.EmbeddingResponse])
|
|
173
|
+
if utils.match_response(http_res, "422", "application/json"):
|
|
174
|
+
data = utils.unmarshal_json(http_res.text, models.HTTPValidationErrorData)
|
|
175
|
+
raise models.HTTPValidationError(data=data)
|
|
176
|
+
if utils.match_response(http_res, ["4XX","5XX"], "*"):
|
|
177
|
+
raise models.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
|
|
178
|
+
|
|
179
|
+
content_type = http_res.headers.get("Content-Type")
|
|
180
|
+
raise models.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
|
|
181
|
+
|
|
182
|
+
|