TokenLake 提供 OpenAI 兼容的 API,无需修改即可直接使用现有的 OpenAI SDK 和工具。
所有请求必须在 Authorization 请求头中携带 Bearer Token。
Authorization: Bearer sk-th-your-api-keyhttps://api.tokenlake.ai/v1/v1/chat/completions对话补全 — 支持流式输出
/v1/models获取可用模型列表
from openai import OpenAI
client = OpenAI(
base_url="https://api.tokenlake.ai/v1",
api_key="sk-th-your-api-key",
)
response = client.chat.completions.create(
model="qwen3-8b",
messages=[{"role": "user", "content": "Hello!"}],
stream=False,
temperature=1.0,
max_tokens=1024,
)
print(response.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.tokenlake.ai/v1",
apiKey: "sk-th-your-api-key",
});
const response = await client.chat.completions.create({
model: "qwen3-8b",
messages: [{ role: "user", content: "Hello!" }],
stream: false,
temperature: 1.0,
max_tokens: 1024,
});
console.log(response.choices[0].message.content);curl https://api.tokenlake.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-th-your-api-key" \
-d '{
"model": "qwen3-8b",
"messages": [{"role": "user", "content": "Hello!"}],
"temperature": 1.0,
"max_tokens": 1024
}'# Streaming example
for chunk in client.chat.completions.create(
model="qwen3-8b",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True,
):
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)| Parameter | Type | Required | Description |
|---|---|---|---|
| model | string | required | 使用的模型(如 qwen3-8b、gemma-3-9b) |
| messages | array | required | 消息数组,每条消息包含 role 和 content |
| stream | boolean | optional | 开启流式响应(Server-Sent Events) |
| temperature | number | optional | 采样温度,取值 0–2(默认:1) |
| max_tokens | integer | optional | 最大生成 Token 数 |
所有响应遵循标准 OpenAI 响应格式,包括流式 delta 事件。
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1714000000,
"model": "qwen3-8b",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 12,
"total_tokens": 22
}
}| Code | Description |
|---|---|
| 401 | 未授权 — API Key 无效或缺失 |
| 402 | 余额不足 — 账户余额不够 |
| 404 | 未找到 — 模型或接口不存在 |
| 429 | 请求过多 — 超出速率限制 |
| 503 | 服务不可用 — 上游模型故障 |
每个 API Key 每分钟的请求数有限制。超出后将收到 429 响应。如需提升限额,请联系支持。
TokenLake 与官方 OpenAI SDK 完全兼容,只需设置 base URL 和 API Key 即可。
pip install openainpm install openai