Skip to content

YuhangJu0622/llm_gateway_pydemo

Repository files navigation

LLM API Gateway

统一 LLM API Gateway - 支持 OneRouter、Wavespeed、LibLib 多个提供商的中间层服务。

功能特性

  • 🔌 统一接口: 提供统一的 REST API 接口,屏蔽不同提供商的差异
  • 🔐 密钥管理: 集中管理 API Key 和 Provider 凭据
  • 📊 用量统计: 记录和查询 API 调用统计
  • 🎯 配额控制: 支持每日/每月调用次数和 token 限制
  • 🔄 多提供商: 支持 OneRouter、Wavespeed、LibLib
  • 🛡️ 安全加密: Provider 凭据加密存储

快速开始

安装依赖

pip install -r requirements.txt

配置环境变量

创建 .env 文件:

# 数据库配置
DATABASE_URL=sqlite+aiosqlite:///./llm_gateway.db

# 加密密钥 (使用 cryptography.fernet.Fernet.generate_key() 生成)
ENCRYPTION_KEY=your-fernet-key-here

# 默认 Provider (可选)
DEFAULT_PROVIDER=onerouter

# 调试模式
DEBUG=false

启动服务

# 开发模式 (自动重载)
python app/main.py

# 或使用 uvicorn
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

访问 API 文档

启动服务后,访问:

API 端点

生成接口

  • POST /api/v1/generate - 统一生成接口
  • POST /api/v1/chat/completions - OpenAI 兼容接口
  • GET /api/v1/tasks/{task_id} - 查询异步任务状态

管理接口

  • POST /api/v1/admin/api-keys - 创建 API Key
  • GET /api/v1/admin/api-keys - 列出 API Keys
  • PUT /api/v1/admin/api-keys/{key_id} - 更新 API Key
  • DELETE /api/v1/admin/api-keys/{key_id} - 删除 API Key
  • POST /api/v1/admin/providers - 添加 Provider 凭据
  • GET /api/v1/admin/providers - 列出 Provider 凭据
  • PUT /api/v1/admin/providers/{id} - 更新 Provider 凭据
  • DELETE /api/v1/admin/providers/{id} - 删除 Provider 凭据

用量接口

  • GET /api/v1/usage/stats - 查询用量统计
  • GET /api/v1/usage/records - 查询用量记录
  • GET /api/v1/usage/quota - 查询配额状态

健康检查

  • GET /health - 健康检查

测试

# 运行所有测试
pytest

# 运行属性测试
pytest tests/test_*_properties.py

# 查看测试覆盖率
pytest --cov=app --cov-report=html

项目结构

llm-api-gateway/
├── app/
│   ├── main.py              # FastAPI 应用入口
│   ├── config.py            # 配置管理
│   ├── api/                 # API 路由
│   ├── adapters/            # Provider 适配器
│   ├── services/            # 业务服务
│   ├── models/              # 数据模型
│   └── utils/               # 工具函数
├── tests/                   # 测试文件
├── requirements.txt         # 依赖列表
└── README.md               # 项目文档

开发

查看 .kiro/specs/llm-api-gateway/ 目录下的设计文档:

  • requirements.md - 需求文档
  • design.md - 设计文档
  • tasks.md - 实现任务列表

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors