推理對話 (Reasoning Models)

GPT-5.4 Pro / GPT-5 Pro 等推理模型, 適用於數學 / 邏輯 / 程式碼場景

POST /v1/chat/completions

## 推理對話 (Reasoning Models) GPT-5.4 Pro 是 OpenAI 新一代**推理模型** (Reasoning Model), 內建 chain-of-thought 推理鏈, 在複雜場景下表現遠超傳統對話模型: - 複雜數學推導 / 證明 - 多步驟邏輯推理 - 程式碼生成 / 除錯 / 重構 - 長上下文深度分析 (支援 1M token context) ### 支援的推理模型 | Model | Context Window | Output Limit | |---|---|---| | `gpt-5.4-pro` | 1M tokens | 128K tokens | | `gpt-5-pro` | 200K tokens | 100K tokens | ### 呼叫方式 跟普通 chat completions 完全一致, 使用 OpenAI 標準 SDK 即可, 無需安裝額外依賴: ```python from openai import OpenAI client = OpenAI(api_key="sk-xxx", base_url="https://api.router.ai/v1") resp = client.chat.completions.create( model="gpt-5.4-pro", messages=[{"role": "user", "content": "Solve: x^2 - 5x + 6 = 0"}], max_completion_tokens=500, ) print(resp.choices[0].message.content) print("Reasoning tokens:", resp.usage.completion_tokens_details.reasoning_tokens) ``` ### 協議差異 (跟普通 chat model 的區別) - 必須使用 `max_completion_tokens` 欄位 (不是 `max_tokens`); 該欄位包含推理過程消耗的 token + 可見輸出 token 之和 - 不支援自定義 `temperature` (推理模型行為固定, 傳入會被忽略) - 不支援流式呼叫 (`stream: true`) — 請使用同步呼叫, 等推理完成後一次性返回 - 響應 `usage.completion_tokens_details.reasoning_tokens` 欄位返回推理過程 token 計數 (透明化) - 可選參數 `reasoning_effort`: `"low"` / `"medium"` (預設) / `"high"`, 控制推理深度 (深度越高耗時越長) ### 適用場景建議 | 場景 | 推薦模型 | |---|---| | 簡單對話 / 閒聊 | `gpt-4o` / `gpt-5.4` 普通模型 (更快、更便宜) | | 數學題 / 演算法 / 推理 | `gpt-5.4-pro` | | 長上下文分析 (程式碼庫 review / 論文閱讀) | `gpt-5.4-pro` (1M context) | | 工具呼叫 (function calling) | `gpt-5.4-pro` (原生支援) | > 推理模型的響應延遲通常 10-30 秒, 比普通 chat 模型 (1-3 秒) 顯著更長. 請設定合理的客戶端超時 (建議 ≥60 秒).

请求体

modelstringrequired推理模型 ID, e.g. `gpt-5.4-pro` / `gpt-5-pro`
messagesarrayrequired對話訊息陣列, 每個元素含 `role` (system/user/assistant) + `content` (string 或多模態陣列)
max_completion_tokensinteger輸出 token 上限 (含推理過程 token + 可見輸出 token). 推理模型用此欄位, **不要用 max_tokens** (已棄用)
reasoning_effortstring推理深度: `low` / `medium` (預設) / `high`. 深度越高推理更細緻但耗時更長
toolsarray工具呼叫定義, 跟 OpenAI function calling 規範一致
tool_choicestring | object工具呼叫策略: `auto` / `none` / `required` / `{type:'function',function:{name:'X'}}`
streamboolean**當前必須為 false**. 推理模型流式呼叫暫不支援, 設 true 會返 400
userstring終端使用者標識, 跟 OpenAI 一致, 用於審計 / 濫用追溯

响应

调用示例

curl -X POST https://api.router.ai/v1/chat/completions \
  -H "Authorization: Bearer $YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-pro",
    "messages": [
      {"role": "user", "content": "Solve: x^2 - 5x + 6 = 0"}
    ],
    "max_completion_tokens": 500,
    "reasoning_effort": "medium"
  }'

API 文件