agentic_huge_data_base / wiki
页面 Open WebUI · 13.1 模型聚合与发现·DeepWiki 中文全文译文

13.1 · 模型聚合与发现(Model Aggregation and Discovery)

多模型对话工作台与知识应用入口 · 本章是 Open WebUI DeepWiki 中文译文的独立章节页,保留原始链接、源码锚点、模块标签和章节层级。

项目Open WebUI 章节13.1 状态全文译文 模块工具、记忆与模型调用、界面与交互、接口与服务契约、频道、笔记与协作
源码线索
  • backend/open_webui/functions.py
  • backend/open_webui/migrations/versions/c1d2e3f4a5b6_add_shared_chat_table.py
  • backend/open_webui/models/models.py
  • backend/open_webui/models/shared_chats.py
  • backend/open_webui/routers/models.py
  • backend/open_webui/routers/prompts.py
  • backend/open_webui/routers/tools.py
  • backend/open_webui/utils/access_control/__init__.py
  • backend/open_webui/utils/access_control/files.py
  • backend/open_webui/utils/chat.py
模块标签
  • 工具、记忆与模型调用
  • 界面与交互
  • 接口与服务契约
  • 频道、笔记与协作
  • 系统架构

中文译文

模型聚合与发现(中文译文)

原始 DeepWiki 页面:https://deepwiki.com/open-webui/open-webui/13.1-model-aggregation-and-discovery
翻译时间:2026-06-09T16:11:00.078Z
翻译模型:deepseek-chat
原文字符数:10700
项目:Open WebUI (open-webui)

---

模型聚合与发现

相关源文件

以下文件为本 Wiki 页面的生成提供了上下文:

  • backend/open_webui/functions.py
  • backend/open_webui/migrations/versions/c1d2e3f4a5b6_add_shared_chat_table.py
  • backend/open_webui/models/models.py
  • backend/open_webui/models/shared_chats.py
  • backend/open_webui/routers/models.py
  • backend/open_webui/routers/prompts.py
  • backend/open_webui/routers/tools.py
  • backend/open_webui/utils/access_control/__init__.py
  • backend/open_webui/utils/access_control/files.py
  • backend/open_webui/utils/chat.py
  • backend/open_webui/utils/filter.py
  • backend/open_webui/utils/models.py
  • backend/open_webui/utils/plugin.py
  • src/lib/apis/models/index.ts
  • src/lib/apis/streaming/index.ts
  • src/lib/components/chat/Messages/RateComment.svelte
  • src/lib/components/chat/Messages/ResponseMessage/TaskList.svelte
  • src/lib/components/chat/ModelSelector.svelte
  • src/lib/components/chat/ModelSelector/ModelItem.svelte
  • src/lib/components/chat/ModelSelector/ModelItemMenu.svelte
  • src/lib/components/chat/ModelSelector/Selector.svelte
  • src/lib/components/icons/Label.svelte
  • src/lib/components/icons/Tag.svelte

本文档记录了 Open WebUI 中的模型聚合与发现系统,该系统负责从多个提供方来源收集、转换并呈现可用的 LLM 模型。内容涵盖 utils/models.py 中的后端聚合逻辑、OpenAI 和 Ollama 路由中的提供方特定发现机制,以及确保性能的缓存机制。

有关模型执行和聊天补全的信息,请参阅后端处理管道。有关模型特定配置和访问控制,请参阅模型配置与访问

---

系统概述

Open WebUI 从三个主要来源聚合模型:

  1. Ollama:通过 /api/tags/api/models 端点访问本地或远程 Ollama 实例 backend/open_webui/utils/models.py:40-54
  2. OpenAI 兼容提供方:任何遵循 OpenAI 规范的 API(包括通过转换层接入的 Anthropic)backend/open_webui/utils/models.py:57-59
  3. 函数/管道:由内部或外部 Python 脚本生成的动态模型,包括流形(manifolds)backend/open_webui/functions.py:77-150

系统合并这些来源,应用管理覆盖(自定义名称、系统提示),并实施基于角色的访问控制(RBAC)。

模型发现架构

graph TB
    subgraph "逻辑层"
        get_all_models["get_all_models()<br/>backend/open_webui/utils/models.py"]
        get_all_base_models["get_all_base_models()<br/>backend/open_webui/utils/models.py"]
    end

    subgraph "提供方路由"
        OllamaRouter["ollama.get_all_models()<br/>backend/open_webui/routers/ollama.py"]
        OpenAIRouter["openai.get_all_models()<br/>backend/open_webui/routers/openai.py"]
        FunctionModels["get_function_models()<br/>backend/open_webui/functions.py"]
    end

    subgraph "数据存储"
        ModelsDB["模型表<br/>(SQL ModelsTable)"]
        FunctionsDB["函数表"]
    end

    subgraph "外部 API"
        OllamaAPI["Ollama /api/tags"]
        OpenAIAPI["OpenAI /v1/models"]
    end

    get_all_models --> get_all_base_models
    get_all_models --> ModelsDB

    get_all_base_models --> OllamaRouter
    get_all_base_models --> OpenAIRouter
    get_all_base_models --> FunctionModels

    OllamaRouter --> OllamaAPI
    OpenAIRouter --> OpenAIAPI

来源:backend/open_webui/utils/models.py:75-90backend/open_webui/functions.py:77-150backend/open_webui/models/models.py:199-208

---

模型编译逻辑

发现系统的核心是 backend/open_webui/utils/models.py 中的 get_all_models 函数。它遵循多阶段管道来生成最终列表。

1. 基础模型获取

系统收集"基础"模型——即上游提供方直接报告的模型。此过程使用 asyncio.gather 并行化处理 backend/open_webui/utils/models.py:75

  • Ollama:通过 fetch_ollama_models 获取,该函数调用 ollama.get_all_models。它会规范化 ID 并添加 connection_type(本地/外部)backend/open_webui/utils/models.py:40-54
  • OpenAI:通过 fetch_openai_models 获取,该函数处理多个配置的基础 URL backend/open_webui/utils/models.py:57-59
  • 函数:通过 get_function_models 获取。该函数识别单个"管道"和"流形"(返回多个子模型的函数)backend/open_webui/functions.py:77-150
2. 自定义模型合并

获取基础模型后,系统使用 Models.get_all_models() 查询本地数据库(Models 表)中的自定义定义 backend/open_webui/utils/models.py:139

  • 直接覆盖:如果自定义模型与基础模型共享 ID(例如 llama3:latest),则会更新基础模型的元数据和名称 backend/open_webui/utils/models.py:151-175
  • 派生模型:如果自定义模型具有 base_model_id,则会创建一个继承提供方属性(如 owned_bypipe 标志)的新条目,但应用特定参数和元数据 backend/open_webui/utils/models.py:177-230
3. 竞技场模型

如果 ENABLE_EVALUATION_ARENA_MODELS 为 true,则会附加虚拟的"竞技场"模型。这些模型充当模型比较逻辑的占位符 backend/open_webui/utils/models.py:99-131

来源:backend/open_webui/utils/models.py:80-230backend/open_webui/models/models.py:65-98

---

提供方发现详情

函数与流形

get_function_models 工具检查类型为 pipe 的活动函数 backend/open_webui/functions.py:78

  • 单管道:实现单个模型端点的函数 backend/open_webui/functions.py:135-145
  • 流形:包含 pipes 属性或方法的函数,返回子模型列表。系统使用 function_id.sub_pipe_id 格式将这些子模型映射到 ID backend/open_webui/functions.py:90-127
模型选择器前端

Selector.svelte 组件提供用于发现和切换这些聚合模型的 UI。它使用 fuse.js 在客户端对模型名称和标签进行模糊搜索 src/lib/components/chat/ModelSelector/Selector.svelte:136-150

模型数据流:后端到前端

sequenceDiagram
    participant B as "backend/open_webui/utils/models.py"
    participant R as "backend/open_webui/routers/models.py"
    participant S as "src/lib/stores.ts"
    participant UI as "src/lib/components/chat/ModelSelector.svelte"

    B->>R: "get_all_models() 聚合提供方"
    R-->>S: "API 响应:ModelAccessListResponse"
    Note over S: "models store 已更新"
    S->>UI: "响应式 $models 订阅"
    UI->>UI: "Selector items={$models.map(...)}"

来源:backend/open_webui/routers/models.py:90-161src/lib/components/chat/ModelSelector.svelte:61-65src/lib/stores.ts:21

---

缓存与性能

为确保 UI 保持响应,Open WebUI 实现了多层缓存策略:

  1. 应用状态缓存request.app.state.BASE_MODELS 将编译后的基础列表存储在内存中,以避免对提供方进行冗余 API 调用 backend/open_webui/utils/models.py:82-90
  2. 函数模块缓存get_function_module_from_cache 防止每次请求都重新加载和重新编译 Python 函数模块 backend/open_webui/utils/filter.py:17
  3. 前端 Store:Svelte 的 models store 使列表对所有 UI 组件可用,无需重复网络请求 src/lib/stores.ts:21
缓存级别实现方式失效触发条件
全局模型app.state.MODELS请求查询中的 refresh=True backend/open_webui/utils/models.py:80
基础模型app.state.BASE_MODELSENABLE_BASE_MODELS_CACHE=False backend/open_webui/utils/models.py:84
函数get_function_module_from_cache模块更新/数据库变更 backend/open_webui/utils/plugin.py:28

来源:backend/open_webui/utils/models.py:80-92backend/open_webui/utils/filter.py:13-18

---

访问控制与可见性

模型发现对请求用户的角色和权限敏感。

基于角色的过滤

虽然 get_all_models 编译主列表,但 API 端点会应用过滤器:

  • 搜索与列表Models.search_models 根据 user_idgroup_ids 过滤模型,除非用户是具有 BYPASS_ADMIN_ACCESS_CONTROL 的管理员 backend/open_webui/routers/models.py:122-128
  • 访问授权:系统检查 AccessGrants 以确定用户是否对特定自定义模型具有 readwrite 权限 backend/open_webui/routers/models.py:132-139
  • 写入权限ModelAccessResponse 包含 write_access 布尔值,允许 UI 启用/禁用编辑功能 backend/open_webui/models/models.py:127-128
可见性控制
  • 隐藏模型:可通过 meta.hidden 将模型从选择器中隐藏 src/lib/components/chat/ModelSelector/Selector.svelte:219
  • 活动状态:数据库中的 is_active 标志决定自定义模型是否包含在聚合中 backend/open_webui/utils/models.py:176

来源:backend/open_webui/routers/models.py:122-156backend/open_webui/utils/models.py:176backend/open_webui/models/access_grants.py:17