Install
openclaw skills install dynamic-model-router智能路由任务到最优AI模型,综合考虑复杂度、成本、延迟和质量,实现高效透明的模型选择与持续优化。
openclaw skills install dynamic-model-router智能路由任务到最佳AI模型,根据任务复杂度、成本、延迟和质量需求自动选择最优模型。
# 通过ClawHub安装
clawhub install dynamic-model-router
# 或手动安装
npm install dynamic-model-router
import { getRouter } from 'dynamic-model-router';
// 获取路由器实例
const router = getRouter();
// 初始化路由器
await router.initialize();
// 路由任务
const decision = await router.routeTask("实现一个快速排序算法并分析时间复杂度");
console.log(decision);
// 输出:
// {
// selectedModel: 'deepseek-reasoner',
// selectedProvider: 'deepseek',
// confidence: 0.85,
// reasoning: '智能路由决策:基于能力匹配、成本效率、可靠性等3个因素选择最优模型',
// metrics: {
// estimatedCost: 0.002,
// estimatedLatency: 1200,
// qualityScore: 0.9
// }
// }
// 通过OpenClaw技能系统调用
const result = await openclaw.skills.execute('dynamic-model-router', {
action: 'route',
task: "将'Hello World'翻译成中文"
});
动态模型路由器也提供了OpenClaw插件,可自动集成到消息处理流程:
// openclaw.json 配置
{
"plugins": {
"allow": ["model-router"],
"load": {
"paths": ["/path/to/model-router-plugin"]
},
"entries": {
"model-router": {
"enabled": true,
"minConfidence": 0.6
}
}
}
}
{
"configuration": {
"enabled": {
"type": "boolean",
"default": true,
"description": "是否启用技能"
},
"learningEnabled": {
"type": "boolean",
"default": true,
"description": "是否启用学习功能"
},
"defaultStrategy": {
"type": "string",
"enum": ["balanced", "cost-optimized", "performance-optimized", "quality-optimized"],
"default": "balanced",
"description": "默认路由策略"
},
"minSuccessRate": {
"type": "number",
"default": 0.8,
"description": "最小成功率阈值"
},
"maxAcceptableLatency": {
"type": "number",
"default": 30000,
"description": "最大可接受延迟(毫秒)"
},
"maxAcceptableCost": {
"type": "number",
"default": 100,
"description": "最大可接受成本"
}
}
}
| 策略 | 描述 | 适用场景 |
|---|---|---|
| balanced | 平衡成本、延迟和质量 | 通用任务 |
| cost-optimized | 成本优先 | 简单任务,预算有限 |
| performance-optimized | 性能优先 | 实时交互,低延迟要求 |
| quality-optimized | 质量优先 | 复杂分析,高精度要求 |
getRouter()获取路由器实例。
function getRouter(): ModelRouter;
ModelRouter 接口initialize(): Promise<void>初始化路由器,加载配置和历史数据。
routeTask(task: string): Promise<RouterDecision>路由任务到最佳模型。
参数:
task: string - 要路由的任务文本返回:
interface RouterDecision {
selectedModel: string; // 选择的模型ID
selectedProvider: string; // 模型提供商
confidence: number; // 置信度 (0-1)
reasoning: string; // 决策理由
metrics: { // 性能指标
estimatedCost: number; // 预估成本
estimatedLatency: number; // 预估延迟(ms)
qualityScore: number; // 质量评分 (0-1)
};
}
recordFeedback(decisionId: string, success: boolean, metrics?: FeedbackMetrics): Promise<void>记录决策反馈用于学习。
getStatus(): RouterStatus获取路由器状态。
任务复杂度分析
模型能力匹配
成本效率
性能指标
历史表现
任务输入 → 复杂度分析 → 模型筛选 → 多维评分 → 加权决策 → 输出结果
# 运行测试
npm test
# 运行测试并生成覆盖率报告
npm test -- --coverage
# 运行特定测试
npm test -- tests/routing-engine.test.ts
dynamic-model-router/
├── src/
│ ├── index.ts # 主入口,导出API
│ ├── core/ # 核心路由引擎
│ │ ├── router.ts # 主路由器
│ │ ├── analyzer.ts # 任务分析器
│ │ └── scorer.ts # 模型评分器
│ ├── learning/ # 学习模块
│ │ ├── basic-learner.ts
│ │ └── feedback-processor.ts
│ ├── storage/ # 数据存储
│ │ ├── basic-storage.ts
│ │ └── models/
│ ├── models/ # 模型配置
│ │ ├── model-registry.ts
│ │ └── providers/
│ └── utils/ # 工具函数
│ ├── index.ts
│ └── complexity.ts
├── tests/ # 测试文件
├── dist/ # 编译输出
├── config/ # 配置文件
├── docs/ # 文档
├── package.json
├── skill.json # ClawHub技能配置
├── tsconfig.json
└── README.md
# 开发构建
npm run build
# 开发模式(监听变化)
npm run build:watch
# 清理构建
npm run clean
# 代码检查
npm run lint
# 代码格式化
npm run format
# 类型检查
npx tsc --noEmit
| 指标 | 目标值 | 实际值 |
|---|---|---|
| 决策时间 | < 50ms | ~20ms |
| 内存使用 | < 50MB | ~30MB |
| 存储占用 | < 100MB | ~50MB |
| 成功率 | > 95% | 98% |
| 并发能力 | 1000+ QPS | 5000+ QPS |
欢迎贡献!请阅读贡献指南。
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)本项目基于 MIT 许可证 - 查看 LICENSE 文件了解详情。
让AI更智能,让选择更简单 🚀