YTokenHub 网关完全兼容 OpenAI 接口协议:把 BaseURL 换成本站的
/v1、API Key 换成控制台创建的密钥,现有代码零改造即可切换任意模型。
所有 /v1 接口通过请求头携带 API Key:
| 方法 | 路径 | 说明 |
|---|---|---|
| POST | /v1/chat/completions | 对话补全,支持 stream=true(SSE 流式)与 stream=false |
| GET | /v1/models | 当前可调用模型列表(同样需要携带 API Key) |
curl http://www.yziss.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxxxx" \
-d '{
"model": "deepseek-chat",
"messages": [{"role": "user", "content": "你好"}],
"stream": false
}'
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxxxx",
base_url="http://www.yziss.com/v1",
)
resp = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "你好"}],
)
print(resp.choices[0].message.content)
const resp = await fetch(
"http://www.yziss.com/v1/chat/completions",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer sk-xxxxxxxx",
},
body: JSON.stringify({
model: "deepseek-chat",
messages: [{ role: "user", content: "你好" }],
}),
}
);
const data = await resp.json();
console.log(data.choices[0].message.content);
传 stream=true 时网关逐块透传 SSE 分片,以 data: [DONE] 结束;
上游兼容 OpenAI / Anthropic / Gemini 三种协议,网关会统一转换为 OpenAI 分片格式,客户端无需区分厂商。
stream = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "写一首短诗"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
| HTTP | code | 含义与处理 |
|---|---|---|
| 400 | invalid_request | 请求体不合法(如缺少 model / messages) |
| 401 | invalid_api_key | 密钥不存在、已禁用或已过期 |
| 402 | insufficient_balance | 余额不足,请前往「钱包」充值 |
| 404 | model_not_found | 模型未上架,见价格表 |
| 429 | rate_limit_exceeded | 超过单密钥每分钟调用上限 |
| 502 | upstream_error | 上游渠道全部失败(不扣费,可重试) |
| 503 | no_available_channel | 该模型当前无可用渠道 |