相容 OpenAI

可用路徑族

在已設定的情況下,使用相容 OpenAI 的路由呼叫文字、圖片、音訊、嵌入、即時、重排、內容審核和 Responses API。

POST /v1/chat/completions

將範例中的 <MODEL_ID> 替換為目前 API Key 可呼叫且支援對應端點的模型 ID。

GPT-5.6 Luna(gpt-5.6-luna)和 GPT-6 Luna(gpt-6-luna)使用 reasoning_effort 設定推理強度,請省略 thinking。

聊天完成
curl https://www.token168.cc/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "<MODEL_ID>",
    "messages": [{"role": "user", "content": "Summarize the release notes."}]
  }'

Claude Fable 5

Claude Fable 5 採用自適應思考。reasoning_effort 可設為 low、medium 或 high,明確傳入 none 時使用 low,省略時採用模型預設行為。正數 reasoning.max_tokens 優先,並對應至思考強度。透過 Chat 呼叫此模型時會忽略 temperature、top_p 與 top_k。請使用有權呼叫此模型的 API 金鑰。

聊天完成
curl https://www.token168.cc/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "claude-fable-5",
    "messages": [{"role": "user", "content": "Summarize the release notes."}],
    "reasoning_effort": "high",
    "max_tokens": 5000
  }'

POST /v1/completions

Completions 使用同一條 token168 文字轉送路徑,並採用以 prompt 為主的欄位。

文字完成
curl https://www.token168.cc/v1/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "<MODEL_ID>",
    "prompt": "Write a one-line changelog entry.",
    "prefix": "",
    "suffix": "",
    "stream": false,
    "stream_options": {"include_usage": true},
    "max_tokens": 256,
    "temperature": 0.3,
    "top_p": 0.9,
    "top_k": 40,
    "stop": ["\n\n"],
    "n": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "seed": 123,
    "logprobs": false,
    "top_logprobs": 0,
    "user": "user_123",
    "metadata": {"trace_id": "trace_123"},
    "service_tier": "default",
    "store": false
  }'

POST /v1/responses

中國大陸站的 Kimi 模型不支援 POST /v1/responses,請使用 POST /v1/chat/completions

Responses 支援相容 OpenAI 的回應結構,可處理輸入陣列、工具、推理、快取和串流傳輸選項。

使用 Claude Fable 5 時,可將 reasoning.effort 設為 high 以提高思考強度。明確指定 none 時,會使用最低強度 low。

續接 Claude Fable 5 的工具呼叫時,請帶上原呼叫 ID 和工具結果。Responses 傳回的推理摘要可作為顯示用的歷史紀錄隨請求傳回。

Responses 回應
curl https://www.token168.cc/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "<MODEL_ID>",
    "input": [{"role": "user", "content": [{"type": "input_text", "text": "Draft a launch checklist."}]}],
    "include": ["usage"],
    "conversation": {"id": "conv_123"},
    "context_management": {"type": "auto"},
    "instructions": "Return concise bullets.",
    "max_output_tokens": 800,
    "top_logprobs": 0,
    "metadata": {"trace_id": "trace_123"},
    "parallel_tool_calls": true,
    "previous_response_id": "resp_previous",
    "reasoning": {"effort": "medium"},
    "service_tier": "default",
    "store": false,
    "prompt_cache_key": "launch-checklist",
    "prompt_cache_retention": "24h",
    "safety_identifier": "safe_user_123",
    "stream": false,
    "stream_options": {"include_usage": true},
    "temperature": 0.4,
    "text": {"format": {"type": "text"}},
    "tool_choice": "auto",
    "tools": [],
    "top_p": 0.9,
    "truncation": "auto",
    "user": "user_123",
    "max_tool_calls": 4,
    "prompt": {"id": "prompt_123"},
    "enable_thinking": true,
    "preset": "default"
  }'

POST /v1/responses/compact

請選擇支援 Compact 的模型。將傳回的完整 output 陣列原樣作為下一次 /v1/responses 請求的 input。

Responses 壓縮
curl https://www.token168.cc/v1/responses/compact \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "<MODEL_ID>",
    "input": [{"role": "user", "content": "Compact this conversation."}]
  }'

Image 2.5 產生與編輯

使用 gpt-image-2.5-flare 或 gpt-image-2.5-sunburst。產生的圖片透過 data[].b64_json 傳回,解碼後可儲存為 PNG。請勿傳送 response_format。

圖片產生
curl https://www.token168.cc/v1/images/generations \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2.5-flare",
  "prompt": "A blue circle on a white background",
  "n": 1,
  "size": "1024x1024",
  "quality": "low",
  "output_format": "png"
}'

編輯圖片時,使用 JSON images 和 image_url 參照。將下方的來源 URL 替換為可存取的圖片位址,模型名稱可選擇任一 Image 2.5 型號。

圖片編輯
curl https://www.token168.cc/v1/images/edits \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2.5-sunburst",
  "prompt": "Change the blue circle to red",
  "images": [
    {
      "image_url": "https://your-cdn.example/source.png"
    }
  ],
  "n": 1,
  "size": "1024x1024",
  "quality": "low",
  "output_format": "png"
}'

Seedream 5.5 產生與參考圖

API Key 取得 Seedream 5.5 權限後,向 POST /v1/images/generations 傳送 JSON。純文字請省略 image。參考圖可傳入一個 HTTPS URL、一個 Base64 data URI,或最多 10 張圖片的有序陣列。

Text to image
curl https://www.token168.cc/v1/images/generations \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "dola-seedream-5-5-261028",
  "prompt": "A blue circle on a white background",
  "size": "1024x1024",
  "response_format": "b64_json",
  "output_format": "png",
  "watermark": false
}'
Single reference
curl https://www.token168.cc/v1/images/generations \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "dola-seedream-5-5-261028",
  "prompt": "Keep the composition and turn the blue circle red",
  "image": "https://your-cdn.example/source.png",
  "size": "1024x1024",
  "response_format": "b64_json",
  "output_format": "png",
  "watermark": false
}'
Multiple references
curl https://www.token168.cc/v1/images/generations \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "dola-seedream-5-5-261028",
  "prompt": "Keep the composition and turn the blue circle red",
  "image": [
    "https://your-cdn.example/source.png",
    "https://your-cdn.example/reference.png"
  ],
  "size": "1024x1024",
  "response_format": "b64_json",
  "output_format": "png",
  "watermark": false
}'

尺寸可選 1K、1.5K、2K(預設),或指定寬高。回傳方式可選 url(預設)或 b64_json,圖檔格式可選 PNG(預設)或 JPEG。每次回傳一張圖。輸入圖和輸出圖分別計費,輸出依實際像素數分級。若結果待核對帳務,請保留請求 ID,查詢原請求後再決定是否重送。

POST /v1/images/generations

產生新圖片時傳送 application/json,填寫 model 和 prompt。帶參考圖的編輯使用 /v1/images/edits。GPT Image 使用 output_format 指定檔案格式,省略 response_format。解碼 data[].b64_json 讀取圖片。

圖片產生
curl https://www.token168.cc/v1/images/generations \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2",
  "prompt": "A blue circle on a white background",
  "n": 1,
  "size": "1024x1024",
  "quality": "low",
  "output_format": "png"
}'

POST /v1/images/edits

JSON 圖片編輯傳送 application/json,填寫 model、prompt 和 images。images 的每一項都是包含可存取 image_url 的物件。上傳本機檔案時使用 multipart/form-data 的 image[] 欄位。gpt-image-2 省略 input_fidelity 和 response_format。從非串流回應的 data[].b64_json 解碼圖片。

application/json
curl https://www.token168.cc/v1/images/edits \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2",
  "prompt": "Change the blue circle to red",
  "images": [
    {
      "image_url": "https://your-cdn.example/source.png"
    }
  ],
  "n": 1,
  "size": "1024x1024",
  "quality": "low",
  "output_format": "png"
}'
multipart/form-data
curl https://www.token168.cc/v1/images/edits \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -F 'model=gpt-image-2' \
  -F 'prompt=Change the blue circle to red' \
  -F 'n=1' \
  -F 'size=1024x1024' \
  -F 'quality=low' \
  -F 'output_format=png' \
  -F 'image[]=@source.png'

設定 stream=true 接收伺服器傳送事件。讀到 image_edit.completed 後,解碼該事件的 b64_json 取得最終圖片。

stream=true
curl -N https://www.token168.cc/v1/images/edits \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2",
  "prompt": "Change the blue circle to red",
  "images": [
    {
      "image_url": "https://your-cdn.example/source.png"
    }
  ],
  "stream": true,
  "n": 1,
  "size": "1024x1024",
  "quality": "low",
  "output_format": "png"
}'

POST /v1/edits

/v1/edits 已註冊至轉送路由器,但目前原始碼將其識別為 RelayModeEdits,並驗證舊版文字編輯 instruction 欄位。在 owner 決策或 fork 修補程式驗證前,該路由保持停用。

舊版 edits 分派缺口
curl https://www.token168.cc/v1/edits \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "<MODEL_ID>",
    "input": "The quik brown fox.",
    "instruction": "Fix the spelling mistakes."
  }'

已停用的相容路由

不得將 /v1/edits 記載或測試為 multipart 圖片編輯成功路徑。圖片編輯請使用 /v1/images/edits。

POST /v1/embeddings

Embeddings 接受 model、input、output dimensions、encoding 以及可選的 sampling / user metadata 欄位。

向量嵌入
curl https://www.token168.cc/v1/embeddings \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "<MODEL_ID>",
    "input": ["Token168 routes native APIs."],
    "encoding_format": "float",
    "dimensions": 1536,
    "user": "user_123",
    "seed": 123,
    "temperature": 0,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0
  }'

POST /v1/audio/*

目前可用的相容 OpenAI 音訊路由包括語音合成、轉錄和翻譯。語音合成使用 JSON 請求內容;轉錄和翻譯通常使用 multipart 上傳。

音訊範例
curl https://www.token168.cc/v1/audio/speech \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "<MODEL_ID>",
    "input": "Token168 unifies native model APIs.",
    "voice": "alloy",
    "instructions": "Neutral narration.",
    "response_format": "mp3",
    "speed": 1,
    "stream_format": "none",
    "metadata": {"trace_id": "trace_123"},
    "task_type": "tts",
    "language": "en",
    "ref_audio": "",
    "ref_text": "",
    "x_vector_only_mode": false,
    "max_new_tokens": 1024,
    "initial_codec_chunk_frames": 0
  }'


curl https://www.token168.cc/v1/audio/transcriptions \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -F model="qwen3-asr-flash" \
  -F file="@speech.wav" \
  -F response_format="json" \
  -F language="en"


curl https://www.token168.cc/v1/audio/translations \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -F model="<MODEL_ID>" \
  -F file="@speech.wav" \
  -F input="speech.wav" \
  -F response_format="json" \
  -F language="en" \
  -F speed="1"
路徑方法狀態
/v1/audio/transcriptionsPOST可用
/v1/audio/translationsPOST可用
/v1/audio/speechPOST可用

POST /v1/rerank

Rerank 接受 query、documents 和結果控制參數。

重排
curl https://www.token168.cc/v1/rerank \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "<MODEL_ID>",
    "query": "native model API docs",
    "documents": ["OpenAI-compatible docs", "Billing docs", "Console docs"],
    "top_n": 2,
    "return_documents": true,
    "max_chunk_per_doc": 4,
    "overlap_tokens": 32
  }'

POST /v1/moderations

當設定相容模型時,Moderations 使用相容 OpenAI 的轉送結構執行內容安全檢查。

內容審核
curl https://www.token168.cc/v1/moderations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "model": "<MODEL_ID>",
    "input": "User-provided content to check",
    "user": "user_123",
    "metadata": {"trace_id": "trace_123"}
  }'

GET /v1/realtime

新加坡 Qwen Realtime 使用其原生事件協定。在模型目錄選擇標註 qwen-realtime 的型號,替換範例中的 MODEL,並使用 Token168 API Key 驗證。範例以 16kHz、16bit、單聲道 PCM WAV(input.wav)提交完整語音回合,儲存 24kHz 回答音訊(answer.wav)。完整回合與多輪上下文已驗收;取消回應和自動打斷尚未驗收。

wss://service.token168.ai/v1/realtime?model=MODEL_ID

Python 語音範例
# python3 -m pip install websocket-client==1.9.2
# Set TOKEN168_API_KEY before running. Input: input.wav (16 kHz / 16-bit / mono).
import base64
import json
import os
import time
import wave
from urllib.parse import urlencode
import websocket


MODEL = "qwen3.8-omni-flash-realtime"
# Also verified: qwen3.5-omni-flash-realtime, qwen3.5-omni-plus-realtime.
# Other models with the same qwen-realtime protocol reuse this example.
with wave.open("input.wav", "rb") as source:
    assert (source.getframerate(), source.getsampwidth(), source.getnchannels(), source.getcomptype()) == (16000, 2, 1, "NONE")
    assert 0 < source.getnframes() <= 8 * 16000  # This example limits input to 8 seconds.
    pcm = source.readframes(source.getnframes())


ws = websocket.create_connection(
    "wss://service.token168.ai/v1/realtime?" + urlencode({"model": MODEL}),
    header={"Authorization": "Bearer " + os.environ["TOKEN168_API_KEY"]},
    timeout=10,
)
deadline = time.monotonic() + 45


def send(event):
    ws.send(json.dumps(event))


def receive():
    remaining = deadline - time.monotonic()
    if remaining <= 0:
        raise TimeoutError("Session deadline exceeded; check usage before retrying")
    ws.settimeout(min(10, remaining))
    event = json.loads(ws.recv())
    if event.get("type") == "error" or (not event.get("type") and event.get("code")):
        raise RuntimeError(event.get("error", event))
    return event


def wait_for(event_type):
    while True:
        event = receive()
        if event.get("type") == event_type:
            return event


try:
    wait_for("session.created")
    send({"type": "session.update", "session": {
        "modalities": ["text", "audio"], "input_audio_format": "pcm",
        "output_audio_format": "pcm", "turn_detection": None, "max_tokens": 64,
        "instructions": "Please answer briefly in Chinese, using at most four characters.",
    }})
    session = wait_for("session.updated")["session"]
    assert session.get("turn_detection") is None and session.get("max_tokens") == 64
    for offset in range(0, len(pcm), 16000):
        send({"type": "input_audio_buffer.append", "audio": base64.b64encode(pcm[offset:offset + 16000]).decode()})
    send({"type": "input_audio_buffer.commit"})
    wait_for("input_audio_buffer.committed")
    send({"type": "response.create"})
    audio = bytearray()
    while True:
        event = receive()
        if event.get("type") == "response.audio.delta":
            audio.extend(base64.b64decode(event["delta"]))
        elif event.get("type") == "response.audio_transcript.done":
            print(event["transcript"])
        elif event.get("type") == "response.done":
            response = event["response"]
            if response.get("status") != "completed" or not response.get("usage"):
                raise RuntimeError("Turn not confirmed; check usage before retrying")
            print(json.dumps(response["usage"]))
            break
    assert audio, "No audio returned"
    with wave.open("answer.wav", "wb") as output:
        output.setnchannels(1)
        output.setsampwidth(2)
        output.setframerate(24000)
        output.writeframes(audio)
finally:
    ws.close(timeout=1)