基于 ML 的模型选择
本文档介绍 Semantic Router 中基于机器学习的模型选择技术的配置方式和实验结果。
概述
基于 ML 的模型选择通过机器学习算法,根据查询特征和历史性能数据,将请求路由到最合适的 LLM。在基准测试 中,ML 路由的质量分数比随机选择高出 13%-45%。
支持的算法
| 算法 | 描述 | 适用场景 |
|---|---|---|
| KNN(K-Nearest Neighbors,K 近邻) | 基于相似查询的质量加权投票 | 高精度,多样化查询类型 |
| KMeans | 基于聚类的路由,优化效率 | 快速推理,均衡负载 |
| SVM(Support Vector Machine,支持向量机) | RBF 核决策边界 | 领域边界清晰的场景 |
参考论文
- FusionFactory (arXiv:2507.10540) — 基于 LLM 路由器的查询级融合
- Avengers-Pro (arXiv:2508.12631) — 性能-效率优化路由
配置
基础配置
在 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"
算法参数
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)
实验结果
基准测试设置
- 测试查询: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 性能均衡,提升 34%,决策边界清晰
- KNN 模型选择多样化,根据查询相似度调用不同模型
架构
┌─────────────────────────────────────────────────────────────────────┐
│ 在线推理 │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ 请求 (model="auto") │
│ ↓ │
│ 生成查询 embedding (Qwen3, 1024 维) │
│ ↓ │
│ 添加类别 One-Hot (14 维) → 1038 维特征向量 │
│ ↓ │
│ 决策引擎 → 按领域匹配决策 │
│ ↓ │
│ 加载 ML 选择器 (从 JSON 加载 KNN/KMeans/SVM) │
│ ↓ │
│ 执行推理 → 选择最佳模型 │
│ ↓ │
│ 路由到选中的 LLM 端点 │
│ │
└─────────────────────────────────────────────────────────────────────┘