mistral.types¶
Types for working with Mistral prompts.
AssistantMessage
¶
Bases: TypedDict
A message with the assistant
role.
Attributes:
Name | Type | Description |
---|---|---|
role |
Required[Literal['assistant']]
|
The role of the message's author, in this case |
content |
Required[str]
|
The contents of the message. |
Source code in mirascope/base/types.py
BaseAsyncStream
¶
Bases: Generic[BaseCallResponseChunkT, UserMessageParamT, AssistantMessageParamT, BaseToolT]
, ABC
A base class for async streaming responses from LLMs.
Source code in mirascope/base/types.py
BaseCallParams
¶
Bases: BaseModel
, Generic[BaseToolT]
The parameters with which to make a call.
Source code in mirascope/base/types.py
kwargs(tool_type=None, exclude=None)
¶
Returns all parameters for the call as a keyword arguments dictionary.
Source code in mirascope/base/types.py
BaseCallResponse
¶
Bases: BaseModel
, Generic[ResponseT, BaseToolT]
, ABC
A base abstract interface for LLM call responses.
Attributes:
Name | Type | Description |
---|---|---|
response |
ResponseT
|
The original response from whichever model response this wraps. |
Source code in mirascope/base/types.py
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 |
|
content: str
abstractmethod
property
¶
Should return the string content of the response.
If there are multiple choices in a response, this method should select the 0th choice and return it's string content.
If there is no string content (e.g. when using tools), this method must return the empty string.
finish_reasons: Union[None, list[str]]
abstractmethod
property
¶
Should return the finish reasons of the response.
If there is no finish reason, this method must return None.
id: Optional[str]
abstractmethod
property
¶
Should return the id of the response.
input_tokens: Optional[Union[int, float]]
abstractmethod
property
¶
Should return the number of input tokens.
If there is no input_tokens, this method must return None.
message_param: Any
abstractmethod
property
¶
Returns the assistant's response as a message parameter.
model: Optional[str]
abstractmethod
property
¶
Should return the name of the response model.
output_tokens: Optional[Union[int, float]]
abstractmethod
property
¶
Should return the number of output tokens.
If there is no output_tokens, this method must return None.
tool: Optional[BaseToolT]
abstractmethod
property
¶
Returns the 0th tool for the 0th choice message.
tools: Optional[list[BaseToolT]]
abstractmethod
property
¶
Returns the tools for the 0th choice message.
usage: Any
abstractmethod
property
¶
Should return the usage of the response.
If there is no usage, this method must return None.
tool_message_params(tools_and_outputs)
abstractmethod
classmethod
¶
Returns the tool message parameters for tool call results.
BaseCallResponseChunk
¶
Bases: BaseModel
, Generic[ChunkT, BaseToolT]
, ABC
A base abstract interface for LLM streaming response chunks.
Attributes:
Name | Type | Description |
---|---|---|
response |
The original response chunk from whichever model response this wraps. |
Source code in mirascope/base/types.py
content: str
abstractmethod
property
¶
Should return the string content of the response chunk.
If there are multiple choices in a chunk, this method should select the 0th choice and return it's string content.
If there is no string content (e.g. when using tools), this method must return the empty string.
finish_reasons: Union[None, list[str]]
abstractmethod
property
¶
Should return the finish reasons of the response.
If there is no finish reason, this method must return None.
id: Optional[str]
abstractmethod
property
¶
Should return the id of the response.
input_tokens: Optional[Union[int, float]]
abstractmethod
property
¶
Should return the number of input tokens.
If there is no input_tokens, this method must return None.
model: Optional[str]
abstractmethod
property
¶
Should return the name of the response model.
output_tokens: Optional[Union[int, float]]
abstractmethod
property
¶
Should return the number of output tokens.
If there is no output_tokens, this method must return None.
usage: Any
abstractmethod
property
¶
Should return the usage of the response.
If there is no usage, this method must return None.
BaseStream
¶
Bases: Generic[BaseCallResponseChunkT, UserMessageParamT, AssistantMessageParamT, BaseToolT]
, ABC
A base class for streaming responses from LLMs.
Source code in mirascope/base/types.py
MistralAsyncStream
¶
Bases: BaseAsyncStream[MistralCallResponseChunk, UserMessage, AssistantMessage, MistralTool]
A class for streaming responses from Mistral's API.
Source code in mirascope/mistral/types.py
MistralCallParams
¶
Bases: BaseCallParams[MistralTool]
The parameters to use when calling the Mistral API.
Source code in mirascope/mistral/types.py
MistralCallResponse
¶
Bases: BaseCallResponse[ChatCompletionResponse, MistralTool]
Convenience wrapper for Mistral's chat model completions.
When using Mirascope's convenience wrappers to interact with Mistral models via
MistralCall
, responses using MistralCall.call()
will return a
MistralCallResponse
, whereby the implemented properties allow for simpler syntax
and a convenient developer experience.
Example:
from mirascope.mistral import MistralCall
class BookRecommender(MistralCall):
prompt_template = "Please recommend a {genre} book"
genre: str
response = Bookrecommender(genre="fantasy").call()
print(response.content)
#> The Name of the Wind
print(response.message)
#> ChatMessage(content='The Name of the Wind', role='assistant',
# function_call=None, tool_calls=None)
print(response.choices)
#> [Choice(finish_reason='stop', index=0, logprobs=None,
# message=ChatMessage(content='The Name of the Wind', role='assistant',
# function_call=None, tool_calls=None))]
Source code in mirascope/mistral/types.py
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 |
|
choice: ChatCompletionResponseChoice
property
¶
Returns the 0th choice.
choices: list[ChatCompletionResponseChoice]
property
¶
Returns the array of chat completion choices.
content: str
property
¶
The content of the chat completion for the 0th choice.
finish_reasons: list[str]
property
¶
Returns the finish reasons of the response.
id: str
property
¶
Returns the id of the response.
input_tokens: int
property
¶
Returns the number of input tokens.
message: ChatMessage
property
¶
Returns the message of the chat completion for the 0th choice.
message_param: Message
property
¶
Returns the assistants's response as a message parameter.
model: str
property
¶
Returns the name of the response model.
output_tokens: Optional[int]
property
¶
Returns the number of output tokens.
tool: Optional[MistralTool]
property
¶
Returns the 0th tool for the 0th choice message.
Raises:
Type | Description |
---|---|
ValidationError
|
if the tool call doesn't match the tool's schema. |
tool_calls: Optional[list[ToolCall]]
property
¶
Returns the tool calls for the 0th choice message.
tools: Optional[list[MistralTool]]
property
¶
Returns the tools for the 0th choice message.
Raises:
Type | Description |
---|---|
ValidationError
|
if the tool call doesn't match the tool's schema. |
usage: UsageInfo
property
¶
Returns the usage of the chat completion.
dump()
¶
Dumps the response to a dictionary.
tool_message_params(tools_and_outputs)
classmethod
¶
Returns the tool message parameters for tool call results.
Source code in mirascope/mistral/types.py
MistralCallResponseChunk
¶
Bases: BaseCallResponseChunk[ChatCompletionStreamResponse, MistralTool]
Convenience wrapper around chat completion streaming chunks.
When using Mirascope's convenience wrappers to interact with Mistral models via
MistralCall.stream
, responses will return an MistralCallResponseChunk
, whereby
the implemented properties allow for simpler syntax and a convenient developer
experience.
Example:
from mirascope.mistral import MistralCall
class Math(MistralCall):
prompt_template = "What is 1 + 2?"
content = ""
for chunk in Math().stream():
content += chunk.content
print(content)
#> 1
# 1 +
# 1 + 2
# 1 + 2 equals
# 1 + 2 equals
# 1 + 2 equals 3
# 1 + 2 equals 3.
Source code in mirascope/mistral/types.py
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 |
|
choice: ChatCompletionResponseStreamChoice
property
¶
Returns the 0th choice.
choices: list[ChatCompletionResponseStreamChoice]
property
¶
Returns the array of chat completion choices.
content: str
property
¶
Returns the content of the delta.
delta: DeltaMessage
property
¶
Returns the delta of the 0th choice.
finish_reasons: list[str]
property
¶
Returns the finish reasons of the response.
id: str
property
¶
Returns the id of the response.
input_tokens: Optional[int]
property
¶
Returns the number of input tokens.
model: str
property
¶
Returns the name of the response model.
output_tokens: Optional[int]
property
¶
Returns the number of output tokens.
tool_calls: Optional[list[ToolCall]]
property
¶
Returns the partial tool calls for the 0th choice message.
usage: Optional[UsageInfo]
property
¶
Returns the usage of the chat completion.
MistralStream
¶
Bases: BaseStream[MistralCallResponseChunk, UserMessage, AssistantMessage, MistralTool]
A class for streaming responses from Mistral's API.
Source code in mirascope/mistral/types.py
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. |
Source code in mirascope/mistral/tools.py
ToolMessage
¶
Bases: TypedDict
A message with the tool
role.
Attributes:
Name | Type | Description |
---|---|---|
role |
Required[Literal['tool']]
|
The role of the message's author, in this case |
content |
Required[str]
|
The contents of the message. |
Source code in mirascope/base/types.py
UserMessage
¶
Bases: TypedDict
A message with the user
role.
Attributes:
Name | Type | Description |
---|---|---|
role |
Required[Literal['user']]
|
The role of the message's author, in this case |
content |
Required[str]
|
The contents of the message. |