切换外观
对话补全
POST/chat/completions接口说明
使用 Bearer API Key 发起 OpenAI 兼容对话补全;支持图片、OCR/看图问答、内联文件、音频输入与音频回答、Reasoning、工具、结构化输出、Prompt Cache,以及普通 JSON 或 stream=true 的 SSE。
请求
方法:
POST鉴权:
Authorization: Bearer ${TOKENFACTORY_API_KEY}
请求头
| 名称 | 必填 | 值或类型 | 说明 |
|---|---|---|---|
Authorization | 是 | Bearer ${TOKENFACTORY_API_KEY} | Token Factory API Key |
Content-Type | 是 | application/json | 请求体媒体类型 |
路径参数:无
查询参数:无
请求字段
| 字段 | 类型 | 必填 | 说明 | 示例或约束 |
|---|---|---|---|---|
audio | object | 否 | 请求 audio 输出时的格式与音色设置。 | 示例:{"format":"wav","voice":"alloy"};可为 null |
audio.format | string | 父字段存在时必填 | 音频输出格式。 | 示例:"wav";可选值:"wav"、"aac"、"mp3"、"flac"、"opus"、"pcm16" |
audio.voice | string 或 object | 父字段存在时必填 | 官方预置音色名或自定义音色引用对象。 | 示例:"alloy";形式:string 或 object |
frequency_penalty | number | 否 | 词频惩罚,范围 -2 到 2。 | 最小值:-2;最大值:2;可为 null |
function_call | string 或 object | 否 | 已弃用;请改用 tool_choice。 | 形式:string 或 object |
function_call.name | string | 父字段存在且匹配该形式时必填 | 函数名。 | — |
functions | object[] | 否 | 已弃用的函数定义数组;请改用 tools。 | — |
functions[].description | string | 否 | 函数说明。 | — |
functions[].name | string | 父字段存在时必填 | 函数名。 | — |
functions[].parameters | object | 否 | 函数参数 JSON Schema。 | — |
functions[].strict | boolean | 否 | 是否严格遵守 JSON Schema。 | — |
logit_bias | object | 否 | Token ID 到偏置值的映射,值范围 -100 到 100。 | 可为 null |
logprobs | boolean | 否 | 是否返回输出 Token 的对数概率。 | 可为 null |
max_completion_tokens | integer (int32) | 否 | 最大输出 Token 数,包含可见与 Reasoning Token。 | 最小值:1;可为 null |
max_tokens | integer (int32) | 否 | 已弃用的最大输出 Token 数;请改用 max_completion_tokens。 | 最小值:1;可为 null |
messages | object[] | 是 | OpenAI 兼容消息数组;每项必须包含 role,content、tool_calls、function_call、tool_call_id 与 name 按角色条件提供。 | 示例:[{"content":"你好","role":"user"}] |
messages[].audio | object | 否 | assistant 对先前音频回答的引用。 | — |
messages[].audio.id | string | 父字段存在时必填 | 先前音频回答 ID。 | — |
messages[].content | string 或 object[] | 否 | 消息内容;可为字符串、text/image_url/input_audio/file 内容块数组,或工具调用 assistant 消息中的 null。 | 示例:"你好";可为 null;形式:string 或 object[] |
messages[].content[].file | object | 否 | 内联文件输入;当前不支持 Files 生命周期引用。 | — |
messages[].content[].file.file_data | string | 否 | Base64 data URL 文件内容。 | — |
messages[].content[].file.file_id | string | 否 | 官方 Files 引用 ID;Token Factory 当前未实现 Files 生命周期,发送即返回 400 invalid_request_error。 | — |
messages[].content[].file.filename | string | 否 | 内联文件名。 | — |
messages[].content[].image_url | object | 否 | 内联 data URL 或 HTTPS 图片 URL。支持图片理解、OCR 与看图问答。 | — |
messages[].content[].image_url.detail | string | 否 | 图片细节级别。 | 最短:1 字符 |
messages[].content[].image_url.url | string | 父字段存在且匹配该形式时必填 | 图片 URL 或 data URL。 | — |
messages[].content[].input_audio | object | 否 | Base64 内联输入音频。 | — |
messages[].content[].input_audio.data | string | 父字段存在且匹配该形式时必填 | Base64 音频数据。 | — |
messages[].content[].input_audio.format | string | 父字段存在且匹配该形式时必填 | 输入音频格式。 | 可选值:"wav"、"mp3" |
messages[].content[].prompt_cache_breakpoint | object | 否 | 显式 Prompt Cache 断点。 | 示例: |
messages[].content[].prompt_cache_breakpoint.mode | string | 父字段存在且匹配该形式时必填 | 固定为 explicit。 | 示例:"explicit";可选值:"explicit" |
messages[].content[].text | string | 否 | type=text 时的文本。 | 示例:"结合这些资料查询无锡天气" |
messages[].content[].type | string | 父字段存在且匹配该形式时必填 | 内容块类型。 | 示例:"text";可选值:"text"、"image_url"、"input_audio"、"file" |
messages[].function_call | object | 否 | 旧版 assistant 函数调用对象。 | 可为 null |
messages[].name | string | 否 | function 消息对应的非空函数名。 | — |
messages[].refusal | string | 否 | assistant 的拒答说明;允许为 null。 | 可为 null |
messages[].role | string | 是 | 消息角色;字段条件由 role 决定。 | 示例:"user";可选值:"developer"、"system"、"user"、"tool"、"function"、"assistant" |
messages[].tool_call_id | string | 否 | tool 消息对应的非空工具调用 ID。 | — |
messages[].tool_calls | object[] | 否 | assistant 发起的工具调用数组;用于满足 assistant 消息时必须非空。 | 最少:1 项 |
messages[].tool_calls[].custom | object | 否 | 自定义工具调用。 | — |
messages[].tool_calls[].custom.input | string | 否 | 自定义工具输入。 | — |
messages[].tool_calls[].custom.name | string | 否 | 自定义工具名。 | — |
messages[].tool_calls[].function | object | 否 | 函数调用。 | — |
messages[].tool_calls[].function.arguments | string | 父字段存在时必填 | 函数参数字符串;官方不保证内容一定是有效 JSON,也允许空字符串。 | — |
messages[].tool_calls[].function.name | string | 父字段存在时必填 | 函数名。 | — |
messages[].tool_calls[].id | string | 父字段存在时必填 | 工具调用 ID。 | — |
messages[].tool_calls[].type | string | 父字段存在时必填 | 工具调用类型。 | 最短:1 字符 |
metadata | object | 否 | 随请求保存的元数据;最多 16 个字符串键值对。 | 可为 null |
modalities | string[] | 否 | 输出模态数组;允许为 null,可选择 text 和 audio。 | 示例:["text","audio"];可为 null |
model | string | 是 | 要调用的模型 ID;先通过当前入口的模型列表确认可用值。 | 示例:"YOUR_MODEL_ID" |
moderation | object | 否 | 输入和输出的可选 Moderation 配置。 | 可为 null |
moderation.model | string | 父字段存在时必填 | Moderation 模型 ID。 | — |
moderation.policy | object | 否 | 输入和输出的 score/block 策略。 | 可为 null |
moderation.policy.input | object | 否 | 输入 Moderation 策略。 | 可为 null |
moderation.policy.input.mode | string | 父字段存在时必填 | 策略模式。 | 可选值:"score"、"block" |
moderation.policy.output | object | 否 | 输出 Moderation 策略。 | 可为 null |
moderation.policy.output.mode | string | 父字段存在时必填 | 策略模式。 | 可选值:"score"、"block" |
n | integer (int32) | 否 | 每个输入生成的候选数;至少为 1。 | 最小值:1;可为 null |
parallel_tool_calls | boolean | 否 | 是否允许模型并行发起工具调用。 | — |
prediction | object | 否 | 静态预测输出,用于加速已知内容的重新生成。 | 可为 null |
prediction.content | object | 父字段存在时必填 | 预测文本或内容块数组。 | — |
prediction.type | string | 父字段存在时必填 | 固定为 content。 | 可选值:"content" |
presence_penalty | number | 否 | 主题出现惩罚,范围 -2 到 2。 | 最小值:-2;最大值:2;可为 null |
prompt_cache_key | string | 否 | Prompt Cache 路由键;允许为 null。 | 示例:"example-cache-key";可为 null |
prompt_cache_options | object | 否 | Prompt Cache 选项;提供时必须是对象。 | 示例: |
prompt_cache_options.mode | string | 否 | 缓存断点模式。 | 示例:"explicit";最短:1 字符 |
prompt_cache_options.ttl | string | 否 | 缓存生存时间。 | 示例:"30m";最短:1 字符 |
prompt_cache_retention | string | 否 | 已弃用的 Prompt Cache 保留策略;允许为 null。 | 最短:1 字符;可为 null |
reasoning_effort | string | 否 | Reasoning 模型思考强度;允许为 null,实际可用值取决于模型。 | 示例:"high";最短:1 字符;可为 null |
response_format | object | 否 | Chat 结构化输出设置。 | 示例:{"json_schema":{"name":"weather_answer","schema":{"properties":{"answer":{"ty... |
response_format.json_schema | object | 否 | JSON Schema 的 name、description、schema 与 strict 设置。 | 示例:{"name":"weather_answer","schema":{"properties":{"answer":{"type":"string"}},... |
response_format.type | string | 父字段存在时必填 | 输出格式类型。 | 示例:"json_schema";可选值:"text"、"json_object"、"json_schema" |
safety_identifier | string | 否 | 非识别性的安全标识;建议使用哈希值,禁止直接发送 PII。 | 最长:64 字符;可为 null |
seed | integer (int32) | 否 | 尽力提供确定性采样的 Beta 随机种子。 | 可为 null |
service_tier | string | 否 | 服务处理等级;允许为 null。 | 最短:1 字符;可为 null |
stop | string 或 string[] | 否 | 一个或最多四个停止序列;最新 Reasoning 模型可能不支持。 | 可为 null;形式:string 或 string[] |
store | boolean | 否 | 是否存储输出;允许为 null。 | 可为 null |
stream | boolean | 否 | 是否使用 text/event-stream 流式返回;省略、null 或 false 时返回 JSON。 | 示例:false;可为 null |
stream_options | object | 否 | 仅在 stream=true 时使用的流式选项。 | 示例:{"include_usage":true};可为 null |
stream_options.include_obfuscation | boolean | 否 | 是否在流式 delta 中包含混淆填充字段。 | — |
stream_options.include_usage | boolean | 否 | true 时在 [DONE] 前返回 choices 为空的 usage-only chunk;Token Factory 始终从上游取得可信 Usage 用于计费,但不会在未请求时向客户端暴露该块。 | 示例:true |
temperature | number | 否 | 采样温度,范围 0 到 2。 | 最小值:0;最大值:2;可为 null |
tool_choice | string 或 object 或 object 或 object | 否 | 工具选择模式或指定工具对象。 | 示例:"auto";形式:string 或 object 或 object 或 object |
tool_choice.function | object | 父字段存在且匹配该形式时必填 | 被选择的函数。 | — |
tool_choice.function.name | string | 父字段存在且匹配该形式时必填 | 函数名。 | — |
tool_choice.type | string | 父字段存在且匹配该形式时必填 | 被选择的工具类型。;固定为 custom。;固定为 allowed_tools。 | 可选值:"function";可选值:"custom";可选值:"allowed_tools" |
tool_choice.custom | object | 父字段存在且匹配该形式时必填 | 被选择的自定义工具。 | — |
tool_choice.custom.name | string | 父字段存在且匹配该形式时必填 | 自定义工具名。 | — |
tool_choice.allowed_tools | object | 父字段存在且匹配该形式时必填 | 允许使用的工具及选择模式。 | — |
tool_choice.allowed_tools.mode | string | 父字段存在且匹配该形式时必填 | 允许自动选择或要求至少调用一个工具。 | 可选值:"auto"、"required" |
tool_choice.allowed_tools.tools | object[] | 父字段存在且匹配该形式时必填 | 允许模型调用的工具集合。 | — |
tools | object[] | 否 | 模型可调用的函数或自定义工具数组。 | 示例:[{"function":{"description":"查询指定城市天气","name":"lookup_weather","parameters":{... |
tools[].custom | object | 否 | 自定义工具定义。 | — |
tools[].function | object | 否 | 函数工具定义。 | 示例:{"description":"查询指定城市天气","name":"lookup_weather","parameters":{"properties":... |
tools[].function.description | string | 否 | 函数说明。 | 示例:"查询指定城市天气" |
tools[].function.name | string | 父字段存在时必填 | 函数名。 | 示例:"lookup_weather" |
tools[].function.parameters | object | 否 | 函数参数 JSON Schema。 | 示例:{"properties":{"city":{"type":"string"}},"required":["city"],"type":"object"} |
tools[].function.strict | boolean | 否 | 是否严格遵守 JSON Schema。 | 示例:true |
tools[].type | string | 父字段存在时必填 | 工具类型。 | 示例:"function";可选值:"function"、"custom" |
top_logprobs | integer (int32) | 否 | 每个位置返回的候选 Token 数,范围 0 到 20。 | 最小值:0;最大值:20;可为 null |
top_p | number | 否 | 核采样概率质量,范围 0 到 1。 | 最小值:0;最大值:1;可为 null |
user | string | 否 | 已弃用的终端用户标识;请改用 safety_identifier 与 prompt_cache_key。 | — |
verbosity | string | 否 | 文本回答详细程度;允许为 null。 | 最短:1 字符;可为 null |
web_search_options | object | 否 | Chat Web Search 配置。 | — |
web_search_options.search_context_size | string | 否 | 搜索上下文规模。 | 最短:1 字符 |
web_search_options.user_location | object | 否 | 近似用户位置。 | 可为 null |
web_search_options.user_location.approximate | object | 父字段存在时必填 | city、country、region 与 IANA timezone。 | — |
web_search_options.user_location.type | string | 父字段存在时必填 | 固定为 approximate。 | 可选值:"approximate" |
请求报文
媒体类型:application/json
json
{
"audio": {
"format": "wav",
"voice": "alloy"
},
"messages": [
{
"content": [
{
"prompt_cache_breakpoint": {
"mode": "explicit"
},
"text": "结合这些资料查询无锡天气",
"type": "text"
},
{
"image_url": {
"detail": "auto",
"url": "data:image/png;base64,AAAA"
},
"type": "image_url"
},
{
"file": {
"file_data": "data:application/pdf;base64,AAAA",
"filename": "example.pdf"
},
"type": "file"
},
{
"input_audio": {
"data": "AAAA",
"format": "wav"
},
"type": "input_audio"
}
],
"role": "user"
},
{
"role": "assistant",
"tool_calls": [
{
"function": {
"arguments": "{\"city\":\"无锡\"}",
"name": "lookup_weather"
},
"id": "call_example",
"type": "function"
}
]
},
{
"content": "晴,25℃",
"role": "tool",
"tool_call_id": "call_example"
}
],
"modalities": [
"text",
"audio"
],
"model": "YOUR_MODEL_ID",
"prompt_cache_key": "example-cache-key",
"prompt_cache_options": {
"mode": "explicit",
"ttl": "30m"
},
"reasoning_effort": "high",
"response_format": {
"json_schema": {
"name": "weather_answer",
"schema": {
"properties": {
"answer": {
"type": "string"
}
},
"required": [
"answer"
],
"type": "object"
},
"strict": true
},
"type": "json_schema"
},
"stream": true,
"stream_options": {
"include_usage": true
},
"tool_choice": "auto",
"tools": [
{
"function": {
"description": "查询指定城市天气",
"name": "lookup_weather",
"parameters": {
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
],
"type": "object"
},
"strict": true
},
"type": "function"
}
]
}响应
成功状态:200
响应头
| 名称 | 值或类型 | 说明 |
|---|---|---|
Content-Type | application/json 或 text/event-stream | 响应媒体类型 |
request-id | string | Token Factory 独立生成的平台请求编号;Anthropic 错误体的 request_id 与其相同。 |
retry-after | string | 可选重试等待提示:非负秒数或标准 HTTP 日期。重试前仍需确认请求可安全重复。 |
x-request-id | string | Token Factory 独立生成的平台请求编号,与 request-id 相同。 |
响应字段
OpenAI Chat Completion 成功响应;列出当前官方顶层字段,开放扩展仅用于向前兼容官方未来新增字段。 下表完整列出当前公开协议明确声明的字段;开放扩展只允许未声明的附加字段,不会放宽已声明字段的必填、类型或枚举约束。
| 字段 | 类型 | 必填 | 说明 | 示例或约束 |
|---|---|---|---|---|
choices | object[] | 是 | 非空候选输出数组;元素结构随模型能力变化。 | 示例:[{"finish_reason":"stop","index":0,"logprobs":null,"message":{"audio":{"data"...;最少:1 项 |
choices[].finish_reason | string | 是 | 生成结束原因;成功 JSON 响应中必须为非空字符串。 | 示例:"stop";最短:1 字符 |
choices[].index | integer (int32) | 是 | 候选结果序号。 | 示例:0 |
choices[].logprobs | object | 否 | 候选 Token 对数概率;官方允许整个字段省略或为 null。 | 示例:null;可为 null |
choices[].logprobs.content | object[] | 父字段存在时必填 | 消息正文 Token 对数概率;未请求时为 null。 | 可为 null |
choices[].logprobs.content[].bytes | integer (int32)[] | 父字段存在时必填 | 所选 Token 的 UTF-8 字节;无法表示时为 null。 | 可为 null |
choices[].logprobs.content[].logprob | number | 父字段存在时必填 | 所选 Token 对数概率。 | — |
choices[].logprobs.content[].token | string | 父字段存在时必填 | 所选 Token 文本。 | — |
choices[].logprobs.content[].top_logprobs | object[] | 父字段存在时必填 | 该位置返回的候选 Token 列表。 | — |
choices[].logprobs.content[].top_logprobs[].bytes | integer (int32)[] | 父字段存在时必填 | Token 的 UTF-8 字节;无法表示时为 null。 | 可为 null |
choices[].logprobs.content[].top_logprobs[].logprob | number | 父字段存在时必填 | Token 对数概率。 | — |
choices[].logprobs.content[].top_logprobs[].token | string | 父字段存在时必填 | Token 文本。 | — |
choices[].logprobs.refusal | object[] | 父字段存在时必填 | 拒答 Token 对数概率;未请求时为 null。 | 可为 null |
choices[].logprobs.refusal[].bytes | integer (int32)[] | 父字段存在时必填 | 所选 Token 的 UTF-8 字节;无法表示时为 null。 | 可为 null |
choices[].logprobs.refusal[].logprob | number | 父字段存在时必填 | 所选 Token 对数概率。 | — |
choices[].logprobs.refusal[].token | string | 父字段存在时必填 | 所选 Token 文本。 | — |
choices[].logprobs.refusal[].top_logprobs | object[] | 父字段存在时必填 | 该位置返回的候选 Token 列表。 | — |
choices[].logprobs.refusal[].top_logprobs[].bytes | integer (int32)[] | 父字段存在时必填 | Token 的 UTF-8 字节;无法表示时为 null。 | 可为 null |
choices[].logprobs.refusal[].top_logprobs[].logprob | number | 父字段存在时必填 | Token 对数概率。 | — |
choices[].logprobs.refusal[].top_logprobs[].token | string | 父字段存在时必填 | Token 文本。 | — |
choices[].message | object | 是 | OpenAI Chat 助手消息;可返回文本、拒答、工具调用或音频回答。 | 示例:{"audio":{"data":"AAAA","expires_at":1787835600,"id":"audio_example","transcr... |
choices[].message.annotations | object[] | 否 | 助手文本 URL 引用;官方可省略或为 null,提供时必须是数组。 | 可为 null |
choices[].message.annotations[].type | string | 父字段存在时必填 | 固定为 url_citation。 | 可选值:"url_citation" |
choices[].message.annotations[].url_citation | object | 父字段存在时必填 | Chat Web Search URL 引用位置。 | — |
choices[].message.annotations[].url_citation.end_index | integer (int32) | 父字段存在时必填 | 引用在文本中的结束字符下标。 | 最小值:0 |
choices[].message.annotations[].url_citation.start_index | integer (int32) | 父字段存在时必填 | 引用在文本中的起始字符下标。 | 最小值:0 |
choices[].message.annotations[].url_citation.title | string | 父字段存在时必填 | 引用页面标题。 | — |
choices[].message.annotations[].url_citation.url | string | 父字段存在时必填 | 引用页面 URL。 | — |
choices[].message.audio | object | 否 | Chat 音频回答。 | 示例:{"data":"AAAA","expires_at":1787835600,"id":"audio_example","transcript":"晴,2...;可为 null |
choices[].message.audio.data | string | 父字段存在时必填 | Base64 音频数据。 | 示例:"AAAA" |
choices[].message.audio.expires_at | integer (int64) | 父字段存在时必填 | 音频到期 Unix 秒时间戳。 | 示例:1787835600 |
choices[].message.audio.id | string | 父字段存在时必填 | 音频回答 ID。 | 示例:"audio_example" |
choices[].message.audio.transcript | string | 父字段存在时必填 | 音频文字稿。 | 示例:"晴,25℃" |
choices[].message.content | string | 是 | 助手文本内容;工具调用时可能为 null。 | 示例:"{"answer":"晴,25℃"}";可为 null |
choices[].message.function_call | object | 否 | 旧版函数调用对象。 | 可为 null |
choices[].message.function_call.arguments | string | 父字段存在时必填 | 函数参数字符串;官方不保证内容一定是有效 JSON,也允许空字符串。 | — |
choices[].message.function_call.name | string | 父字段存在时必填 | 函数名。 | — |
choices[].message.refusal | string | 是 | 模型拒答文本;没有拒答时可为 null。 | 示例:null;可为 null |
choices[].message.role | string | 是 | 固定为 assistant。 | 示例:"assistant";可选值:"assistant" |
choices[].message.tool_calls | object[] | 否 | 助手发起的函数或自定义工具调用数组;官方可省略。 | 最少:1 项 |
choices[].message.tool_calls[].custom | object | 否 | 自定义工具调用。 | — |
choices[].message.tool_calls[].custom.input | string | 否 | 自定义工具输入。 | — |
choices[].message.tool_calls[].custom.name | string | 否 | 自定义工具名。 | — |
choices[].message.tool_calls[].function | object | 否 | 函数调用。 | — |
choices[].message.tool_calls[].function.arguments | string | 父字段存在时必填 | 函数参数字符串;官方不保证内容一定是有效 JSON,也允许空字符串。 | — |
choices[].message.tool_calls[].function.name | string | 父字段存在时必填 | 函数名。 | — |
choices[].message.tool_calls[].id | string | 父字段存在时必填 | 工具调用 ID。 | — |
choices[].message.tool_calls[].type | string | 父字段存在时必填 | 工具调用类型。 | 最短:1 字符 |
created | integer (int64) | 是 | 响应创建时间的 Unix 秒时间戳。 | 示例:1787832000 |
id | string | 是 | 本次响应 ID。 | 示例:"chatcmpl_example" |
metadata | object | 否 | 随 Chat Completion 返回的元数据;允许为 null。 | 示例:null;可为 null |
model | string | 是 | 实际处理请求的模型 ID。 | 示例:"YOUR_MODEL_ID" |
moderation | object | 否 | 输入与输出 Moderation 结果;官方允许省略或为 null。 | 示例:null;可为 null |
moderation.input | object | 父字段存在时必填 | 输入内容的 Moderation 结果或错误。 | — |
moderation.output | object | 父字段存在时必填 | 输出内容的 Moderation 结果或错误。 | — |
object | string | 是 | 固定为 chat.completion。 | 示例:"chat.completion";可选值:"chat.completion" |
service_tier | string | 否 | 实际服务处理等级;允许为 null。 | 示例:"default";最短:1 字符;可为 null |
system_fingerprint | string | 否 | 已弃用的后端配置指纹;官方可省略或为 null。 | 示例:"fp_example";可为 null |
usage | object | 否 | 官方可省略;Token Factory 对可计费成功终态额外要求可信 Usage,缺失时失败关闭。 | 示例:{"completion_tokens":4,"prompt_tokens":8,"total_tokens":12};可为 null |
usage.completion_tokens | integer (int32) | 父字段存在时必填 | 生成结果使用的 Token 数。 | 示例:4;最小值:0 |
usage.completion_tokens_details | object | 否 | 输出 Token 分类明细;官方可省略。 | 可为 null |
usage.completion_tokens_details.accepted_prediction_tokens | integer (int32) | 否 | 该类别的 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.completion_tokens_details.audio_tokens | integer (int32) | 否 | 该类别的 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.completion_tokens_details.reasoning_tokens | integer (int32) | 否 | 该类别的 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.completion_tokens_details.rejected_prediction_tokens | integer (int32) | 否 | 该类别的 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.completion_tokens_details.text_tokens | integer (int32) | 否 | 该类别的 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.prompt_tokens | integer (int32) | 父字段存在时必填 | 输入使用的 Token 数。 | 示例:8;最小值:0 |
usage.prompt_tokens_details | object | 否 | 输入 Token 分类明细;官方可省略。 | 可为 null |
usage.prompt_tokens_details.audio_tokens | integer (int32) | 否 | 该类别的输入 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.prompt_tokens_details.cache_write_tokens | integer (int32) | 否 | 该类别的输入 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.prompt_tokens_details.cached_tokens | integer (int32) | 否 | 该类别的输入 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.prompt_tokens_details.image_tokens | integer (int32) | 否 | 该类别的输入 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.prompt_tokens_details.text_tokens | integer (int32) | 否 | 该类别的输入 Token 数;允许为 null。 | 最小值:0;可为 null |
usage.total_tokens | integer (int32) | 父字段存在时必填 | 输入与输出 Token 总数。 | 示例:12;最小值:0 |
返回报文
媒体类型:application/json
json
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"audio": {
"data": "AAAA",
"expires_at": 1787835600,
"id": "audio_example",
"transcript": "晴,25℃"
},
"content": "{\"answer\":\"晴,25℃\"}",
"refusal": null,
"role": "assistant"
}
}
],
"created": 1787832000,
"id": "chatcmpl_example",
"metadata": null,
"model": "YOUR_MODEL_ID",
"moderation": null,
"object": "chat.completion",
"service_tier": "default",
"system_fingerprint": "fp_example",
"usage": {
"completion_tokens": 4,
"prompt_tokens": 8,
"total_tokens": 12
}
}SSE 返回报文
媒体类型:text/event-stream
允许事件:data、[DONE]、error
协议终态:[DONE]、error
text
data: {"id":"chatcmpl_example","object":"chat.completion.chunk","created":1787832000,"model":"YOUR_MODEL_ID","choices":[{"index":0,"delta":{"role":"assistant","content":"{\"answer\":\"晴,25℃\"}","audio":{"id":"audio_example","data":"AAAA","expires_at":1787835600,"transcript":"晴,25℃"}},"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl_example","object":"chat.completion.chunk","created":1787832000,"model":"YOUR_MODEL_ID","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":null}
data: {"id":"chatcmpl_example","object":"chat.completion.chunk","created":1787832000,"model":"YOUR_MODEL_ID","choices":[],"usage":{"prompt_tokens":8,"completion_tokens":4,"total_tokens":12}}
data: [DONE]只有上方列出的协议终态才表示流正常结束;连接中断不代表请求未执行。若已经收到部分内容,不要无条件重放同一请求。
错误
| 适用完整地址 | 状态 | 错误码 | 常见触发 | 重试 | 建议动作 | 结果风险 |
|---|---|---|---|---|---|---|
https://api.tokenfactory.cn/v1/chat/completions | 401 | invalid_api_key | Authorization 缺失、Bearer 格式错误、Key 不可用或账户未实名 | 否 | 检查 Bearer API Key、Key 状态与实名状态 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 400 | model_not_found | 当前模型不可用 | 否 | 核对模型名称与请求地址;持续出现时联系支持 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 404 | model_not_found | 模型不存在、未上架或当前入口不可用 | 否 | 调用模型列表并改用当前入口可用模型 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 404 | MODEL_ROUTING_PROTOCOL_UNSUPPORTED | 当前模型不可用 | 否 | 核对模型名称与当前入口的模型列表;持续出现时联系支持 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 503 | MODEL_ROUTING_NOT_CONFIGURED | 当前模型服务不可用 | 视情况 | 稍后重试;持续出现时提供平台请求编号联系支持 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 503 | MODEL_ROUTING_POLICY_INVALID | 当前模型服务不可用 | 视情况 | 稍后重试;持续出现时提供平台请求编号联系支持 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 503 | MODEL_ROUTING_NO_AVAILABLE_ROUTE | 当前模型服务不可用 | 否 | 提供平台请求编号联系支持,确认服务恢复后再发送 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 429 | insufficient_balance | 账户可用余额不足 | 否 | 充值后重新发起请求 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 429 | balance_daily_limit_exceeded | 账户当天余额消费已达到自助上限 | 否 | 调整每日上限或等待次日恢复 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 413 | request_too_large | 请求体超过平台接收上限 | 否 | 缩小请求体后重试 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 400 | invalid_request_error | 请求参数或请求体缺字段、格式错误或不受支持 | 否 | 修正请求后重试 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 502 | upstream_error | 服务响应异常,请稍后重试 | 视情况 | 确认未收到有效结果后再决定是否重试 | 中到高 |
https://api.tokenfactory.cn/v1/chat/completions | 504 | upstream_timeout | 请求处理超时,请稍后重试 | 视情况 | 先确认是否已有部分输出,再决定是否重试 | 中到高 |
https://api.tokenfactory.cn/v1/chat/completions | 500 | internal_error | 平台内部出现未预期错误 | 视情况 | 确认未收到有效结果并记录请求时间后再决定是否重试 | 中到高 |
https://api.tokenfactory.cn/v1/chat/completions | 405 | method_not_allowed | 请求方法不是该公开地址允许的方法 | 否 | 改用文档列出的 HTTP 方法 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 415 | unsupported_media_type | Content-Type 与该公开地址要求的媒体类型不一致 | 否 | 按文档发送正确 Content-Type | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 403 | permission_denied | 当前 API Key 无权执行该请求 | 否 | 检查 API Key 权限与当前入口的模型可见性 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 409 | conflict_error | 请求与当前资源状态冲突 | 视情况 | 读取最新状态后修正请求或稍后重试 | 中到高 |
https://api.tokenfactory.cn/v1/chat/completions | 422 | unprocessable_entity | 请求结构正确但当前请求无法处理 | 否 | 按错误信息修正参数或输入内容 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 503 | service_unavailable | 服务暂不可用,请稍后重试 | 视情况 | 保留平台请求编号,稍后重试或联系支持 | 中到高 |
https://api.tokenfactory.cn/v1/chat/completions | 503 | service_busy | 模型服务繁忙,请稍后重试 | 视情况 | 参考 Retry-After 并限制重试次数;已收到输出时先确认是否可安全重复 | 中到高 |
https://api.tokenfactory.cn/v1/chat/completions | 400 | context_length_exceeded | 输入超过模型上下文限制 | 否 | 缩短输入或减少历史上下文后再发送 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 400 | missing_required_parameter | 缺少必填请求参数 | 否 | 按文档补齐错误指出的公开参数后再发送 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 400 | invalid_parameter | 请求参数的类型或格式不正确 | 否 | 修正错误指出的公开参数类型或格式后再发送 | 无 |
https://api.tokenfactory.cn/v1/chat/completions | 502 | upstream_usage_missing | 服务响应缺少可验证的用量信息 | 视情况 | 保留平台请求编号联系支持;确认可安全重复后再发送 | 中到高 |
错误返回报文
适用完整地址
https://api.tokenfactory.cn/v1/chat/completions
HTTP 400;Content-Type: application/json;响应头:request-id: string、retry-after: string、x-request-id: string
json
{
"error": {
"code": "model_not_found",
"message": "当前模型不可用",
"param": null,
"type": "invalid_request_error"
}
}适用完整地址:https://api.tokenfactory.cn/v1/chat/completions
供应商非 2xx 响应会归一化为当前公开协议的错误信封,并只保留安全响应头;502、504 或连接中断时,调用结果可能未知。
限制与计费
| 适用完整地址 | 限制或计费说明 |
|---|---|
https://api.tokenfactory.cn/v1/chat/completions | 请求成功后按通用余额计费;余额不足时调用失败。 受全局请求体上限约束。 模型上下文与输出限制依当前模型和上游事实而定。 支持文本、图片/OCR/看图问答、内联文件、音频输入与音频回答、Reasoning、Tools、Structured Output 和 Prompt Cache;模型参数支持由供应商判断,诊断结果不参与选路。 不支持 file_id/Files 生命周期、独立 Audio API、Realtime 或通过私有 OCR 接口冒充对话能力。 |
相关说明
请求与响应按当前 OpenAI 兼容能力开放;供应商扩展字段可能随所选模型变化。