Chat Completions
Chat Completions 是最常用的對話接口,適合普通聊天、編程助手、摘要、分類、工具調用和多輪對話。
Endpoint
Section titled “Endpoint”POST /v1/chat/completions完整地址:
https://www.routeapi.ai/v1/chat/completionscurl https://www.routeapi.ai/v1/chat/completions \ -H "Authorization: Bearer $ROUTEAPI_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o", "messages": [ { "role": "user", "content": "請用一句話介紹 RouteAPI" } ] }'| 字段 | 類型 | 必填 | 說明 |
|---|---|---|---|
model | string | 是 | 模型 ID,必須是當前賬戶可用模型 |
messages | array | 是 | 對話消息列表 |
stream | boolean | 否 | 是否使用 SSE 流式輸出 |
temperature | number | 否 | 採樣溫度 |
top_p | number | 否 | nucleus sampling 參數 |
max_tokens | number | 否 | 最大輸出 token 數 |
tools | array | 否 | 工具定義 |
tool_choice | string/object | 否 | 工具選擇策略 |
response_format | object | 否 | JSON mode 或 JSON Schema |
stream_options | object | 否 | 流式輸出附加選項 |
messages 格式
Section titled “messages 格式”[ { "role": "system", "content": "你是一個嚴謹的技術助手。" }, { "role": "user", "content": "解釋什麼是 API 網關。" }]常見 role:
| Role | 說明 |
|---|---|
system | 系統提示詞 |
user | 用戶輸入 |
assistant | 模型歷史回覆 |
tool | 工具調用結果 |
{ "id": "chatcmpl_xxx", "object": "chat.completion", "created": 1730000000, "model": "gpt-4o", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "RouteAPI 是一個統一管理多家 AI 模型供應商的 API 網關。" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 24, "completion_tokens": 18, "total_tokens": 42 }}- 同一個參數在不同模型服務中的支持程度可能不同。
- 如果請求包含
tools、response_format、stream_options等可選參數,請先確認所選模型支持對應能力。 - 明確傳入
0或false的可選參數應被視為用戶顯式設置。