mistralai 0.5.5a50__py3-none-any.whl → 1.0.0rc2__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/agents.py +434 -0
- mistralai/basesdk.py +43 -6
- mistralai/chat.py +29 -34
- mistralai/embeddings.py +4 -4
- mistralai/files.py +10 -10
- mistralai/fim.py +17 -18
- mistralai/fine_tuning.py +10 -849
- mistralai/jobs.py +854 -0
- mistralai/models/__init__.py +4 -2
- mistralai/models/agentscompletionrequest.py +96 -0
- mistralai/models/agentscompletionstreamrequest.py +92 -0
- mistralai/models/assistantmessage.py +4 -9
- mistralai/models/chatcompletionchoice.py +4 -15
- mistralai/models/chatcompletionrequest.py +11 -16
- mistralai/models/chatcompletionstreamrequest.py +11 -16
- mistralai/models/completionresponsestreamchoice.py +4 -9
- mistralai/models/deltamessage.py +4 -9
- mistralai/models/detailedjobout.py +4 -9
- mistralai/models/embeddingrequest.py +4 -9
- mistralai/models/eventout.py +4 -9
- mistralai/models/fileschema.py +4 -9
- mistralai/models/fimcompletionrequest.py +11 -16
- mistralai/models/fimcompletionstreamrequest.py +11 -16
- mistralai/models/ftmodelout.py +4 -9
- mistralai/models/githubrepositoryin.py +4 -9
- mistralai/models/githubrepositoryout.py +4 -9
- mistralai/models/httpvalidationerror.py +1 -1
- mistralai/models/jobin.py +4 -9
- mistralai/models/jobmetadataout.py +4 -9
- mistralai/models/jobout.py +4 -9
- mistralai/models/jobs_api_routes_fine_tuning_create_fine_tuning_jobop.py +4 -9
- mistralai/models/jobs_api_routes_fine_tuning_get_fine_tuning_jobsop.py +4 -9
- mistralai/models/legacyjobmetadataout.py +4 -9
- mistralai/models/metricout.py +4 -9
- mistralai/models/modelcard.py +4 -9
- mistralai/models/retrievefileout.py +4 -9
- mistralai/models/security.py +4 -4
- mistralai/models/toolmessage.py +4 -9
- mistralai/models/trainingparameters.py +4 -9
- mistralai/models/trainingparametersin.py +4 -9
- mistralai/models/updateftmodelin.py +4 -9
- mistralai/models/uploadfileout.py +4 -9
- mistralai/models/wandbintegration.py +4 -9
- mistralai/models/wandbintegrationout.py +4 -9
- mistralai/models_.py +14 -14
- mistralai/sdk.py +14 -6
- mistralai/sdkconfiguration.py +5 -4
- mistralai/types/basemodel.py +10 -6
- mistralai/utils/__init__.py +4 -0
- mistralai/utils/eventstreaming.py +8 -9
- mistralai/utils/logger.py +16 -0
- mistralai/utils/retries.py +2 -2
- mistralai/utils/security.py +5 -2
- {mistralai-0.5.5a50.dist-info → mistralai-1.0.0rc2.dist-info}/METADATA +113 -53
- {mistralai-0.5.5a50.dist-info → mistralai-1.0.0rc2.dist-info}/RECORD +96 -89
- mistralai_azure/basesdk.py +42 -4
- mistralai_azure/chat.py +15 -20
- mistralai_azure/models/__init__.py +2 -2
- mistralai_azure/models/assistantmessage.py +4 -9
- mistralai_azure/models/chatcompletionchoice.py +4 -15
- mistralai_azure/models/chatcompletionrequest.py +7 -12
- mistralai_azure/models/chatcompletionstreamrequest.py +7 -12
- mistralai_azure/models/completionresponsestreamchoice.py +4 -9
- mistralai_azure/models/deltamessage.py +4 -9
- mistralai_azure/models/httpvalidationerror.py +1 -1
- mistralai_azure/models/toolmessage.py +4 -9
- mistralai_azure/sdk.py +7 -2
- mistralai_azure/sdkconfiguration.py +5 -4
- mistralai_azure/types/basemodel.py +10 -6
- mistralai_azure/utils/__init__.py +4 -0
- mistralai_azure/utils/eventstreaming.py +8 -9
- mistralai_azure/utils/logger.py +16 -0
- mistralai_azure/utils/retries.py +2 -2
- mistralai_gcp/basesdk.py +42 -4
- mistralai_gcp/chat.py +12 -17
- mistralai_gcp/fim.py +12 -13
- mistralai_gcp/models/__init__.py +2 -2
- mistralai_gcp/models/assistantmessage.py +4 -9
- mistralai_gcp/models/chatcompletionchoice.py +4 -15
- mistralai_gcp/models/chatcompletionrequest.py +9 -14
- mistralai_gcp/models/chatcompletionstreamrequest.py +9 -14
- mistralai_gcp/models/completionresponsestreamchoice.py +4 -9
- mistralai_gcp/models/deltamessage.py +4 -9
- mistralai_gcp/models/fimcompletionrequest.py +11 -16
- mistralai_gcp/models/fimcompletionstreamrequest.py +11 -16
- mistralai_gcp/models/httpvalidationerror.py +1 -1
- mistralai_gcp/models/toolmessage.py +4 -9
- mistralai_gcp/sdk.py +9 -0
- mistralai_gcp/sdkconfiguration.py +5 -4
- mistralai_gcp/types/basemodel.py +10 -6
- mistralai_gcp/utils/__init__.py +4 -0
- mistralai_gcp/utils/eventstreaming.py +8 -9
- mistralai_gcp/utils/logger.py +16 -0
- mistralai_gcp/utils/retries.py +2 -2
- {mistralai-0.5.5a50.dist-info → mistralai-1.0.0rc2.dist-info}/LICENSE +0 -0
- {mistralai-0.5.5a50.dist-info → mistralai-1.0.0rc2.dist-info}/WHEEL +0 -0
mistralai/agents.py
ADDED
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
|
2
|
+
|
|
3
|
+
from .basesdk import BaseSDK
|
|
4
|
+
from mistralai import models, utils
|
|
5
|
+
from mistralai._hooks import HookContext
|
|
6
|
+
from mistralai.types import Nullable, OptionalNullable, UNSET
|
|
7
|
+
from mistralai.utils import eventstreaming, get_security_from_env
|
|
8
|
+
from typing import Any, AsyncGenerator, Generator, List, Optional, Union
|
|
9
|
+
|
|
10
|
+
class Agents(BaseSDK):
|
|
11
|
+
r"""Agents API."""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def complete(
|
|
15
|
+
self, *,
|
|
16
|
+
messages: Union[List[models.AgentsCompletionRequestMessages], List[models.AgentsCompletionRequestMessagesTypedDict]],
|
|
17
|
+
agent_id: str,
|
|
18
|
+
max_tokens: OptionalNullable[int] = UNSET,
|
|
19
|
+
min_tokens: OptionalNullable[int] = UNSET,
|
|
20
|
+
stream: Optional[bool] = False,
|
|
21
|
+
stop: Optional[Union[models.AgentsCompletionRequestStop, models.AgentsCompletionRequestStopTypedDict]] = None,
|
|
22
|
+
random_seed: OptionalNullable[int] = UNSET,
|
|
23
|
+
response_format: Optional[Union[models.ResponseFormat, models.ResponseFormatTypedDict]] = None,
|
|
24
|
+
tools: OptionalNullable[Union[List[models.Tool], List[models.ToolTypedDict]]] = UNSET,
|
|
25
|
+
tool_choice: Optional[models.AgentsCompletionRequestToolChoice] = "auto",
|
|
26
|
+
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
|
27
|
+
server_url: Optional[str] = None,
|
|
28
|
+
timeout_ms: Optional[int] = None,
|
|
29
|
+
) -> Optional[models.ChatCompletionResponse]:
|
|
30
|
+
r"""Chat Completion
|
|
31
|
+
|
|
32
|
+
:param messages: The prompt(s) to generate completions for, encoded as a list of dict with role and content.
|
|
33
|
+
:param agent_id: The ID of the agent to use for this completion.
|
|
34
|
+
:param max_tokens: The maximum number of tokens to generate in the completion. The token count of your prompt plus `max_tokens` cannot exceed the model's context length.
|
|
35
|
+
:param min_tokens: The minimum number of tokens to generate in the completion.
|
|
36
|
+
:param stream: Whether to stream back partial progress. If set, tokens will be sent as data-only server-side events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.
|
|
37
|
+
:param stop: Stop generation if this token is detected. Or if one of these tokens is detected when providing an array
|
|
38
|
+
:param random_seed: The seed to use for random sampling. If set, different calls will generate deterministic results.
|
|
39
|
+
:param response_format:
|
|
40
|
+
:param tools:
|
|
41
|
+
:param tool_choice:
|
|
42
|
+
:param retries: Override the default retry configuration for this method
|
|
43
|
+
:param server_url: Override the default server URL for this method
|
|
44
|
+
:param timeout_ms: Override the default request timeout configuration for this method in milliseconds
|
|
45
|
+
"""
|
|
46
|
+
base_url = None
|
|
47
|
+
url_variables = None
|
|
48
|
+
if timeout_ms is None:
|
|
49
|
+
timeout_ms = self.sdk_configuration.timeout_ms
|
|
50
|
+
|
|
51
|
+
if server_url is not None:
|
|
52
|
+
base_url = server_url
|
|
53
|
+
|
|
54
|
+
request = models.AgentsCompletionRequest(
|
|
55
|
+
max_tokens=max_tokens,
|
|
56
|
+
min_tokens=min_tokens,
|
|
57
|
+
stream=stream,
|
|
58
|
+
stop=stop,
|
|
59
|
+
random_seed=random_seed,
|
|
60
|
+
messages=utils.get_pydantic_model(messages, List[models.AgentsCompletionRequestMessages]),
|
|
61
|
+
response_format=utils.get_pydantic_model(response_format, Optional[models.ResponseFormat]),
|
|
62
|
+
tools=utils.get_pydantic_model(tools, OptionalNullable[List[models.Tool]]),
|
|
63
|
+
tool_choice=tool_choice,
|
|
64
|
+
agent_id=agent_id,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
req = self.build_request(
|
|
68
|
+
method="POST",
|
|
69
|
+
path="/v1/agents/completions",
|
|
70
|
+
base_url=base_url,
|
|
71
|
+
url_variables=url_variables,
|
|
72
|
+
request=request,
|
|
73
|
+
request_body_required=True,
|
|
74
|
+
request_has_path_params=False,
|
|
75
|
+
request_has_query_params=True,
|
|
76
|
+
user_agent_header="user-agent",
|
|
77
|
+
accept_header_value="application/json",
|
|
78
|
+
security=self.sdk_configuration.security,
|
|
79
|
+
get_serialized_body=lambda: utils.serialize_request_body(request, False, False, "json", models.AgentsCompletionRequest),
|
|
80
|
+
timeout_ms=timeout_ms,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
if retries == UNSET:
|
|
84
|
+
if self.sdk_configuration.retry_config is not UNSET:
|
|
85
|
+
retries = self.sdk_configuration.retry_config
|
|
86
|
+
|
|
87
|
+
retry_config = None
|
|
88
|
+
if isinstance(retries, utils.RetryConfig):
|
|
89
|
+
retry_config = (retries, [
|
|
90
|
+
"429",
|
|
91
|
+
"500",
|
|
92
|
+
"502",
|
|
93
|
+
"503",
|
|
94
|
+
"504"
|
|
95
|
+
])
|
|
96
|
+
|
|
97
|
+
http_res = self.do_request(
|
|
98
|
+
hook_ctx=HookContext(operation_id="agents_completion_v1_agents_completions_post", oauth2_scopes=[], security_source=get_security_from_env(self.sdk_configuration.security, models.Security)),
|
|
99
|
+
request=req,
|
|
100
|
+
error_status_codes=["422","4XX","5XX"],
|
|
101
|
+
retry_config=retry_config
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
data: Any = None
|
|
105
|
+
if utils.match_response(http_res, "200", "application/json"):
|
|
106
|
+
return utils.unmarshal_json(http_res.text, Optional[models.ChatCompletionResponse])
|
|
107
|
+
if utils.match_response(http_res, "422", "application/json"):
|
|
108
|
+
data = utils.unmarshal_json(http_res.text, models.HTTPValidationErrorData)
|
|
109
|
+
raise models.HTTPValidationError(data=data)
|
|
110
|
+
if utils.match_response(http_res, ["4XX","5XX"], "*"):
|
|
111
|
+
raise models.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
|
|
112
|
+
|
|
113
|
+
content_type = http_res.headers.get("Content-Type")
|
|
114
|
+
raise models.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
async def complete_async(
|
|
119
|
+
self, *,
|
|
120
|
+
messages: Union[List[models.AgentsCompletionRequestMessages], List[models.AgentsCompletionRequestMessagesTypedDict]],
|
|
121
|
+
agent_id: str,
|
|
122
|
+
max_tokens: OptionalNullable[int] = UNSET,
|
|
123
|
+
min_tokens: OptionalNullable[int] = UNSET,
|
|
124
|
+
stream: Optional[bool] = False,
|
|
125
|
+
stop: Optional[Union[models.AgentsCompletionRequestStop, models.AgentsCompletionRequestStopTypedDict]] = None,
|
|
126
|
+
random_seed: OptionalNullable[int] = UNSET,
|
|
127
|
+
response_format: Optional[Union[models.ResponseFormat, models.ResponseFormatTypedDict]] = None,
|
|
128
|
+
tools: OptionalNullable[Union[List[models.Tool], List[models.ToolTypedDict]]] = UNSET,
|
|
129
|
+
tool_choice: Optional[models.AgentsCompletionRequestToolChoice] = "auto",
|
|
130
|
+
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
|
131
|
+
server_url: Optional[str] = None,
|
|
132
|
+
timeout_ms: Optional[int] = None,
|
|
133
|
+
) -> Optional[models.ChatCompletionResponse]:
|
|
134
|
+
r"""Chat Completion
|
|
135
|
+
|
|
136
|
+
:param messages: The prompt(s) to generate completions for, encoded as a list of dict with role and content.
|
|
137
|
+
:param agent_id: The ID of the agent to use for this completion.
|
|
138
|
+
:param max_tokens: The maximum number of tokens to generate in the completion. The token count of your prompt plus `max_tokens` cannot exceed the model's context length.
|
|
139
|
+
:param min_tokens: The minimum number of tokens to generate in the completion.
|
|
140
|
+
:param stream: Whether to stream back partial progress. If set, tokens will be sent as data-only server-side events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.
|
|
141
|
+
:param stop: Stop generation if this token is detected. Or if one of these tokens is detected when providing an array
|
|
142
|
+
:param random_seed: The seed to use for random sampling. If set, different calls will generate deterministic results.
|
|
143
|
+
:param response_format:
|
|
144
|
+
:param tools:
|
|
145
|
+
:param tool_choice:
|
|
146
|
+
:param retries: Override the default retry configuration for this method
|
|
147
|
+
:param server_url: Override the default server URL for this method
|
|
148
|
+
:param timeout_ms: Override the default request timeout configuration for this method in milliseconds
|
|
149
|
+
"""
|
|
150
|
+
base_url = None
|
|
151
|
+
url_variables = None
|
|
152
|
+
if timeout_ms is None:
|
|
153
|
+
timeout_ms = self.sdk_configuration.timeout_ms
|
|
154
|
+
|
|
155
|
+
if server_url is not None:
|
|
156
|
+
base_url = server_url
|
|
157
|
+
|
|
158
|
+
request = models.AgentsCompletionRequest(
|
|
159
|
+
max_tokens=max_tokens,
|
|
160
|
+
min_tokens=min_tokens,
|
|
161
|
+
stream=stream,
|
|
162
|
+
stop=stop,
|
|
163
|
+
random_seed=random_seed,
|
|
164
|
+
messages=utils.get_pydantic_model(messages, List[models.AgentsCompletionRequestMessages]),
|
|
165
|
+
response_format=utils.get_pydantic_model(response_format, Optional[models.ResponseFormat]),
|
|
166
|
+
tools=utils.get_pydantic_model(tools, OptionalNullable[List[models.Tool]]),
|
|
167
|
+
tool_choice=tool_choice,
|
|
168
|
+
agent_id=agent_id,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
req = self.build_request(
|
|
172
|
+
method="POST",
|
|
173
|
+
path="/v1/agents/completions",
|
|
174
|
+
base_url=base_url,
|
|
175
|
+
url_variables=url_variables,
|
|
176
|
+
request=request,
|
|
177
|
+
request_body_required=True,
|
|
178
|
+
request_has_path_params=False,
|
|
179
|
+
request_has_query_params=True,
|
|
180
|
+
user_agent_header="user-agent",
|
|
181
|
+
accept_header_value="application/json",
|
|
182
|
+
security=self.sdk_configuration.security,
|
|
183
|
+
get_serialized_body=lambda: utils.serialize_request_body(request, False, False, "json", models.AgentsCompletionRequest),
|
|
184
|
+
timeout_ms=timeout_ms,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
if retries == UNSET:
|
|
188
|
+
if self.sdk_configuration.retry_config is not UNSET:
|
|
189
|
+
retries = self.sdk_configuration.retry_config
|
|
190
|
+
|
|
191
|
+
retry_config = None
|
|
192
|
+
if isinstance(retries, utils.RetryConfig):
|
|
193
|
+
retry_config = (retries, [
|
|
194
|
+
"429",
|
|
195
|
+
"500",
|
|
196
|
+
"502",
|
|
197
|
+
"503",
|
|
198
|
+
"504"
|
|
199
|
+
])
|
|
200
|
+
|
|
201
|
+
http_res = await self.do_request_async(
|
|
202
|
+
hook_ctx=HookContext(operation_id="agents_completion_v1_agents_completions_post", oauth2_scopes=[], security_source=get_security_from_env(self.sdk_configuration.security, models.Security)),
|
|
203
|
+
request=req,
|
|
204
|
+
error_status_codes=["422","4XX","5XX"],
|
|
205
|
+
retry_config=retry_config
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
data: Any = None
|
|
209
|
+
if utils.match_response(http_res, "200", "application/json"):
|
|
210
|
+
return utils.unmarshal_json(http_res.text, Optional[models.ChatCompletionResponse])
|
|
211
|
+
if utils.match_response(http_res, "422", "application/json"):
|
|
212
|
+
data = utils.unmarshal_json(http_res.text, models.HTTPValidationErrorData)
|
|
213
|
+
raise models.HTTPValidationError(data=data)
|
|
214
|
+
if utils.match_response(http_res, ["4XX","5XX"], "*"):
|
|
215
|
+
raise models.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
|
|
216
|
+
|
|
217
|
+
content_type = http_res.headers.get("Content-Type")
|
|
218
|
+
raise models.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def stream(
|
|
223
|
+
self, *,
|
|
224
|
+
model: Nullable[str],
|
|
225
|
+
prompt: str,
|
|
226
|
+
temperature: Optional[float] = 0.7,
|
|
227
|
+
top_p: Optional[float] = 1,
|
|
228
|
+
max_tokens: OptionalNullable[int] = UNSET,
|
|
229
|
+
min_tokens: OptionalNullable[int] = UNSET,
|
|
230
|
+
stream: Optional[bool] = True,
|
|
231
|
+
stop: Optional[Union[models.AgentsCompletionStreamRequestStop, models.AgentsCompletionStreamRequestStopTypedDict]] = None,
|
|
232
|
+
random_seed: OptionalNullable[int] = UNSET,
|
|
233
|
+
suffix: OptionalNullable[str] = UNSET,
|
|
234
|
+
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
|
235
|
+
server_url: Optional[str] = None,
|
|
236
|
+
timeout_ms: Optional[int] = None,
|
|
237
|
+
) -> Optional[Generator[models.CompletionEvent, None, None]]:
|
|
238
|
+
r"""Stream Agents completion
|
|
239
|
+
|
|
240
|
+
Mistral AI provides the ability to stream responses back to a client in order to allow partial results for certain requests. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.
|
|
241
|
+
|
|
242
|
+
:param model: ID of the model to use. Only compatible for now with: - `codestral-2405` - `codestral-latest`
|
|
243
|
+
:param prompt: The text/code to complete.
|
|
244
|
+
:param temperature: What sampling temperature to use, between 0.0 and 1.0. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or `top_p` but not both.
|
|
245
|
+
:param top_p: Nucleus sampling, where the model considers the results of the tokens with `top_p` probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or `temperature` but not both.
|
|
246
|
+
:param max_tokens: The maximum number of tokens to generate in the completion. The token count of your prompt plus `max_tokens` cannot exceed the model's context length.
|
|
247
|
+
:param min_tokens: The minimum number of tokens to generate in the completion.
|
|
248
|
+
:param stream:
|
|
249
|
+
:param stop: Stop generation if this token is detected. Or if one of these tokens is detected when providing an array
|
|
250
|
+
:param random_seed: The seed to use for random sampling. If set, different calls will generate deterministic results.
|
|
251
|
+
:param suffix: Optional text/code that adds more context for the model. When given a `prompt` and a `suffix` the model will fill what is between them. When `suffix` is not provided, the model will simply execute completion starting with `prompt`.
|
|
252
|
+
:param retries: Override the default retry configuration for this method
|
|
253
|
+
:param server_url: Override the default server URL for this method
|
|
254
|
+
:param timeout_ms: Override the default request timeout configuration for this method in milliseconds
|
|
255
|
+
"""
|
|
256
|
+
base_url = None
|
|
257
|
+
url_variables = None
|
|
258
|
+
if timeout_ms is None:
|
|
259
|
+
timeout_ms = self.sdk_configuration.timeout_ms
|
|
260
|
+
|
|
261
|
+
if server_url is not None:
|
|
262
|
+
base_url = server_url
|
|
263
|
+
|
|
264
|
+
request = models.AgentsCompletionStreamRequest(
|
|
265
|
+
model=model,
|
|
266
|
+
temperature=temperature,
|
|
267
|
+
top_p=top_p,
|
|
268
|
+
max_tokens=max_tokens,
|
|
269
|
+
min_tokens=min_tokens,
|
|
270
|
+
stream=stream,
|
|
271
|
+
stop=stop,
|
|
272
|
+
random_seed=random_seed,
|
|
273
|
+
prompt=prompt,
|
|
274
|
+
suffix=suffix,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
req = self.build_request(
|
|
278
|
+
method="POST",
|
|
279
|
+
path="/v1/agents/completions#stream",
|
|
280
|
+
base_url=base_url,
|
|
281
|
+
url_variables=url_variables,
|
|
282
|
+
request=request,
|
|
283
|
+
request_body_required=True,
|
|
284
|
+
request_has_path_params=False,
|
|
285
|
+
request_has_query_params=True,
|
|
286
|
+
user_agent_header="user-agent",
|
|
287
|
+
accept_header_value="text/event-stream",
|
|
288
|
+
security=self.sdk_configuration.security,
|
|
289
|
+
get_serialized_body=lambda: utils.serialize_request_body(request, False, False, "json", models.AgentsCompletionStreamRequest),
|
|
290
|
+
timeout_ms=timeout_ms,
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
if retries == UNSET:
|
|
294
|
+
if self.sdk_configuration.retry_config is not UNSET:
|
|
295
|
+
retries = self.sdk_configuration.retry_config
|
|
296
|
+
|
|
297
|
+
retry_config = None
|
|
298
|
+
if isinstance(retries, utils.RetryConfig):
|
|
299
|
+
retry_config = (retries, [
|
|
300
|
+
"429",
|
|
301
|
+
"500",
|
|
302
|
+
"502",
|
|
303
|
+
"503",
|
|
304
|
+
"504"
|
|
305
|
+
])
|
|
306
|
+
|
|
307
|
+
http_res = self.do_request(
|
|
308
|
+
hook_ctx=HookContext(operation_id="stream_agents", oauth2_scopes=[], security_source=get_security_from_env(self.sdk_configuration.security, models.Security)),
|
|
309
|
+
request=req,
|
|
310
|
+
error_status_codes=["422","4XX","5XX"],
|
|
311
|
+
stream=True,
|
|
312
|
+
retry_config=retry_config
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
data: Any = None
|
|
316
|
+
if utils.match_response(http_res, "200", "text/event-stream"):
|
|
317
|
+
return eventstreaming.stream_events(http_res, lambda raw: utils.unmarshal_json(raw, models.CompletionEvent), sentinel="[DONE]")
|
|
318
|
+
if utils.match_response(http_res, "422", "application/json"):
|
|
319
|
+
data = utils.unmarshal_json(http_res.text, models.HTTPValidationErrorData)
|
|
320
|
+
raise models.HTTPValidationError(data=data)
|
|
321
|
+
if utils.match_response(http_res, ["4XX","5XX"], "*"):
|
|
322
|
+
raise models.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
|
|
323
|
+
|
|
324
|
+
content_type = http_res.headers.get("Content-Type")
|
|
325
|
+
raise models.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
async def stream_async(
|
|
330
|
+
self, *,
|
|
331
|
+
model: Nullable[str],
|
|
332
|
+
prompt: str,
|
|
333
|
+
temperature: Optional[float] = 0.7,
|
|
334
|
+
top_p: Optional[float] = 1,
|
|
335
|
+
max_tokens: OptionalNullable[int] = UNSET,
|
|
336
|
+
min_tokens: OptionalNullable[int] = UNSET,
|
|
337
|
+
stream: Optional[bool] = True,
|
|
338
|
+
stop: Optional[Union[models.AgentsCompletionStreamRequestStop, models.AgentsCompletionStreamRequestStopTypedDict]] = None,
|
|
339
|
+
random_seed: OptionalNullable[int] = UNSET,
|
|
340
|
+
suffix: OptionalNullable[str] = UNSET,
|
|
341
|
+
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
|
342
|
+
server_url: Optional[str] = None,
|
|
343
|
+
timeout_ms: Optional[int] = None,
|
|
344
|
+
) -> Optional[AsyncGenerator[models.CompletionEvent, None]]:
|
|
345
|
+
r"""Stream Agents completion
|
|
346
|
+
|
|
347
|
+
Mistral AI provides the ability to stream responses back to a client in order to allow partial results for certain requests. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.
|
|
348
|
+
|
|
349
|
+
:param model: ID of the model to use. Only compatible for now with: - `codestral-2405` - `codestral-latest`
|
|
350
|
+
:param prompt: The text/code to complete.
|
|
351
|
+
:param temperature: What sampling temperature to use, between 0.0 and 1.0. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or `top_p` but not both.
|
|
352
|
+
:param top_p: Nucleus sampling, where the model considers the results of the tokens with `top_p` probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or `temperature` but not both.
|
|
353
|
+
:param max_tokens: The maximum number of tokens to generate in the completion. The token count of your prompt plus `max_tokens` cannot exceed the model's context length.
|
|
354
|
+
:param min_tokens: The minimum number of tokens to generate in the completion.
|
|
355
|
+
:param stream:
|
|
356
|
+
:param stop: Stop generation if this token is detected. Or if one of these tokens is detected when providing an array
|
|
357
|
+
:param random_seed: The seed to use for random sampling. If set, different calls will generate deterministic results.
|
|
358
|
+
:param suffix: Optional text/code that adds more context for the model. When given a `prompt` and a `suffix` the model will fill what is between them. When `suffix` is not provided, the model will simply execute completion starting with `prompt`.
|
|
359
|
+
:param retries: Override the default retry configuration for this method
|
|
360
|
+
:param server_url: Override the default server URL for this method
|
|
361
|
+
:param timeout_ms: Override the default request timeout configuration for this method in milliseconds
|
|
362
|
+
"""
|
|
363
|
+
base_url = None
|
|
364
|
+
url_variables = None
|
|
365
|
+
if timeout_ms is None:
|
|
366
|
+
timeout_ms = self.sdk_configuration.timeout_ms
|
|
367
|
+
|
|
368
|
+
if server_url is not None:
|
|
369
|
+
base_url = server_url
|
|
370
|
+
|
|
371
|
+
request = models.AgentsCompletionStreamRequest(
|
|
372
|
+
model=model,
|
|
373
|
+
temperature=temperature,
|
|
374
|
+
top_p=top_p,
|
|
375
|
+
max_tokens=max_tokens,
|
|
376
|
+
min_tokens=min_tokens,
|
|
377
|
+
stream=stream,
|
|
378
|
+
stop=stop,
|
|
379
|
+
random_seed=random_seed,
|
|
380
|
+
prompt=prompt,
|
|
381
|
+
suffix=suffix,
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
req = self.build_request(
|
|
385
|
+
method="POST",
|
|
386
|
+
path="/v1/agents/completions#stream",
|
|
387
|
+
base_url=base_url,
|
|
388
|
+
url_variables=url_variables,
|
|
389
|
+
request=request,
|
|
390
|
+
request_body_required=True,
|
|
391
|
+
request_has_path_params=False,
|
|
392
|
+
request_has_query_params=True,
|
|
393
|
+
user_agent_header="user-agent",
|
|
394
|
+
accept_header_value="text/event-stream",
|
|
395
|
+
security=self.sdk_configuration.security,
|
|
396
|
+
get_serialized_body=lambda: utils.serialize_request_body(request, False, False, "json", models.AgentsCompletionStreamRequest),
|
|
397
|
+
timeout_ms=timeout_ms,
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
if retries == UNSET:
|
|
401
|
+
if self.sdk_configuration.retry_config is not UNSET:
|
|
402
|
+
retries = self.sdk_configuration.retry_config
|
|
403
|
+
|
|
404
|
+
retry_config = None
|
|
405
|
+
if isinstance(retries, utils.RetryConfig):
|
|
406
|
+
retry_config = (retries, [
|
|
407
|
+
"429",
|
|
408
|
+
"500",
|
|
409
|
+
"502",
|
|
410
|
+
"503",
|
|
411
|
+
"504"
|
|
412
|
+
])
|
|
413
|
+
|
|
414
|
+
http_res = await self.do_request_async(
|
|
415
|
+
hook_ctx=HookContext(operation_id="stream_agents", oauth2_scopes=[], security_source=get_security_from_env(self.sdk_configuration.security, models.Security)),
|
|
416
|
+
request=req,
|
|
417
|
+
error_status_codes=["422","4XX","5XX"],
|
|
418
|
+
stream=True,
|
|
419
|
+
retry_config=retry_config
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
data: Any = None
|
|
423
|
+
if utils.match_response(http_res, "200", "text/event-stream"):
|
|
424
|
+
return eventstreaming.stream_events_async(http_res, lambda raw: utils.unmarshal_json(raw, models.CompletionEvent), sentinel="[DONE]")
|
|
425
|
+
if utils.match_response(http_res, "422", "application/json"):
|
|
426
|
+
data = utils.unmarshal_json(http_res.text, models.HTTPValidationErrorData)
|
|
427
|
+
raise models.HTTPValidationError(data=data)
|
|
428
|
+
if utils.match_response(http_res, ["4XX","5XX"], "*"):
|
|
429
|
+
raise models.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
|
|
430
|
+
|
|
431
|
+
content_type = http_res.headers.get("Content-Type")
|
|
432
|
+
raise models.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
|
|
433
|
+
|
|
434
|
+
|
mistralai/basesdk.py
CHANGED
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
from .sdkconfiguration import SDKConfiguration
|
|
4
4
|
import httpx
|
|
5
|
-
from mistralai import models
|
|
5
|
+
from mistralai import models, utils
|
|
6
6
|
from mistralai._hooks import AfterErrorContext, AfterSuccessContext, BeforeRequestContext
|
|
7
|
-
|
|
8
|
-
from mistralai.utils import RetryConfig, SerializedRequestBody
|
|
7
|
+
from mistralai.utils import RetryConfig, SerializedRequestBody, get_body_content
|
|
9
8
|
from typing import Callable, List, Optional, Tuple
|
|
10
9
|
|
|
11
10
|
class BaseSDK:
|
|
@@ -70,8 +69,7 @@ class BaseSDK:
|
|
|
70
69
|
if security is not None:
|
|
71
70
|
if callable(security):
|
|
72
71
|
security = security()
|
|
73
|
-
|
|
74
|
-
security = utils.get_security_from_env(models.Security)
|
|
72
|
+
security = utils.get_security_from_env(security, models.Security)
|
|
75
73
|
if security is not None:
|
|
76
74
|
security_headers, security_query_params = utils.get_security(security)
|
|
77
75
|
headers = {**headers, **security_headers}
|
|
@@ -118,6 +116,7 @@ class BaseSDK:
|
|
|
118
116
|
retry_config: Optional[Tuple[RetryConfig, List[str]]] = None,
|
|
119
117
|
) -> httpx.Response:
|
|
120
118
|
client = self.sdk_configuration.client
|
|
119
|
+
logger = self.sdk_configuration.debug_logger
|
|
121
120
|
|
|
122
121
|
def do():
|
|
123
122
|
http_res = None
|
|
@@ -125,26 +124,45 @@ class BaseSDK:
|
|
|
125
124
|
req = self.sdk_configuration.get_hooks().before_request(
|
|
126
125
|
BeforeRequestContext(hook_ctx), request
|
|
127
126
|
)
|
|
127
|
+
logger.debug(
|
|
128
|
+
"Request:\nMethod: %s\nURL: %s\nHeaders: %s\nBody: %s",
|
|
129
|
+
req.method,
|
|
130
|
+
req.url,
|
|
131
|
+
req.headers,
|
|
132
|
+
get_body_content(req)
|
|
133
|
+
)
|
|
128
134
|
http_res = client.send(req, stream=stream)
|
|
129
135
|
except Exception as e:
|
|
130
136
|
_, e = self.sdk_configuration.get_hooks().after_error(
|
|
131
137
|
AfterErrorContext(hook_ctx), None, e
|
|
132
138
|
)
|
|
133
139
|
if e is not None:
|
|
140
|
+
logger.debug("Request Exception", exc_info=True)
|
|
134
141
|
raise e
|
|
135
142
|
|
|
136
143
|
if http_res is None:
|
|
144
|
+
logger.debug("Raising no response SDK error")
|
|
137
145
|
raise models.SDKError("No response received")
|
|
138
146
|
|
|
147
|
+
logger.debug(
|
|
148
|
+
"Response:\nStatus Code: %s\nURL: %s\nHeaders: %s\nBody: %s",
|
|
149
|
+
http_res.status_code,
|
|
150
|
+
http_res.url,
|
|
151
|
+
http_res.headers,
|
|
152
|
+
"<streaming response>" if stream else http_res.text
|
|
153
|
+
)
|
|
154
|
+
|
|
139
155
|
if utils.match_status_codes(error_status_codes, http_res.status_code):
|
|
140
156
|
result, err = self.sdk_configuration.get_hooks().after_error(
|
|
141
157
|
AfterErrorContext(hook_ctx), http_res, None
|
|
142
158
|
)
|
|
143
159
|
if err is not None:
|
|
160
|
+
logger.debug("Request Exception", exc_info=True)
|
|
144
161
|
raise err
|
|
145
162
|
if result is not None:
|
|
146
163
|
http_res = result
|
|
147
164
|
else:
|
|
165
|
+
logger.debug("Raising unexpected SDK error")
|
|
148
166
|
raise models.SDKError("Unexpected error occurred")
|
|
149
167
|
|
|
150
168
|
return http_res
|
|
@@ -170,33 +188,52 @@ class BaseSDK:
|
|
|
170
188
|
retry_config: Optional[Tuple[RetryConfig, List[str]]] = None,
|
|
171
189
|
) -> httpx.Response:
|
|
172
190
|
client = self.sdk_configuration.async_client
|
|
173
|
-
|
|
191
|
+
logger = self.sdk_configuration.debug_logger
|
|
174
192
|
async def do():
|
|
175
193
|
http_res = None
|
|
176
194
|
try:
|
|
177
195
|
req = self.sdk_configuration.get_hooks().before_request(
|
|
178
196
|
BeforeRequestContext(hook_ctx), request
|
|
179
197
|
)
|
|
198
|
+
logger.debug(
|
|
199
|
+
"Request:\nMethod: %s\nURL: %s\nHeaders: %s\nBody: %s",
|
|
200
|
+
req.method,
|
|
201
|
+
req.url,
|
|
202
|
+
req.headers,
|
|
203
|
+
get_body_content(req)
|
|
204
|
+
)
|
|
180
205
|
http_res = await client.send(req, stream=stream)
|
|
181
206
|
except Exception as e:
|
|
182
207
|
_, e = self.sdk_configuration.get_hooks().after_error(
|
|
183
208
|
AfterErrorContext(hook_ctx), None, e
|
|
184
209
|
)
|
|
185
210
|
if e is not None:
|
|
211
|
+
logger.debug("Request Exception", exc_info=True)
|
|
186
212
|
raise e
|
|
187
213
|
|
|
188
214
|
if http_res is None:
|
|
215
|
+
logger.debug("Raising no response SDK error")
|
|
189
216
|
raise models.SDKError("No response received")
|
|
190
217
|
|
|
218
|
+
logger.debug(
|
|
219
|
+
"Response:\nStatus Code: %s\nURL: %s\nHeaders: %s\nBody: %s",
|
|
220
|
+
http_res.status_code,
|
|
221
|
+
http_res.url,
|
|
222
|
+
http_res.headers,
|
|
223
|
+
"<streaming response>" if stream else http_res.text
|
|
224
|
+
)
|
|
225
|
+
|
|
191
226
|
if utils.match_status_codes(error_status_codes, http_res.status_code):
|
|
192
227
|
result, err = self.sdk_configuration.get_hooks().after_error(
|
|
193
228
|
AfterErrorContext(hook_ctx), http_res, None
|
|
194
229
|
)
|
|
195
230
|
if err is not None:
|
|
231
|
+
logger.debug("Request Exception", exc_info=True)
|
|
196
232
|
raise err
|
|
197
233
|
if result is not None:
|
|
198
234
|
http_res = result
|
|
199
235
|
else:
|
|
236
|
+
logger.debug("Raising unexpected SDK error")
|
|
200
237
|
raise models.SDKError("Unexpected error occurred")
|
|
201
238
|
|
|
202
239
|
return http_res
|