mistral.tools¶
Classes for using tools with Mistral Chat APIs
MistralTool
¶
Bases: BaseTool[ToolCall]
A base class for easy use of tools with the Mistral client.
MistralTool
internally handles the logic that allows you to use tools with simple
calls such as MistralCallResponse.tool
or MistralTool.fn
, as seen in the
examples below.
Example:
```python import os
from mirascope.mistral import MistralCall, MistralCallParams
def animal_matcher(fav_food: str, fav_color: str) -> str: """Tells you your most likely favorite animal from personality traits.
Args:
fav_food: your favorite food.
fav_color: your favorite color.
Returns:
The animal most likely to be your favorite based on traits.
"""
return "Your favorite animal is the best one, a frog."
class AnimalMatcher(MistralCall): prompt_template = """\ Tell me my favorite animal if my favorite food is {food} and my favorite color is {color}. """
food: str
color: str
api_key = os.getenv("MISTRAL_API_KEY")
call_params = MistralCallParams(
model="mistral-large-latest", tools=[animal_matcher]
)
prompt = AnimalMatcher(food="pizza", color="green") response = prompt.call()
if tools := response.tools: for tool in tools: print(tool.fn(**tool.args))
> Your favorite animal is the best one, a frog.¶
Source code in mirascope/mistral/tools.py
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 |
|
from_base_type(base_type)
classmethod
¶
Constructs a MistralTool
type from a BaseType
type.
from_fn(fn)
classmethod
¶
from_model(model)
classmethod
¶
Constructs a MistralTool
type from a BaseModel
type.
from_tool_call(tool_call)
classmethod
¶
Extracts an instance of the tool constructed from a tool call response.
Given ToolCall
from a Mistral chat completion response, takes its function
arguments and creates a MistralTool
instance from it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_call |
ToolCall
|
The Mistral |
required |
Returns:
Type | Description |
---|---|
MistralTool
|
An instance of the tool constructed from the tool call. |
Raises:
Type | Description |
---|---|
ValueError
|
if the tool call doesn't match the tool schema. |
Source code in mirascope/mistral/tools.py
tool_schema()
classmethod
¶
Constructs a tool schema for use with the Mistral Chat client.
A Mirascope MistralTool
is deconstructed into a JSON schema, and relevant keys
are renamed to match the Mistral API schema used to make functional/tool calls
in Mistral API.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The constructed tool schema. |