# 對話補全 (Chat)

OpenAI 相容：`POST /v1/chat/completions`。統一串接主流大型語言模型。

* **路徑**: `https://www.tokenpad.ai/v1/chat/completions`
* **驗證**: `Authorization: Bearer sk-your_token_here`

多模態時請將 `content` 從字串改為陣列；各 part 的 `type` 請見下方範例。是否支援圖片/音訊/影片取決於所選模型。

---

## 1. 純文字

```bash
curl -X POST https://www.tokenpad.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "user", "content": "你好，請用一句話介紹你自己。"}
    ],
    "stream": false
  }'
```

---

## 2. 多模態：圖片 + 文字

`type: image_url`，`url` 支援 HTTP(S) 或 `data:image/...;base64,...`。

```bash
curl -X POST https://www.tokenpad.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": [
          { "type": "text", "text": "這張圖裡有什麼？簡要說明。" },
          {
            "type": "image_url",
            "image_url": { "url": "https://example.com/cat.jpg" }
          }
        ]
      }
    ]
  }'
```

多圖時請繼續追加多個 `image_url` part。

---

## 3. 多模態：音訊 + 文字

兩種常見寫法（依模型能力選擇）：

**A. OpenAI 風格 `input_audio`（base64）**

```bash
curl -X POST https://www.tokenpad.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-audio-preview",
    "messages": [
      {
        "role": "user",
        "content": [
          { "type": "text", "text": "這段音訊在說什麼？用中文總結。" },
          {
            "type": "input_audio",
            "input_audio": {
              "data": "<base64-audio>",
              "format": "wav"
            }
          }
        ]
      }
    ]
  }'
```

**B. URL 風格 `audio_url`（部分通道）**

```bash
curl -X POST https://www.tokenpad.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-audio-capable-model",
    "messages": [
      {
        "role": "user",
        "content": [
          { "type": "text", "text": "請轉錄並總結這段音訊。" },
          {
            "type": "audio_url",
            "audio_url": { "url": "https://example.com/sample.wav" }
          }
        ]
      }
    ]
  }'
```

> 文字轉語音（TTS）請使用獨立介面 `POST /v1/audio/speech`，不屬於聊天 `messages` 多模態。

---

## 4. 多模態：影片 + 文字（部分模型）

```bash
curl -X POST https://www.tokenpad.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-video-capable-model",
    "messages": [
      {
        "role": "user",
        "content": [
          { "type": "text", "text": "概括這段影片的主要內容。" },
          {
            "type": "video_url",
            "video_url": { "url": "https://example.com/demo.mp4" }
          }
        ]
      }
    ]
  }'
```

---

## 5. 常用參數

| 參數 | 型別 | 必填 | 說明 |
| :--- | :--- | :--- | :--- |
| `model` | string | 是 | 模型 ID |
| `messages` | array | 是 | `content`：字串或 `[text / image_url / input_audio / audio_url / video_url]` |
| `stream` | boolean | 否 | 預設 `false` |
| `temperature` | number | 否 | 0~2 |
| `max_tokens` | integer | 否 | 最大生成長度 |

SDK：官方 `openai`，`base_url="https://www.tokenpad.ai/v1"`，`api_key` 填寫本平台 Token。



### 5. 常見錯誤碼與問題排解

HTTP 狀態碼、錯誤主體格式及分情境排解已 **統一維護** 於專頁，避免與各呼叫範例重複導致不同步。

👉 **[閘道常見錯誤碼與排解](/docs/zh-TW/errors/error-codes)**

**速記**（詳情見專頁）：

| 碼 | 含義 |
| :---: | :--- |
| **400** | 參數無效 / 內容風控或版權攔截 |
| **402** | 帳戶餘額不足（**不是** 429） |
| **429** | 權杖 RPS/RPM 頻率限制，或低餘額在途任務過多 |
| **403** | 權杖/IP/模型權限（餘額見 402；內容攔截見 400） |
| **200 + `status: failed`** | 非同步影片等長任務的業務失敗（輪詢時常見） |

HTTP 回應碼與控制台呼叫日誌 `status_code` 一致，可依 `log_id` 追溯。

> **維護約定**：新增或調整錯誤說明時，請只更新專頁 `error-codes`；本附錄透過連結自動指向最新內容。
