prompt-router

基于文本匹配的快速路由引擎,为简单任务提供零 LLM 决策的快速路径。支持中英文混合输入,自动匹配技能/工具,低置信度时降级到 LLM 语义路由。

3,891 stars

Best use case

prompt-router is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

基于文本匹配的快速路由引擎,为简单任务提供零 LLM 决策的快速路径。支持中英文混合输入,自动匹配技能/工具,低置信度时降级到 LLM 语义路由。

Teams using prompt-router should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/prompt-router/SKILL.md --create-dirs "https://raw.githubusercontent.com/openclaw/skills/main/skills/aiwell0721/prompt-router/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/prompt-router/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How prompt-router Compares

Feature / Agentprompt-routerStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

基于文本匹配的快速路由引擎,为简单任务提供零 LLM 决策的快速路径。支持中英文混合输入,自动匹配技能/工具,低置信度时降级到 LLM 语义路由。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

# Prompt-Router 技能

> 🚀 基于文本匹配的快速路由引擎,为简单任务提供 **零 LLM 决策** 的快速路径。

## 核心价值

- ⚡ **极速响应** - <5ms 路由决策(vs 500ms+ LLM 推理)
- 💰 **零成本** - 简单任务无需 LLM 调用
- 🛡️ **可降级** - LLM 故障时仍可工作
- 🎯 **确定性** - 相同输入始终相同输出

---

## ⚡ 快速开始

### 1. 加载技能

```python
from skills.prompt_router.scripts.router import PromptRouter

# 创建路由器
router = PromptRouter(
    skills_dir='skills/',
    confidence_threshold=0.6  # 置信度阈值
)

# 加载技能元数据
router.load_skills()
```

### 2. 路由 Prompt

```python
# 路由用户输入
result = router.route("搜索 Python 教程")

if result.match:
    print(f"匹配技能:{result.match['name']}")
    print(f"置信度:{result.confidence:.2f} ({result.confidence_level})")
else:
    print("未找到匹配,降级到 LLM")
```

### 3. 决策流程

```python
if router.should_invoke_skill(result):
    # 高置信度:直接调用技能
    skill_name = result.match['name']
    invoke_skill(skill_name)
else:
    # 低置信度:降级到 LLM
    fallback_to_llm()
```

---

## 🎯 工作原理

### 路由流程

```
用户 Prompt
    ↓
分词处理(Tokenizer)
    ↓
评分匹配(Scorer)
    ↓
排序选择(Router)
    ↓
{
    高置信度 (≥0.8) → 直接调用技能
    中置信度 (0.6-0.8) → 建议用户确认
    低置信度 (<0.6) → 降级到 LLM
}
```

### 评分算法

```python
# 多字段加权匹配
总分 = Σ(字段匹配分 × 权重)

字段权重:
- name: 3.0(名称匹配最重要)
- triggers: 2.5(触发词权重高)
- keywords: 2.0(关键词)
- description: 1.5(描述)
```

---

## 📋 配置选项

### 置信度阈值

| 阈值 | 默认值 | 说明 |
|------|--------|------|
| `confidence_threshold` | 0.6 | 低于此值降级到 LLM |
| `high_confidence_threshold` | 0.8 | 高于此值直接调用 |

**调整建议:**
- 提高阈值 → 更准确,但更多降级到 LLM
- 降低阈值 → 更快,但可能误匹配

### 技能目录

```python
# 默认:当前 skills/ 目录
router = PromptRouter()

# 自定义目录
router = PromptRouter(skills_dir='/path/to/skills')
```

---

## 🔍 路由示例

### 示例 1:天气查询

```python
prompt = "北京今天天气怎么样"
result = router.route(prompt)

# 输出:
# 匹配:weather
# 分数:8.50
# 置信度:0.85 (high)
# 调用技能:True
```

### 示例 2:文件读取

```python
prompt = "读取 config.json 文件"
result = router.route(prompt)

# 输出:
# 匹配:read
# 分数:7.20
# 置信度:0.72 (medium)
# 调用技能:True
```

### 示例 3:复杂任务

```python
prompt = "帮我搭建一个完整的自动化工作流"
result = router.route(prompt)

# 输出:
# 匹配:None
# 分数:2.10
# 置信度:0.21 (low)
# 调用技能:False
# 降级到 LLM:True
```

---

## 📊 性能指标

| 指标 | 目标值 | 实测值 |
|------|--------|--------|
| 路由延迟 | <10ms | ~3ms |
| 分词速度 | <1ms | ~0.5ms |
| 评分速度 | <5ms | ~2ms |
| 内存占用 | <10MB | ~5MB |
| 启动时间 | <100ms | ~50ms |

---

## 🎯 适用场景

### ✅ 推荐使用

| 场景 | 示例 | 收益 |
|------|------|------|
| **简单查询** | "天气"、"搜索"、"读取" | 延迟 -80%,成本 -50% |
| **工具调用** | "打开浏览器"、"执行命令" | 确定性行为 |
| **高频操作** | 自动化脚本、语音助手 | 实时响应 |
| **LLM 故障** | API 限流、网络中断 | 降级方案 |

### ⚠️ 不推荐

| 场景 | 原因 |
|------|------|
| **复杂任务规划** | 需要 LLM 语义理解 |
| **创意写作** | 需要 LLM 创造力 |
| **多步骤推理** | 需要 LLM 逻辑能力 |
| **模糊意图** | 需要 LLM 澄清 |

---

## 🔧 高级功能

### 1. 自定义同义词

```python
# 添加同义词映射
synonyms = {
    '搜索': ['查找', 'search', 'find'],
    '天气': ['气温', 'weather', 'temperature'],
    '读取': ['打开', '查看', 'read'],
}

# 在分词时扩展
tokens = tokenizer.tokenize(prompt)
expanded_tokens = set()
for token in tokens:
    expanded_tokens.add(token)
    if token in synonyms:
        expanded_tokens.update(synonyms[token])
```

### 2. 路由日志

```python
result = router.route(prompt)

# 记录路由决策
log = {
    'prompt': prompt,
    'matched': result.match['name'] if result.match else None,
    'score': result.score,
    'confidence': result.confidence,
    'level': result.confidence_level,
    'invoke': router.should_invoke_skill(result),
}
```

### 3. 性能监控

```python
import time

start = time.time()
result = router.route(prompt)
elapsed = time.time() - start

print(f"路由耗时:{elapsed*1000:.2f}ms")
```

---

## 📁 API 参考

### PromptRouter 类

```python
class PromptRouter:
    def __init__(
        self,
        skills_dir: str = None,
        confidence_threshold: float = 0.6,
        high_confidence_threshold: float = 0.8,
    )
    
    def load_skills(self, skills_dir: str = None) -> int
    def route(self, prompt: str, limit: int = 3) -> RouteResult
    def should_invoke_skill(self, result: RouteResult) -> bool
    def should_fallback_to_llm(self, result: RouteResult) -> bool
    def get_stats(self) -> Dict
```

### RouteResult 类

```python
@dataclass
class RouteResult:
    match: Optional[Dict]      # 匹配的技能
    score: float               # 匹配分数
    confidence: float          # 置信度 (0-1)
    confidence_level: str      # high/medium/low/none
    all_matches: List[Dict]    # 所有匹配
```

---

## 🧪 测试

### 运行测试

```bash
# 单元测试
cd skills/prompt-router
python -m pytest tests/

# 性能测试
python tests/benchmark.py
```

### 测试覆盖

- [x] 分词器测试(中英文混合)
- [x] 评分算法测试
- [x] 路由决策测试
- [x] 置信度计算测试
- [ ] 端到端测试
- [ ] 性能基准测试

---

## 📝 更新日志

### v1.0.0 (2026-04-05)

- ✅ 初始版本
- ✅ 核心路由引擎
- ✅ 中英文分词器
- ✅ 多字段评分算法
- ✅ 置信度阈值
- ✅ LLM 降级机制

---

## 🔗 参考文档

- [差异化分析报告](../../../output/docs/PromptRouter-Differential-Analysis.md)
- [可行性分析](../../../output/docs/ClaudeLeak-Skill-Feasibility-Analysis.md)
- [实现计划](../../../output/docs/PromptRouter-Implementation-Plan.md)
- [Claude Leak 路由算法](references/ClaudeLeak-Router-Analysis.md)

---

## 💡 最佳实践

### 1. 阈值调优

```python
# 初始使用默认值
router = PromptRouter(confidence_threshold=0.6)

# 收集路由日志
# 分析误匹配和漏匹配
# 调整阈值
router.confidence_threshold = 0.7  # 提高准确率
```

### 2. 关键词优化

```yaml
# 在技能 SKILL.md 中添加明确触发词
---
name: weather
triggers: 天气,气温,weather, 温度,forecast
keywords: 天气,气象,预报,weather, forecast
---
```

### 3. 监控与告警

```python
# 监控路由成功率
stats = {
    'total': 0,
    'matched': 0,
    'high_confidence': 0,
    'fallback': 0,
}

# 告警:如果降级率突然升高
if stats['fallback'] / stats['total'] > 0.5:
    alert("路由降级率异常")
```

---

*最后更新:2026-04-05 00:30*

Related Skills

llm-router

3891
from openclaw/skills

Unified LLM Gateway - One API for 70+ AI models. Route to GPT, Claude, Gemini, Qwen, Deepseek, Grok and more with a single API key.

LLM Management & Routing

clawrouter

3891
from openclaw/skills

Smart LLM router — save 67% on inference costs. Routes every request to the cheapest capable model across 41 models from OpenAI, Anthropic, Google, DeepSeek, and xAI.

AI Optimization & Cost Savings

promptcache

3891
from openclaw/skills

Estimate the cost savings from caching frequently-used prompts across AI models.

ai-prompt-library-pro

3891
from openclaw/skills

AI 绘画提示词库,包含 1000+ 高质量 Midjourney/Stable Diffusion 提示词模板,覆盖人像、风景、产品、插画等场景。

prompt-injection-defense

3891
from openclaw/skills

Harden agent sessions against prompt injection from untrusted content. Use when the agent reads web search results, emails, downloaded files, PDFs, or any external text that could contain adversarial instructions. Provides content scanning, memory write guardrails (scan → lint → accept or quarantine), untrusted content tagging, and canary detection. Also use when setting up new tools that ingest external content (email checkers, RSS readers, web scrapers).

CinePrompt Skill

3891
from openclaw/skills

AI video prompt builder for cinematographers. Translates natural language shot descriptions into structured prompts optimized for AI video generators.

astrai-inference-router

3891
from openclaw/skills

Route all LLM calls through Astrai for 40%+ cost savings with intelligent routing and privacy controls

openrouter-image-generation

3891
from openclaw/skills

Generate or edit images through OpenRouter's multimodal image generation endpoint (`/api/v1/chat/completions`) using OpenRouter-compatible image models. Use for text-to-image or image-to-image requests when the user wants OpenRouter, `OPENROUTER_API_KEY`, model overrides, or provider-specific `image_config` options.

prompt-agent

3891
from openclaw/skills

将中文创意需求转换为 SDXL 或 Flux 可用的高质量英文图像提示词。当用户要求生成图片、画一张图、出图、AI绘画时触发。

reprompter

3891
from openclaw/skills

Transform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.

indirect-prompt-injection

3891
from openclaw/skills

Detect and reject indirect prompt injection attacks when reading external content (social media posts, comments, documents, emails, web pages, user uploads). Use this skill BEFORE processing any untrusted external content to identify manipulation attempts that hijack goals, exfiltrate data, override instructions, or social engineer compliance. Includes 20+ detection patterns, homoglyph detection, and sanitization scripts.

prompt-inspector

3891
from openclaw/skills

Detect prompt injection attacks and adversarial inputs in user text before passing it to your LLM. Use when you need to validate or screen user-provided text for jailbreak attempts, instruction overrides, role-play escapes, or other prompt manipulation techniques. Returns a safety verdict, risk score (0–1), and threat categories. Ideal for guarding AI pipelines, chatbots, and any application that feeds user input into a language model.