JSON mode
Users have the option to set response_format
to {"type": "json_object"}
to enable JSON mode.
Currently, JSON mode is available for all of our models through API.
warning
It's important to explicitly ask the model to generate JSON output in your message.
To prevent infinite generations, users are encouraged to ask the model for short JSON objects.
- python
- typescript
- curl
import os
from mistralai import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"
client = Mistral(api_key=api_key)
messages = [
{
"role": "user",
"content": "What is the best French meal? Return the name and the ingredients in short JSON object.",
}
]
chat_response = client.chat.complete(
model = model,
messages = messages,
response_format = {
"type": "json_object",
}
)
print(chat_response.choices[0].message.content)
Example output:
{"name": "Coq au Vin", "ingredients": ["chicken", "red wine", "bacon", "mushrooms", "onions", "garlic", "chicken broth", "thyme", "bay leaf", "flour", "butter", "olive oil", "salt", "pepper"]}
import { Mistral } from "mistralai";
const apiKey = process.env.MISTRAL_API_KEY;
const mistral = new Mistral({apiKey: apiKey});
const chatResponse = await mistral.chat.complete({
model: "mistral-large-latest",
messages: [{role: 'user', content: 'What is the best French meal? Return the name and the ingredients in JSON format.'}],
response_format: {type: 'json_object'},
}
);
console.log('JSON:', chatResponse.choices[0].message.content)
curl --location "https://api.mistral.ai/v1/chat/completions" \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $MISTRAL_API_KEY" \
--data '{
"model": "mistral-large-latest",
"messages": [
{
"role": "user",
"content": "What is the best French cheese? Return the product and produce location in JSON format"
}
],
"response_format": {"type": "json_object"}
}'