Skip to content

idcdog/lyzr2api

 
 

Repository files navigation

Lyzr2API

将 Lyzr AI 服务封装为 OpenAI 兼容的 API 接口,支持多账号池管理和自动轮询。

功能特性

  • OpenAI 兼容接口 - 提供 /v1/models/v1/chat/completions 标准接口
  • 多账号池管理 - 支持多账号轮询,自动切换
  • 自动重试机制 - 账号额度用完 (429) 自动切换到下一个账号
  • 会话记忆 - 使用零宽字符隐式传递 session_id,保持对话上下文
  • 自动初始化 - 新账号自动创建 organization、获取 API Key、创建 Agent
  • Web 管理面板 - 可视化查看账号状态和额度使用情况
  • 流式响应 - 支持 SSE 流式输出

快速开始

环境要求

  • Python 3.9+
  • Docker (可选)

本地运行

# 安装依赖
pip install -r requirements.txt

# 配置账号 (编辑 new_account.json)
# 启动服务
python server.py

Docker 运行

# 构建并启动
docker-compose up -d --build

# 查看日志
docker-compose logs -f

# 停止服务
docker-compose down

账号配置

编辑 new_account.json 文件:

[
  {
    "email": "your-email@example.com",
    "password": "your-password",
    "member_id": null,
    "access_token": null,
    "organization_id": null,
    "api_key": null,
    "agent_id": null
  }
]

首次运行时,系统会自动:

  1. 登录获取 access_token
  2. 创建 organization(如不存在)
  3. 获取 API Key
  4. 创建 Agent

API 接口

获取模型列表

GET /v1/models

聊天补全

POST /v1/chat/completions
Content-Type: application/json

{
  "model": "anthropic/claude-opus-4-5",
  "messages": [
    {"role": "user", "content": "Hello!"}
  ],
  "stream": true
}

账号信息

GET /api/accounts

管理面板

访问 http://localhost:8000/ 查看账号池状态。

使用示例

Python

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="not-needed"
)

response = client.chat.completions.create(
    model="anthropic/claude-opus-4-5",
    messages=[{"role": "user", "content": "你好"}],
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

cURL

curl -X POST http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-opus-4-5",
    "messages": [{"role": "user", "content": "Hello!"}],
    "stream": false
  }'

项目结构

lyzr2api/
├── server.py           # 主服务 (FastAPI)
├── lyzr_client.py      # Lyzr 客户端库
├── register.py         # 账号注册工具
├── new_account.json    # 账号池配置
├── requirements.txt    # Python 依赖
├── Dockerfile          # Docker 镜像
├── docker-compose.yml  # Docker Compose 配置
├── .gitignore
├── .dockerignore
└── README.md

会话记忆机制

系统使用零宽字符在响应末尾隐式嵌入 session 信息:

  1. 首次请求:生成新的 session_id,发送完整对话内容
  2. 后续请求:从消息中提取 session_id,仅发送最新消息(Lyzr 服务端维护上下文)
  3. 账号绑定:session_id 与账号索引绑定,确保对话连续性

自动重试

当账号返回 429 (额度用完) 时:

  1. 自动切换到下一个可用账号
  2. 生成新的 session_id
  3. 发送完整对话内容重建上下文

注意事项

  • 账号文件 new_account.json 包含敏感信息,已加入 .gitignore
  • 日志文件保存在 server.log
  • 建议定期检查账号额度使用情况

License

APACHE 2.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.7%
  • Dockerfile 0.3%