(chat)
Chat Completion API.
Chat Completion
from mistralai import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = mistral.chat.complete(model="mistral-small-latest", messages=[
{
"content": "Who is the best French painter? Answer in one short sentence.",
"role": "user",
},
])
assert res is not None
# Handle response
print(res)
Parameter | Type | Required | Description | Example |
---|---|---|---|---|
model |
Nullable[str] | ✔️ | ID of the model to use. You can use the List Available Models API to see all of your available models, or see our Model overview for model descriptions. | mistral-small-latest |
messages |
List[models.Messages] | ✔️ | 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." } ] |
temperature |
OptionalNullable[float] | ➖ | What sampling temperature to use, we recommend between 0.0 and 0.7. Higher values like 0.7 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. The default value varies depending on the model you are targeting. Call the /models endpoint to retrieve the appropriate value. |
|
top_p |
Optional[float] | ➖ | 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. |
|
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.Stop] | ➖ | 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. | |
response_format |
Optional[models.ResponseFormat] | ➖ | N/A | |
tools |
List[models.Tool] | ➖ | N/A | |
tool_choice |
Optional[models.ChatCompletionRequestToolChoice] | ➖ | N/A | |
presence_penalty |
Optional[float] | ➖ | 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] | ➖ | 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. | |
safe_prompt |
Optional[bool] | ➖ | Whether to inject a safety prompt before all conversations. | |
retries |
Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Error Type | Status Code | Content Type |
---|---|---|
models.HTTPValidationError | 422 | application/json |
models.SDKError | 4XX, 5XX | */* |
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.
from mistralai import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = mistral.chat.stream(model="mistral-small-latest", messages=[
{
"content": "Who is the best French painter? Answer in one short sentence.",
"role": "user",
},
])
assert res is not None
with res as event_stream:
for event in event_stream:
# handle event
print(event, flush=True)
Parameter | Type | Required | Description | Example |
---|---|---|---|---|
model |
Nullable[str] | ✔️ | ID of the model to use. You can use the List Available Models API to see all of your available models, or see our Model overview for model descriptions. | mistral-small-latest |
messages |
List[models.ChatCompletionStreamRequestMessages] | ✔️ | 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." } ] |
temperature |
OptionalNullable[float] | ➖ | What sampling temperature to use, we recommend between 0.0 and 0.7. Higher values like 0.7 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. The default value varies depending on the model you are targeting. Call the /models endpoint to retrieve the appropriate value. |
|
top_p |
Optional[float] | ➖ | 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. |
|
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.ChatCompletionStreamRequestStop] | ➖ | 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. | |
response_format |
Optional[models.ResponseFormat] | ➖ | N/A | |
tools |
List[models.Tool] | ➖ | N/A | |
tool_choice |
Optional[models.ChatCompletionStreamRequestToolChoice] | ➖ | N/A | |
presence_penalty |
Optional[float] | ➖ | 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] | ➖ | 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. | |
safe_prompt |
Optional[bool] | ➖ | Whether to inject a safety prompt before all conversations. | |
retries |
Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Error Type | Status Code | Content Type |
---|---|---|
models.HTTPValidationError | 422 | application/json |
models.SDKError | 4XX, 5XX | */* |