配置
本指南涵盖了 Semantic Router 的配置选项。系统使用单个 YAML 配置文件来控制 Signal-Driven Routing、Plugin Chain 处理和模型选择。
架构概览
配置定义了三个主要层:
- Signal Extraction Layer(信号提取层):定义 6 种类型的信号(keyword、embedding、domain、fact_check、user_feedback、preference)
- Decision Engine(决策引擎):使用 AND/OR 运算符组合信号以做出路由决策
- Plugin Chain(插件链):配置用于缓存、安全和优化的插件
配置文件
配置文件位于 config/config.yaml。以下是基于实际实现的结构:
# config/config.yaml - 实际配置结构
# 用于语义相似度的 BERT 模型
bert_model:
model_id: sentence-transformers/all-MiniLM-L12-v2
threshold: 0.6
use_cpu: true
# 语义缓存
semantic_cache:
backend_type: "memory" # 选项: "memory" 或 "milvus"
enabled: false
similarity_threshold: 0.8 # 全局默认阈值
max_entries: 1000
ttl_seconds: 3600
eviction_policy: "fifo" # 选项: "fifo", "lru", "lfu"
# 工具自动选择
tools:
enabled: false
top_k: 3
similarity_threshold: 0.2
tools_db_path: "config/tools_db.json"
fallback_to_empty: true
# Jailbreak 防护
prompt_guard:
enabled: false # 全局默认 - 可以针对每个类别覆盖
use_modernbert: true
model_id: "models/jailbreak_classifier_modernbert-base_model"
threshold: 0.7
use_cpu: true
# vLLM 端点 - 您的后端模型
vllm_endpoints:
- name: "endpoint1"
address: "192.168.1.100" # 替换为您的服务器 IP 地址
port: 11434
models:
- "your-model" # 替换为您的模型
weight: 1
# 模型配置
model_config:
"your-model":
pii_policy:
allow_by_default: true
pii_types_allowed: ["EMAIL_ADDRESS", "PERSON"]
preferred_endpoints: ["endpoint1"]
# 示例:具有自定义名称的 DeepSeek 模型
"ds-v31-custom":
reasoning_family: "deepseek" # 使用 DeepSeek 推理语法
preferred_endpoints: ["endpoint1"]
# 示例:具有自定义名称的 Qwen3 模型
"my-qwen3-model":
reasoning_family: "qwen3" # 使用 Qwen3 推理语法
preferred_endpoints: ["endpoint2"]
# 示例:不支持推理的模型
"phi4":
preferred_endpoints: ["endpoint1"]
# 分类模型
classifier:
category_model:
model_id: "models/category_classifier_modernbert-base_model"
use_modernbert: true
threshold: 0.6
use_cpu: true
pii_model:
model_id: "models/pii_classifier_modernbert-base_presidio_token_model"
use_modernbert: true
threshold: 0.7
use_cpu: true
# 信号 - 信号提取配置
signals:
# 基于关键词的信号(快速模式匹配)
keywords:
- name: "math_keywords"
operator: "OR"
keywords:
- "calculate"
- "equation"
- "solve"
- "derivative"
- "integral"
case_sensitive: false
- name: "code_keywords"
operator: "OR"
keywords:
- "function"
- "class"
- "debug"
- "compile"
case_sensitive: false
# 基于嵌入的信号(语义相似度)
embeddings:
- name: "code_debug"
threshold: 0.70
candidates:
- "how to debug the code"
- "troubleshooting steps for my code"
aggregation_method: "max"
- name: "math_intent"
threshold: 0.75
candidates:
- "solve mathematical problem"
- "calculate the result"
aggregation_method: "max"
# 领域信号(MMLU 分类)
domains:
- name: "mathematics"
description: "Mathematical and computational problems"
mmlu_categories:
- "abstract_algebra"
- "college_mathematics"
- "elementary_mathematics"
- name: "computer_science"
description: "Programming and computer science"
mmlu_categories:
- "computer_security"
- "machine_learning"
# 事实核查信号(检测验证需求)
fact_check:
- name: "needs_verification"
description: "Queries requiring fact verification"
# 用户反馈信号(满意度分析)
user_feedbacks:
- name: "correction_needed"
description: "User indicates previous answer was wrong"
# 偏好信号(基于 LLM 的匹配)
preferences:
- name: "complex_reasoning"
description: "Requires deep reasoning and analysis"
llm_endpoint: "http://localhost:11434"
# 类别 - 定义领域类别
categories:
- name: math
- name: computer science
- name: other
# 决策 - 结合信号以做出路由决策
decisions:
- name: math
description: "Route mathematical queries"
priority: 10
rules:
operator: "OR" # 匹配任何条件
conditions:
- type: "keyword"
name: "math_keywords"
- type: "embedding"
name: "math_intent"
- type: "domain"
name: "mathematics"
modelRefs:
- model: your-model
use_reasoning: true # 为数学问题启用推理
# 可选:决策级插件
plugins:
- type: "semantic-cache"
configuration:
enabled: true
similarity_threshold: 0.9 # 数学问题需要更高的阈值
- type: "jailbreak"
configuration:
enabled: true
- type: "pii"
configuration:
enabled: true
threshold: 0.8
- type: "system_prompt"
configuration:
enabled: true
prompt: "You are a mathematics expert. Solve problems step by step."
- name: computer_science
description: "Route computer science queries"
priority: 10
rules:
operator: "OR"
conditions:
- type: "keyword"
name: "code_keywords"
- type: "embedding"
name: "code_debug"
- type: "domain"
name: "computer_science"
modelRefs:
- model: your-model
use_reasoning: true # 为代码启用推理
plugins:
- type: "semantic-cache"
configuration:
enabled: true
similarity_threshold: 0.85
- type: "system_prompt"
configuration:
enabled: true
prompt: "You are a programming expert. Provide clear code examples."
- name: other
description: "Route general queries"
priority: 5
rules:
operator: "OR"
conditions:
- type: "domain"
name: "other"
modelRefs:
- model: your-model
use_reasoning: false # 通用查询不使用推理
plugins:
- type: "semantic-cache"
configuration:
enabled: true
similarity_threshold: 0.75 # 通用查询使用较低的阈值
default_model: your-model
# 推理家族配置 - 定义不同模型家族如何处理推理语法
reasoning_families:
deepseek:
type: "chat_template_kwargs"
parameter: "thinking"
qwen3:
type: "chat_template_kwargs"
parameter: "enable_thinking"
gpt-oss:
type: "reasoning_effort"
parameter: "reasoning_effort"
gpt:
type: "reasoning_effort"
parameter: "reasoning_effort"
# 全局默认推理努力等级
default_reasoning_effort: "medium"
在上面的 model_config 块中分配推理家族——每个模型使用 reasoning_family(参见示例中的 ds-v31-custom 和 my-qwen3-model)。不支持推理语法的模型只需省略该字段(例如 phi4)。
配置方案 (预设)
我们提供精心挑选的、版本化的预设,您可以直接使用或作为起点:
- 精度优化:https://github.com/vllm-project/semantic-router/blob/main/config/config.recipe-accuracy.yaml
- Token 效率优化:https://github.com/vllm-project/semantic-router/blob/main/config/config.recipe-token-efficiency.yaml
- 延迟优化:https://github.com/vllm-project/semantic-router/blob/main/config/config.recipe-latency.yaml
- 指南和用法:https://github.com/vllm-project/semantic-router/blob/main/config/RECIPES.md
快速使用:
- 本地:将方案复制到 config.yaml,然后运行
- cp config/config.recipe-accuracy.yaml config/config.yaml
- make run-router
- Helm/Argo:在您的 ConfigMap 中引用方案文件内容(示例在上述指南中)。
信号配置
信号是智能路由的基础。系统支持 6 种类型的信号,可以组合起来做出路由决策。
1. 关键词信号 - 快速模式匹配
signals:
keywords:
- name: "math_keywords"
operator: "OR" # OR: 匹配任意关键词, AND: 匹配所有关键词
keywords:
- "calculate"
- "equation"
- "solve"
case_sensitive: false
用例:
- 针对特定术语的确定性路由
- 合规性和安全性(PII 关键词、违禁术语)
- 需要 <1ms 延迟的高吞吐量场景
2. 嵌入信号 - 语义理解
signals:
embeddings:
- name: "code_debug"
threshold: 0.70 # 相似度阈值 (0-1)
candidates:
- "how to debug the code"
- "troubleshooting steps"
aggregation_method: "max" # max, avg, 或 min
用例:
- 对释义具有鲁棒性的意图检测
- 语义相似度匹配
- 处理多样化的用户措辞
3. 领域信号 - MMLU 分类
signals:
domains:
- name: "mathematics"
description: "Mathematical problems"
mmlu_categories:
- "abstract_algebra"
- "college_mathematics"
用例:
- 学术和专业领域路由
- 领域专家模型选择
- 支持 14 个 MMLU 类别
4. 事实核查信号 - 验证需求检测
signals:
fact_check:
- name: "needs_verification"
description: "Queries requiring fact verification"
用例:
- 识别事实查询与创意/代码任务
- 路由到具有幻觉检测的模型
- 触发事实核查插件
5. 用户反馈信号 - 满意度分析
signals:
user_feedbacks:
- name: "correction_needed"
description: "User indicates previous answer was wrong"
用例:
- 处理后续更正("that's wrong", "try again")
- 检测满意度水平
- 路由到更强大的模型进行重试
6. 偏好信号 - 基于 LLM 的匹配
signals:
preferences:
- name: "complex_reasoning"
description: "Requires deep reasoning"
llm_endpoint: "http://localhost:11434"
用例:
- 通过外部 LLM 进行复杂意图分析
- 细致的路由决策
- 当其他信号不足时
决策规则 - 信号融合
使用 AND/OR 运算符组合信号:
decisions:
- name: math
description: "Route mathematical queries"
priority: 10
rules:
operator: "OR" # 匹配任意条件
conditions:
- type: "keyword"
name: "math_keywords"
- type: "embedding"
name: "math_intent"
- type: "domain"
name: "mathematics"
modelRefs:
- model: math-specialist
weight: 1.0
策略:
- 基于优先级:首先评估优先级较高的决策
- 基于置信度:选择置信度得分最高的决策
- 混合:结合优先级和置信度
插件链配置
插件在链中处理请求/响应。每个决策都可以覆盖全局插件设置。
全局插件配置
# 全局默认值
semantic_cache:
enabled: true
similarity_threshold: 0.8
prompt_guard:
enabled: true
threshold: 0.7
classifier:
pii_model:
enabled: true
threshold: 0.8
决策级插件覆盖
decisions:
- name: math
description: "Route mathematical queries"
priority: 10
plugins:
- type: "semantic-cache"
configuration:
enabled: true
similarity_threshold: 0.9 # 数学问题更高
- type: "jailbreak"
configuration:
enabled: true
- type: "pii"
configuration:
enabled: true
threshold: 0.8
- type: "system_prompt"
configuration:
enabled: true
prompt: "You are a mathematics expert."
- type: "header_mutation"
configuration:
enabled: true
headers:
X-Math-Mode: "enabled"
- type: "hallucination"
configuration:
enabled: false # 可选的实时检测
插件类型
| 插件 | 描述 | 配置 |
|---|---|---|
| semantic-cache | 基于语义相似度的缓存 | similarity_threshold, ttl_seconds |
| jailbreak | 对抗性提示词检测 | threshold, model_id |
| pii | PII 检测和脱敏 | threshold, pii_types_allowed |
| system_prompt | 动态提示词注入 | prompt |
| header_mutation | HTTP Header 操控 | headers |
| hallucination | Token 级幻觉检测 | enabled |
关键配置部分
后端端点
配置您的 LLM 服务器:
vllm_endpoints:
- name: "my_endpoint"
address: "127.0.0.1" # 您的服务器 IP - 必须是 IP 地址格式
port: 8000 # 您的服务器端口
weight: 1 # 负载均衡权重
# 模型配置 - 将模型映射到端点
model_config:
"llama2-7b": # 模型名称 - 必须与 vLLM --served-model-name 匹配
preferred_endpoints: ["my_endpoint"]
"qwen3": # 由同一端点服务的另一个模型
preferred_endpoints: ["my_endpoint"]
示例:Llama / Qwen 后端配置
vllm_endpoints:
- name: "local-vllm"
address: "127.0.0.1"
port: 8000
model_config:
"llama2-7b":
preferred_endpoints: ["local-vllm"]
"qwen3":
preferred_endpoints: ["local-vllm"]
地址格式要求
重要:address 字段必须包含有效的 IP 地址(IPv4 或 IPv6)。不支持域名和其他格式。
✅ 支持的格式:
# IPv4 地址
address: "127.0.0.1"
# IPv6 地址
address: "2001:db8::1"
❌ 不支持的格式:
# 域名
address: "localhost" # ❌ 请使用 127.0.0.1 代替
address: "api.openai.com" # ❌ 请使用 IP 地址代替
# 协议前缀
address: "http://127.0.0.1" # ❌ 删除协议前缀
# 路径
address: "127.0.0.1/api" # ❌ 删除路径,仅使用 IP
# 地址中的端口
address: "127.0.0.1:8080" # ❌ 使用单独的 'port' 字段
模型名称一致性
model_config 中的模型名称必须与启动 vLLM 服务器时使用的 --served-model-name 参数完全匹配:
# vLLM 服务器命令(示例):
vllm serve meta-llama/Llama-2-7b-hf --served-model-name llama2-7b --port 8000
vllm serve Qwen/Qwen3-1.8B --served-model-name qwen3 --port 8000
# config.yaml 必须在 model_config 中引用模型:
model_config:
"llama2-7b": # ✅ 匹配 --served-model-name
preferred_endpoints: ["your-endpoint"]
"qwen3": # ✅ 匹配 --served-model-name
preferred_endpoints: ["your-endpoint"]
模型设置
配置模型特定的设置:
model_config:
"llama2-7b":
pii_policy:
allow_by_default: true # 默认允许 PII
pii_types_allowed: ["EMAIL_ADDRESS", "PERSON"]
preferred_endpoints: ["my_endpoint"] # 可选:指定可以为该模型提供服务的端点
"gpt-4":
pii_policy:
allow_by_default: false
# preferred_endpoints 省略 - 路由器将不设置端点头
# 当外部负载均衡器处理端点选择时很有用
关于 preferred_endpoints 的说明:
- 可选字段:如果省略,路由将不会设置
x-vsr-destination-endpoint头 - 如果指定:路由根据权重选择最佳端点并设置头
- 如果省略:上游负载均衡器或服务网格处理端点选择
- 验证:在类别中使用或作为
default_model的模型必须配置preferred_endpoints
定价(可选)
如 果您希望路由计算请求成本并公开 Prometheus 成本指标,请在 model_config 的每个模型下添加每 100 万 token 的定价和货币。
model_config:
phi4:
pricing:
currency: USD
prompt_per_1m: 0.07
completion_per_1m: 0.35
"mistral-small3.1":
pricing:
currency: USD
prompt_per_1m: 0.1
completion_per_1m: 0.3
gemma3:27b:
pricing:
currency: USD
prompt_per_1m: 0.067
completion_per_1m: 0.267
- 成本公式:
(prompt_tokens * prompt_per_1m + completion_tokens * completion_per_1m) / 1_000_000(使用给定货币)。 - 如果未配置,路由仍会报告 token 和延迟指标;成本视为 0。
分类模型
配置 BERT 分类模型:
classifier:
category_model:
model_id: "models/category_classifier_modernbert-base_model"
use_modernbert: true
threshold: 0.6 # 分类置信度阈值
use_cpu: true # 使用 CPU(不需要 GPU)
pii_model:
model_id: "models/pii_classifier_modernbert-base_presidio_token_model"
threshold: 0.7 # PII 检测阈值
use_cpu: true
类别和路由
使用基于决策的路由系统定义如何处理不同的查询类型:
# 类别定义用于分类的领域
categories:
- name: math
- name: computer science
- name: other
# 决策定义带有规则和模型选择的路由逻辑
decisions:
- name: math
description: "Route mathematical queries"
priority: 10
rules:
operator: "OR"
conditions:
- type: "domain"
name: "math"
modelRefs:
- model: your-model
use_reasoning: true # 为该模型在数学问题上启用推理
- name: computer science
description: "Route computer science queries"
priority: 10
rules:
operator: "OR"
conditions:
- type: "domain"
name: "computer science"
modelRefs:
- model: your-model
use_reasoning: true # 为代码启用推理
- name: other
description: "Route general queries"
priority: 5
rules:
operator: "OR"
conditions:
- type: "domain"
name: "other"
modelRefs:
- model: your-model
use_reasoning: false # 通用查询不进行推理
default_model: your-model # 回退模型
模型特定的推理
use_reasoning 字段在每个决策的 modelRefs 中为每个模型配置,允许细粒度控制:
decisions:
- name: math
description: "Route mathematical queries"
priority: 10
rules:
operator: "OR"
conditions:
- type: "domain"
name: "math"
modelRefs:
- model: gpt-oss-120b
use_reasoning: true # GPT-OSS-120b 支持数学推理
- model: phi4
use_reasoning: false # phi4 不支持推理模式
- model: deepseek-v31
use_reasoning: true # DeepSeek 支持数学推理