""" Modell-Capabilities — EINE Quelle der Wahrheit für Modell-Eigenschaften (MoE / Tools / Vision / Coder / Reasoning / Embedding / Kontext). Portiert aus Mission Control v1 (model_caps.py). Quellen, geschichtet: GGUF-Header (offline, authoritativ) → cmd-Flags (--jinja/--mmproj) → HF-Block (tags + chat_template) → Familien-Fallback. Tool-Fähigkeit dreistufig: yes (bestätigt) | likely (Familie) | no. """ import re import struct from services.fit import extract_active_params_b, extract_params_b _GGUF_FIXED = {0: 1, 1: 1, 2: 2, 3: 2, 4: 4, 5: 4, 6: 4, 7: 1, 10: 8, 11: 8, 12: 8} def _read_gguf_meta(path: str) -> dict: """Liest nur den GGUF-Metadaten-Header (architecture/context_length/expert_count/ parameter_count). Bricht vor dem Tokenizer-Array ab → schnell, lädt NICHT das Modell.""" out: dict = {} try: with open(path, "rb") as f: if f.read(4) != b"GGUF": return {} struct.unpack(" int: return struct.unpack(" int: return struct.unpack(" str: return f.read(ru64()).decode("utf-8", "replace") def rval(t: int): if t == 8: return rstr() if t == 0: return struct.unpack(" dict: """Capability-Tag-Set für ein Modell. Alle Quellen optional — nutzt, was da ist.""" low = (name or "").lower() cmdl = (cmd or "").lower() hf = hf or {} meta = _read_gguf_meta(gguf_path) if gguf_path else {} arch = str(meta.get("architecture") or hf.get("architecture") or "").lower() tags = [str(t).lower() for t in (hf.get("tags") or [])] chat_tpl = str(hf.get("chat_template") or "") expert_count = int(meta.get("expert_count") or 0) moe = ( expert_count > 1 or any(a in arch for a in _MOE_ARCH) or bool(re.search(r"\d+x\d+\.?\d*b", low)) or bool(re.search(r"a\d+\.?\d*b", low)) ) active_b = extract_active_params_b(name) pcount = int(meta.get("parameter_count") or 0) params_b = round(pcount / 1e9, 1) if pcount else extract_params_b(name) ctx = meta.get("context_length") if not ctx: m = re.search(r"-(?:c|-ctx-size)\s+(\d+)", cmdl) ctx = int(m.group(1)) if m else None # Cap context length at 131072 for Qwen / Hermes models to prevent reporting scaled RoPE context of 256k+ which might OOM or be unstable. if ctx and ctx > 131072 and ("qwen" in low or "hermes" in low): ctx = 131072 tool_confirmed = "--jinja" in cmdl or "tool_call" in chat_tpl or "" in chat_tpl tool_family = any(fam in low for fam in _TOOL_FAMILIES) or "function-calling" in tags tools = "yes" if tool_confirmed else ("likely" if tool_family else "no") vision = "--mmproj" in cmdl or "vl" in arch or "clip" in arch or any(k in low for k in _VISION_KW) coder = any(k in low for k in _CODE_KW) reasoning = any(k in low for k in _REASON_KW) or "reasoning" in tags embedding = "bert" in arch or any(k in low for k in _EMBED_KW) return { "moe": moe, "active_b": active_b, "tools": tools, "vision": vision, "coder": coder, "reasoning": reasoning, "embedding": embedding, "ctx": ctx, "params_b": params_b or None, "arch": arch or None, }