跳转到内容
控制台

OpenAI 兼容

Chat Completions

POST /v1/chat/completions —— 兼容 OpenAI 协议的对话补全,覆盖鉴权、参数、流式、工具调用与视觉输入

OpenAI Chat Completions 协议接口,官方 openai SDK 只需修改 Base URL 即可使用。Claude、DeepSeek、Qwen、GLM 等非 OpenAI 模型也可从这里调用。

POSThttps://visioncoder.cn/v1/chat/completions

两种 Header 任选其一,使用同一个 Key(在 API Key 管理 创建):

Authorization: Bearer YOUR_API_KEY
x-api-key: YOUR_API_KEY
参数 类型 必填 说明
model string 是 模型 ID。可用值见 GET /v1/models
messages array 是 对话历史。每项形如 { role, content },role 取 system / user / assistant / tool
max_tokens integer 否 最大输出 token 数,上限受模型自身限制
max_completion_tokens integer 否 新版字段,作用同 max_tokens。两个都传时以本字段为准
temperature number 否 采样温度,越低越确定。默认值随上游模型而定
top_p number 否 核采样。和 temperature 二选一
n integer 否 生成候选回复数。按所有候选的输出 token 总和计费,输入只算一次
stream boolean 否 为 true 时以 SSE 流式返回,见流式响应
stop string | array 否 停止序列,生成到它就截断
presence_penalty number 否 话题新鲜度惩罚
frequency_penalty number 否 重复惩罚
seed integer 否 采样种子。尽力复现,不保证逐字一致
response_format object 否 { "type": "json_object" } 强制输出合法 JSON,见结构化输出
tools array 否 可调用的函数定义,见工具调用
tool_choice string | object 否 "auto"(默认)/ "none" / { "type": "function", "function": { "name": "..." } } 强制指定
user string 否 终端用户标识,用于你自己做用量归因
Terminal window
curl https://visioncoder.cn/v1/chat/completions \
-H "Authorization: Bearer $VISIONCODER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.4",
"messages": [
{"role": "system", "content": "你是一个简洁的助手。"},
{"role": "user", "content": "法国的首都是哪里?"}
],
"max_tokens": 200,
"temperature": 0.7
}'

换用其它模型只需修改 model,其余不变:

resp = client.chat.completions.create(
model="claude-sonnet-5", # 换成 Claude,其余不变
messages=[{"role": "user", "content": "法国的首都是哪里?"}],
)
字段 类型 说明
id string 本次补全的 ID,形如 chatcmpl-xxx
object string 非流式为 chat.completion,流式为 chat.completion.chunk
created integer Unix 时间戳(秒)
model string 应答的模型 ID
choices array 候选回复,形如 [{ index, message: { role, content, tool_calls? }, finish_reason }]
choices[].finish_reason string stop 正常结束 / length 撞上 token 上限 / tool_calls 要调工具 / content_filter 被拦
usage object { prompt_tokens, completion_tokens, total_tokens },计费依据
{
"id": "chatcmpl-9aB2k4xQwR7vL8mN",
"object": "chat.completion",
"created": 1779438767,
"model": "gpt-5.4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "法国的首都是巴黎。"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 24,
"completion_tokens": 8,
"total_tokens": 32
}
}

finish_reason 为 length 表示输出被 max_tokens 截断,内容不完整,解析 JSON 前先判断该字段。

响应头 X-Request-Id 是请求唯一标识,X-Served-Model 等于请求的模型(网关不改写模型)。

传 "stream": true,响应变成 SSE,每个 chunk 是一个 chat.completion.chunk,增量在 choices[0].delta 里。最后以 data: [DONE] 结束。流式开始后出错,以 SSE 错误事件结束。

data: {"id":"chatcmpl-9aB2k4xQwR7vL8mN","object":"chat.completion.chunk","created":1779438767,"model":"gpt-5.4","choices":[{"index":0,"delta":{"role":"assistant","content":"法国"},"finish_reason":null}]}
data: {"id":"chatcmpl-9aB2k4xQwR7vL8mN","object":"chat.completion.chunk","created":1779438767,"model":"gpt-5.4","choices":[{"index":0,"delta":{"content":"的首都是巴黎。"},"finish_reason":null}]}
data: {"id":"chatcmpl-9aB2k4xQwR7vL8mN","object":"chat.completion.chunk","created":1779438767,"model":"gpt-5.4","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
stream = client.chat.completions.create(
model="gpt-5.4",
messages=[{"role": "user", "content": "写一首关于秋天的短诗"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)

在 tools 里给出函数定义,模型需要外部数据时会返回 tool_calls 而不是直接作答。

{
"model": "gpt-5.4",
"messages": [{ "role": "user", "content": "北京现在天气怎么样?" }],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的当前天气",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "城市名,例如 北京" }
},
"required": ["city"]
}
}
}
]
}

finish_reason 变成 tool_calls,content 为空:

{
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\":\"北京\"}" }
}
]
},
"finish_reason": "tool_calls"
}
]
}

把模型返回的 assistant 消息原样放回 messages,后面追加一条 role: "tool" 的结果,tool_call_id 要对上:

{
"model": "gpt-5.4",
"messages": [
{ "role": "user", "content": "北京现在天气怎么样?" },
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\":\"北京\"}" }
}
]
},
{
"role": "tool",
"tool_call_id": "call_abc123",
"content": "{\"temp\":12,\"desc\":\"晴\"}"
}
]
}

模型随后基于工具结果作答。

content 从字符串换成内容块数组,就能传图:

{
"model": "gpt-5.4",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "这张图里有什么?" },
{ "type": "image_url", "image_url": { "url": "https://example.com/photo.jpg" } }
]
}
]
}

也可以传 base64:

{ "type": "image_url", "image_url": { "url": "data:image/jpeg;base64,/9j/4AAQSkZJRg..." } }

要求模型只输出 JSON:

{
"model": "gpt-5.4",
"messages": [
{ "role": "system", "content": "只输出 JSON,不要任何解释文字。" },
{ "role": "user", "content": "把这句话拆成结构化数据:张三,32 岁,北京" }
],
"response_format": { "type": "json_object" }
}

response_format 对不支持的模型会被忽略,因此 system 中的约束不可省略。

错误体为 OpenAI 格式。流式开始后出错,以 SSE 错误事件结束。排障时记录响应头 X-Request-Id 并报给客服。

状态码 含义 处理
400 请求体格式或参数错误 查看 error.message,通常是 messages 结构或参数类型不对
401 API Key 无效 到 API Key 管理 确认,注意不要带入空格
403 模型对该 Key 不可用 模型不在平台、企业、成员或 Key 的可用范围内,用 GET /v1/models 确认
404 路径或模型不存在 Base URL 缺少 /v1,或 model 拼写错误
429 限流,或额度用完 限流带 Retry-After,退避重试。error.code 为 insufficient_quota 表示没有可用的套餐、加油包或企业余额(业务码 402072)或额度已用完,不带 Retry-After,重试无效,需到 定价 获取额度
503 上游全部过载或无可用端点 稍后重试
5xx 其它服务端错误 重试;持续失败时报告 X-Request-Id
{
"error": {
"message": "model not found: gpt-5.9",
"type": "invalid_request_error",
"code": "model_not_found"
}
}