模型聚合与发现(中文译文)
原始 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.pybackend/open_webui/migrations/versions/c1d2e3f4a5b6_add_shared_chat_table.pybackend/open_webui/models/models.pybackend/open_webui/models/shared_chats.pybackend/open_webui/routers/models.pybackend/open_webui/routers/prompts.pybackend/open_webui/routers/tools.pybackend/open_webui/utils/access_control/__init__.pybackend/open_webui/utils/access_control/files.pybackend/open_webui/utils/chat.pybackend/open_webui/utils/filter.pybackend/open_webui/utils/models.pybackend/open_webui/utils/plugin.pysrc/lib/apis/models/index.tssrc/lib/apis/streaming/index.tssrc/lib/components/chat/Messages/RateComment.sveltesrc/lib/components/chat/Messages/ResponseMessage/TaskList.sveltesrc/lib/components/chat/ModelSelector.sveltesrc/lib/components/chat/ModelSelector/ModelItem.sveltesrc/lib/components/chat/ModelSelector/ModelItemMenu.sveltesrc/lib/components/chat/ModelSelector/Selector.sveltesrc/lib/components/icons/Label.sveltesrc/lib/components/icons/Tag.svelte
本文档记录了 Open WebUI 中的模型聚合与发现系统,该系统负责从多个提供方来源收集、转换并呈现可用的 LLM 模型。内容涵盖 utils/models.py 中的后端聚合逻辑、OpenAI 和 Ollama 路由中的提供方特定发现机制,以及确保性能的缓存机制。
有关模型执行和聊天补全的信息,请参阅后端处理管道。有关模型特定配置和访问控制,请参阅模型配置与访问。
---
系统概述
Open WebUI 从三个主要来源聚合模型:
- Ollama:通过
/api/tags或/api/models端点访问本地或远程 Ollama 实例backend/open_webui/utils/models.py:40-54。 - OpenAI 兼容提供方:任何遵循 OpenAI 规范的 API(包括通过转换层接入的 Anthropic)
backend/open_webui/utils/models.py:57-59。 - 函数/管道:由内部或外部 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-90,backend/open_webui/functions.py:77-150,backend/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获取,该函数处理多个配置的基础 URLbackend/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_by或pipe标志)的新条目,但应用特定参数和元数据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-230,backend/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格式将这些子模型映射到 IDbackend/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-161,src/lib/components/chat/ModelSelector.svelte:61-65,src/lib/stores.ts:21
---
缓存与性能
为确保 UI 保持响应,Open WebUI 实现了多层缓存策略:
- 应用状态缓存:
request.app.state.BASE_MODELS将编译后的基础列表存储在内存中,以避免对提供方进行冗余 API 调用backend/open_webui/utils/models.py:82-90。 - 函数模块缓存:
get_function_module_from_cache防止每次请求都重新加载和重新编译 Python 函数模块backend/open_webui/utils/filter.py:17。 - 前端 Store:Svelte 的
modelsstore 使列表对所有 UI 组件可用,无需重复网络请求src/lib/stores.ts:21。
| 缓存级别 | 实现方式 | 失效触发条件 |
|---|---|---|
| 全局模型 | app.state.MODELS | 请求查询中的 refresh=True backend/open_webui/utils/models.py:80 |
| 基础模型 | app.state.BASE_MODELS | ENABLE_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-92,backend/open_webui/utils/filter.py:13-18
---
访问控制与可见性
模型发现对请求用户的角色和权限敏感。
基于角色的过滤
虽然 get_all_models 编译主列表,但 API 端点会应用过滤器:
- 搜索与列表:
Models.search_models根据user_id和group_ids过滤模型,除非用户是具有BYPASS_ADMIN_ACCESS_CONTROL的管理员backend/open_webui/routers/models.py:122-128。 - 访问授权:系统检查
AccessGrants以确定用户是否对特定自定义模型具有read或write权限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-156,backend/open_webui/utils/models.py:176,backend/open_webui/models/access_grants.py:17