This commit is contained in:
Timothy Jaeryang Baek committed 2026-03-07 16:59:06 -06:00
1 parent 7820a311ba
commit 29160741a3
1 file changed
+22 -27
+22 -27
View File
@@ -149,22 +149,25 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
] ]
custom_models = Models.get_all_models() 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: for custom_model in custom_models:
if custom_model.base_model_id is None: if custom_model.base_model_id is None:
# Applied directly to a base model model = model_lookup.get(custom_model.id)
for model in models:
if custom_model.id == model["id"] or ( if model:
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: if custom_model.is_active:
model["name"] = custom_model.name model["name"] = custom_model.name
model["info"] = custom_model.model_dump() model["info"] = custom_model.model_dump()
# Set action_ids and filter_ids
action_ids = [] action_ids = []
filter_ids = [] filter_ids = []
@@ -178,7 +181,6 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
) )
if "params" in model["info"]: if "params" in model["info"]:
# Remove params to avoid exposing sensitive info
del model["info"]["params"] del model["info"]["params"]
model["action_ids"] = action_ids model["action_ids"] = action_ids
@@ -186,26 +188,19 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
else: else:
models.remove(model) models.remove(model)
elif custom_model.is_active and ( elif custom_model.is_active and custom_model.id not in existing_ids:
custom_model.id not in [model["id"] for model in models]
):
# Custom model based on a base model
owned_by = "openai" owned_by = "openai"
connection_type = None connection_type = None
pipe = None pipe = None
for m in models: base_model = model_lookup.get(custom_model.base_model_id)
if ( if base_model is None:
custom_model.base_model_id == m["id"] base_model = model_lookup.get(custom_model.base_model_id.split(":")[0])
or custom_model.base_model_id == m["id"].split(":")[0] if base_model:
): owned_by = base_model.get("owned_by", "unknown")
owned_by = m.get("owned_by", "unknown") if "pipe" in base_model:
if "pipe" in m: pipe = base_model["pipe"]
pipe = m["pipe"] connection_type = base_model.get("connection_type", None)
connection_type = m.get("connection_type", None)
break
model = { model = {
"id": f"{custom_model.id}", "id": f"{custom_model.id}",