{"id":50371914,"url":"https://github.com/lxfu1/ai-router","last_synced_at":"2026-05-30T07:30:47.831Z","repository":{"id":357606214,"uuid":"1236371939","full_name":"lxfu1/ai-router","owner":"lxfu1","description":"Intelligent Relay Hub for AI Models","archived":false,"fork":false,"pushed_at":"2026-05-13T13:01:48.000Z","size":232,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-13T14:38:27.828Z","etag":null,"topics":["ai-gateway","llm-gateway","openai-proxy"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lxfu1.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-05-12T07:31:27.000Z","updated_at":"2026-05-13T13:04:58.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/lxfu1/ai-router","commit_stats":null,"previous_names":["lxfu1/ai-router"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/lxfu1/ai-router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lxfu1%2Fai-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lxfu1%2Fai-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lxfu1%2Fai-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lxfu1%2Fai-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lxfu1","download_url":"https://codeload.github.com/lxfu1/ai-router/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lxfu1%2Fai-router/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33684413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-30T02:00:06.278Z","response_time":92,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai-gateway","llm-gateway","openai-proxy"],"created_at":"2026-05-30T07:30:47.548Z","updated_at":"2026-05-30T07:30:47.824Z","avatar_url":"https://github.com/lxfu1.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AI Router\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"public/favicon.svg\" width=\"80\" height=\"80\" alt=\"AI Router Logo\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cstrong\u003eAI Router\u003c/strong\u003e - 开源的 LLM API 网关 / OpenAI API 代理 / 大模型负载均衡器\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003eAI API Gateway · OpenAI Compatible Proxy · Multi-Model Router · 智谱/通义/DeepSeek 统一接入\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/badge/License-MIT-blue.svg\" alt=\"License: MIT\"\u003e\u003c/a\u003e\n  \u003cimg src=\"https://img.shields.io/badge/Next.js-15-black\" alt=\"Next.js 15\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/React-19-61dafb\" alt=\"React 19\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/TypeScript-5.8-3178c6\" alt=\"TypeScript 5.8\"\u003e\n\u003c/p\u003e\n\n---\n\n## ✨ 功能特性\n\n- **🔄 OpenAI 兼容接口** - `/v1/chat/completions` 完全兼容 OpenAI Chat Completions API，一行代码迁移现有应用\n- **🤖 多模型支持** - 内置 DeepSeek V4、智谱 GLM-5、通义千问 qwen-plus、MiniMax M1\n- **⚖️ 智能路由** - `auto` 模式自动负载均衡，渠道故障自动切换，无需人工干预\n- **🔑 密钥管理** - 完整的 API Key 生命周期管理（创建/禁用/删除）+ 余额消耗控制\n- **📊 用量统计** - Token 消耗实时记录，30天趋势图表可视化\n- **🖥️ 管理后台** - 仪表盘 + 渠道健康监控 + 调用日志查询\n- **🚀 生产就绪** - Docker 一键部署，自动 HTTPS，Prometheus 监控\n\n---\n\n## 🚀 快速开始\n\n### Docker 部署（推荐）\n\n```bash\n# 克隆项目\ngit clone https://github.com/lxfu1/ai-router.git\ncd ai-router\n\n# 创建环境配置\ncp .env.example .env.local\n# 编辑 .env.local 填入你的 API 密钥\n\n# 启动服务\ndocker compose up -d\n\n# 访问 http://localhost:3000\n```\n\n### 本地开发\n\n```bash\n# 安装依赖\nnpm install\n\n# 配置环境变量\ncp .env.example .env.local\n# 编辑 .env.local 填入你的 API 密钥\n\n# 开发模式\nnpm run dev\n\n# 生产构建\nnpm run build\nnpm start\n```\n\n---\n\n## ⚙️ 环境变量\n\n### 必需配置\n\n| 变量名 | 说明 |\n|--------|------|\n| `DEEPSEEK_API_KEY` | DeepSeek API 密钥 |\n| `ZHIPU_API_KEY` | 智谱 AI API 密钥 |\n| `QWEN_API_KEY` | 通义千问 API 密钥 |\n| `ADMIN_PASSWORD` | 管理后台登录密码 |\n| `JWT_SECRET` | 会话加密密钥（随机字符串） |\n\n### 可选配置\n\n| 变量名 | 默认值 | 说明 |\n|--------|--------|------|\n| `DATABASE_TYPE` | sqlite | 数据库类型: sqlite / postgres |\n| `REDIS_URL` | - | Redis 连接串（用于缓存，留空使用内存） |\n| `ENCRYPTION_KEY` | - | API Key 加密密钥（32字节 hex） |\n| `RATE_LIMIT_ENABLED` | true | 是否启用限流 |\n| `LOG_LEVEL` | info | 日志级别: debug, info, warn, error |\n\n详见 [.env.example](.env.example)\n\n---\n\n## 📖 使用说明\n\n### 1. 创建 API Key\n\n访问管理后台 `http://localhost:3000`，使用管理员密码登录，在 **密钥管理** 页面创建 API Key。\n\n创建的 Key 格式：`sk-router-{prefix}-{random}`\n\n### 2. API 调用\n\n```bash\ncurl http://localhost:3000/v1/chat/completions \\\n  -H \"Authorization: Bearer sk-router-your-key\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"model\": \"deepseek-v4\",\n    \"messages\": [{\"role\": \"user\", \"content\": \"Hello\"}]\n  }'\n```\n\n### 3. 支持的模型\n\n| 模型参数 | 对应渠道 | 备注 |\n|---------|---------|------|\n| `deepseek-v4` | DeepSeek V4 | 最强推理 |\n| `glm-5` | 智谱 GLM-5 | 中文优化 |\n| `qwen-plus` | 通义千问 | 阿里生态 |\n| `minimax-m1` | MiniMax | 多模态 |\n| `auto` | 自动选择 | 负载均衡+故障切换 |\n\n### 4. 流式响应\n\n```bash\ncurl http://localhost:3000/v1/chat/completions \\\n  -H \"Authorization: Bearer sk-router-your-key\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"model\": \"auto\",\n    \"messages\": [{\"role\": \"user\", \"content\": \"Hello\"}],\n    \"stream\": true\n  }'\n```\n\n---\n\n## 🐍 Python SDK 示例\n\n```python\nfrom openai import OpenAI\n\nclient = OpenAI(\n    base_url=\"http://localhost:3000/v1\",\n    api_key=\"sk-router-your-key\"\n)\n\n# 使用特定模型\nresponse = client.chat.completions.create(\n    model=\"deepseek-v4\",\n    messages=[{\"role\": \"user\", \"content\": \"你好\"}]\n)\nprint(response.choices[0].message.content)\n\n# 使用自动路由\nresponse = client.chat.completions.create(\n    model=\"auto\",  # 自动选择最佳渠道\n    messages=[{\"role\": \"user\", \"content\": \"你好\"}],\n    stream=True\n)\nfor chunk in response:\n    print(chunk.choices[0].delta.content or \"\", end=\"\")\n```\n\n---\n\n## 🗂️ 项目结构\n\n```\n├── app/                    # Next.js App Router\n│   ├── api/               # API 路由\n│   │   ├── v1/chat/completions    # 核心中转接口\n│   │   ├── admin/               # 管理后台 API\n│   │   └── key/                 # 密钥管理 API\n│   ├── dashboard/         # 管理后台页面\n│   └── page.tsx           # 首页\n├── components/            # React 组件\n├── lib/                   # 工具函数/服务\n│   ├── db.ts             # 数据库封装\n│   ├── usage.ts          # 用量统计\n│   └── auth.ts           # 认证逻辑\n├── types/                 # TypeScript 类型定义\n├── public/                # 静态资源\n└── config/                # 配置文件\n```\n\n---\n\n## 🧪 开发\n\n```bash\n# 安装依赖\nnpm install\n\n# 运行测试\nnpm test\n\n# 代码检查\nnpm run lint\n\n# 类型检查\nnpx tsc --noEmit\n```\n\n---\n\n## 🐳 Docker 部署\n\n### 单机部署\n\n```bash\ndocker compose up -d\n```\n\n### 多副本部署（配合负载均衡）\n\n```yaml\n# docker-compose.yml\nservices:\n  ai-router:\n    build: .\n    deploy:\n      replicas: 3\n    environment:\n      - DATABASE_TYPE=postgres\n      - DATABASE_URL=postgresql://... \n```\n\n---\n\n## 🔐 安全建议\n\n1. **强密码**：使用高强度 `ADMIN_PASSWORD` 和 `JWT_SECRET`\n2. **HTTPS**：生产环境必须启用 HTTPS\n3. **密钥加密**：设置 `ENCRYPTION_KEY` 保护存储的 API Key\n4. **网络隔离**：不要将数据库端口暴露到公网\n5. **定期备份**：备份 `data/` 目录（SQLite）或配置自动备份（PostgreSQL）\n\n---\n\n## 📊 监控\n\n访问 `/metrics` 端点获取 Prometheus 格式指标：\n\n```\nai_router_requests_total{model=\"deepseek-v4\",status=\"success\"} 1234\nai_router_tokens_total{type=\"prompt\"} 56789\nai_router_latency_seconds{model=\"deepseek-v4\",quantile=\"0.99\"} 1.234\n```\n\n---\n\n## 🤝 贡献\n\n欢迎提交 Issue 和 Pull Request！\n\n- [Bug 报告](.github/ISSUE_TEMPLATE/bug_report.yml)\n- [功能请求](.github/ISSUE_TEMPLATE/feature_request.yml)\n- [贡献指南](CONTRIBUTING.md)\n\n---\n\n## 📄 License\n\n[MIT](LICENSE) © AI Router Contributors\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flxfu1%2Fai-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flxfu1%2Fai-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flxfu1%2Fai-router/lists"}