Skip to content

feat: SmartRouter - intelligent model routing based on task complexity#315

Open
zhangPinkdolphin wants to merge 4 commits into
lsdefine:mainfrom
zhangPinkdolphin:smart-router
Open

feat: SmartRouter - intelligent model routing based on task complexity#315
zhangPinkdolphin wants to merge 4 commits into
lsdefine:mainfrom
zhangPinkdolphin:smart-router

Conversation

@zhangPinkdolphin
Copy link
Copy Markdown

概述

为 GenericAgent 添加智能路由能力。根据用户查询的复杂度(关键词/长度/代码模式),自动选择简单模型(flash)或复杂模型(pro)。

改动内容

1. llmcore.py — SmartRouter 类 (+56行)

新增 SmartRouter 类,通过分析查询文本中的关键词、长度和代码特征,将任务分类为 simple/complex。

  • COMPLEX_KEYWORDS: 涉及代码、框架、架构、性能优化等复杂主题的关键词列表
  • classify(query): 返回 'simple''complex'
  • disable()/enable(): 手动锁定/恢复自动判断

2. agentmain.py — 三处修改

  • import: 引入 SmartRouter
  • __init__: 初始化 self.router = SmartRouter()self.auto_route = False
  • /llm auto 命令: 运行时切换自动路由开关
  • /llm simple|complex 命令: 手动指定模型组
  • run() 方法: 在调用 agent_runner_loop 前,若 auto_route 开启,自动 classify(query) 并切换 llmclient

使用方式

命令 作用
/llm auto 切换智能路由模式(默认关闭)
/llm simple 手动切换到简单模型组(flash)
/llm complex 手动切换到复杂模型组(pro)

前置条件

mykey.py 中配置两个 MixinSession:

  • 0: 简单模型组(flash 优先),命名规则含 _simple
  • 1: 复杂模型组(pro 优先),命名规则含 _complex

当自动路由生效时,SmartRouter 会根据任务复杂度自动切换 self.llm_no 指向对应的 MixinSession。

兼容性

  • 向后兼容,默认不开启 auto_route
  • 不影响原有的 /llm list/resume 等功能
  • 不依赖外部 API,纯本地规则引擎

Add SmartRouter class that classifies user queries into simple/complex
based on keywords, query length, and code patterns.
Add /llm auto command, auto_route property, and automatic
model routing in gen_answer based on task complexity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant