Chat Completions
根據對話歷史建立模型響應,支援流式與非流式。採用業界通用的 Chat Completions 協議格式,主流 SDK 即插即用。
POST https://wrouter.ai/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://wrouter.ai/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://wrouter.ai/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.ai/models。
錯誤
常見錯誤見 錯誤碼。