POST
/api/v1/chat/completions聊天补全
与大语言模型进行多轮对话,兼容 OpenAI Chat Completions API
接口地址
HTTP
POST {YOUR_DOMAIN}/api/v1/chat/completions需要在 Header 中携带 Authorization: Bearer YOUR_API_KEY
请求参数
| 参数 | 类型 | 必填 |
|---|---|---|
| model | string | 必填 |
| messages | array | 必填 |
| temperature | number | 可选 |
| max_tokens | integer | 可选 |
| stream | boolean | 可选 |
| top_p | number | 可选 |
响应格式
JSON
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "your-model-id",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "回复内容"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30
}
}流式响应
设置 stream: true 后,响应为 SSE 格式:
SSE
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"你"},"index":0}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"好"},"index":0}]}
data: [DONE]完整示例
cURL
curl -X POST {YOUR_DOMAIN}/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "YOUR_MODEL_ID",
"messages": [
{"role": "system", "content": "你是一个有帮助的助手"},
{"role": "user", "content": "你好"}
],
"temperature": 0.7,
"max_tokens": 1000
}'