基于 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"