ChatCompletions格式

POST /v1/chat/completions

根据对话历史创建模型响应。支持流式和非流式响应,兼容 OpenAI Chat Completions API

请求参数

请求头

Authorization

使用 Bearer Token 认证。

格式: Authorization: Bearer sk-xxxxxx

以下参数遵循 OpenAI Chat Completions 请求格式。参数是否可用取决于所选模型和兼容网关;网关不支持的字段会返回 400。

请求体

请求体参数(21 个)
参数类型默认值说明是否必填
modelstring—要调用的模型 ID。是
messagesarray—对话消息数组,按时间顺序传入。每条消息至少包含 role 和 content;常见角色包括 system、user、assistant 和 tool。是
temperaturenumber1采样温度,范围 0 到 2。是否支持取决于模型。否
top_pnumber1核采样参数,范围 0 到 1。一般只调整 temperature 或 top_p 其中一个。否
max_completion_tokensinteger模型上限限制本次请求生成的最大 token 数。支持推理的模型可能会将推理 token 计入此上限。否
max_tokensinteger—旧版长度限制字段,部分新模型不支持;新请求优先使用 max_completion_tokens。否
stopstring / arraynull命中停止序列后结束生成。部分模型不支持此参数。否
ninteger1为每个输入生成的候选数量,最小值为 1。大于 1 会按候选数量增加用量。否
presence_penaltynumber0根据已有内容惩罚重复主题,通常范围为 -2 到 2。否
frequency_penaltynumber0根据出现频率惩罚重复 token,通常范围为 -2 到 2。否
logit_biasobjectnull以 token ID 为键、数值为值调整生成概率。否
seedinteger—尽量复现结果的随机种子;只在支持的模型上生效,不能保证绝对一致。否
response_formatobject—设置输出格式。可使用 { "type": "text" }、{ "type": "json_object" },或使用 json_schema 约束 JSON 结构。否
modalitiesarray["text"]指定输出模态,可选 text 或 audio。否
audioobject—音频输出配置,包含 voice 和 format;仅适用于支持音频的模型。否
reasoning_effortstring—推理强度,可选 low、medium、high;仅适用于支持推理的模型。否
streambooleanfalse设置为 true 时以 SSE 增量事件返回结果。否
stream_optionsobject—流式选项,例如 { "include_usage": true } 用于请求最终用量统计。仅在 stream=true 时使用。否
toolsarray—声明模型可以调用的工具,目前最常见的是 function 工具。否
tool_choicestring / objectauto控制工具调用策略:none、auto、required,或指定某个函数。否
userstring—旧版终端用户标识字段;新项目请根据网关要求使用对应的安全标识字段。否

其中只有 model 和 messages 为必填字段;其余参数均为可选,是否生效取决于所选模型和兼容网关。

消息字段

messages 是按时间顺序排列的消息数组。网关会保留下列字段并按所选上游适配器进行转换;不同模型对多模态内容和推理字段的支持不同。

消息对象与内容块字段(16 个)
参数类型默认值说明是否必填
messages[].rolestringsystem、developer、user、assistant、tool消息角色。是
messages[].contentstring / array文本或内容块数组文本,或由 text、image_url、input_audio、file、video_url 组成的内容块数组。助手消息仅包含工具调用时可以省略。条件
messages[].namestring—可选的参与者名称。否
messages[].reasoning_contentstring—向兼容推理模型传递的推理内容;请勿在不需要时回传隐藏推理。否
messages[].tool_callsarray—助手消息发起的工具调用。通常与 role: "assistant" 一起回传。否
messages[].tool_call_idstring—工具消息对应的调用 ID,必须与助手消息中的工具调用匹配。条件
messages[].content[].typestringtext、image_url、input_audio、file、video_url内容块类型,用于确定当前内容块的字段结构。是
messages[].content[].textstring—文本内容,仅用于 type: "text"。条件
messages[].content[].image_url.urlstring—图片 URL 或 base64 数据,仅用于 type: "image_url"。条件
messages[].content[].image_url.detailstringlow / high / auto图片细节级别,仅用于 type: "image_url"。否
messages[].content[].input_audio.datastring—Base64 编码的音频数据,仅用于 type: "input_audio"。条件
messages[].content[].input_audio.formatstringwav / mp3音频格式,仅用于 type: "input_audio"。条件
messages[].content[].file.filenamestring—文件名,仅用于 type: "file"。条件
messages[].content[].file.file_datastring—文件数据,仅用于 type: "file";通常与 file_id 二选一。条件
messages[].content[].file.file_idstring—已上传文件 ID,仅用于 type: "file";通常与 file_data 二选一。条件
messages[].content[].video_url.urlstring—视频 URL,仅用于 type: "video_url"。条件

“条件”表示仅在对应消息角色或内容块类型下必填;消息对象中的 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[].typestring工具类型,当前示例为 function。
tools[].function.namestring函数名称。
tools[].function.descriptionstring函数用途说明。
tools[].function.parametersobjectJSON Schema 格式的参数定义。
tool_choicestring / objectnone、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 个)
字段类型说明
idstring本次 Chat Completion 的唯一标识。
objectstring对象类型,非流式响应通常为 chat.completion。
createdinteger响应创建时间,Unix 时间戳(秒)。
modelstring实际生成响应的模型 ID。
choicesarray<object>模型生成的候选结果;数量由请求参数 n 决定。
usageobjectToken 用量统计。部分网关或流式中间响应可能不返回此字段。
system_fingerprintstring服务端配置或模型版本指纹,可用于排查结果变化;部分模型或网关可能不返回。
choices[] 字段(4 个)
字段类型说明
indexinteger候选结果在 choices 数组中的索引。
messageobject助手消息。通常包含 role: "assistant" 和文本 content;工具调用时还可能包含 tool_calls。
logprobsobject / null请求启用 logprobs 时返回的 token 对数概率信息,否则为 null。
finish_reasonstring生成结束原因:stop、length、tool_calls 或 content_filter。流式中间事件尚未结束时可能为 null。
choices[].message 字段(6 个)

message 可能包含以下字段:

字段类型说明
rolestring消息角色,通常为 assistant。
contentstring / array文本或内容块数组;当响应仅包含工具调用时可能为空或由网关省略。
namestring发送者名称;部分模型响应中会返回。
tool_callsarray<object>模型请求调用的工具及其参数。仅在模型选择工具调用时返回。
tool_call_idstring工具调用 ID;工具消息用于对应此前的调用。
reasoning_contentstring支持推理模型时返回的推理内容。
usage 字段(5 个)
字段类型说明
prompt_tokensinteger输入消息使用的 token 数。
completion_tokensinteger输出内容使用的 token 数。支持推理的模型可能包含推理 token。
total_tokensintegerprompt_tokens 与 completion_tokens 的总和。
prompt_tokens_detailsobject输入 token 的细分统计,例如缓存 token;按模型和网关返回。
completion_tokens_detailsobject输出 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 的统计块。

results matching ""

    No results matching ""