跳到主要内容
版本:Canary 🚧

对话补全(Chat Completions)

创建一个模型对话补全。这是最常见的接口,用于向模型发送一组消息并接收回复。

POST /v1/chat/completions

请求参数​

参数类型必填说明
modelstring是要使用的模型名称,例如 example-chat。
messagesarray是对话消息数组,每条消息含 role 与 content。
temperaturenumber否采样温度,取值 0–2,越大越随机。默认 1.0。
streamboolean否是否流式返回。默认 false。
max_tokensinteger否回复的最大 token 数。

请求示例​

curl https://api.example.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "example-chat",
"messages": [
{"role": "system", "content": "你是一个助手。"},
{"role": "user", "content": "用一句话介绍自己。"}
],
"temperature": 0.7
}'

响应示例​

{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"model": "example-chat",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "我是你的智能助手,很高兴为你服务。"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 18,
"completion_tokens": 12,
"total_tokens": 30
}
}