ChatCompletions格式
/v1/chat/completions
根据对话历史创建模型响应。支持流式和非流式响应,兼容 OpenAI Chat Completions API
请求参数
请求头
Authorization
使用 Bearer Token 认证。
格式: Authorization: Bearer sk-xxxxxx
以下参数遵循 OpenAI Chat Completions 请求格式。参数是否可用取决于所选模型和兼容网关;网关不支持的字段会返回 400。
请求体
请求体参数(21 个)
| 参数 | 类型 | 默认值 | 说明 | 是否必填 |
|---|---|---|---|---|
model | string | — | 要调用的模型 ID。 | 是 |
messages | array | — | 对话消息数组,按时间顺序传入。每条消息至少包含 role 和 content;常见角色包括 system、user、assistant 和 tool。 | 是 |
temperature | number | 1 | 采样温度,范围 0 到 2。是否支持取决于模型。 | 否 |
top_p | number | 1 | 核采样参数,范围 0 到 1。一般只调整 temperature 或 top_p 其中一个。 | 否 |
max_completion_tokens | integer | 模型上限 | 限制本次请求生成的最大 token 数。支持推理的模型可能会将推理 token 计入此上限。 | 否 |
max_tokens | integer | — | 旧版长度限制字段,部分新模型不支持;新请求优先使用 max_completion_tokens。 | 否 |
stop | string / array | null | 命中停止序列后结束生成。部分模型不支持此参数。 | 否 |
n | integer | 1 | 为每个输入生成的候选数量,最小值为 1。大于 1 会按候选数量增加用量。 | 否 |
presence_penalty | number | 0 | 根据已有内容惩罚重复主题,通常范围为 -2 到 2。 | 否 |
frequency_penalty | number | 0 | 根据出现频率惩罚重复 token,通常范围为 -2 到 2。 | 否 |
logit_bias | object | null | 以 token ID 为键、数值为值调整生成概率。 | 否 |
seed | integer | — | 尽量复现结果的随机种子;只在支持的模型上生效,不能保证绝对一致。 | 否 |
response_format | object | — | 设置输出格式。可使用 { "type": "text" }、{ "type": "json_object" },或使用 json_schema 约束 JSON 结构。 | 否 |
modalities | array | ["text"] | 指定输出模态,可选 text 或 audio。 | 否 |
audio | object | — | 音频输出配置,包含 voice 和 format;仅适用于支持音频的模型。 | 否 |
reasoning_effort | string | — | 推理强度,可选 low、medium、high;仅适用于支持推理的模型。 | 否 |
stream | boolean | false | 设置为 true 时以 SSE 增量事件返回结果。 | 否 |
stream_options | object | — | 流式选项,例如 { "include_usage": true } 用于请求最终用量统计。仅在 stream=true 时使用。 | 否 |
tools | array | — | 声明模型可以调用的工具,目前最常见的是 function 工具。 | 否 |
tool_choice | string / object | auto | 控制工具调用策略:none、auto、required,或指定某个函数。 | 否 |
user | string | — | 旧版终端用户标识字段;新项目请根据网关要求使用对应的安全标识字段。 | 否 |
其中只有 model 和 messages 为必填字段;其余参数均为可选,是否生效取决于所选模型和兼容网关。
消息字段
messages 是按时间顺序排列的消息数组。网关会保留下列字段并按所选上游适配器进行转换;不同模型对多模态内容和推理字段的支持不同。
消息对象与内容块字段(16 个)
“条件”表示仅在对应消息角色或内容块类型下必填;消息对象中的 role 和每个内容块中的 type 始终必填。
内容块示例:
{
"role": "user",
"content": [
{ "type": "text", "text": "描述这张图片" },
{ "type": "image_url", "image_url": { "url": "https://example.com/image.png", "detail": "auto" } }
]
}
输出格式与推理
输出格式、音频输出和推理强度参数请参阅上方请求体参数表。
音频输出配置示例:
{
"modalities": ["text", "audio"],
"audio": { "voice": "alloy", "format": "wav" }
}
流式输出
设置 stream 为 true 后,接口会以 SSE 增量事件返回结果;如需在最终事件中获取用量统计,可同时设置 stream_options.include_usage。
工具调用
tools 和 tool_choice 的用途请参阅上方请求体参数表。工具定义使用 type 和 function 对象。function 至少需要 name;可选 description 和 parameters(JSON Schema)。
{
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的天气",
"parameters": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}
工具字段
| 字段 | 类型 | 说明 |
|---|---|---|
tools[].type | string | 工具类型,当前示例为 function。 |
tools[].function.name | string | 函数名称。 |
tools[].function.description | string | 函数用途说明。 |
tools[].function.parameters | object | JSON Schema 格式的参数定义。 |
tool_choice | string / object | none、auto、required,或 { "type": "function", "function": { "name": "..." } }。 |
请求体示例
查看 JSON 请求体示例
{
"model": "gpt-5.6-sol",
"messages": [
{ "role": "system", "content": "你是一个简洁的助手." },
{ "role": "user", "content": "介绍一下自己." }
],
"max_completion_tokens": 512,
"response_format": { "type": "text" },
"stream": false
}
请求示例代码
curl -X POST "https://10000router.com/v1/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "gpt-5.6-sol",
"messages": [
{ "role": "system", "content": "You are a concise assistant." },
{ "role": "user", "content": "Introduce yourself." }
],
"max_completion_tokens": 512,
"response_format": { "type": "text" },
"stream": false
}'
const payload = {
model: "gpt-5.6-sol",
messages: [
{ role: "system", content: "You are a concise assistant." },
{ role: "user", content: "Introduce yourself." }
],
max_completion_tokens: 512,
response_format: { type: "text" },
stream: false
};
const response = await fetch("https://10000router.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer " + process.env.API_KEY,
},
body: JSON.stringify(payload)
});
console.log(await response.json());
payload := `{
"model": "gpt-5.6-sol",
"messages": [
{ "role": "system", "content": "You are a concise assistant." },
{ "role": "user", "content": "Introduce yourself." }
],
"max_completion_tokens": 512,
"response_format": { "type": "text" },
"stream": false
}`
req, err := http.NewRequest("POST", "https://10000router.com/v1/chat/completions", strings.NewReader(payload))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("API_KEY"))
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["API_KEY"], base_url="https://10000router.com/v1")
payload = {
"model": "gpt-5.6-sol",
"messages": [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Introduce yourself."}
],
"max_completion_tokens": 512,
"response_format": {"type": "text"},
"stream": False
}
response = client.chat.completions.create(**payload)
print(response.model_dump())
var client = java.net.http.HttpClient.newHttpClient();
var payload = "{"
+ "\"model\":\"gpt-5.6-sol\","
+ "\"messages\":["
+ "{\"role\":\"system\",\"content\":\"You are a concise assistant.\"},"
+ "{\"role\":\"user\",\"content\":\"Introduce yourself.\"}"
+ "],\"max_completion_tokens\":512,"
+ "\"response_format\":{\"type\":\"text\"},\"stream\":false}";
var request = java.net.http.HttpRequest.newBuilder()
.uri(java.net.URI.create("https://10000router.com/v1/chat/completions"))
.header("Authorization", "Bearer " + System.getenv("API_KEY"))
.POST(java.net.http.HttpRequest.BodyPublishers.ofString(payload))
.build();
var response = client.send(request, java.net.http.HttpResponse.BodyHandlers.ofString());
using System.Net.Http.Json;
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new(
"Bearer",
Environment.GetEnvironmentVariable("API_KEY")
);
var payload = new
{
model = "gpt-5.6-sol",
messages = new[]
{
new { role = "system", content = "You are a concise assistant." },
new { role = "user", content = "Introduce yourself." }
},
max_completion_tokens = 512,
response_format = new { type = "text" },
stream = false
};
var response = await client.PostAsJsonAsync(
"https://10000router.com/v1/chat/completions",
payload
);
Console.WriteLine(await response.Content.ReadAsStringAsync());
返回响应
响应示例
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1710000000,
"model": "gpt-5.6-sol",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 18,
"completion_tokens": 4,
"total_tokens": 22
},
"system_fingerprint": "fp_example"
}
{
"error": {
"message": "Missing required parameter: messages",
"type": "invalid_request_error",
"param": "messages",
"code": null
}
}
{
"error": {
"message": "Rate limit reached",
"type": "rate_limit_exceeded",
"param": null,
"code": null
}
}
{
"error": {
"message": "Invalid authentication credentials",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
返回字段参数
响应字段按对象层级拆分为可折叠区块,默认展开;可收起暂时不关注的对象,减少长响应的视觉干扰。
顶层字段(7 个)
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 本次 Chat Completion 的唯一标识。 |
object | string | 对象类型,非流式响应通常为 chat.completion。 |
created | integer | 响应创建时间,Unix 时间戳(秒)。 |
model | string | 实际生成响应的模型 ID。 |
choices | array<object> | 模型生成的候选结果;数量由请求参数 n 决定。 |
usage | object | Token 用量统计。部分网关或流式中间响应可能不返回此字段。 |
system_fingerprint | string | 服务端配置或模型版本指纹,可用于排查结果变化;部分模型或网关可能不返回。 |
choices[] 字段(4 个)
| 字段 | 类型 | 说明 |
|---|---|---|
index | integer | 候选结果在 choices 数组中的索引。 |
message | object | 助手消息。通常包含 role: "assistant" 和文本 content;工具调用时还可能包含 tool_calls。 |
logprobs | object / null | 请求启用 logprobs 时返回的 token 对数概率信息,否则为 null。 |
finish_reason | string | 生成结束原因:stop、length、tool_calls 或 content_filter。流式中间事件尚未结束时可能为 null。 |
choices[].message 字段(6 个)
message 可能包含以下字段:
| 字段 | 类型 | 说明 |
|---|---|---|
role | string | 消息角色,通常为 assistant。 |
content | string / array | 文本或内容块数组;当响应仅包含工具调用时可能为空或由网关省略。 |
name | string | 发送者名称;部分模型响应中会返回。 |
tool_calls | array<object> | 模型请求调用的工具及其参数。仅在模型选择工具调用时返回。 |
tool_call_id | string | 工具调用 ID;工具消息用于对应此前的调用。 |
reasoning_content | string | 支持推理模型时返回的推理内容。 |
usage 字段(5 个)
| 字段 | 类型 | 说明 |
|---|---|---|
prompt_tokens | integer | 输入消息使用的 token 数。 |
completion_tokens | integer | 输出内容使用的 token 数。支持推理的模型可能包含推理 token。 |
total_tokens | integer | prompt_tokens 与 completion_tokens 的总和。 |
prompt_tokens_details | object | 输入 token 的细分统计,例如缓存 token;按模型和网关返回。 |
completion_tokens_details | object | 输出 token 的细分统计,例如推理 token;按模型和网关返回。 |
流式响应
当请求设置 stream=true 时,响应为 Server-Sent Events(SSE),每个事件包含一个 chat.completion.chunk 对象,而不是上面的完整对象:
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1710000000,"model":"gpt-5.6-sol","choices":[{"index":0,"delta":{"role":"assistant","content":"你好"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1710000000,"model":"gpt-5.6-sol","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":"stop"}]}
data: [DONE]
客户端应按顺序拼接 choices[].delta.content。如果请求同时设置 stream_options.include_usage=true,结束事件前还会返回一个包含 usage 的统计块。