Skip to content

Latest commit

 

History

History
142 lines (108 loc) · 103 KB

File metadata and controls

142 lines (108 loc) · 103 KB

Agents

Overview

Agents API.

Available Operations

complete

Agents Completion

Example Usage

from mistralai.client import Mistral
import os


with Mistral(
    api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:

    res = mistral.agents.complete(messages=[
        {
            "role": "user",
            "content": "Who is the best French painter? Answer in one short sentence.",
        },
    ], agent_id="<id>", stream=False, response_format={
        "type": "text",
    })

    # Handle response
    print(res)

Parameters

Parameter Type Required Description Example
messages List[models.AgentsCompletionRequestMessage] ✔️ The prompt(s) to generate completions for, encoded as a list of dict with role and content. [
{
"role": "user",
"content": "Who is the best French painter? Answer in one short sentence."
}
]
agent_id str ✔️ The ID of the agent to use for this completion.
max_tokens OptionalNullable[int] 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.
stream Optional[bool] 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.
stop Optional[models.AgentsCompletionRequestStop] Stop generation if this token is detected. Or if one of these tokens is detected when providing an array
random_seed OptionalNullable[int] The seed to use for random sampling. If set, different calls will generate deterministic results.
metadata Dict[str, Any] N/A
response_format Optional[models.ResponseFormat] Specify the format that the model must output. By default it will use { "type": "text" }. Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is in JSON. When using JSON mode you MUST also instruct the model to produce JSON yourself with a system or a user message. Setting to { "type": "json_schema" } enables JSON schema mode, which guarantees the message the model generates is in JSON and follows the schema you provide. Example 1: {
"type": "text"
}
Example 2: {
"type": "json_object"
}
Example 3: {
"type": "json_schema",
"json_schema": {
"schema": {
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"authors": {
"items": {
"type": "string"
},
"title": "Authors",
"type": "array"
}
},
"required": [
"name",
"authors"
],
"title": "Book",
"type": "object",
"additionalProperties": false
},
"name": "book",
"strict": true
}
}
tools List[models.AgentsCompletionRequestTool] N/A
tool_choice Optional[models.AgentsCompletionRequestToolChoice] N/A
presence_penalty Optional[float] The presence_penalty determines how much the model penalizes the repetition of words or phrases. A higher presence penalty encourages the model to use a wider variety of words and phrases, making the output more diverse and creative.
frequency_penalty Optional[float] The frequency_penalty penalizes the repetition of words based on their frequency in the generated text. A higher frequency penalty discourages the model from repeating words that have already appeared frequently in the output, promoting diversity and reducing repetition.
n OptionalNullable[int] Number of completions to return for each request, input tokens are only billed once.
prediction Optional[models.Prediction] Enable users to specify an expected completion, optimizing response times by leveraging known or predictable content.
parallel_tool_calls Optional[bool] N/A
reasoning_effort OptionalNullable[models.ReasoningEffort] N/A
prompt_mode OptionalNullable[models.MistralPromptMode] Allows toggling between the reasoning mode and no system prompt. When set to reasoning the system prompt for reasoning models will be used.
guardrails List[models.GuardrailConfig] N/A
retries Optional[utils.RetryConfig] Configuration to override the default retry behavior of the client.

Response

models.ChatCompletionResponse

Errors

Error Type Status Code Content Type
errors.HTTPValidationError 422 application/json
errors.SDKError 4XX, 5XX */*

stream

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.

Example Usage

from mistralai.client import Mistral
import os


with Mistral(
    api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:

    res = mistral.agents.stream(messages=[
        {
            "role": "user",
            "content": "Who is the best French painter? Answer in one short sentence.",
        },
    ], agent_id="<id>", stream=True, response_format={
        "type": "text",
    })

    with res as event_stream:
        for event in event_stream:
            # handle event
            print(event, flush=True)

Parameters

Parameter Type Required Description Example
messages List[models.AgentsCompletionStreamRequestMessage] ✔️ The prompt(s) to generate completions for, encoded as a list of dict with role and content. [
{
"role": "user",
"content": "Who is the best French painter? Answer in one short sentence."
}
]
agent_id str ✔️ The ID of the agent to use for this completion.
max_tokens OptionalNullable[int] 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.
stream Optional[bool] N/A
stop Optional[models.AgentsCompletionStreamRequestStop] Stop generation if this token is detected. Or if one of these tokens is detected when providing an array
random_seed OptionalNullable[int] The seed to use for random sampling. If set, different calls will generate deterministic results.
metadata Dict[str, Any] N/A
response_format Optional[models.ResponseFormat] Specify the format that the model must output. By default it will use { "type": "text" }. Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is in JSON. When using JSON mode you MUST also instruct the model to produce JSON yourself with a system or a user message. Setting to { "type": "json_schema" } enables JSON schema mode, which guarantees the message the model generates is in JSON and follows the schema you provide. Example 1: {
"type": "text"
}
Example 2: {
"type": "json_object"
}
Example 3: {
"type": "json_schema",
"json_schema": {
"schema": {
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"authors": {
"items": {
"type": "string"
},
"title": "Authors",
"type": "array"
}
},
"required": [
"name",
"authors"
],
"title": "Book",
"type": "object",
"additionalProperties": false
},
"name": "book",
"strict": true
}
}
tools List[models.AgentsCompletionStreamRequestTool] N/A
tool_choice Optional[models.AgentsCompletionStreamRequestToolChoice] N/A
presence_penalty Optional[float] The presence_penalty determines how much the model penalizes the repetition of words or phrases. A higher presence penalty encourages the model to use a wider variety of words and phrases, making the output more diverse and creative.
frequency_penalty Optional[float] The frequency_penalty penalizes the repetition of words based on their frequency in the generated text. A higher frequency penalty discourages the model from repeating words that have already appeared frequently in the output, promoting diversity and reducing repetition.
n OptionalNullable[int] Number of completions to return for each request, input tokens are only billed once.
prediction Optional[models.Prediction] Enable users to specify an expected completion, optimizing response times by leveraging known or predictable content.
parallel_tool_calls Optional[bool] N/A
reasoning_effort OptionalNullable[models.ReasoningEffort] N/A
prompt_mode OptionalNullable[models.MistralPromptMode] Allows toggling between the reasoning mode and no system prompt. When set to reasoning the system prompt for reasoning models will be used.
guardrails List[models.GuardrailConfig] N/A
retries Optional[utils.RetryConfig] Configuration to override the default retry behavior of the client.

Response

Union[eventstreaming.EventStream[models.CompletionEvent], eventstreaming.EventStreamAsync[models.CompletionEvent]]

Errors

Error Type Status Code Content Type
errors.HTTPValidationError 422 application/json
errors.SDKError 4XX, 5XX */*