跳到主要内容

JSON 模式

用户可以选择将 response_format 设置为 {"type": "json_object"} 以启用 JSON 模式。目前,我们的所有模型都通过 API 支持 JSON 模式。

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)


示例输出

{"name": "Coq au Vin", "ingredients": ["chicken", "red wine", "bacon", "mushrooms", "onions", "garlic", "chicken broth", "thyme", "bay leaf", "flour", "butter", "olive oil", "salt", "pepper"]}