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:
认证
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 | 2048 | 最大生成 token 数 | Maximum tokens to generate | |||
| temperature | float | 否 | no | 0.7 | 采样温度,越高越随机 | Sampling temperature | |||
| top_p | float | 否 | no | 0.9 | 核采样概率阈值 | Nucleus sampling threshold |
示例(非流式)
Example (non-streaming)
curl http://<server>:9090/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "Qwen3.6",
"messages": [{"role": "user", "content": "你好"}],
"max_tokens": 100
}'from openai import OpenAI
client = OpenAI(
base_url="http://<server>:9090/v1",
api_key="llm-xxx"
)
response = client.chat.completions.create(
model="Qwen3.6",
messages=[{"role": "user", "content": "你好"}],
max_tokens=100
)
print(response.choices[0].message.content)const resp = await fetch("http://<server>:9090/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer llm-xxx"
},
body: JSON.stringify({
model: "Qwen3.6",
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.6",
"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 http://<server>:9090/v1/messages \
-H "x-api-key: llm-xxx" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen3.6",
"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.6",
"stop_reason": "end_turn",
"usage": {"input_tokens": 13, "output_tokens": 14}
}
Python SDK 示例
Python SDK Example
import anthropic
client = anthropic.Anthropic(
base_url="http://<server>:9090/v1",
api_key="llm-xxx",
)
response = client.messages.create(
model="Qwen3.6",
messages=[{"role": "user", "content": "你好"}],
max_tokens=100,
)
print(response.content[0].text)
列出所有可用模型。
List all available models.
示例
Example
curl http://<server>:9090/v1/models -H "Authorization: Bearer $API_KEY"
响应
Response
{
"object": "list",
"data": [{
"id": "Qwen3.6",
"object": "model",
"owned_by": "llamacpp-qwen",
"description": "Qwen3.6-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 http://<server>:9090/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 http://<server>:9090/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": "..."}}