创建聊天补全
最主要的 OpenAI 兼容端点。任何 OpenAI SDK、LangChain、LlamaIndex 或原生 HTTP 客户端都能直接使用。
/v1/chat/completions同时也提供 /chat/completions 和 /v1/v1/chat/completions 两个别名,方便 base URL 会自行增删 /v1 前缀的客户端。
curl http://localhost:8080/v1/chat/completions \
-H "Authorization: Bearer $LLM_PROXY_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
}'from openai import OpenAI
client = OpenAI(base_url="http://localhost:8080/v1", api_key="sk-your-proxy-key")
resp = client.chat.completions.create(
model="auto",
messages=[{"role": "user", "content": "Hello!"}],
)
print(resp.choices[0].message.content){
"id": "chatcmpl-9f2c…",
"object": "chat.completion",
"created": 1718047200,
"model": "auto",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 9,
"total_tokens": 34
}
}请求字段
未知顶层字段会被接受并转发给上游,但只属于 Responses 的键(previous_response_id、 background、truncation、include、conversation、max_tool_calls、text) 在发往 Chat Completions 上游时会被剔除。
支持的字段:temperature、top_p、max_tokens、max_completion_tokens、n、 stream、stream_options、stop、presence_penalty、frequency_penalty、 logit_bias、logprobs、top_logprobs、user、response_format、seed、tools、 tool_choice、parallel_tool_calls、thinking、audio、modalities、 reasoning_effort、prediction、web_search_options、service_tier、verbosity、 store、metadata、prompt_cache_key、prompt_cache_retention、 safety_identifier。
需要留意的转换:
| 输入 | 行为 |
|---|---|
只传 max_completion_tokens | 映射为 max_tokens;两者都设置时,max_tokens 优先 |
response_format: {"type":"json_schema", …} | 原样保留在发往上游的 Chat Completions 请求中 |
tool_choice: {"type":"allowed_tools", …} | 仅适用于 Responses 风格:折叠为其模式(auto/required)并过滤工具列表。在 Chat Completions 请求中,该对象会原样转发给上游 |
parallel_tool_calls: false | 转发给上游并关闭并行工具调用 |
| 未知工具类型 | 校验时接受,但从上游工具列表中移除 |
路由到 OpenAI 上游(使用 Responses API)时,seed、n、logit_bias、各种 penalty、prediction、modalities、audio 和 prompt_cache_retention 会被丢弃, response_format: json_schema 映射为 text.format。
消息
- 角色:
system、developer(按原样保留)、user、assistant、tool(通过tool_call_id与待处理的工具调用匹配),以及原样透传的未知角色。 - 内容片段:
text、image_url(远程 URL 或data:base64)、input_audio(base64 +format)、file(file_data/file_id/filename)、video_url、refusal。无法识别的片段会降级为文本,而不是直接报错。 - assistant 轮次可以包含
tool_calls(函数或自定义;保留thought_signature)、reasoning_content(附带reasoning_signature、reasoning_is_redacted、encrypted_content)、reasoning_segments、refusal和audio。 - 输入时,推理内容从
reasoning_content或 OpenRouter/NanoGPT 的reasoning写法读取(两者都存在时以reasoning_content为准)。被遮蔽的推理内容从reasoning_is_redacted+encrypted_content读取,不透明载荷在后者里,reasoning_content只保存[redacted]显示占位符。 - 交错的 assistant 轮次(
thinking → tool_call → thinking)无法用单个reasoning_content字段表达。这时代理会输出reasoning_segments数组,每项形如{type, text?, signature?, encrypted_content?, after_tool_calls?}(被遮蔽时为{type: "redacted", data}),其中after_tool_calls表示其前面有多少个工具调用。 原样回传即可保持工具调用的位置;丢弃这个未知字段的客户端会回退到拼接后的reasoning_content。
响应
{"id", "object":"chat.completion", "created", "model", "choices":[…], "usage", …}
model回显客户端请求的别名(auto或你定义的模型名),不会暴露解析后的 上游模型。- 每个 choice:
index、message、finish_reason(stop、length、tool_calls、content_filter、context_length)。 - message:
content(文本拼接结果;为空或只有 refusal 时为null)、arguments为 JSON 字符串的tool_calls、reasoning_content(始终输出reasoning_content,不输出reasoning)、reasoning_signature、encrypted_content(被遮蔽推理或桥接提供商签名的封装载荷)、reasoning_segments(仅交错轮次,见上文)、refusal、audio,适用时还有annotations。生成的图像以 Markdown 形式出现在content里。 usage:prompt_tokens、completion_tokens、total_tokens,以及明细 (prompt_tokens_details.cached_tokens/audio_tokens、completion_tokens_details.reasoning_tokens/audio_tokens/…)。n > 1时产生n个 choice;如果提供商返回的不同输出不足,最后一个会被复制,并在日志里记录 警告。- 提供商提供时,
logprobs与system_fingerprint/service_tier会透传。
上游的限流响应头(x-ratelimit-*、retry-after、openai-processing-ms 等)会随 响应一并转发,代理自己的 X-Request-Id 也在其中。
流式
stream: true 会返回 chat.completion.chunk 帧,然后是一个只含用量的分块,最后是 data: [DONE]。只要上游报告了用量,就会发出这个用量分块。当请求需要转换为其他上游 格式时,无论客户端的 stream_options 如何设置,代理都会自动请求 include_usage: true。流中途出错时先发一个错误帧,再发 [DONE]。详见 流式。
校验错误
存在两种错误结构,与上游行为对应:
- JSON 端点(chat、embeddings、speech)返回 422
{"detail": [...]}。 - Multipart 端点(audio、images)返回 400,响应体是 OpenAI 风格的
{"error": {…}}。
相关内容
curl http://localhost:8080/v1/chat/completions \
-H "Authorization: Bearer $LLM_PROXY_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
}'from openai import OpenAI
client = OpenAI(base_url="http://localhost:8080/v1", api_key="sk-your-proxy-key")
resp = client.chat.completions.create(
model="auto",
messages=[{"role": "user", "content": "Hello!"}],
)
print(resp.choices[0].message.content){
"id": "chatcmpl-9f2c…",
"object": "chat.completion",
"created": 1718047200,
"model": "auto",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 9,
"total_tokens": 34
}
}