聊天補全
POST /v1/chat/completions 請求與回應。
聊天補全
請求
POST https://apimaster.ai/v1/chat/completions
標頭
| 標頭 | 必要 | 備註 |
|---|---|---|
Authorization |
是 | Bearer YOUR_API_KEY |
Content-Type |
是 | application/json |
請求主體(通用欄位)
| 欄位 | 類型 | 備註 |
|---|---|---|
model |
字串 | 例如 claude-sonnet-4-6 |
messages |
陣列 | {role, content} |
max_tokens |
數字 | 最大補全 token 數 |
temperature |
數字 | 可選 |
stream |
布林值 | SSE 串流 |
範例
curl -s "https://apimaster.ai/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.5",
"messages": [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Say hi in one word."}
],
"max_tokens": 64
}'
非串流回應
{
"id": "chatcmpl-...",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "..." },
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 5,
"total_tokens": 15
}
}
串流
當 "stream": true 時,主體為 text/event-stream;每行格式為 data: {json},直到 data: [DONE]。
狀態碼
| 代碼 | 意義 |
|---|---|
| 400 | 參數錯誤 |
| 401 | 認證失敗 |
| 402 / 403 | 配額或權限問題 |
| 429 | 請求速率限制 |
| 502 / 503 | 上游服務錯誤 — 請重試 |