Skip to content

Chat Completions ​

根據對話歷史建立模型響應,支援流式與非流式。採用業界通用的 Chat Completions 協議格式,主流 SDK 即插即用。

POST https://router.accels.tech/v1/chat/completions

請求頭 ​

Header必填說明
Authorization✓Bearer sk-...
Content-Type✓application/json

請求體引數 ​

引數型別必填說明
modelstring✓模型 ID,如 gpt-4o、claude-sonnet-4-5、gemini-2.5-pro、deepseek-chat 等
messagesarray✓對話訊息列表,詳見下文
temperaturenumber取樣溫度,0~2,預設 1
top_pnumber核取樣,0~1
ninteger生成數量,預設 1
streambooleantrue 時啟用 SSE 流式
stream_optionsobject例如 {"include_usage": true}
stopstring | string[]停止序列
max_tokensinteger最大輸出 token 數
max_completion_tokensintegerOpenAI o-系列推理模型使用
presence_penaltynumber-2~2
frequency_penaltynumber-2~2
logit_biasobjecttoken 偏置
userstring業務方使用者標識
toolsarrayFunction calling 工具定義
tool_choicestring | object"auto"、"none"、"required" 或指定函式
response_formatobject例如 {"type":"json_object"} 或 JSON Schema
seedinteger確定性取樣種子
reasoning_effortstring"low" | "medium" | "high"(推理模型)
modalitiesarray多模態輸出,如 ["text","audio"]
audioobject音訊輸出引數
top_kintegerTop-K 取樣(取決於模型)
logprobsboolean返回 token 對數機率
top_logprobsinteger0–20;返回對數機率的 top token 數(需 logprobs: true)
parallel_tool_callsboolean允許並行工具呼叫;預設 true
service_tierstring如 "auto"、"default"
storeboolean持久化本次補全(支援的模型)
metadataobject附加到請求的鍵值標籤
predictionobject預測輸出,用於降低延遲
verbositystring輸出詳細程度(GPT-5 系列)
prompt_cache_keystring提示快取路由提示

廠商專屬引數透傳。 推理/思考類(reasoning、thinking、enable_thinking、thinking_budget、reasoning_effort)與聯網搜尋類(web_search_options、enable_search、search_mode、search_recency_filter)引數,在所選模型支援時會原樣轉發到上游。

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 | tool
  • content:字串 或 多模態陣列(支援 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://router.accels.tech/v1/chat/completions \
  -H "Authorization: Bearer $ACCELSROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role":"user","content":"用一句話介紹 AccelsRouter"}],
    "temperature": 0.7
  }'

Python(流式 + 工具) ​

python
from openai import OpenAI
client = OpenAI(api_key="sk-...", base_url="https://router.accels.tech/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
OpenAIgpt-4o、gpt-4o-mini、gpt-5、o3-mini
Anthropicclaude-sonnet-4-5、claude-opus-4-1、claude-haiku-4-5
Googlegemini-2.5-pro、gemini-2.5-flash
DeepSeekdeepseek-chat、deepseek-reasoner
Qwenqwen-max、qwen-plus、qwen3-coder
Doubaodoubao-1-5-pro-256k、doubao-seed-1-6

完整列表見 https://router.accels.tech/pricing。

錯誤 ​

常見錯誤見 錯誤碼。