API 参考文档
API Reference
通过 OpenAI 兼容接口调用大语言模型。支持流式和非流式输出。
Call large language models via an OpenAI-compatible API. Supports streaming and non-streaming responses.
基础 URL
Base URL
所有 API 请求以以下地址为前缀:
All API requests are prefixed with:
三步跑通
Get running in three steps
1. 拿一个 API Key
1. Get an API key
登录后进「Keys」标签页点新建。明文只在创建时显示一次,关掉对话框就再也看不到了,请立刻存好。Key 以 llm- 开头。
Log in, open the "Keys" tab, and create one. The plaintext key is shown exactly once — once you close the dialog it is gone for good, so save it immediately. Keys start with llm-.
2. 确认能连通
2. Check that it works
填入你的 Key,这条命令应该列出可用模型:
Substitute your key; this should list the available models:
export API_KEY="llm-your-key"
curl https://ai.hechenyu.xin/v1/models -H "Authorization: Bearer $API_KEY"
返回里的 id 就是下一步要填的模型名。
The id in the response is the model name you pass next.
3. 发第一个请求
3. Send your first request
curl https://ai.hechenyu.xin/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"Qwen3.8","messages":[{"role":"user","content":"你好"}]}'
这是个推理模型。它先把思考过程输出到 reasoning_content 字段,正文才在 content 里。所以 max_tokens 别设太小 —— 设成 100 很可能思考还没结束就被截断,content 返回空字符串。不填最省心。思考可以关掉,见下方关闭思考链。
This is a reasoning model. It emits its chain of thought into a separate reasoning_content field before the answer lands in content. Keep max_tokens generous — a value like 100 will often be consumed entirely by reasoning, leaving content empty. Omitting it is the safe default. Thinking can be switched off — see disabling the chain of thought.
用现成的客户端
Using an existing client
接口同时兼容 OpenAI 和 Anthropic 两套协议,多数工具改个地址就能用。
The API speaks both the OpenAI and Anthropic protocols, so most tools only need their endpoint changed.
from openai import OpenAI
client = OpenAI(base_url="https://ai.hechenyu.xin/v1", api_key="llm-your-key")
r = client.chat.completions.create(
model="Qwen3.8",
messages=[{"role": "user", "content": "你好"}],
)
print(r.choices[0].message.content)# Claude Code 走 Anthropic 协议。五个模型变量都要指向同一个模型,
# 否则它的后台任务会去请求本站没有的 haiku。
export ANTHROPIC_BASE_URL="https://ai.hechenyu.xin"
export ANTHROPIC_AUTH_TOKEN="llm-your-key"
export ANTHROPIC_MODEL="Qwen3.8"
export ANTHROPIC_DEFAULT_OPUS_MODEL="Qwen3.8"
export ANTHROPIC_DEFAULT_SONNET_MODEL="Qwen3.8"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="Qwen3.8"
export CLAUDE_CODE_SUBAGENT_MODEL="Qwen3.8"
# 后端不支持 prompt caching,不关会每轮带上无用的标记
export DISABLE_PROMPT_CACHING=1
claude# 很多工具认这两个变量,设好即可直接用
export OPENAI_BASE_URL="https://ai.hechenyu.xin/v1"
export OPENAI_API_KEY="llm-your-key"关闭思考链
Disabling the chain of thought
思考默认开启,强度默认是 xhigh(最高档),所以思考链常比答案本身长得多。简单问题、要控成本、或只想拿答案时,用 chat_template_kwargs 关掉:
Thinking is on by default at xhigh effort — the highest setting — so the reasoning is often far longer than the answer itself. For simple questions, cost control, or when you only want the answer, switch it off with chat_template_kwargs:
curl https://ai.hechenyu.xin/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen3.8",
"messages": [{"role": "user", "content": "1+1等于几?"}],
"chat_template_kwargs": {"enable_thinking": false}
}'
同一个问题的实测对比:
Measured on the same question:
| reasoning_content | 输出 token | output tokens | |
|---|---|---|---|
| 默认(思考开启) | default (thinking on) | 40 | 38 |
enable_thinking: false | 0 | 10 |
这个例子省了约 3/4 的输出 token。问题越复杂,思考链越长,省得越多。
Roughly a 4× reduction in output tokens here. The harder the question, the longer the reasoning, and the bigger the saving.
chat_template_kwargs 在 /v1/chat/completions(流式与非流式)和 /v1/messages 上都生效。关掉后 reasoning_content 为空,content 直接是答案。也可以只降强度而不完全关闭,例如 {"reasoning_effort": "low"}。
chat_template_kwargs works on /v1/chat/completions (streaming and not) and on /v1/messages. With thinking off, reasoning_content comes back empty and content holds the answer directly. You can also dial the effort down instead of switching it off entirely, e.g. {"reasoning_effort": "low"}.
认证
Authentication
支持两种认证方式:
Two authentication methods are supported:
方式一:API Key(推荐用于程序调用)
Method 1: API Key (recommended for programmatic use)
在 Web 界面「Keys」标签页创建 API Key,然后在请求头中携带:
Create an API Key in the "Keys" tab of the web UI, then include it in the request header:
Authorization: Bearer llm-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
方式二:JWT Cookie(用于浏览器会话)
Method 2: JWT Cookie (for browser sessions)
通过 POST /api/auth/login 登录后,浏览器自动携带 llm_gateway_token Cookie。
After logging in via POST /api/auth/login, the browser automatically carries the llm_gateway_token cookie.
接口
Endpoints
创建聊天补全。与 OpenAI Chat Completions API 兼容。
Create a chat completion. Compatible with the OpenAI Chat Completions API.
请求参数
Request Parameters
| 参数 | Parameter | 类型 | Type | 必填 | Required | 默认 | Default | 说明 | Description |
|---|---|---|---|---|---|---|---|---|---|
| model | string | 是 | yes | — | 模型 ID,从 /v1/models 获取 | Model ID, from /v1/models | |||
| messages | array | 是 | yes | — | 消息列表,每项含 role 和 content | List of messages, each with role and content | |||
| stream | bool | 否 | no | false | 是否流式输出 | Enable streaming | |||
| max_tokens | int | 否 | no | 不限 | unlimited | 最大生成 token 数。不填则生成到模型自然结束或触达上下文上限。注意:本模型会先输出思考内容(reasoning_content),设得太小会导致正文为空。 | Max tokens to generate. Omit to let the model run to EOS or the context limit. Note: this model emits reasoning_content first; a small value can leave content empty. | ||
| temperature | float | 否 | no | 0.7 | 采样温度,越高越随机 | Sampling temperature | |||
| top_p | float | 否 | no | 0.9 | 核采样概率阈值 | Nucleus sampling threshold |
示例(非流式)
Example (non-streaming)
curl https://ai.hechenyu.xin/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "Qwen3.8",
"messages": [{"role": "user", "content": "你好"}],
"max_tokens": 100
}'from openai import OpenAI
client = OpenAI(
base_url="https://ai.hechenyu.xin/v1",
api_key="llm-xxx"
)
response = client.chat.completions.create(
model="Qwen3.8",
messages=[{"role": "user", "content": "你好"}],
max_tokens=100
)
print(response.choices[0].message.content)const resp = await fetch("https://ai.hechenyu.xin/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer llm-xxx"
},
body: JSON.stringify({
model: "Qwen3.8",
messages: [{role: "user", content: "你好"}],
max_tokens: 100
})
});
const data = await resp.json();
console.log(data.choices[0].message.content);流式响应
Streaming Response
设置 stream: true 后,响应以 SSE 格式返回:
When stream: true, the response is returned as SSE:
data: {"choices":[{"delta":{"role":"assistant","content":null}}],...}
data: {"choices":[{"delta":{"content":"你好"}}],...}
data: {"choices":[{"delta":{"content":"!"}}],...}
...
data: [DONE]
每条 data: 行包含一个 JSON 对象,delta.content 为增量文本。流以 data: [DONE] 结束。
Each data: line contains a JSON object with delta.content as incremental text. The stream ends with data: [DONE].
响应格式
Response Format
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1784790525,
"model": "Qwen3.8",
"choices": [{
"index": 0,
"message": { "role": "assistant", "content": "你好!有什么可以帮助你的?" },
"finish_reason": "stop"
}],
"usage": { "prompt_tokens": 13, "completion_tokens": 10, "total_tokens": 23 }
}
Anthropic Messages API 兼容接口。与 Claude API 格式一致。
Anthropic-compatible Messages API. Same format as the Claude API.
认证
Authentication
使用 x-api-key 请求头(也支持 Authorization: Bearer):
Use the x-api-key header (also supports Authorization: Bearer):
curl -X POST https://ai.hechenyu.xin/v1/messages \
-H "x-api-key: llm-xxx" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen3.8",
"messages": [{"role": "user", "content": "你好"}],
"max_tokens": 100
}'
请求参数
Request Parameters
| 参数 | Parameter | 类型 | Type | 说明 | Description |
|---|---|---|---|---|---|
| model | string | 模型 ID | Model ID | ||
| messages | array | 消息列表,含 role 和 content | List of messages with role and content | ||
| max_tokens | int | 最大生成 token 数 | Max tokens to generate | ||
| stream | bool | 是否流式输出(SSE) | Enable SSE streaming | ||
| system | string | 系统提示词 | System prompt | ||
| temperature | float | 采样温度 | Sampling temperature | ||
| stop_sequences | array | 停止序列 | Stop sequences |
响应格式
Response Format
{
"id": "msg_...",
"type": "message",
"role": "assistant",
"content": [{"type": "text", "text": "你好!有什么可以帮助你的?"}],
"model": "Qwen3.8",
"stop_reason": "end_turn",
"usage": {"input_tokens": 13, "output_tokens": 14}
}
Python SDK 示例
Python SDK Example
import anthropic
client = anthropic.Anthropic(
base_url="https://ai.hechenyu.xin/v1",
api_key="llm-xxx",
)
response = client.messages.create(
model="Qwen3.8",
messages=[{"role": "user", "content": "你好"}],
max_tokens=100,
)
print(response.content[0].text)
列出所有可用模型。
List all available models.
示例
Example
curl https://ai.hechenyu.xin/v1/models -H "Authorization: Bearer $API_KEY"
响应
Response
{
"object": "list",
"data": [{
"id": "Qwen3.8",
"object": "model",
"owned_by": "llamacpp-qwen",
"description": "Qwen3.8-27B (llama.cpp)"
}]
}
创建 API Key。需要 JWT Cookie 认证。Key 仅在创建时返回一次,请妥善保存。
Create an API Key. Requires JWT Cookie auth. The key is shown only once — save it securely.
请求
Request
curl https://ai.hechenyu.xin/api/keys/create \
-H "Content-Type: application/json" \
-b "llm_gateway_token=$TOKEN" \
-d '{"name": "my-app-key"}'
响应
Response
{
"name": "my-app-key",
"key": "llm-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"prefix": "llm-xxxxxxxx"
}
列出当前用户的所有 API Key(不包含完整 Key 值)。
List all API Keys for the current user (full key value not included).
响应
Response
[{
"id": 1, "user_id": 1, "name": "my-app-key",
"key_prefix": "llm-xxxxxxxx", "usage_count": 42,
"last_used_at": "2026-07-23 08:30:00",
"created_at": "2026-07-22 12:00:00"
}]
删除指定的 API Key。删除后使用该 Key 的客户端将无法调用 API。
Delete the specified API Key. Clients using this key will lose API access.
curl -X DELETE https://ai.hechenyu.xin/api/keys/1 -b "llm_gateway_token=$TOKEN"
错误码
Error Codes
| HTTP | 类型 | Type | 说明 | Description |
|---|---|---|---|---|
| 400 | invalid_request_error | 请求参数缺失或格式错误 | Missing or malformed request parameters | |
| 401 | authentication_error | API Key 无效或已过期 | Invalid or expired API Key | |
| 404 | not_found_error | 资源不存在 | Resource not found | |
| 409 | duplicate_error | 用户名已存在(注册时) | Username already taken (registration) | |
| 502 | connection_error | 无法连接到推理引擎 | Cannot connect to inference engine |
错误响应统一格式:
Error response format:
{"error": {"message": "...", "type": "..."}}