Chat Completions
POST /v1/chat/completions 用於建立聊天補全,是大多數用戶端和 SDK 的預設介面。
請求參數
| 欄位 | 類型 | 必填 | 說明 |
|---|---|---|---|
model | string | 是 | 平台模型名稱 |
messages | array | 是 | 對話訊息清單 |
stream | boolean | 否 | 是否使用串流回應 |
temperature | number | 否 | 取樣溫度,是否生效取決於模型 |
top_p | number | 否 | nucleus sampling 參數 |
max_tokens | number | 否 | 最大輸出 Token,部分模型使用其他欄位 |
max_completion_tokens | number | 否 | 部分推理模型使用的最大完成 Token |
tools | array | 否 | 工具或函式定義 |
tool_choice | string/object | 否 | 工具選擇策略 |
response_format | object | 否 | JSON 或結構化輸出要求 |
Message 物件
| 欄位 | 說明 |
|---|---|
role | system、user、assistant、tool 等 |
content | 文字或多模態 content part 陣列 |
name | 可選名稱 |
tool_calls | 模型發起的工具呼叫 |
tool_call_id | 工具回傳訊息關聯的呼叫 ID |
回應
非串流回應通常包含 id、object、created、model、choices 和 usage。串流回應會按事件回傳增量 delta。
請求範例
bash
curl https://api.cloudaikit.com/v1/chat/completions \
-H "Authorization: Bearer sk-your-token" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "你好"}],
"temperature": 0.2
}'非串流回應中的文字通常位於 choices[0].message.content。使用工具呼叫時,應執行 choices[0].message.tool_calls 中的函式,再將結果作為 role: tool 的訊息傳回。
相容性差異
工具呼叫、結構化輸出、圖片輸入、音訊輸入、推理參數和快取計費欄位取決於模型與渠道。若欄位被忽略或報錯,請查看日誌中的實際渠道,並查詢對應模型能力。
