Chat Completions
兼容 OpenAI Chat Completions。所有文本模型都能用这个接口调用,包括 Claude、Gemini、DeepSeek 等。
https://api.codingrouter.ai/v1/chat/completions鉴权
Authorization: Bearer <令牌>
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名,令牌分组里要有这个模型 |
messages | array | 是 | 对话消息,见下文 |
stream | boolean | 否 | true 时以 SSE 流式返回 |
stream_options | object | 否 | {"include_usage": true}:在最后一个数据块里返回用量 |
max_tokens | integer | 否 | 最多生成多少 token |
max_completion_tokens | integer | 否 | 同上,新版 OpenAI 写法,包含推理 token |
temperature | number | 否 | 0 到 2,越高越随机 |
top_p | number | 否 | 核采样,0 到 1 |
n | integer | 否 | 返回几个候选回答 |
stop | string / array | 否 | 遇到这些字符串时停止生成 |
presence_penalty | number | 否 | -2 到 2 |
frequency_penalty | number | 否 | -2 到 2 |
seed | integer | 否 | 固定随机种子,尽量让结果可复现 |
tools | array | 否 | 可调用的函数列表 |
tool_choice | string / object | 否 | auto、none、required,或指定某个函数 |
response_format | object | 否 | {"type": "json_object"} 或 {"type": "json_schema", ...} |
reasoning_effort | string | 否 | 推理模型的思考深度:low、medium、high |
参数是否生效取决于模型本身,例如 n 和 seed 只有部分模型支持;模型不支持的参数通常会被忽略。
messages
| 字段 | 类型 | 说明 |
|---|---|---|
role | string | system、user、assistant 或 tool |
content | string / array | 文本,或由文本和图片组成的数组 |
tool_calls | array | 助手发起的函数调用(role 为 assistant 时) |
tool_call_id | string | 对应的函数调用 ID(role 为 tool 时) |
请求示例
curl https://api.codingrouter.ai/v1/chat/completions \
-H "Authorization: Bearer $CODINGROUTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.5",
"messages": [
{"role": "system", "content": "你是一个简洁的助手。"},
{"role": "user", "content": "用一句话解释什么是 API 网关"}
]
}'from openai import OpenAI
client = OpenAI(base_url="https://api.codingrouter.ai/v1", api_key="sk-你的令牌")
resp = client.chat.completions.create(
model="gpt-5.5",
messages=[
{"role": "system", "content": "你是一个简洁的助手。"},
{"role": "user", "content": "用一句话解释什么是 API 网关"},
],
)
print(resp.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.codingrouter.ai/v1",
apiKey: process.env.CODINGROUTER_KEY,
});
const resp = await client.chat.completions.create({
model: "gpt-5.5",
messages: [
{ role: "system", content: "你是一个简洁的助手。" },
{ role: "user", content: "用一句话解释什么是 API 网关" },
],
});
console.log(resp.choices[0].message.content);响应示例
{
"id": "chatcmpl-...",
"object": "chat.completion",
"created": 1794806345,
"model": "gpt-5.5",
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "API 网关是统一接收、路由和管理客户端 API 请求的入口层。"},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 28,
"completion_tokens": 23,
"total_tokens": 51,
"prompt_tokens_details": {"cached_tokens": 0}
}
}finish_reason:stop 正常结束,length 达到长度上限,tool_calls 模型要求调用函数。
流式输出
stream = client.chat.completions.create(
model="gpt-5.5",
messages=[{"role": "user", "content": "写一首关于星空的短诗"}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.usage:
print("\n用量:", chunk.usage.total_tokens)每个数据块形如 data: {"object":"chat.completion.chunk","choices":[{"delta":{"content":"..."}}]},最后一行是 data: [DONE]。开启 include_usage 时,[DONE] 之前有一个 choices 为空、带 usage 的数据块。
图片输入
支持视觉的模型可以在 content 里放图片,url 填公网地址或 data: 开头的 base64:
{
"model": "gpt-5.5",
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "这张图是什么颜色?"},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,iVBORw0KGgo..."}}
]
}]
}工具调用
{
"model": "gpt-5.5",
"messages": [{"role": "user", "content": "北京今天天气怎么样?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询城市天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}]
}模型决定调用时,返回:
{
"message": {
"role": "assistant",
"tool_calls": [{
"id": "call_...",
"type": "function",
"function": {"name": "get_weather", "arguments": "{\"city\":\"北京\"}"}
}]
},
"finish_reason": "tool_calls"
}执行函数后,把结果作为 {"role": "tool", "tool_call_id": "call_...", "content": "晴,25°C"} 追加到 messages 再请求一次。
结构化输出
{
"model": "gpt-5.5",
"messages": [{"role": "user", "content": "北京属于哪个国家?"}],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "answer",
"strict": true,
"schema": {
"type": "object",
"properties": {"city": {"type": "string"}, "country": {"type": "string"}},
"required": ["city", "country"],
"additionalProperties": false
}
}
}
}message.content 会是符合 schema 的 JSON 字符串。只要合法 JSON、不限结构时,用 {"type": "json_object"},并在提示里说明要输出 JSON。
调用其他厂商的模型
把 model 换成对应模型、令牌换成对应分组即可:
curl https://api.codingrouter.ai/v1/chat/completions \
-H "Authorization: Bearer $CLAUDE_GROUP_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "claude-sonnet-5-5", "messages": [{"role": "user", "content": "你好"}]}'流式、工具调用、图片输入对 Claude 和 Gemini 模型同样可用。厂商专属的参数(如 Claude 的 cache_control)在这个接口里不一定生效,需要时用 Claude Messages 或 Gemini 原生接口。
推理模型
GLM 等推理模型会先输出思考内容(reasoning_content),再输出正文。max_tokens 太小时可能只有思考、正文为空(finish_reason 为 length),请留足 2000 以上。
