ChatCompletions 格式
根據對話歷史建立模型回應。支援串流與非串流回應。相容 OpenAI Chat Completions API。
注意: 完整路徑為 POST /v1/chat/completions。OpenAI SDK 的 base_url 需填寫完整路徑(含 /v1)。
示例模型
下文均以 gpt-4(GPT-4)為例;更換其他模型時,請替換 model 參數,並參考控制台模型說明調整 size、quality 等欄位。
建立聊天補全
POST
https://token.easyapi.com/v1/chat/completions根據對話歷史建立模型回應,相容 OpenAI Chat Completions API,支援串流與非串流。
請求參數
| 名稱 | 位置 | 類型 | 必選 | 說明 |
|---|---|---|---|---|
Authorization | header | string | 是 | EasyAPI API Key。格式:Bearer `API_KEY` |
Content-Type | header | string | 是 | 请求体格式,固定 application/json |
請求體
| 名稱 | 類型 | 必選 | 說明 |
|---|---|---|---|
model | string | 是 | 模型 ID |
messages | array<object> | 是 | 對話訊息列表 |
temperature | number | 否 | 取樣溫度,預設 1,範圍 0–2 |
top_p | number | 否 | 核取樣參數,預設 1,範圍 0–1 |
n | integer | 否 | 生成數量,預設 1,範圍 ≥ 1 |
stream | boolean | 否 | 是否串流回應,預設 false |
stream_options | object | 否 | 串流回應附加選項 |
stop | string | array<string> | 否 | 停止序列,字串或字串陣列 |
max_tokens | integer | 否 | 最大生成 Token 數 |
max_completion_tokens | integer | 否 | 最大補全 Token 數 |
presence_penalty | number | 否 | 存在懲罰,預設 0,範圍 -2–2 |
frequency_penalty | number | 否 | 頻率懲罰,預設 0,範圍 -2–2 |
logit_bias | object | 否 | 調整指定 token 出現機率 |
user | string | 否 | 終端使用者識別,便於濫用監測 |
tools | array<object> | 否 | 可呼叫的工具定義列表 |
tool_choice | string | object | 否 | 工具選擇策略,字串或物件 |
response_format | object | 否 | 回應格式,如 JSON Schema |
seed | integer | 否 | 隨機種子,用於盡量重現結果 |
reasoning_effort | string | 否 | medium |
modalities | array<string> | 否 | 輸出模態,如 text、audio |
audio | object | 否 | 音訊輸出相關設定 |
示例
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
],
"temperature": 0.7,
"stream": false
}響應示例
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1718452800,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 12,
"total_tokens": 32,
"prompt_tokens_details": {
"cached_tokens": 0,
"text_tokens": 20,
"audio_tokens": 0,
"image_tokens": 0
},
"completion_tokens_details": {
"text_tokens": 12,
"audio_tokens": 0,
"reasoning_tokens": 0
}
},
"system_fingerprint": "fp_abc123"
}