基于 ML 的模型选择
本文说明 Semantic Router 中基于 ML 的模型选择配置与实验数据。
概述
基于 ML 的路由模型通过算法分析查询特征和历史性能数据,匹配最佳 LLM。基准测试显示,其质量分数比随机选择高出 13%-45%。
支持的算法
| 算法 | 描述 | 适用场景 |
|---|---|---|
| KNN(K-Nearest Neighbors,K 近邻) | 基于相似查询的质量加权投票 | 高精度,多样化查询类型 |
| KMeans | 基于聚类的路由,优化效率 | 快速推理,均衡负载 |
| SVM(Support Vector Machine,支持向量机) | RBF 核决策边界 | 领域边界清晰的场景 |
| MLP(Multi-Layer Perceptron,多层感知机) | GPU 加速神经网络 | 高吞吐量、GPU 可用环境 |
参考论文
- FusionFactory (arXiv:2507.10540) — 基于 LLM 路由器的查询级融合
- Avengers-Pro (arXiv:2508.12631) — 性能-效率优化路由
Dashboard GUI 设置
Semantic Router Dashboard 提供图形化的三步配置向导。这是推荐的入门方式,完全免除 CLI 操作。
打开向导
访问 http://localhost:8700/ml-setup(或您的 Dashboard 地址)。
第一步:基准测试
上传您的 models YAML(列出 LLM 端点)和 queries JSONL 文件(含 ground truth 的测试查询)。配置并发数和最大 token 数,点击 Run Benchmark。进度实时流式显示,精确到每条查询。
第二步:训练
选择一个或多个算法:
| 算法 | 是否需要 GPU | 说明 |
|---|---|---|
| KNN | 否 | 仅 CPU(scikit-learn) |
| K-Means | 否 | 仅 CPU(scikit-learn) |
| SVM | 否 | 仅 CPU(scikit-learn) |
| MLP | 可选 | PyTorch — 选中后出现 Device 选择器(CPU/CUDA) |
通过 高级设置 面板调整超参数,点击 Train Models。训练好的模型文件(knn_model.json、kmeans_model.json 等)保存到固定目录 ml-train/。
第三步:生成配置
定义路由决策——每个决策包含名称、优先级、算法、领域条件和目标模型名称。点击 Generate Config 即可生成可直接部署的 ml-model-selection-values.yaml。
生成的 YAML 遵循 semantic-router 配置 schema。将 model_selection 和 decisions 部分合并到主 config.yaml,或直接作为独立配置文件供路由器使用。
示例生成配置
config:
model_selection:
enabled: true
ml:
models_path: /data/ml-pipeline/ml-train
embedding_dim: 1024
knn:
k: 5
pretrained_path: /data/ml-pipeline/ml-train/knn_model.json
strategy: priority
decisions:
- name: math-decision
priority: 100
rules:
operator: OR
conditions:
- type: domain
name: math
algorithm:
type: knn
modelRefs:
- model: llama3.2:3b
use_reasoning: false
配置
基础配置
在 config.yaml 中启用基于 ML 的模型选择:
# 启用 ML 模型选择
model_selection:
ml:
enabled: true
models_path: ".cache/ml-models" # 训练好的模型文件路径
# 查询表示的嵌入模型
embedding_models:
qwen3_model_path: "models/mom-embedding-pro" # Qwen3-Embedding-0.6B
按决策类型配置算法
为不同的决策类型配置不同的算法:
decisions:
# 数学查询 - 使用 KNN 进行质量加权选择
- name: "math_decision"
description: "Mathematics and quantitative reasoning"
priority: 100
rules:
operator: "AND"
conditions:
- type: "domain"
name: "math"
algorithm:
type: "knn"
knn:
k: 5
modelRefs:
- model: "llama-3.2-1b"
- model: "llama-3.2-3b"
- model: "mistral-7b"
# 编程查询 - 使用 SVM 实现清晰边界
- name: "code_decision"
description: "Programming and software development"
priority: 100
rules:
operator: "AND"
conditions:
- type: "domain"
name: "computer science"
algorithm:
type: "svm"
svm:
kernel: "rbf"
gamma: 1.0
modelRefs:
- model: "codellama-7b"
- model: "llama-3.2-3b"
- model: "mistral-7b"
# 通用查询 - 使用 KMeans 追求效率
- name: "general_decision"
description: "General knowledge queries"
priority: 50
rules:
operator: "AND"
conditions:
- type: "domain"
name: "other"
algorithm:
type: "kmeans"
kmeans:
num_clusters: 8
modelRefs:
- model: "llama-3.2-1b"
- model: "llama-3.2-3b"
- model: "mistral-7b"
# 高吞吐量查询 - 使用 MLP GPU 加速
- name: "gpu_accelerated_decision"
description: "High-volume inference with GPU"
priority: 100
rules:
operator: "AND"
conditions:
- type: "domain"
name: "engineering"
algorithm:
type: "mlp"
mlp:
device: "cuda" # 或 "cpu", "metal"
modelRefs:
- model: "llama-3.2-1b"
- model: "llama-3.2-3b"
- model: "mistral-7b"
- model: "codellama-7b"
算法参数
KNN 参数
algorithm:
type: "knn"
knn:
k: 5 # 邻居数量(默认:5)
KMeans 参数
algorithm:
type: "kmeans"
kmeans:
num_clusters: 8 # 聚类数量(默认:8)
SVM 参数
algorithm:
type: "svm"
svm:
kernel: "rbf" # 核函数类型:rbf、linear(默认:rbf)
gamma: 1.0 # RBF 核的 gamma 值(默认:1.0)
MLP 参数
algorithm:
type: "mlp"
mlp:
device: "cuda" # 设备:cpu、cuda、metal(默认:cpu)
MLP(多层感知机)通过 Candle Rust 框架实现 GPU 加速推理。专为具备 GPU 资源的生产环境设计,提供极高的吞吐量。
设备选项:
| 设备 | 描述 | 要求 |
|---|---|---|
cpu | CPU 推理(默认) | 无特殊硬件要求 |
cuda | NVIDIA GPU 加速 | 支持 CUDA 的 GPU + CUDA 工具包 |
metal | Apple Silicon GPU | 搭载 M1/M2/M3 芯片的 macOS |
实验结果
基准测试设置
- 测试查询:109 条跨多个领域的查询
- 评估模型:4 个 LLM(codellama-7b、llama-3.2-1b、llama-3.2-3b、mistral-7b)
- 嵌入模型:Qwen3-Embedding-0.6B(1024 维)
- 验证数据:带有真实性能评分的基准查询
性能对比
| 策略 | 平均质量 | 平均延迟 | 最佳模型命中率 |
|---|---|---|---|
| Oracle(理论最优) | 0.495 | 10.57s | 100.0% |
| KMEANS 选择 | 0.252 | 20.23s | 23.9% |
| 始终使用 llama-3.2-3b | 0.242 | 25.08s | 15.6% |
| SVM 选择 | 0.233 | 25.83s | 14.7% |
| 始终使用 mistral-7b | 0.215 | 70.08s | 13.8% |
| 始终使用 llama-3.2-1b | 0.212 | 3.65s | 26.6% |
| KNN 选择 | 0.196 | 36.62s | 13.8% |
| 随机选择 | 0.174 | 40.12s | 9.2% |
| 始终使用 codellama-7b | 0.161 | 53.78s | 4.6% |
ML 路由相对随机选择的提升
| 算法 | 质量提升 | 最佳模型选择率 |
|---|---|---|
| KMEANS | +45.5% | 提高 2.6 倍 |
| SVM | +34.4% | 提高 1.6 倍 |
| KNN | +13.1% | 提高 1.5 倍 |
关键发现
- 所有 ML 方法均优于随机挑选
- KMEANS 提升最显著(+45%)
- SVM 决策边界清晰
- KNN 提供多样化的高质量匹配
- MLP 独占 GPU 加速能力
MLP GPU 加速
MLP 算法利用 Candle Rust 框架进行 GPU 加速推理:
| 设备 | 推理延迟 | 吞吐量 |
|---|---|---|
| CPU | ~5-10ms | ~100-200 QPS |
| CUDA(NVIDIA) | ~0.5-1ms | ~1000+ QPS |
| Metal(Apple) | ~1-2ms | ~500+ QPS |
适用场景:
- 拥有 GPU 资源的高吞吐量生产环境
- 对延迟敏感、需要亚毫秒推理的应用
- 需要最小化模型选择开销的场景
架构
┌─────────────────────────────────────────────────────────────────────┐
│ 在线推理 │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ 请求 (model="auto") │
│ ↓ │
│ 生成查询 embedding (Qwen3, 1024 维) │
│ ↓ │
│ 添加类别 One-Hot (14 维) → 1038 维特征向量 │
│ ↓ │
│ 决策引擎 → 按领域匹配决策 │
│ ↓ │
│ 加载 ML 选择器 (从 JSON 加载 KNN/KMeans/SVM/MLP) │
│ ↓ │
│ 执行推理 → 选择最佳模型 │
│ ↓ │
│ 路由到选中的 LLM 端点 │
│ │
└─────────────────────────────────────────────────────────────────────┘
训练自有模型
离线训练与在线推理:
- 离线训练:使用 Python + scikit-learn 完成 KNN、KMeans 和 SVM 的训练,使用 PyTorch 完成 MLP 的训练
- 在线推理:KNN/KMeans/SVM 使用 Rust 中的 Linfa(通过
ml-binding