Skip to content

qingchencloud/smart-router

Repository files navigation

SmartRouter

智能 LLM 路由代理 — 三层架构自动选择最合适的模型,完整支持 OpenAI API 协议。

特性

  • 三层智能路由:快速规则 → LLM 分类器(带缓存)→ 模型选择
  • 透明代理:完整透传 OpenAI /v1/chat/completions 协议,只替换 model 字段
  • Tool Calling:完整支持 tools / tool_choice / tool_calls 透传
  • 流式响应:SSE (Server-Sent Events) 逐 chunk 透传
  • 多轮对话:messages 数组完整转发,不丢失上下文
  • 全配置化:模型、规则、分类器全部在 config.yaml 中配置,改配置不改代码
  • 异步高并发:基于 aiohttp,支持并发请求
  • 零侵入:不修改请求体(除 model)和响应体,上游返回什么就返回什么

路由架构

请求进入
    │
    ▼
┌─ Layer 1: 快速规则 (<1ms) ──────────────────────────┐
│  • 带 tools 参数 → code tier                         │
│  • 消息含代码块 → code tier                           │
│  • system prompt 含代码/推理关键词 → 对应 tier         │
│  • 短消息 (<10字符) → simple tier                     │
│  命中 → 直接路由(拦截 60-70% 请求)                   │
└──────────────────────────────────────────────────────┘
    │ 未命中
    ▼
┌─ Layer 2: LLM 分类器 (带缓存) ──────────────────────┐
│  • 用便宜模型做一次任务分类                            │
│  • 结果缓存,相似请求直接命中                          │
│  • 超时/异常自动 fallback 到 general                  │
└──────────────────────────────────────────────────────┘
    │
    ▼
┌─ Layer 3: 模型选择 ─────────────────────────────────┐
│  根据 tier 从 config.yaml 选择首选模型                │
└──────────────────────────────────────────────────────┘

默认 Tier 配置

Tier 场景 默认模型
simple 问候、闲聊 MiniMax-M2.5
code 代码、调试、技术实现 GLM-5
reasoning 数学推理、逻辑证明 SSY-DeepSeek-V3.2
creative 创意写作、文案、翻译 SSY-GPT-5.2
general 通用对话、知识问答 MiniMax-M2.5
premium 手动指定 smart-premium claude-opus-4-6

所有模型和 tier 均可在 config.yaml 中自定义。

快速开始

Docker(推荐)

git clone https://github.com/qingchencloud/smart-router.git
cd smart-router

cp .env.example .env
# 编辑 .env 填入上游 API 密钥
# 编辑 config.yaml 自定义模型和路由规则

docker compose up -d

curl http://localhost:30081/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"auto","messages":[{"role":"user","content":"你好"}]}'

本地运行

pip install aiohttp pyyaml

export NEW_API_URL="http://127.0.0.1:30080/v1/chat/completions"
export NEW_API_KEY="sk-your-api-key"

python server.py

API

POST /v1/chat/completions

完全兼容 OpenAI Chat Completions API,所有参数原样透传。

model 行为
auto / smart-auto 三层路由自动选择模型
smart-premium 使用 premium tier 模型
其他已知模型名 直接使用该模型

POST /route

查看路由决策(调试用):

curl -X POST http://localhost:30081/route \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "user", "content": "证明勾股定理"}]}'

GET /v1/models

返回可用模型列表。

配置

环境变量 (.env)

变量 说明 默认值
NEW_API_URL 上游 API 地址 http://127.0.0.1:30080/v1/chat/completions
NEW_API_KEY 上游 API 密钥 (必填)
SMART_ROUTER_PORT 服务端口 30081

config.yaml

所有路由逻辑均在 config.yaml 中配置:

  • tiers: 每个 tier 的模型列表,第一个为首选
  • fast_rules: Layer 1 快速规则,按顺序匹配
  • classifier: Layer 2 LLM 分类器(模型、缓存、超时、prompt)
  • upstream: 上游 API(支持 ${ENV_VAR} 语法)

响应头

X-SmartRouter-Model: GLM-5

架构

SmartRouter/
├── server.py          # aiohttp 异步透明代理服务
├── router.py          # 三层路由决策模块
├── config.yaml        # 全量配置(模型、规则、分类器)
├── Dockerfile
├── docker-compose.yml
└── .env.example

License

MIT

About

智能 LLM 路由透明代理 - 根据请求内容自动选择最便宜的模型,完整支持 OpenAI API 协议

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors