Skip to content

feat: 多模型池支持 - 私聊场景智能模型选择#36

Merged
69gg merged 12 commits intomainfrom
feature/multiple-models-support
Feb 19, 2026
Merged

feat: 多模型池支持 - 私聊场景智能模型选择#36
69gg merged 12 commits intomainfrom
feature/multiple-models-support

Conversation

@69gg
Copy link
Owner

@69gg 69gg commented Feb 19, 2026

功能概述

为 Undefined 添加多模型池功能,允许用户在私聊场景下使用多个 AI 模型,并通过对比选择最佳回复。

主要特性

核心功能

  • 多模型池架构:支持配置多个聊天模型,按策略(轮询/随机/用户偏好)自动选择
  • 对比模式/compare 命令触发多模型并行响应,用户可选择最佳回复
  • 用户偏好记忆:自动记录用户选择,优化后续模型分配
  • 默认关闭:需显式配置 models.chat.pool.enabled = true,单模型用户零影响

架构设计

  • 解耦设计:拆分 ModelPoolService,职责清晰
    • ai/model_selector.py:纯选择逻辑(策略/偏好/compare状态)
    • services/model_pool.py:私聊交互服务
    • services/ai_coordinator.py:持有 ModelPoolService,队列投递时选模型
  • 仅支持私聊:群聊始终使用主模型,避免复杂度
  • Agent 池支持:Agent 按策略自动分配模型,无用户干预

WebUI 增强

  • 模型池配置界面
  • 拆分大文件,降低维护难度

代码质量

  • 添加 ModelPool 单元测试
  • 集成 Biome/Stylelint 代码检查
  • CI 工作流添加测试环节

配置示例

[models.chat.pool]
enabled = true
strategy = "round_robin"  # default | round_robin | random

[[models.chat.pool.models]]
model_name = "claude-sonnet-4-5-20250929"
api_url = "https://api.anthropic.com/v1"
api_key = "sk-ant-..."

[[models.chat.pool.models]]
model_name = "gpt-4o"
api_url = "https://api.openai.com/v1"
api_key = "sk-..."

未指定的字段会继承主模型配置(models.chat.*)。

使用流程

  1. 用户发送 /compare 你好 → 所有模型并行响应
  2. 系统回复 [1] Claude: ... / [2] GPT: ...
  3. 用户回复 1选1 → 记录偏好,后续优先使用 Claude

测试

uv run pytest tests/test_model_pool.py

相关文件

  • src/Undefined/ai/model_selector.py
  • src/Undefined/services/model_pool.py
  • src/Undefined/services/ai_coordinator.py
  • tests/test_model_pool.py
  • webui/src/components/ModelPoolConfig.tsx

Breaking Changes

无。默认关闭,现有用户行为不受影响。

69gg and others added 10 commits February 18, 2026 09:07
新增 services/model_pool.py,集中私聊 pool 交互逻辑:
- /compare、/pk 命令处理
- 「选X」偏好切换
- select_chat_config 封装

handlers.py 私聊 pool 块从 ~20 行降至 4 行,零感知 model_selector。
runner.py 移除 hasattr 检查,直接调用 model_selector。
agent 默认 global_enabled 改为 False,与全局开关语义一致。

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
- routes.py (904行) → routes/ 包(_shared/_index/_auth/_bot/_config/_logs/_system)
- utils.py (402行) → utils/ 包(config_io/comment/toml_render)
- main.js (2163行) → 9个模块(i18n/state/ui/api/auth/bot/config-form/log-view/main)
- style.css (1128行) → 6个文件(variables/base/landing/app/components/responsive)

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
- biome.json: 配置 Biome 检查 webui JS(禁用全局作用域误报规则)
- .stylelintrc.json: 配置 Stylelint 检查 webui CSS
- api.js: Math.pow(2, n) → 2 ** n
- state.js: 字符串拼接 → 模板字面量

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
chatgpt-codex-connector[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

- 修复私聊模型双重选择导致 round-robin 计数器错误递增
- 修复群聊错误参与模型池选择,现在群聊始终使用主模型
- 为 round-robin 计数器添加线程安全保护
- 自动清除无效的用户模型偏好(当模型从池中移除时)
- 修复 get_all_chat_models 可能返回重复主模型的问题
- 修复 thinking_enabled 检查使用错误的配置对象
- 修复 pool.enabled 默认值从 True 改为 False
- 添加 3 个新测试覆盖上述修复

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
devin-ai-integration[bot]

This comment was marked as resolved.

当通过热重载添加/移除/修改池模型时,队列间隔不会更新,
导致新添加的池模型使用默认间隔而不是配置的间隔。

修复方法:在 _QUEUE_INTERVAL_KEYS 中添加 chat_model.pool
和 agent_model.pool,使池配置变化时触发队列间隔更新。

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
@69gg 69gg merged commit a0a2042 into main Feb 19, 2026
1 check passed
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

Comments