Chat Completions
根据对话历史创建模型响应,支持流式与非流式。完全兼容 OpenAI Chat Completions API。
POST https://api.wrouter.io/v1/chat/completions请求头
| Header | 必填 | 说明 |
|---|---|---|
Authorization | ✓ | Bearer sk-... |
Content-Type | ✓ | application/json |
请求体参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | ✓ | 模型 ID,如 gpt-4o、claude-sonnet-4-5、gemini-2.5-pro、deepseek-chat 等 |
messages | array | ✓ | 对话消息列表,详见下文 |
temperature | number | 采样温度,0~2,默认 1 | |
top_p | number | 核采样,0~1 | |
n | integer | 生成数量,默认 1 | |
stream | boolean | true 时启用 SSE 流式 | |
stream_options | object | 例如 {"include_usage": true} | |
stop | string | string[] | 停止序列 | |
max_tokens | integer | 最大输出 token 数 | |
max_completion_tokens | integer | OpenAI o-系列推理模型使用 | |
presence_penalty | number | -2~2 | |
frequency_penalty | number | -2~2 | |
logit_bias | object | token 偏置 | |
user | string | 业务方用户标识 | |
tools | array | Function calling 工具定义 | |
tool_choice | string | object | "auto"、"none"、"required" 或指定函数 | |
response_format | object | 例如 {"type":"json_object"} 或 JSON Schema | |
seed | integer | 确定性采样种子 | |
reasoning_effort | string | "low" | "medium" | "high"(推理模型) | |
modalities | array | 多模态输出,如 ["text","audio"] | |
audio | object | 音频输出参数 |
messages 结构
json
[
{"role": "system", "content": "你是一个有帮助的助手。"},
{"role": "user", "content": "你好"},
{"role": "assistant", "content": "你好!有什么可以帮你?"},
{"role": "user", "content": [
{"type": "text", "text": "描述这张图"},
{"type": "image_url", "image_url": {"url": "https://..."}}
]}
]role:system|user|assistant|toolcontent:字符串 或 多模态数组(支持text/image_url/input_audio)
响应(非流式)
json
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1718000000,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "你好!"},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 6,
"total_tokens": 18
}
}响应(流式 SSE)
设置 stream: true 后,响应为 text/event-stream,每个事件:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"你"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","choices":[{"index":0,"delta":{"content":"好"}}]}
data: [DONE]设置 stream_options.include_usage = true 时,最后一个非 [DONE] chunk 会带 usage 字段。
示例
curl
bash
curl https://api.wrouter.io/v1/chat/completions \
-H "Authorization: Bearer $WROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role":"user","content":"用一句话介绍 wrouter"}],
"temperature": 0.7
}'Python(流式 + 工具)
python
from openai import OpenAI
client = OpenAI(api_key="sk-...", base_url="https://api.wrouter.io/v1")
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}]
stream = client.chat.completions.create(
model="claude-sonnet-4-5",
messages=[{"role": "user", "content": "上海现在天气?"}],
tools=tools,
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta
if delta.content:
print(delta.content, end="", flush=True)调用 Claude / Gemini
只需更换 model ID:
| 模型族 | 示例 ID |
|---|---|
| OpenAI | gpt-4o、gpt-4o-mini、gpt-5、o3-mini |
| Anthropic | claude-sonnet-4-5、claude-opus-4-1、claude-haiku-4-5 |
gemini-2.5-pro、gemini-2.5-flash | |
| DeepSeek | deepseek-chat、deepseek-reasoner |
| Qwen | qwen-max、qwen-plus、qwen3-coder |
| Doubao | doubao-1-5-pro-256k、doubao-seed-1-6 |
完整列表见 https://wrouter.io/models。
错误
常见错误见 错误码。