From 29160741a3defa8768a43100cb6e63c56400279c Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 7 Mar 2026 16:59:06 -0600 Subject: [PATCH] refac --- backend/open_webui/utils/models.py | 87 ++++++++++++++---------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/backend/open_webui/utils/models.py b/backend/open_webui/utils/models.py index 564100b14..e67fa6229 100644 --- a/backend/open_webui/utils/models.py +++ b/backend/open_webui/utils/models.py @@ -149,63 +149,58 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None) ] custom_models = Models.get_all_models() + + # Single O(1) lookup: Ollama base names first, then exact IDs (exact wins). + model_lookup = {} + for model in models: + if model.get("owned_by") == "ollama": + model_lookup.setdefault(model["id"].split(":")[0], model) + model_lookup[model["id"]] = model + + existing_ids = {m["id"] for m in models} + for custom_model in custom_models: if custom_model.base_model_id is None: - # Applied directly to a base model - for model in models: - if custom_model.id == model["id"] or ( - model.get("owned_by") == "ollama" - and custom_model.id - == model["id"].split(":")[ - 0 - ] # Ollama may return model ids in different formats (e.g., 'llama3' vs. 'llama3:7b') - ): - if custom_model.is_active: - model["name"] = custom_model.name - model["info"] = custom_model.model_dump() + model = model_lookup.get(custom_model.id) - # Set action_ids and filter_ids - action_ids = [] - filter_ids = [] + if model: + if custom_model.is_active: + model["name"] = custom_model.name + model["info"] = custom_model.model_dump() - if "info" in model: - if "meta" in model["info"]: - action_ids.extend( - model["info"]["meta"].get("actionIds", []) - ) - filter_ids.extend( - model["info"]["meta"].get("filterIds", []) - ) + action_ids = [] + filter_ids = [] - if "params" in model["info"]: - # Remove params to avoid exposing sensitive info - del model["info"]["params"] + if "info" in model: + if "meta" in model["info"]: + action_ids.extend( + model["info"]["meta"].get("actionIds", []) + ) + filter_ids.extend( + model["info"]["meta"].get("filterIds", []) + ) - model["action_ids"] = action_ids - model["filter_ids"] = filter_ids - else: - models.remove(model) + if "params" in model["info"]: + del model["info"]["params"] - elif custom_model.is_active and ( - custom_model.id not in [model["id"] for model in models] - ): - # Custom model based on a base model + model["action_ids"] = action_ids + model["filter_ids"] = filter_ids + else: + models.remove(model) + + elif custom_model.is_active and custom_model.id not in existing_ids: owned_by = "openai" connection_type = None - pipe = None - for m in models: - if ( - custom_model.base_model_id == m["id"] - or custom_model.base_model_id == m["id"].split(":")[0] - ): - owned_by = m.get("owned_by", "unknown") - if "pipe" in m: - pipe = m["pipe"] - - connection_type = m.get("connection_type", None) - break + base_model = model_lookup.get(custom_model.base_model_id) + if base_model is None: + base_model = model_lookup.get(custom_model.base_model_id.split(":")[0]) + if base_model: + owned_by = base_model.get("owned_by", "unknown") + if "pipe" in base_model: + pipe = base_model["pipe"] + connection_type = base_model.get("connection_type", None) model = { "id": f"{custom_model.id}",