对话补全 Chat Completions
对话补全 Chat Completions
文档版本: v2.2.0 | 更新日期: 2026-05-15
1. 接口概述
对话补全(Chat Completions)是平台最核心的接口,支持多轮对话、流式输出、工具调用、视觉理解等能力。
2. 请求参数
2.1 顶层参数
注意:当前平台上的模型不支持 n(候选数量,仅支持 n=1)和 logprobs/top_logprobs(Token 对数概率)参数。传入这些参数可能返回错误或被忽略。
2.2 消息格式(messages)
messages 是一个消息对象数组,每条消息包含 role 和 content 字段。
系统消息(system)
设定模型的行为指令与角色设定。
{
"role": "system",
"content": "你是一位专业的技术文档撰写助手。"
}
用户消息(user)
用户输入的对话内容。支持纯文本和多模态(图文混合)。
纯文本:
{
"role": "user",
"content": "请解释什么是 Transformer 架构。"
}
多模态(图文混合):
{
"role": "user",
"content": [
{"type": "text", "text": "请描述这张图片的内容。"},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg",
"detail": "auto"
}
}
]
}
detail 可选值:auto(自动)、low(低分辨率,节省 Token)、high(高分辨率)。
Base64 图片:
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgo..."
}
}
助手消息(assistant)
模型生成的回复,用于多轮对话中的历史上下文。
{
"role": "assistant",
"content": "Transformer 是一种基于自注意力机制的深度学习架构……"
}
工具调用结果的回传消息,必须包含 tool_call_id。
{
"role": "tool",
"tool_call_id": "call_abc123",
"content": "{\"temperature\": 22, \"unit\": \"celsius\"}"
}
2.3 多模态支持说明
具体模型的多模态能力请参考控制台的模型能力矩阵,或通过 GET /v1/models 查询模型的 supported_endpoint_types。
json_object 示例:
{
"model": "deepseek-v4.1-flash",
"response_format": {"type": "json_object"},
"messages": [
{"role": "system", "content": "请以 json 格式回答用户的问题。"},
{"role": "user", "content": "列出三种编程语言及其特点。"}
]
}
注意:
- 部分模型(如 DeepSeek 系列)在使用
json_object 模式时,要求 system 或 user 消息中必须包含"json"一词,否则可能返回错误。建议始终在提示词中明确提及 JSON 输出要求。
json_schema 类型当前平台支持有限,部分模型会返回 "This response_format type is unavailable now" 错误。如需结构化输出,建议优先使用 json_object 配合提示词约束输出格式。
3. 响应格式
3.1 非流式响应
以下为 deepseek-v4.1-flash 模型的实际响应示例:
{
"id": "chatcmpl-ffe850e6-3b4f-92b5-bead-63015931b73e",
"object": "chat.completion",
"created": 1778819669,
"model": "deepseek-v4.1-flash",
"system_fingerprint": null,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么我可以帮助你的吗?",
"reasoning_content": null
},
"finish_reason": "stop",
"logprobs": null
}
],
"usage": {
"prompt_tokens": 35,
"completion_tokens": 34,
"total_tokens": 69,
"prompt_tokens_details": {
"cached_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 31
}
}
}
3.2 响应字段说明
注意:不同模型返回的字段可能存在差异。例如部分模型的 usage 中可能包含 estimated_cost、prompt_cache_hit_tokens 等扩展字段,建议客户端解析时对未知字段做兼容处理。
finish_reason 取值:
3.3 推理内容(reasoning_content)
平台上多款模型具备深度推理能力,响应中会包含 reasoning_content 字段,展示模型的推理思考过程。
已验证支持推理的模型:
实际响应示例(deepseek-v4.1-flash,请求:"1+1等于几?只回答数字"):
{
"id": "chatcmpl-ffe850e6-3b4f-92b5-bead-63015931b73e",
"object": "chat.completion",
"created": 1778819669,
"model": "deepseek-v4.1-flash",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "2",
"reasoning_content": "用户问的是1+1等于几,要求只回答数字。答案是2。"
},
"finish_reason": "stop",
"logprobs": null
}
],
"usage": {
"prompt_tokens": 35,
"completion_tokens": 34,
"total_tokens": 69,
"prompt_tokens_details": {
"cached_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 31
}
}
}
说明:
reasoning_content 为 string 类型,包含模型的推理思维链文本。非推理模型该字段为 null 或不返回。
usage.completion_tokens_details.reasoning_tokens 记录推理过程消耗的 Token 数量,该部分 Token 同样计入 completion_tokens 总量并参与计费。
- 推理 Token 消耗通常大于最终输出 Token,请合理设置
max_completion_tokens 以确保推理过程有充足空间。
4. 流式响应
4.1 启用流式
设置 "stream": true 即可启用流式输出。服务端通过 Server-Sent Events(SSE)逐步推送生成内容。
curl https://api.sapis.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的API-Key" \
-d '{
"model": "deepseek-v4.1-flash",
"stream": true,
"stream_options": {"include_usage": true},
"messages": [{"role": "user", "content": "你好"}]
}'
4.2 流式数据格式
每个 SSE 事件以 data: 开头,最后一条为 data: [DONE]。
首个事件(包含 role):
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1778819720,"model":"deepseek-v4.1-flash","system_fingerprint":null,"choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":"让我思考"},"finish_reason":null,"logprobs":null}],"usage":null}
推理阶段事件(推理模型):
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1778819720,"model":"deepseek-v4.1-flash","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":null,"reasoning_content":"这个问题的答案是"},"finish_reason":null,"logprobs":null}],"usage":null}
内容生成事件:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1778819720,"model":"deepseek-v4.1-flash","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"你好","reasoning_content":null},"finish_reason":null,"logprobs":null}],"usage":null}
结束事件:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1778819720,"model":"deepseek-v4.1-flash","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"","reasoning_content":null},"finish_reason":"stop","logprobs":null}],"usage":null}
用量统计事件(需 stream_options.include_usage: true):
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1778819720,"model":"deepseek-v4.1-flash","system_fingerprint":null,"choices":[],"usage":{"prompt_tokens":11,"completion_tokens":451,"total_tokens":462,"completion_tokens_details":{"reasoning_tokens":448},"prompt_tokens_details":{"cached_tokens":0}}}
结束标记:
4.3 流式响应字段说明
注意: 推理模型通常先输出一系列 reasoning_content 事件(此时 content 为 null),再输出 content 事件(此时 reasoning_content 为 null)。客户端应分别拼接这两个字段。
4.4 Python 流式处理示例
from openai import OpenAI
client = OpenAI(
api_key="sk-你的API-Key",
base_url="https://api.sapis.cn/v1"
)
stream = client.chat.completions.create(
model="deepseek-v4.1-flash",
messages=[{"role": "user", "content": "解释量子计算的基本原理"}],
stream=True,
stream_options={"include_usage": True}
)
reasoning = ""
content = ""
for chunk in stream:
if chunk.choices:
delta = chunk.choices[0].delta
if hasattr(delta, 'reasoning_content') and delta.reasoning_content:
reasoning += delta.reasoning_content
if delta.content:
content += delta.content
print(delta.content, end="", flush=True)
if chunk.usage:
print(f"\n\n用量: {chunk.usage}")
5.1 定义工具
通过 tools 参数定义可供模型调用的函数。
{
"model": "GLM-5.2",
"messages": [
{"role": "user", "content": "北京今天的天气怎么样?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的当前天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称,如 北京"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "温度单位"
}
},
"required": ["city"]
}
}
}
]
}
注意: DeepSeek 系列模型使用推理模式时不支持 tool_choice: "required",调用会返回错误 "deepseek-reasoner does not support this tool_choice"。建议始终使用 "auto",这是兼容性最好的选项。
5.3 工具调用响应
当模型决定调用工具时,响应的 finish_reason 为 "tool_calls"。
以下为 GLM-5.2 的实际响应示例:
{
"id": "chatcmpl-RJbq47MfzkorhtetXx3D7DDr",
"object": "chat.completion",
"created": 1778819728,
"model": "GLM-5.2",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_e272",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"北京\"}"
}
}
],
"reasoning_content": "The user is asking about the weather in Beijing. Let me call the weather function for this city."
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 167,
"total_tokens": 199,
"completion_tokens": 32
}
}
注意:
- 工具调用时
content 字段可能为 null、空字符串或包含文字说明,不同模型行为不同。客户端应以 tool_calls 字段是否存在来判断是否需要执行工具。
- 部分模型(如 deepseek-v4.1-flash)可能同时返回
content(文字说明)和 tool_calls,这属于正常行为。
5.4 工具调用完整流程
- 发送用户消息和工具定义。
- 模型返回
tool_calls。
- 客户端执行对应函数,获取结果。
- 将助手消息(含
tool_calls)和工具结果(role: "tool")追加到 messages,再次请求。
- 模型根据工具结果生成最终回复。
第二次请求示例:
{
"model": "GLM-5.2",
"messages": [
{"role": "user", "content": "北京今天的天气怎么样?"},
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_e272",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"北京\"}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_e272",
"content": "{\"temperature\": 28, \"condition\": \"晴\", \"humidity\": 45}"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的当前天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
}
]
}
5.5 并行工具调用
模型可能在一次响应中返回多个 tool_calls,表示需要并行调用多个工具。客户端应分别执行每个工具,并将所有结果回传:
{
"role": "assistant",
"tool_calls": [
{"id": "call_001", "type": "function", "function": {"name": "get_weather", "arguments": "{\"city\": \"北京\"}"}},
{"id": "call_002", "type": "function", "function": {"name": "get_weather", "arguments": "{\"city\": \"上海\"}"}}
]
}
回传时需为每个 tool_call_id 提供对应的 tool 消息。
6. 完整请求示例
6.1 基础对话
curl https://api.sapis.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的API-Key" \
-d '{
"model": "deepseek-v4.1-flash",
"messages": [
{"role": "system", "content": "你是一位知识渊博的科学顾问。"},
{"role": "user", "content": "简要介绍一下黑洞的形成过程。"}
],
"temperature": 0.7,
"max_tokens": 500
}'
6.2 多轮对话
curl https://api.sapis.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的API-Key" \
-d '{
"model": "GLM-5.2",
"messages": [
{"role": "system", "content": "你是一位编程导师。"},
{"role": "user", "content": "什么是递归?"},
{"role": "assistant", "content": "递归是一种函数直接或间接调用自身的编程技巧……"},
{"role": "user", "content": "能给一个 Python 的递归示例吗?"}
]
}'
6.3 视觉理解
curl https://api.sapis.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的API-Key" \
-d '{
"model": "Qwen3.8-Omni-Flash",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "描述这张图片中的内容。"},
{"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
]
}
],
"max_tokens": 300
}'
6.4 JSON 结构化输出
curl https://api.sapis.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的API-Key" \
-d '{
"model": "deepseek-v4.1-flash",
"response_format": {"type": "json_object"},
"messages": [
{"role": "system", "content": "你是一个数据提取助手,请始终以 json 格式输出。"},
{"role": "user", "content": "提取以下文本的关键信息:张三,男,30岁,工程师,就职于汇飞智能。"}
]
}'
7. 错误处理
7.1 常见错误
完整错误码见 CODE & 错误码。
平台实际错误消息示例:
7.2 错误响应格式
{
"error": {
"code": "model_not_found",
"message": "No available channel for model xxx under group 国模 (distributor) (request id: ...)",
"type": "huifly_error"
}
}
type 取值:huifly_error(平台错误)、openai_error、upstream_error(上游错误)、claude_error 等;message 通常附带 request id,便于排查。
7.3 重试建议
8. 最佳实践
汇飞智能技术团队 — 文档版本 v2.2.0