refac
This commit is contained in:
1 parent
7820a311ba
commit
29160741a3
1 file changed
+41
-46
@@ -149,63 +149,58 @@ 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 (
|
|
||||||
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()
|
|
||||||
|
|
||||||
# Set action_ids and filter_ids
|
if model:
|
||||||
action_ids = []
|
if custom_model.is_active:
|
||||||
filter_ids = []
|
model["name"] = custom_model.name
|
||||||
|
model["info"] = custom_model.model_dump()
|
||||||
|
|
||||||
if "info" in model:
|
action_ids = []
|
||||||
if "meta" in model["info"]:
|
filter_ids = []
|
||||||
action_ids.extend(
|
|
||||||
model["info"]["meta"].get("actionIds", [])
|
|
||||||
)
|
|
||||||
filter_ids.extend(
|
|
||||||
model["info"]["meta"].get("filterIds", [])
|
|
||||||
)
|
|
||||||
|
|
||||||
if "params" in model["info"]:
|
if "info" in model:
|
||||||
# Remove params to avoid exposing sensitive info
|
if "meta" in model["info"]:
|
||||||
del model["info"]["params"]
|
action_ids.extend(
|
||||||
|
model["info"]["meta"].get("actionIds", [])
|
||||||
|
)
|
||||||
|
filter_ids.extend(
|
||||||
|
model["info"]["meta"].get("filterIds", [])
|
||||||
|
)
|
||||||
|
|
||||||
model["action_ids"] = action_ids
|
if "params" in model["info"]:
|
||||||
model["filter_ids"] = filter_ids
|
del model["info"]["params"]
|
||||||
else:
|
|
||||||
models.remove(model)
|
|
||||||
|
|
||||||
elif custom_model.is_active and (
|
model["action_ids"] = action_ids
|
||||||
custom_model.id not in [model["id"] for model in models]
|
model["filter_ids"] = filter_ids
|
||||||
):
|
else:
|
||||||
# Custom model based on a base model
|
models.remove(model)
|
||||||
|
|
||||||
|
elif custom_model.is_active and custom_model.id not in existing_ids:
|
||||||
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}",
|
||||||
|
|||||||
Reference in new issue
Block a user