From 4a0d8939954d599c82dc059e10216d3d12310dc8 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 15 Feb 2026 19:03:08 -0600 Subject: [PATCH] refac --- backend/open_webui/utils/models.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/utils/models.py b/backend/open_webui/utils/models.py index 8bef1591b..f27a2c761 100644 --- a/backend/open_webui/utils/models.py +++ b/backend/open_webui/utils/models.py @@ -290,6 +290,18 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None) function_module, _, _ = get_function_module_from_cache(request, function_id) return function_module + # Batch-prefetch all needed function IDs to avoid N+1 queries + all_function_ids = set() + for model in models: + all_function_ids.update(model.get("action_ids", [])) + all_function_ids.update(model.get("filter_ids", [])) + all_function_ids.update(global_action_ids) + all_function_ids.update(global_filter_ids) + + functions_by_id = { + f.id: f for f in Functions.get_functions_by_ids(list(all_function_ids)) + } + for model in models: action_ids = [ action_id @@ -304,7 +316,7 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None) model["actions"] = [] for action_id in action_ids: - action_function = Functions.get_function_by_id(action_id) + action_function = functions_by_id.get(action_id) if action_function is None: raise Exception(f"Action not found: {action_id}") @@ -315,7 +327,7 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None) model["filters"] = [] for filter_id in filter_ids: - filter_function = Functions.get_function_by_id(filter_id) + filter_function = functions_by_id.get(filter_id) if filter_function is None: raise Exception(f"Filter not found: {filter_id}")