基于领域的路由
本指南展示如何使用微调的分类模型进行基于学术和专业领域的智能路由。领域路由使用带有 LoRA 适配器的专用模型(ModernBERT、Qwen3-Embedding、EmbeddingGemma)将查询分类到数学、物理、法律 、商业等类别。
核心优势
- 高效:带有 LoRA 适配器的微调模型提供快速推理(5-20ms)和高准确率
- 专业化:多种模型选项(ModernBERT 适用于英语,Qwen3 适用于多语言/长上下文,Gemma 适用于小内存占用)
- 多任务:LoRA 使得通过共享基础模型运行多个分类任务(领域 + PII + 越狱检测)成为可能
- 成本效益:比基于 LLM 的分类延迟更低,无 API 成本
解决什么问题?
通用分类方法难以处理领域特定术语和学术/专业领域之间的细微差异。领域路由提供:
- 准确的领域检测:微调模型区分数学、物理、化学、法律、商业等
- 多任务效率:LoRA 适配器通过一次基础模型推理同时进行领域分类、PII 检测和越狱检测
- 长上下文支持:Qwen3-Embedding 支持高达 32K tokens(相比 ModernBERT 的 8K 限制)
- 多语言路由:Qwen3 在 100+ 种语言上训练,ModernBERT 针对英语优化
- 资源优化:仅对受益的领域(数学、物理、化学)启用昂贵的推理
适用场景
- 教育平台:涵盖多样化学科领域(STEM、人文、社 会科学)
- 专业服务:需要领域专业知识(法律、医疗、金融)
- 企业知识库:跨越多个部门
- 研究辅助工具:需要学术领域意识
- 多领域产品:分类准确性至关重要
配置
在 config.yaml 中配置领域分类器:
# 定义领域信号
signals:
domains:
- name: "math"
description: "数学和定量推理"
mmlu_categories: ["math"]
- name: "physics"
description: "物理和物理科学"
mmlu_categories: ["physics"]
- name: "computer_science"
description: "编程和计算机科学"
mmlu_categories: ["computer_science"]
- name: "business"
description: "商业和管理"
mmlu_categories: ["business"]
- name: "health"
description: "健康和医疗信息"
mmlu_categories: ["health"]
- name: "law"
description: "法律和监管信息"
mmlu_categories: ["law"]
- name: "other"
description: "通用查询"
mmlu_categories: ["other"]
# 使用领域信号定义决策
decisions:
- name: math
description: "路由数学查询"
priority: 100
rules:
operator: "OR"
conditions:
- type: "domain"
name: "math"
modelRefs:
- model: "openai/gpt-oss-120b"
use_reasoning: true
plugins:
- type: "system_prompt"
configuration:
system_prompt: "你是一位数学专家。提供带有清晰解释的逐步解决方案。"
- name: physics
description: "路由物理查询"
priority: 100
rules:
operator: "OR"
conditions:
- type: "domain"
name: "physics"
modelRefs:
- model: "openai/gpt-oss-120b"
use_reasoning: true
plugins:
- type: "system_prompt"
configuration:
system_prompt: "你是一位对物理定律有深刻理解的物理专家。用数学推导清晰地解释概念。"
- name: computer_science
description: "路由计算机科学查询"
priority: 100
rules:
operator: "OR"
conditions:
- type: "domain"
name: "computer_science"
modelRefs:
- model: "openai/gpt-oss-120b"
use_reasoning: false
plugins:
- type: "system_prompt"
configuration:
system_prompt: "你是一位精通算法和数据结构的计算机科学专家。提供清晰的代码示 例。"
- name: business
description: "路由商业查询"
priority: 100
rules:
operator: "OR"
conditions:
- type: "domain"
name: "business"
modelRefs:
- model: "openai/gpt-oss-120b"
use_reasoning: false
plugins:
- type: "system_prompt"
configuration:
system_prompt: "你是一位资深商业顾问和战略顾问。"
- name: health
description: "路由健康查询"
priority: 100
rules:
operator: "OR"
conditions:
- type: "domain"
name: "health"
modelRefs:
- model: "openai/gpt-oss-120b"
use_reasoning: false
plugins:
- type: "system_prompt"
configuration:
system_prompt: "你是一位健康和医疗信息专家。"
- type: "semantic-cache"
configuration:
enabled: true
similarity_threshold: 0.95
- name: law
description: "路由法律查询"
priority: 100
rules:
operator: "OR"
conditions:
- type: "domain"
name: "law"
modelRefs:
- model: "openai/gpt-oss-120b"
use_reasoning: false
plugins:
- type: "system_prompt"
configuration:
system_prompt: "你是一位知识渊博的法律专家。"
- name: general_route
description: "通用查询的默认回退路由"
priority: 50
rules:
operator: "OR"
conditions:
- type: "domain"
name: "other"
modelRefs:
- model: "openai/gpt-oss-120b"
use_reasoning: false
plugins:
- type: "semantic-cache"
configuration:
enabled: true
similarity_threshold: 0.85
支持的领域
学术类:数学、物理、化学、生物、计算机科学、工程
专业类:商业、法律、经济、健康、心理学
通用类:哲学、历史、其他
功能特性
- PII 检测:自动检测和处理敏感信息
- 语义缓存:缓存相似查询以加快响应
- 推理控制:按领域启用/禁用推理
- 自定义阈值:按类别调整缓存敏感度
请求示例
# 数学查询(启用推理)
curl -X POST http://localhost:8801/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MoM",
"messages": [{"role": "user", "content": "求解:x^2 + 5x + 6 = 0"}]
}'
# 商业查询(禁用推理)
curl -X POST http://localhost:8801/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MoM",
"messages": [{"role": "user", "content": "什么是 SWOT 分析?"}]
}'
# 健康查询(高缓存阈值)
curl -X POST http://localhost:8801/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "MoM",
"messages": [{"role": "user", "content": "糖尿病的症状有哪些?"}]
}'
真实用例
1. 使用 LoRA 的多任务分类(高效)
问题:每个请求都需要领域分类 + PII 检测 + 越狱检测 解决方案:LoRA 适配器通过一次基础模型推理运行所有 3 个任务,而非 3 个独立模型 影响:比运行 3 个完整模型快 3 倍,每个任务小于 1% 参数开销
2. 长文档分析(专业化 - Qwen3)
问题:研究论文和法律文档超过 ModernBERT 的 8K token 限制 解决方案:Qwen3-Embedding 支持高达 32K tokens,无需截断 影响:完整文档准确分类,截断不会丢失信息
3. 多语言教育平台(专业化 - Qwen3)
问题:学生用 100+ 种语言提问,ModernBERT 仅限于英语 解决方案:Qwen3-Embedding 在 100+ 种语言上训练,处理多语言路由 影响:单一模型服务全球用户,跨语言质量一致
4. 边缘部署(专业化 - Gemma)
问题:移动/IoT 设备无法运行大型分类模型 解决方案:EmbeddingGemma-300M 配合 Matryoshka 嵌入(128-768 维) 影响:模型小 5 倍,在小于 100MB 内存的边缘设备上运行
5. STEM 辅导平台(高效推理控制)
问题:数学/物理需要推理,但历史/文学不需要 解决方案:领域分类器将 STEM → 推理模型,人文 → 快速模型 影响:STEM 准确率提高 2 倍,非 STEM 查询节省 60% 成本