兼容 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 Key。
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 返回的推理摘要可以作为展示历史随请求传回。
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。
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 张图的有序数组。
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
}'
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
}'
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 解码图片。
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"
}'
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 得到最终图片。
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 补丁验证前,该路由保持停用。
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/transcriptions | POST | 可用 |
| /v1/audio/translations | POST | 可用 |
| /v1/audio/speech | POST | 可用 |
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
# 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)