跳到主要内容

聊天补全

API 参考/Chat Completions/创建聊天补全

创建聊天补全

POST/v1/chat/completions

使用 OpenAI 兼容格式调用 GPT 系列模型,支持非流式、流式、多轮对话、工具调用和结构化输出。

认证

通过 Bearer Token 认证。

Authorizationstring必填

API Key,格式为 Bearer YOUR_API_KEY。

默认值:-
Content-Typestring必填

请求体格式。

默认值:application/json
Request-Idstring可选

客户侧生成的唯一请求标识,用于追踪和排查。

默认值:-

请求

请求体使用 OpenAI Chat Completions 兼容格式。

modelstring必填

模型 ID,参考模型与能力页面。

默认值:-
messagesarray必填

对话消息列表。

默认值:-
messages[].rolestring必填

消息角色:system、user、assistant 或 tool。

默认值:-
messages[].contentstring | array必填

消息内容。多模态输入使用数组内容块。

默认值:-
streamboolean可选

是否启用 SSE 流式输出。

默认值:false
temperaturenumber可选

采样温度,取值通常为 0 到 2。

默认值:1
top_pnumber可选

核采样参数。

默认值:1
max_tokensinteger可选

限制本次响应最多生成的 Token 数。

默认值:4096
stopstring | array可选

停止序列,模型生成到指定序列时停止。

默认值:-
toolsarray可选

Function Calling 工具定义列表。

默认值:-
tool_choicestring | object可选

工具调用策略。

默认值:-
response_formatobject可选

输出格式控制,例如 JSON 模式。

默认值:-
seedinteger可选

用于提高采样结果相似度的随机种子。

默认值:-
userstring可选

终端用户唯一标识。

默认值:-

响应

非流式请求返回完整 chat.completion 对象;流式请求返回 chat.completion.chunk SSE 事件。

idstring必填

本次补全唯一标识。

默认值:-
objectstring必填

对象类型。

默认值:chat.completion
createdinteger必填

创建时间,Unix 时间戳。

默认值:-
modelstring必填

实际执行请求的模型。

默认值:-
choicesarray必填

补全结果列表。

默认值:-
choices[].messageobject可选

非流式响应中的模型消息。

默认值:-
choices[].deltaobject可选

流式响应中的增量内容。

默认值:-
choices[].finish_reasonstring可选

停止原因,例如 stop、length 或 tool_calls。

默认值:-
usageobject可选

Token 用量统计。

默认值:-
traceIdstring可选

平台追踪 ID,用于问题排查。

默认值:-

流式响应

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"你"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{},"finish_reason":"stop"}]}
data: [DONE]

错误

状态码说明
400请求体字段错误、消息格式错误或模型不支持指定参数
401API Key 缺失或无效
403当前账户无权调用该模型
429请求超过速率限制
500 / 503平台或上游模型服务异常

相关指南