Mode JSON
Vous pouvez définir response_format à {"type": "json_object"} pour activer le mode JSON.
Ce mode garantit que la réponse du modèle est formatée comme un objet JSON valide, quel que soit le contenu du prompt. Nous recommandons néanmoins de demander explicitement au modèle de retourner un objet JSON et de spécifier le format attendu.
Utilisation
Utilisation
Comment générer du JSON de manière cohérente
Voici un exemple d'utilisation du mode JSON avec l'API Mistral.
import os
from mistralai.client 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",
}
)La sortie sera toujours un JSON valide, et le champ content contiendra un objet JSON stringifié. Dans ce cas :
{
"meal": "Boeuf Bourguignon",
"ingredients": [
"beef",
"red wine (Burgundy)",
"onions",
"carrots",
"garlic",
"mushrooms",
"bacon",
"beef stock",
"tomato paste",
"thyme",
"bay leaf",
"butter",
"flour",
"salt",
"pepper"
]
}FAQ