Chat Completions API
创建对话补全请求。
端点
POST /v1/chat/completions请求参数
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名称 |
messages | array | 是 | 对话消息列表 |
temperature | number | 否 | 温度参数 (0-2),默认 1 |
max_tokens | integer | 否 | 最大输出 tokens |
stream | boolean | 否 | 是否流式输出,默认 false |
top_p | number | 否 | 核采样参数 (0-1) |
请求示例
json
{
"model": "claude-sonnet-4-6",
"messages": [
{
"role": "system",
"content": "你是一个有帮助的助手。"
},
{
"role": "user",
"content": "介绍一下北京"
}
],
"temperature": 0.7,
"max_tokens": 1000
}响应示例
json
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "claude-sonnet-4-6",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "北京是中国的首都..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 100,
"total_tokens": 115
}
}流式响应
设置 "stream": true 启用流式输出:
json
{
"model": "claude-sonnet-4-6",
"messages": [...],
"stream": true
}流式响应格式:
data: {"id":"chatcmpl-123","choices":[{"delta":{"content":"北"}}]}
data: {"id":"chatcmpl-123","choices":[{"delta":{"content":"京"}}]}
...
data: [DONE]