TokenLab

文字

建立聊天補全

為聊天訊息建立補全

POST
/v1/chat/completions

請求本文

選用參數的支援情況、取值與預設值取決於所選模型。設定取樣、推理或工具選項前,請查看其模型詳情。

modelstring必填

要使用的模型 ID。可用選項請參閱 Models。

messagesarray必填

組成對話的一系列訊息。

每個訊息物件包含:

  • role (string): system, developer, user, assistant, tool, function
  • content (string | array | null): 訊息內容

包含 tool_calls 的 assistant 訊息可以省略 content,或將其設為 null。

當 content 為陣列時,TokenLab 支援相容模型的結構化多模態區塊:

  • text: { "type": "text", "text": "..." }
  • 圖片: { "type": "image_url", "image_url": { "url": "https://..." } }
  • 影片: { "type": "video_url", "video_url": { "url": "https://..." } }
  • 音訊: { "type": "audio_url", "audio_url": { "url": "https://..." } }

多模態輸入請使用可公開存取的 https URL。支援的媒體類型取決於所選模型。

temperaturenumber

取樣溫度。是否支援、允許的取值與預設值取決於所選模型;省略此欄位即可使用模型預設值。

max_tokensinteger

要生成的最大 token 數量。

streamboolean預設值: false

若為 true,部分訊息差異將會以 SSE 事件發送。

stream_optionsobject

串流選項。設定 include_usage: true 以在串流分片中接收 token 使用資訊。

top_pnumber

Nucleus 取樣參數。建議調整此參數或 temperature,而非兩者同時調整。

frequency_penaltynumber

數值介於 -2.0 到 2.0。正值會懲罰重複出現的 token。

presence_penaltynumber

數值介於 -2.0 到 2.0。正值會懲罰已出現在文字中的 token。

stopstring | array

停止序列或序列清單。是否支援及序列數量限制取決於所選模型。

toolsarray

模型可能會呼叫的一組工具(函式呼叫)。

tool_choicestring | object

控制模型如何使用工具。選項:auto、none、required,或指定的工具物件。

parallel_tool_callsboolean

在所選模型支援時,允許助理在同一輪中發出多個工具呼叫。

max_completion_tokensinteger

補全的最大 token 數量。為 max_tokens 的替代方案,對於較新的具推理能力的模型族群較有用。

reasoning_effortstring

支援推理的模型使用的推理強度,允許的取值取決於所選模型。

seedinteger

供支援此功能的模型使用的取樣種子,不保證產生完全相同的輸出。

ninteger

要生成的補全數量(1-128)。

logprobsboolean

是否回傳對數機率(log probabilities)。

top_logprobsinteger

要回傳的前 N 個對數機率(0-20)。需要 logprobs: true。

top_kinteger

供支援此功能的模型使用的 Top-K 取樣參數。

response_formatobject

回應格式。JSON 模式使用 {"type": "json_object"},JSON Schema 使用 {"type": "json_schema", "json_schema": {...}}。支援情況取決於所選模型。

logit_biasobject

修改指定 token 出現機率的偏好。將 token ID(以字串)對映到 -100 到 100 的偏差值。

userstring

代表終端使用者的唯一識別碼,用於濫用監控。

回應

idstring

此補全的唯一識別碼。

objectstring

永遠為 chat.completion。

createdinteger

補全建立的 Unix 時間戳記。

modelstring

用於補全的模型。

choicesarray

補全選項清單。

每個選項包含:

  • index (integer): 選項的索引
  • message (object): 生成的訊息
  • finish_reason (string): 模型停止的原因,例如 stop、length 或 tool_calls
usageobject

token 使用統計。

  • prompt_tokens (integer): prompt 中的 token 數
  • completion_tokens (integer): 補全中的 token 數
  • total_tokens (integer): 使用的總 token 數

請求

curl -X POST "https://api.tokenlab.sh/v1/chat/completions" \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.6-terra",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello!"}
    ],
    "max_tokens": 1000
  }'

多模態範例

{
  "model": "gemini-2.5-pro",
  "messages": [
    {
      "role": "user",
      "content": [
        { "type": "text", "text": "Describe this video briefly." },
        { "type": "video_url", "video_url": { "url": "https://example.com/demo.mp4" } }
      ]
    }
  ],
  "max_tokens": 64
}

回應

Response
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1706000000,
  "model": "gpt-5.6-terra",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 20,
    "completion_tokens": 9,
    "total_tokens": 29
  }
}

授權

BearerAuth
AuthorizationBearer <token>

API Key 驗證。請在 Dashboard > API > API Keys 建立或管理 API 金鑰。

位置: header

請求標頭

X-TokenLab-Delivery-Policy?string

單次請求傳遞策略。會覆寫 API key 與 Workspace 的預設值。系統會自動優先嘗試 TokenLab Verified,並可能在輸出、請求接受或建立持久性資源前切換至 Official 模式一次。

可選值

  • "auto"
  • "verified"
  • "official"

請求主體

application/json

回應

application/json

application/json

application/json

application/json

application/json

application/json

application/json