Chat Completions
OpenAI 的 Chat Completions 格式,也是所有文本模型都支持的接口。Claude、GPT、Gemini、DeepSeek 等模型的调用方式完全相同,只有 model 不同。
POST https://api.exoticrouter.com/v1/chat/completions
curl https://api.exoticrouter.com/v1/chat/completions \ -H "Authorization: Bearer $EXOTIC_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "anthropic/claude-sonnet-5-5", "max_tokens": 400, "messages": [ {"role": "system", "content": "You answer in one short paragraph."}, {"role": "user", "content": "Why is the sky blue?"} ] }'reply = client.chat.completions.create( model="anthropic/claude-sonnet-5-5", max_tokens=400, messages=[ {"role": "system", "content": "You answer in one short paragraph."}, {"role": "user", "content": "Why is the sky blue?"}, ],)print(reply.choices[0].message.content)const reply = await client.chat.completions.create({ model: "anthropic/claude-sonnet-5-5", max_tokens: 400, messages: [ { role: "system", content: "You answer in one short paragraph." }, { role: "user", content: "Why is the sky blue?" }, ],});console.log(reply.choices[0].message.content);本页的 Python 和 TypeScript 示例使用 OpenAI SDK,Base URL 设为 https://api.exoticrouter.com/v1。设置方法见 OpenAI SDK。
网关读取的字段
Section titled “网关读取的字段”| 字段 | 类型 | 说明 |
|---|---|---|
model |
string 必填 |
模型 ID。任意文本模型。 |
messages |
array 必填 |
对话内容,至少一条消息。每条消息的 role 为 system、developer、user、assistant 或 tool,content 可以是文本,也可以是内容块列表。 |
max_tokens |
integer |
回答的最大长度,以 token 计。max_completion_tokens 的作用相同,两者同时发送时以它为准。超过模型 max_output_tokens 的值会被调低到该上限。 |
temperature, top_p |
number |
原样传给模型。 |
stop |
`string | string[]` |
tools, tool_choice, parallel_tool_calls |
函数工具,采用 OpenAI 的格式。见工具调用。 | |
response_format |
object |
要求返回 JSON。见 JSON 输出。 |
reasoning_effort |
string |
推理模型的思考强度。见推理。 |
stream |
boolean |
边生成边返回。见流式输出。 |
stream_options.include_usage |
boolean |
在流的末尾加一个带 token 统计的数据块。 |
模型是否支持 tools、response_format 或 reasoning_effort,取决于模型本身。可以参考模型列表里的 capabilities。
{ "id": "chatcmpl-8Zr0mQvT3nXyLw2KpB5cDfGhJ7sUa", "object": "chat.completion", "created": 1796169600, "model": "anthropic/claude-sonnet-5-5", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Sunlight is scattered by the air, and blue light is scattered the most." }, "logprobs": null, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 27, "completion_tokens": 16, "total_tokens": 43 }}model是模型的完整 ID,与发送时的写法无关。id和created由网关生成。finish_reason的取值为stop、length、tool_calls或content_filter。- 当回答只有工具调用时,
message.content为null。模型输出的推理内容(如果有)在message.reasoning_content里。 usage.prompt_tokens是网关自己对提示词的计数,也是计费依据的数字。如果有推理 token,会出现在usage.completion_tokens_details.reasoning_tokens里。logprobs始终为null,没有system_fingerprint字段。
设置 "stream": true 后,回答以 server-sent events 的形式返回:每行以 data: 开头,后面是一个 JSON 数据块,最后以 data: [DONE] 结束。
data: {"id":"chatcmpl-8Zr0…","object":"chat.completion.chunk","created":1796169600,"model":"anthropic/claude-sonnet-5-5","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-8Zr0…","object":"chat.completion.chunk","created":1796169600,"model":"anthropic/claude-sonnet-5-5","choices":[{"index":0,"delta":{"content":"Sunlight"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-8Zr0…","object":"chat.completion.chunk","created":1796169600,"model":"anthropic/claude-sonnet-5-5","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}
data: [DONE]- 第一个数据块带有角色信息,之后的数据块带有
content、reasoning_content或tool_calls,最后一个数据块带有finish_reason。 - 只有在请求时才会返回 token 统计:设置
"stream_options": {"include_usage": true}后,会多出一个数据块,其中choices为空,并带有usage对象。 - 模型没有输出时,网关每 15 秒发送一行注释
: keep-alive。SSE 客户端会自动跳过它。 - 如果模型在流开始之后出错,此时状态码已经是 200。流会以一个包含
error对象的数据帧结束,后面不会再有[DONE]。
stream = client.chat.completions.create( model="anthropic/claude-sonnet-5-5", messages=[{"role": "user", "content": "Write a haiku about glass."}], 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, "tokens")const stream = await client.chat.completions.create({ model: "anthropic/claude-sonnet-5-5", messages: [{ role: "user", content: "Write a haiku about glass." }], stream: true, stream_options: { include_usage: true },});for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content ?? ""); if (chunk.usage) console.log("\n", chunk.usage.total_tokens, "tokens");}在 tools 里描述你的函数。当模型想调用其中一个时,返回的 finish_reason 为 "tool_calls",调用内容在 message.tool_calls 里;执行后,把每个结果作为 tool 消息连同调用的 ID 发回去。
tools = [{ "type": "function", "function": { "name": "get_weather", "description": "The weather in a city, right now.", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, },}]messages = [{"role": "user", "content": "Do I need an umbrella in Paris?"}]
first = client.chat.completions.create(model="anthropic/claude-sonnet-5-5", messages=messages, tools=tools)call = first.choices[0].message.tool_calls[0]
messages += [ first.choices[0].message, {"role": "tool", "tool_call_id": call.id, "content": '{"rain": true, "temp_c": 11}'},]final = client.chat.completions.create(model="anthropic/claude-sonnet-5-5", messages=messages, tools=tools)print(final.choices[0].message.content)const tools = [{ type: "function" as const, function: { name: "get_weather", description: "The weather in a city, right now.", parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"], }, },}];const messages: OpenAI.ChatCompletionMessageParam[] = [ { role: "user", content: "Do I need an umbrella in Paris?" },];
const first = await client.chat.completions.create({ model: "anthropic/claude-sonnet-5-5", messages, tools });const call = first.choices[0].message.tool_calls![0];
messages.push(first.choices[0].message, { role: "tool", tool_call_id: call.id, content: '{"rain": true, "temp_c": 11}',});const final = await client.chat.completions.create({ model: "anthropic/claude-sonnet-5-5", messages, tools });console.log(final.choices[0].message.content);调用的 id 以 call_ 开头,由网关生成。在流式输出中,一个调用的第一个数据块带有 id 和函数 name,之后的数据块会不断追加 function.arguments。请使用 capabilities.tools 为 true 的模型。
在提示词里使用图像
Section titled “在提示词里使用图像”把 content 写成内容块列表,就可以混合文本和图片。图像用 image_url 块表示,内容是链接或 data: URL。模型必须支持读图,可以看它的 capabilities.vision。
{ "model": "openai/gpt-6.2-astra", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "What is in this picture?" }, { "type": "image_url", "image_url": { "url": "https://example.com/cat.jpg" } } ] } ]}JSON 输出
Section titled “JSON 输出”response_format 会原样传给模型。{"type": "json_object"} 要求返回任意合法的 JSON;{"type": "json_schema", "json_schema": {…}} 要求返回符合 schema 的 JSON。并非所有模型都支持,使用前请先检查返回内容。
对于会先思考再回答的模型,reasoning_effort(low、medium、high)会传给模型。如果模型输出了推理过程,文本会放在 reasoning_content 里(消息和流式增量里都是这个字段),无论模型自己怎么命名。推理 token 按输出 token 计费。
格式不正确的请求会返回 400,代码为 invalid_request,消息里会指出出问题的字段。常见的有:
| 消息 | 原因 |
|---|---|
model is required. |
没有 model,或者为空 |
messages must be a non-empty array. |
没有消息 |
n other than 1 is not supported. |
n 大于等于 2 |
max_tokens must be a positive integer. |
为零、负数或不是数字 |
错误页面列出了所有状态码,包括模型暂时不可用或余额用完时的返回。