API 文档
本站提供与 OpenAI API 兼容的接口:只要把 base_url
指向下方地址、换上本站密钥,现有 SDK 与客户端基本无需改动。
Base URL
https://bigmodel.tokenr.cn/api/v1
支持流式(SSE)与非流式两种模式;流式与非流式返回结构均与 OpenAI 一致。
认证
所有请求都需要携带本站 API Key,两种写法等价:
Authorization: Bearer sk-xxxxxxxx
x-api-key: sk-xxxxxxxx
密钥在 控制台 → API 密钥 创建,明文只显示一次,请立即保存。
对话补全
POST| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名,可用 模型列表接口查询 |
messages | array | 是 | 对话消息数组,元素为 {role, content} |
stream | bool | 否 | 为 true 时以 SSE 流式返回,默认 false |
max_tokens | int | 否 | 最大生成 token 数 |
temperature | float | 否 | 采样温度 0~2 |
top_p | float | 否 | 核采样 |
stop | string / array | 否 | 停止序列 |
tools | array | 否 | 可供模型调用的函数定义,见工具调用 |
tool_choice | string / object | 否 | auto / required / none / 指定函数 |
curl 示例
curl https://bigmodel.tokenr.cn/api/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "你好,介绍一下你自己"}
]
}'
Python(官方 openai SDK)
from openai import OpenAI
client = OpenAI(api_key="sk-xxxxxxxx", base_url="https://bigmodel.tokenr.cn/api/v1")
resp = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "你好"}],
)
print(resp.choices[0].message.content)
Node.js
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-xxxxxxxx",
baseURL: "https://bigmodel.tokenr.cn/api/v1",
});
const resp = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "你好" }],
});
console.log(resp.choices[0].message.content);
流式输出
请求体加 "stream": true,响应为 text/event-stream,每条数据形如:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk",
"choices":[{"index":0,"delta":{"content":"你"},"finish_reason":null}]}
data: [DONE]
增量内容在 choices[0].delta.content;结束时 finish_reason 变为 stop(长度截断为 length,工具调用为 tool_calls)。
工具调用(Function Calling)
传入 tools 后,模型可返回 tool_calls,客户端执行函数后把结果以 role: "tool" 回填即可继续对话。
{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "北京今天天气如何?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}]
}
模型返回:
{
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {"name": "get_weather", "arguments": "{\"city\":\"北京\"}"}
}]
},
"finish_reason": "tool_calls"
}]
}
执行函数后,把结果回填并再次请求:
{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "北京今天天气如何?"},
{"role": "assistant", "content": null, "tool_calls": [
{"id": "call_abc123", "type": "function",
"function": {"name": "get_weather", "arguments": "{\"city\":\"北京\"}"}}
]},
{"role": "tool", "tool_call_id": "call_abc123", "content": "晴,22℃"}
]
}
流式模式下工具参数以增量片段下发,需按 index 拼接 function.arguments 后再解析 JSON。
向量嵌入
POSTcurl https://bigmodel.tokenr.cn/api/v1/embeddings \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "text-embedding-3-small", "input": "要向量化的文本"}'
请求与响应结构与 OpenAI 的 embeddings 接口一致。
模型列表
GETcurl https://bigmodel.tokenr.cn/api/v1/models -H "Authorization: Bearer $API_KEY"
{"object":"list","data":[{"id":"gpt-4o","object":"model","created":0,"owned_by":"relay"}, ...]}
返回当前所有可用渠道支持的模型。密钥若限制了模型范围,则只返回范围内的模型。
错误码
错误响应统一为 OpenAI 格式:
{"error": {"message": "...", "type": "...", "code": "..."}}
| HTTP | code | 含义 |
|---|---|---|
| 400 | missing_model / missing_messages | 缺少必填参数 |
| 401 | invalid_api_key | 密钥缺失或无效 |
| 403 | model_not_allowed | 该密钥无权使用此模型 |
| 403 | account_deactivated | 密钥或账号已被停用 |
| 403 | key_expired | 密钥已过期 |
| 404 | model_not_found | 没有任何渠道支持该模型 |
| 429 | insufficient_quota | 密钥额度或账户余额不足 |
| 429 | rate_limit_exceeded | 请求过于频繁,响应头带 Retry-After |
| 502 | upstream_error | 上游服务异常,可重试 |
限流与计费
限流
- 单个 API 密钥:60 次 / 分钟
- 单个 IP:120 次 / 分钟
计费
- 按 token 用量计费,单价以「每百万 token」为基准,从账户余额扣除
- 请求前会按预估用量冻结额度,响应后按实际用量结算,差额自动退回
- 请求失败(上游报错、无可用渠道)不计费
- 余额不足时直接返回
429 insufficient_quota,不会产生欠费
用量明细可在 控制台 → 调用日志 查看。
Huan Api · 如遇问题请联系管理员