创建对话补全,支持流式与非流式返回。完全兼容 OpenAI Chat Completions API 规范,可直接使用 OpenAI SDK 或任意 HTTP 客户端调用。
Endpoint
POST{base_url}/openai/v1/chat/completions
application/json Bearer Token
Request Parameters
| Name | Type | Required | Default | Description |
|---|
model | string | Yes | — | 模型名称,如 gpt-4o、gpt-4o-mini 等 |
messages | array | Yes | — | 对话消息数组,每项含 role(system/user/assistant)与 content 字段 |
stream | boolean | No | false | 是否流式返回(SSE),设为 true 时逐块输出 delta 内容 |
temperature | number | No | 0.7 | 采样温度,范围 0-2,值越高输出越发散 |
max_tokens | integer | No | — | 最大生成 token 数,不传则使用模型默认上限 |
Request Example
curl -X POST {base_url}/openai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer {api_key}" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "你是一个有帮助的助手。"},
{"role": "user", "content": "你好,请介绍一下你自己。"}
],
"stream": false,
"temperature": 0.7
}'
Response Parameters
| Name | Type | Description |
|---|
id | string | 响应唯一标识 |
choices | array | 补全结果数组,每项含 index、message、finish_reason |
choices[].message.role | string | 角色名,通常为 assistant |
choices[].message.content | string | 生成的文本内容 |
choices[].finish_reason | string | 结束原因:stop(正常结束)、length(达到上限)等 |
usage.prompt_tokens | integer | 输入 token 数 |
usage.completion_tokens | integer | 输出 token 数 |
usage.total_tokens | integer | 总 token 数 |
Response Example
{
"id": "chatcmpl-xxxxxxxx",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!我是一个AI助手,很高兴为你服务。"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 18,
"total_tokens": 43
}
}
Notes
- 流式模式下以 SSE(Server-Sent Events)返回,每行以 data: 开头,结尾为 data: [DONE]
- 认证方式:在请求头中携带 Authorization: Bearer {api_key}
- messages 中 role 支持 system、user、assistant 三种角色
- 流式响应中 delta 字段包含增量文本,需逐块拼接