API ReferenceText APIs

OpenAI Chat API 兼容接口

本平臺對外提供兼容 OpenAI Chat Completions API 的聊天接口。

本文檔面向客戶端調用方,重點說明公開請求路徑、認證方式、請求參數和示例寫法。請求體字段名與 JSON 結構儘量保持與 OpenAI Chat Completions API 一致。

說明:

  • 接口路徑:POST /v1/chat/completions
  • 能力類型:聊天 / 文本生成
  • 返回方式:
    • stream = false 或不傳:返回標準 JSON
    • stream = true:返回 text/event-stream SSE 流
  • 協議風格:請求體保持 OpenAI Chat Completions 原生結構,不額外包一層 input

當前範圍:

  • 僅覆蓋 POST /v1/chat/completions
  • POST /v1/responses 已支持,但不在本文展開,詳見:/docs/api-reference/text/openai-response-api
  • 不覆蓋 embeddings、images、audio 等其他 OpenAI 接口

1. 接口概覽

  • 請求方法:POST
  • 請求路徑:/v1/chat/completions
  • Content-Type:application/json
  • 響應類型:
    • 非流式:application/json
    • 流式:text/event-stream

2. 認證與請求頭

請求頭示例:

Authorization: Bearer YOUR_REACH_API_KEY
Content-Type: application/json

其中 Authorizationx-api-key 至少傳一個。

請求頭說明:

請求頭必填說明
Authorization平臺 API Key,格式:Bearer sk-xxxxxx
x-api-key也可直接通過該請求頭傳平臺 API Key
Content-Type固定爲 application/json

3. 請求體

請求體保持 OpenAI Chat Completions 原生對象結構:

{
  "model": "YOUR_MODEL_ID",
  "messages": [
    {
      "role": "user",
      "content": "請用一句話介紹你自己。"
    }
  ],
  "temperature": 0.2,
  "stream": false
}

3.1 頂層參數說明

字段類型必填說明
modelstring模型 ID
messagesarray<object>對話消息列表,至少一項
temperaturenumber採樣溫度
top_pnumbernucleus sampling 參數
ninteger候選結果數
max_tokensinteger最大輸出 token 數
max_completion_tokensinteger部分模型支持的新輸出 token 限制字段
stopstringarray<string>停止序列
streamboolean是否以 SSE 流式返回
stream_optionsobject流式附加配置,例如 include_usage
toolsarray<object>工具定義
tool_choicestring 或 object工具調用策略
parallel_tool_callsboolean是否允許並行工具調用
response_formatobject結構化輸出配置
presence_penaltynumber話題新穎性懲罰
frequency_penaltynumber重複懲罰
logit_biasobjecttoken bias 配置
userstring終端用戶標識

3.2 messages 參數說明

每條消息結構如下:

字段類型必填說明
rolestring角色,常用值:systemuserassistanttool
contentstringarray<object>消息內容,可傳文本或內容塊數組
namestring可選角色名
tool_call_idstringrole = tool 時用於關聯工具調用

文本形式示例:

{
  "role": "user",
  "content": "寫一段 50 字以內的產品介紹。"
}

內容塊形式示例:

{
  "role": "user",
  "content": [
    {
      "type": "text",
      "text": "請描述這張圖片"
    },
    {
      "type": "image_url",
      "image_url": {
        "url": "https://cdn.example.com/demo.png"
      }
    }
  ]
}

4. 請求示例

4.1 標準非流式請求

curl -X POST "https://direct.reachapi.ai/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_REACH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "YOUR_MODEL_ID",
    "messages": [
      {
        "role": "user",
        "content": "請用一句話介紹 reach-api。"
      }
    ],
    "temperature": 0.2
  }'

4.2 流式請求

curl -X POST "https://direct.reachapi.ai/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_REACH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "YOUR_MODEL_ID",
    "messages": [
      {
        "role": "user",
        "content": "請分三行介紹今天的工作重點。"
      }
    ],
    "stream": true,
    "stream_options": {
      "include_usage": true
    }
  }'

4.3 帶工具定義的請求

curl -X POST "https://direct.reachapi.ai/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_REACH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "YOUR_MODEL_ID",
    "messages": [
      {
        "role": "user",
        "content": "幫我查一下上海天氣"
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_weather",
          "description": "查詢指定城市天氣",
          "parameters": {
            "type": "object",
            "properties": {
              "city": {
                "type": "string"
              }
            },
            "required": ["city"]
          }
        }
      }
    ],
    "tool_choice": "auto"
  }'

5. 成功響應

非流式響應示例:

{
  "id": "chatcmpl_123",
  "object": "chat.completion",
  "created": 1710000000,
  "model": "YOUR_MODEL_ID",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好,我是一個由模型驅動的助手。"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 18,
    "total_tokens": 30
  }
}

6. 流式響應

stream = true 時,返回 OpenAI 風格 SSE:

data: {"id":"chatcmpl_123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}

data: {"id":"chatcmpl_123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"}}]}

data: [DONE]

7. 錯誤響應

OpenAI 風格錯誤示例:

{
  "error": {
    "message": "Field `messages` is required",
    "type": "invalid_request_error",
    "param": null,
    "code": "MISSING_REQUIRED_FIELD"
  }
}

常見場景:

  • 請求體缺失 model
  • 請求體缺失 messages
  • 請求 JSON 非法
  • 鑑權失敗

8. 兼容說明

  • 本文檔列出的是常用字段和示例寫法,字段名與結構以 OpenAI Chat Completions API 爲準
  • 本接口僅覆蓋 Chat Completions 路徑,不等同於 OpenAI 全部接口能力
  • 若使用實驗性或較新的官方字段,請結合實際可用能力驗證

9. 支持模型與參考價格

以下價格整理自統一計費文檔,適用於當前已整理的 GPT 文本模型。價格覈對日期爲 2026-04-17,詳細口徑以統一計費文檔 gpt.md 爲準。

模型InputCached inputOutput
gpt-5.4$2.50 / 1M tokens$0.25 / 1M tokens$15.00 / 1M tokens
gpt-5.3-codex$1.75 / 1M tokens$0.175 / 1M tokens$14.00 / 1M tokens
gpt-5.2$1.75 / 1M tokens$0.175 / 1M tokens$14.00 / 1M tokens
gpt-5.1-codex$1.25 / 1M tokens$0.125 / 1M tokens$10.00 / 1M tokens
gpt-5.1$1.25 / 1M tokens$0.125 / 1M tokens$10.00 / 1M tokens
gpt-5$1.25 / 1M tokens$0.125 / 1M tokens$10.00 / 1M tokens
gpt-5.4-mini$0.75 / 1M tokens$0.075 / 1M tokens$4.50 / 1M tokens
gpt-5-mini$0.25 / 1M tokens$0.025 / 1M tokens$2.00 / 1M tokens

說明:

  • Cached input 對應命中緩存的輸入 token 單價
  • 若模型列表後續有增刪,請以控制檯或產品配置中的實際上線模型爲準

10. 參考資料

On this page