File size: 2,435 Bytes
8c5a642 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | """Model profile configuration utilities for A1 bootstrap."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
def _unique_preserve_order(values: list[str]) -> list[str]:
seen: set[str] = set()
unique: list[str] = []
for raw in values:
value = str(raw).strip()
if not value or value in seen:
continue
seen.add(value)
unique.append(value)
return unique
def load_model_ids_from_config(
config_path: Path,
profile: str | None,
) -> tuple[list[str], dict[str, Any]]:
"""Load model IDs from a profile inside the model config JSON."""
config_path = config_path.resolve()
if not config_path.exists():
raise FileNotFoundError(f"Model config not found: {config_path}")
with config_path.open("r", encoding="utf-8") as handle:
payload = json.load(handle)
profiles = payload.get("profiles")
if not isinstance(profiles, dict) or not profiles:
raise ValueError(f"Invalid model config (missing non-empty 'profiles'): {config_path}")
profile_name = profile or payload.get("active_profile")
if profile_name is None:
profile_name = sorted(profiles.keys())[0]
profile_name = str(profile_name)
if profile_name not in profiles:
available = ", ".join(sorted(str(key) for key in profiles.keys()))
raise ValueError(
f"Requested model profile '{profile_name}' not found in {config_path}. "
f"Available profiles: {available}"
)
profile_payload = profiles[profile_name]
if not isinstance(profile_payload, dict):
raise ValueError(
f"Invalid model profile payload for '{profile_name}' in {config_path}"
)
raw_model_ids = profile_payload.get("model_ids")
if not isinstance(raw_model_ids, list):
raise ValueError(
f"Model profile '{profile_name}' must contain a list field 'model_ids'"
)
model_ids = _unique_preserve_order([str(value) for value in raw_model_ids])
if not model_ids:
raise ValueError(
f"Model profile '{profile_name}' resolved to an empty model list"
)
selection_info = {
"source": "model_config",
"config_path": str(config_path),
"profile": profile_name,
"description": str(profile_payload.get("description", "")),
}
return model_ids, selection_info
|