API 文档

犀力 Xlei 提供 OpenAI 兼容的大模型 API,自动选择可用上游,支持流式与非流式响应,并提供限流与错误处理。

100%
OpenAI 兼容
智能路由
自动选择最优模型
高可用
多上游自动容错

快速开始

只需三步,即可开始使用:

  1. 注册账户并获取 API Key
  2. 选择接口端点和请求参数
  3. 发送请求获取响应
POST https://www.xlei.site/xlei/v1/chat/completions
curl -X POST https://www.xlei.site/xlei/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "X-API-Key: sk-your-api-key" \
  -d '{
    "model": "auto",
    "messages": [
      {"role": "user", "content": "Hello"}
    ]
  }'

接口端点

系统提供两个主要端点,后端自动判断请求模式:

POST /xlei/v1/chat/completions

用于聊天模式,使用 messages 参数,推荐用于交互式对话场景。

POST /xlei/v1/completions

用于补全模式,使用 prompt 参数,适用于文本续写场景。

模式自动识别

系统会根据请求参数自动判断模式:

  • 聊天模式: 当提供 messages 参数时
  • 补全模式: 当只提供 prompt 参数时
  • 优先级: messages 参数优先级更高

认证方式

支持两种认证方式,优先使用 X-API-Key:

# 方式1:X-API-Key(推荐)
curl -X POST https://www.xlei.site/xlei/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "X-API-Key: sk-your-api-key" \
  -d '{"messages": [{"role": "user", "content": "Hello"}]}'

# 方式2:Authorization Bearer
curl -X POST https://www.xlei.site/xlei/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-your-api-key" \
  -d '{"messages": [{"role": "user", "content": "Hello"}]}'

获取 API Key

登录用户门户后,在「API 密钥」页面创建和管理您的 API Key。

限流机制

平台采用双重限流策略,保障服务稳定:

IP限流
500次/分钟
单个IP的请求频率限制
API Key限流
100次/分钟
单个API Key的请求频率限制

超出限流阈值将返回 429 Too Many Requests 错误

请求参数

只需提供 messagesprompt 之一;建议将 model 设为 auto(默认值,可省略该字段),由网关自动选择最优上游模型。其余参数由后端自动设置最优值,无需在请求中指定。

参数类型必填默认值说明
messagesarray二选一-聊天消息数组,与prompt二选一(推荐)
promptstring二选一-补全文本,与messages二选一
modelstringauto推荐使用 auto:由网关按健康度与负载自动选路;可省略本字段(默认即为 auto)。仅在需要固定模型时再指定具体模型名

messages数组格式

"messages": [
  {"role": "system", "content": "你是一个乐于助人的助手"},
  {"role": "user", "content": "Hello!"},
  {"role": "assistant", "content": "Hello! How can I help you?"},
  {"role": "user", "content": "Tell me more"}
]

role可选值: system(系统提示,定义助手行为)、 user(用户输入)、 assistant(助手回复,用于多轮对话)

请求示例

聊天模式(推荐)

{
  "model": "auto",
  "messages": [
    {"role": "user", "content": "Hello!"}
  ]
}

多轮对话

{
  "model": "auto",
  "messages": [
    {"role": "user", "content": "什么是人工智能?"},
    {"role": "assistant", "content": "人工智能(AI)是..."},
    {"role": "user", "content": "请再详细介绍一下。"}
  ]
}

补全模式

{
  "model": "auto",
  "prompt": "从前有一座遥远的城堡,"
}

响应格式

流式响应(默认)

省略 stream 时默认返回 SSE(Server-Sent Events),适合实时显示。每行以 data: 开头,结束时发送 data: [DONE]。推理模型还可能在 delta 中返回 reasoning_content 字段。

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1714166400,"choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1714166400,"choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}

data: [DONE]

非流式响应

需要一次性拿到完整 JSON 时,可显式设置 "stream": false(其余生成参数仍由后端自动设置):

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1714166400,
  "model": "deepseek-v4-flash",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 20,
    "total_tokens": 35
  }
}

推理过程(可选)

部分推理模型在非流式响应的 message 中返回 reasoning 字段;流式场景则通过 delta.reasoning_content 增量输出。

{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "model": "deepseek-v4-flash",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "答案是42",
        "reasoning": "首先分析问题...然后计算..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 50,
    "completion_tokens": 30,
    "total_tokens": 80
  }
}

错误码

错误码HTTP状态说明
0200成功
1001400缺少prompt或messages参数
1002400未知模型名(建议使用 auto
1003400模型类型与接口不匹配(chat / completions)
2001401API Key 无效、缺失或账户已禁用
2002402余额不足
2003402/403订阅日限额已达,或当前等级无权使用该模型
429429请求过于频繁(IP 或 API Key 限流)
4001503无可用上游模型
5000500服务器内部错误

错误响应格式

{
  "code": 2002,
  "message": "余额不足,请充值后重试",
  "data": null
}

代码示例

curl

cURL

curl -X POST https://www.xlei.site/xlei/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "X-API-Key: sk-your-api-key" \
  -d '{
    "model": "auto",
    "messages": [{"role": "user", "content": "Hello"}]
  }' \
  --no-buffer
Py

Python

import requests
import os

base_url = "https://www.xlei.site"
api_key = os.getenv("XLEI_API_KEY")

headers = {
    "Content-Type": "application/json",
    "X-API-Key": api_key
}

data = {
    "model": "auto",
    "messages": [{"role": "user", "content": "Hello!"}]
}

response = requests.post(
    f"{base_url}/xlei/v1/chat/completions",
    json=data,
    headers=headers,
    stream=True
)

for line in response.iter_lines():
    if line:
        decoded_line = line.decode('utf-8')
        if decoded_line.startswith('data: '):
            print(decoded_line[6:])
JS

JavaScript

const baseUrl = 'https://www.xlei.site';
const apiKey = process.env.XLEI_API_KEY;

async function chat(message) {
    const response = await fetch(`${baseUrl}/xlei/v1/chat/completions`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-API-Key': apiKey
        },
        body: JSON.stringify({
            model: 'auto',
            messages: [{ role: 'user', content: message }]
        })
    });

    const reader = response.body.getReader();
    const decoder = new TextDecoder('utf-8');
    
    while (true) {
        const { value, done } = await reader.read();
        if (done) break;
        
        const lines = decoder.decode(value).split('\n');
        for (const line of lines) {
            if (line.startsWith('data: ')) {
                const data = JSON.parse(line.slice(6));
                const content = data.choices[0]?.delta?.content;
                if (content) process.stdout.write(content);
            }
        }
    }
}

chat('Hello!');

第三方工具接入

犀力 可以无缝接入支持 OpenAI 兼容接口的第三方工具:

配置示例

在支持自定义 API 的工具中配置以下参数:

API Base URL: https://www.xlei.site/xlei/v1
API Key: sk-your-api-key
Model: auto

支持的工具

  • LangChain / LlamaIndex: AI 应用开发框架
  • Chatbox / LobeChat: 桌面/移动端 AI 客户端
  • Cursor / CodeLlama: 代码编辑器插件
  • OpenAI 兼容 SDK: 各种语言的 OpenAI SDK

LangChain 示例

from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(
    model_name="auto",
    openai_api_base="https://www.xlei.site/xlei/v1",
    openai_api_key="sk-your-api-key"
)

response = llm.predict("Hello!")
print(response)