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:

http://<server-ip>:9090

认证

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

POST/v1/chat/completions

创建聊天补全。与 OpenAI Chat Completions API 兼容。

Create a chat completion. Compatible with the OpenAI Chat Completions API.

请求参数

Request Parameters

参数Parameter类型Type必填Required默认Default说明Description
modelstringyes模型 ID,从 /v1/models 获取Model ID, from /v1/models
messagesarrayyes消息列表,每项含 role 和 contentList of messages, each with role and content
streamboolnofalse是否流式输出Enable streaming
max_tokensintno2048最大生成 token 数Maximum tokens to generate
temperaturefloatno0.7采样温度,越高越随机Sampling temperature
top_pfloatno0.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 }
}
POST/v1/messages

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
modelstring模型 IDModel ID
messagesarray消息列表,含 role 和 contentList of messages with role and content
max_tokensint最大生成 token 数Max tokens to generate
streambool是否流式输出(SSE)Enable SSE streaming
systemstring系统提示词System prompt
temperaturefloat采样温度Sampling temperature
stop_sequencesarray停止序列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)
GET/v1/models

列出所有可用模型。

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)"
  }]
}
POST/api/keys/create

创建 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"
}
GET/api/keys/list

列出当前用户的所有 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"
}]
DELETE/api/keys/{id}

删除指定的 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
400invalid_request_error请求参数缺失或格式错误Missing or malformed request parameters
401authentication_errorAPI Key 无效或已过期Invalid or expired API Key
404not_found_error资源不存在Resource not found
409duplicate_error用户名已存在(注册时)Username already taken (registration)
502connection_error无法连接到推理引擎Cannot connect to inference engine

错误响应统一格式:

Error response format:

{"error": {"message": "...", "type": "..."}}
注意:目前没有速率限制,但请合理使用,避免影响其他用户。
Note: There are currently no rate limits, but please use responsibly.