-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathmistral.py
More file actions
578 lines (457 loc) · 22 KB
/
mistral.py
File metadata and controls
578 lines (457 loc) · 22 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
"""Mistral AI model provider.
- Docs: https://docs.mistral.ai/
"""
import base64
import json
import logging
from collections.abc import AsyncGenerator, Iterable
from typing import Any, TypeVar
import mistralai
from pydantic import BaseModel
from typing_extensions import TypedDict, Unpack, override
from ..types.content import ContentBlock, Messages
from ..types.exceptions import ModelThrottledException
from ..types.streaming import StopReason, StreamEvent
from ..types.tools import ToolChoice, ToolResult, ToolSpec, ToolUse
from ._validation import _has_location_source, validate_config_keys, warn_on_tool_choice_not_supported
from .model import Model
logger = logging.getLogger(__name__)
T = TypeVar("T", bound=BaseModel)
class MistralModel(Model):
"""Mistral API model provider implementation.
The implementation handles Mistral-specific features such as:
- Chat and text completions
- Streaming responses
- Tool/function calling
- System prompts
"""
class MistralConfig(TypedDict, total=False):
"""Configuration parameters for Mistral models.
Attributes:
model_id: Mistral model ID (e.g., "mistral-large-latest", "mistral-medium-latest").
max_tokens: Maximum number of tokens to generate in the response.
temperature: Controls randomness in generation (0.0 to 1.0).
top_p: Controls diversity via nucleus sampling.
stream: Whether to enable streaming responses.
"""
model_id: str
max_tokens: int | None
temperature: float | None
top_p: float | None
stream: bool | None
@classmethod
def from_dict(cls, config: dict[str, Any]) -> "MistralModel":
"""Create a MistralModel from a configuration dictionary.
Handles extraction of ``api_key`` and ``client_args`` as separate constructor parameters.
Args:
config: Model configuration dictionary. A copy is made internally;
the caller's dict is not modified.
Returns:
A configured MistralModel instance.
"""
config = config.copy()
api_key = config.pop("api_key", None)
client_args = config.pop("client_args", None)
kwargs: dict[str, Any] = {}
if api_key is not None:
kwargs["api_key"] = api_key
if client_args is not None:
kwargs["client_args"] = client_args
kwargs.update(config)
return cls(**kwargs)
def __init__(
self,
api_key: str | None = None,
*,
client_args: dict[str, Any] | None = None,
**model_config: Unpack[MistralConfig],
) -> None:
"""Initialize provider instance.
Args:
api_key: Mistral API key. If not provided, will use MISTRAL_API_KEY env var.
client_args: Additional arguments for the Mistral client.
**model_config: Configuration options for the Mistral model.
"""
if "temperature" in model_config and model_config["temperature"] is not None:
temp = model_config["temperature"]
if not 0.0 <= temp <= 1.0:
raise ValueError(f"temperature must be between 0.0 and 1.0, got {temp}")
# Warn if temperature is above recommended range
if temp > 0.7:
logger.warning(
"temperature=%s is above the recommended range (0.0-0.7). "
"High values may produce unpredictable results.",
temp,
)
if "top_p" in model_config and model_config["top_p"] is not None:
top_p = model_config["top_p"]
if not 0.0 <= top_p <= 1.0:
raise ValueError(f"top_p must be between 0.0 and 1.0, got {top_p}")
validate_config_keys(model_config, self.MistralConfig)
self.config = MistralModel.MistralConfig(**model_config)
# Set default stream to True if not specified
if "stream" not in self.config:
self.config["stream"] = True
logger.debug("config=<%s> | initializing", self.config)
self.client_args = client_args or {}
if api_key:
self.client_args["api_key"] = api_key
@override
def update_config(self, **model_config: Unpack[MistralConfig]) -> None: # type: ignore
"""Update the Mistral Model configuration with the provided arguments.
Args:
**model_config: Configuration overrides.
"""
validate_config_keys(model_config, self.MistralConfig)
self.config.update(model_config)
@override
def get_config(self) -> MistralConfig:
"""Get the Mistral model configuration.
Returns:
The Mistral model configuration.
"""
return self.config
def _format_request_message_content(self, content: ContentBlock) -> str | dict[str, Any]:
"""Format a Mistral content block.
Args:
content: Message content.
Returns:
Mistral formatted content.
Raises:
TypeError: If the content block type cannot be converted to a Mistral-compatible format.
"""
if "text" in content:
return content["text"]
if "image" in content:
image_data = content["image"]
if "source" in image_data:
image_bytes = image_data["source"]["bytes"]
base64_data = base64.b64encode(image_bytes).decode("utf-8")
format_value = image_data.get("format", "jpeg")
media_type = f"image/{format_value}"
return {"type": "image_url", "image_url": f"data:{media_type};base64,{base64_data}"}
raise TypeError("content_type=<image> | unsupported image format")
raise TypeError(f"content_type=<{next(iter(content))}> | unsupported type")
def _format_request_message_tool_call(self, tool_use: ToolUse) -> dict[str, Any]:
"""Format a Mistral tool call.
Args:
tool_use: Tool use requested by the model.
Returns:
Mistral formatted tool call.
"""
return {
"function": {
"name": tool_use["name"],
"arguments": json.dumps(tool_use["input"]),
},
"id": tool_use["toolUseId"],
"type": "function",
}
def _format_request_tool_message(self, tool_result: ToolResult) -> dict[str, Any]:
"""Format a Mistral tool message.
Args:
tool_result: Tool result collected from a tool execution.
Returns:
Mistral formatted tool message.
"""
content_parts: list[str] = []
for content in tool_result["content"]:
if "json" in content:
content_parts.append(json.dumps(content["json"]))
elif "text" in content:
content_parts.append(content["text"])
return {
"role": "tool",
"name": tool_result["toolUseId"].split("_")[0]
if "_" in tool_result["toolUseId"]
else tool_result["toolUseId"],
"content": "\n".join(content_parts),
"tool_call_id": tool_result["toolUseId"],
}
def _format_request_messages(self, messages: Messages, system_prompt: str | None = None) -> list[dict[str, Any]]:
"""Format a Mistral compatible messages array.
Args:
messages: List of message objects to be processed by the model.
system_prompt: System prompt to provide context to the model.
Returns:
A Mistral compatible messages array.
"""
formatted_messages: list[dict[str, Any]] = []
if system_prompt:
formatted_messages.append({"role": "system", "content": system_prompt})
for message in messages:
role = message["role"]
contents = message["content"]
text_contents: list[str] = []
tool_calls: list[dict[str, Any]] = []
tool_messages: list[dict[str, Any]] = []
for content in contents:
# Check for location sources and skip with warning
if _has_location_source(content):
logger.warning("Location sources are not supported by Mistral | skipping content block")
continue
if "text" in content:
formatted_content = self._format_request_message_content(content)
if isinstance(formatted_content, str):
text_contents.append(formatted_content)
elif "toolUse" in content:
tool_calls.append(self._format_request_message_tool_call(content["toolUse"]))
elif "toolResult" in content:
tool_messages.append(self._format_request_tool_message(content["toolResult"]))
if text_contents or tool_calls:
formatted_message: dict[str, Any] = {
"role": role,
"content": " ".join(text_contents) if text_contents else "",
}
if tool_calls:
formatted_message["tool_calls"] = tool_calls
formatted_messages.append(formatted_message)
formatted_messages.extend(tool_messages)
return formatted_messages
def format_request(
self, messages: Messages, tool_specs: list[ToolSpec] | None = None, system_prompt: str | None = None
) -> dict[str, Any]:
"""Format a Mistral chat streaming request.
Args:
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
Returns:
A Mistral chat streaming request.
Raises:
TypeError: If a message contains a content block type that cannot be converted to a Mistral-compatible
format.
"""
request: dict[str, Any] = {
"model": self.config["model_id"],
"messages": self._format_request_messages(messages, system_prompt),
}
if "max_tokens" in self.config:
request["max_tokens"] = self.config["max_tokens"]
if "temperature" in self.config:
request["temperature"] = self.config["temperature"]
if "top_p" in self.config:
request["top_p"] = self.config["top_p"]
if "stream" in self.config:
request["stream"] = self.config["stream"]
if tool_specs:
request["tools"] = [
{
"type": "function",
"function": {
"name": tool_spec["name"],
"description": tool_spec["description"],
"parameters": tool_spec["inputSchema"]["json"],
},
}
for tool_spec in tool_specs
]
return request
def format_chunk(self, event: dict[str, Any]) -> StreamEvent:
"""Format the Mistral response events into standardized message chunks.
Args:
event: A response event from the Mistral model.
Returns:
The formatted chunk.
Raises:
RuntimeError: If chunk_type is not recognized.
"""
match event["chunk_type"]:
case "message_start":
return {"messageStart": {"role": "assistant"}}
case "content_start":
if event["data_type"] == "text":
return {"contentBlockStart": {"start": {}}}
tool_call = event["data"]
return {
"contentBlockStart": {
"start": {
"toolUse": {
"name": tool_call.function.name,
"toolUseId": tool_call.id,
}
}
}
}
case "content_delta":
if event["data_type"] == "text":
return {"contentBlockDelta": {"delta": {"text": event["data"]}}}
return {"contentBlockDelta": {"delta": {"toolUse": {"input": event["data"]}}}}
case "content_stop":
return {"contentBlockStop": {}}
case "message_stop":
reason: StopReason
if event["data"] == "tool_calls":
reason = "tool_use"
elif event["data"] == "length":
reason = "max_tokens"
else:
reason = "end_turn"
return {"messageStop": {"stopReason": reason}}
case "metadata":
usage = event["data"]
return {
"metadata": {
"usage": {
"inputTokens": usage.prompt_tokens,
"outputTokens": usage.completion_tokens,
"totalTokens": usage.total_tokens,
},
"metrics": {
"latencyMs": event.get("latency_ms", 0),
},
},
}
case _:
raise RuntimeError(f"chunk_type=<{event['chunk_type']}> | unknown type")
def _handle_non_streaming_response(self, response: Any) -> Iterable[dict[str, Any]]:
"""Handle non-streaming response from Mistral API.
Args:
response: The non-streaming response from Mistral.
Yields:
Formatted events that match the streaming format.
"""
yield {"chunk_type": "message_start"}
content_started = False
if response.choices and response.choices[0].message:
message = response.choices[0].message
if hasattr(message, "content") and message.content:
if not content_started:
yield {"chunk_type": "content_start", "data_type": "text"}
content_started = True
yield {"chunk_type": "content_delta", "data_type": "text", "data": message.content}
yield {"chunk_type": "content_stop"}
if hasattr(message, "tool_calls") and message.tool_calls:
for tool_call in message.tool_calls:
yield {"chunk_type": "content_start", "data_type": "tool", "data": tool_call}
if hasattr(tool_call.function, "arguments"):
yield {"chunk_type": "content_delta", "data_type": "tool", "data": tool_call.function.arguments}
yield {"chunk_type": "content_stop"}
finish_reason = response.choices[0].finish_reason if response.choices[0].finish_reason else "stop"
yield {"chunk_type": "message_stop", "data": finish_reason}
if hasattr(response, "usage") and response.usage:
yield {"chunk_type": "metadata", "data": response.usage}
@override
async def stream(
self,
messages: Messages,
tool_specs: list[ToolSpec] | None = None,
system_prompt: str | None = None,
*,
tool_choice: ToolChoice | None = None,
**kwargs: Any,
) -> AsyncGenerator[StreamEvent, None]:
"""Stream conversation with the Mistral model.
Args:
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation. **Note: This parameter is accepted for
interface consistency but is currently ignored for this model provider.**
**kwargs: Additional keyword arguments for future extensibility.
Yields:
Formatted message chunks from the model.
Raises:
ModelThrottledException: When the model service is throttling requests.
"""
warn_on_tool_choice_not_supported(tool_choice)
logger.debug("formatting request")
request = self.format_request(messages, tool_specs, system_prompt)
logger.debug("request=<%s>", request)
logger.debug("invoking model")
try:
logger.debug("got response from model")
if not self.config.get("stream", True):
# Use non-streaming API
async with mistralai.Mistral(**self.client_args) as client:
response = await client.chat.complete_async(**request)
for event in self._handle_non_streaming_response(response):
yield self.format_chunk(event)
return
# Use the streaming API
async with mistralai.Mistral(**self.client_args) as client:
stream_response = await client.chat.stream_async(**request)
yield self.format_chunk({"chunk_type": "message_start"})
content_started = False
tool_calls: dict[str, list[Any]] = {}
accumulated_text = ""
async for chunk in stream_response:
if hasattr(chunk, "data") and hasattr(chunk.data, "choices") and chunk.data.choices:
choice = chunk.data.choices[0]
if hasattr(choice, "delta"):
delta = choice.delta
if hasattr(delta, "content") and delta.content:
if not content_started:
yield self.format_chunk({"chunk_type": "content_start", "data_type": "text"})
content_started = True
yield self.format_chunk(
{"chunk_type": "content_delta", "data_type": "text", "data": delta.content}
)
accumulated_text += delta.content
if hasattr(delta, "tool_calls") and delta.tool_calls:
for tool_call in delta.tool_calls:
tool_id = tool_call.id
tool_calls.setdefault(tool_id, []).append(tool_call)
if hasattr(choice, "finish_reason") and choice.finish_reason:
if content_started:
yield self.format_chunk({"chunk_type": "content_stop", "data_type": "text"})
for tool_deltas in tool_calls.values():
yield self.format_chunk(
{"chunk_type": "content_start", "data_type": "tool", "data": tool_deltas[0]}
)
for tool_delta in tool_deltas:
if hasattr(tool_delta.function, "arguments"):
yield self.format_chunk(
{
"chunk_type": "content_delta",
"data_type": "tool",
"data": tool_delta.function.arguments,
}
)
yield self.format_chunk({"chunk_type": "content_stop", "data_type": "tool"})
yield self.format_chunk({"chunk_type": "message_stop", "data": choice.finish_reason})
if hasattr(chunk, "data") and hasattr(chunk.data, "usage") and chunk.data.usage:
yield self.format_chunk({"chunk_type": "metadata", "data": chunk.data.usage})
except Exception as e:
if "rate" in str(e).lower() or "429" in str(e):
raise ModelThrottledException(str(e)) from e
raise
logger.debug("finished streaming response from model")
@override
async def structured_output(
self, output_model: type[T], prompt: Messages, system_prompt: str | None = None, **kwargs: Any
) -> AsyncGenerator[dict[str, T | Any], None]:
"""Get structured output from the model.
Args:
output_model: The output model to use for the agent.
prompt: The prompt messages to use for the agent.
system_prompt: System prompt to provide context to the model.
**kwargs: Additional keyword arguments for future extensibility.
Returns:
An instance of the output model with the generated data.
Raises:
ValueError: If the response cannot be parsed into the output model.
"""
tool_spec: ToolSpec = {
"name": f"extract_{output_model.__name__.lower()}",
"description": f"Extract structured data in the format of {output_model.__name__}",
"inputSchema": {"json": output_model.model_json_schema()},
}
formatted_request = self.format_request(messages=prompt, tool_specs=[tool_spec], system_prompt=system_prompt)
formatted_request["tool_choice"] = "any"
formatted_request["parallel_tool_calls"] = False
async with mistralai.Mistral(**self.client_args) as client:
response = await client.chat.complete_async(**formatted_request)
if response.choices and response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
try:
# Handle both string and dict arguments
if isinstance(tool_call.function.arguments, str):
arguments = json.loads(tool_call.function.arguments)
else:
arguments = tool_call.function.arguments
yield {"output": output_model(**arguments)}
return
except (json.JSONDecodeError, TypeError, ValueError) as e:
raise ValueError(f"Failed to parse tool call arguments into model: {e}") from e
raise ValueError("No tool calls found in response")