llm-host/llmhost/hfcache.py
Neil Bantoc 623d093b81 Dashboard: a reasoning chooser, offering only what the model accepts
Reasoning effort isn't a backend flag -- vLLM's --reasoning-parser only parses
the output. It's a chat-template kwarg read per request, and the models differ:
Qwen3.8 27B's template takes enable_thinking plus reasoning_effort in
('low','medium','xhigh') and raises on anything else, while Ornith 1.5 9B takes
only enable_thinking. Note 'xhigh', not OpenAI's 'high'.

So the chooser is built from what the loaded model's own template actually reads
(hfcache.template_controls), with the level names from the catalog, which is the
part that can't be inferred -- a level the template doesn't know would make it
raise, so an undeclared or unreadable one is never offered or sent.

The proxy injects the setting into chat completions, with a key the client set
itself always winning, the same precedence mlx_lm gives its own
--chat-template-kwargs. Reading it from config per request (mtime-cached) means
the dashboard changes it with no restart, so the model stays loaded -- the
startup flag would have cost a full reload per change.

vLLM gets no control: llm-host runs it without a proxy, so there is nothing in
the path to inject the kwarg. The panel hides itself rather than pretend.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-11-03 02:15:12 -08:00

94 lines
3.3 KiB
Python

"""Read-only checks against the Hugging Face cache (no huggingface_hub dependency)."""
import json
import os
from . import paths
def repo_dir(repo):
return paths.hf_hub_dir() / ("models--" + repo.replace("/", "--"))
def is_downloaded(repo):
"""True when a snapshot has every weight file its index lists (or a single-file model)."""
snaps = repo_dir(repo) / "snapshots"
if not snaps.is_dir():
return False
for snap in snaps.iterdir():
idx = snap / "model.safetensors.index.json"
if idx.exists():
try:
files = set(json.loads(idx.read_text())["weight_map"].values())
except (OSError, ValueError, KeyError):
continue
if files and all(os.path.exists(snap / f) for f in files):
return True
elif (snap / "model.safetensors").exists():
return True
return False
def disk_gib(repo):
"""Size of this repo's downloaded files, in GiB. Measured through the snapshot symlinks
because newer huggingface_hub versions keep large blobs in a shared hub/blobs directory."""
seen, total = set(), 0
snaps = repo_dir(repo) / "snapshots"
if snaps.is_dir():
for root, _dirs, files in os.walk(snaps):
for name in files:
real = os.path.realpath(os.path.join(root, name))
if real in seen:
continue
seen.add(real)
try:
total += os.stat(real).st_size
except OSError:
pass
return total / 2**30
def download_gib(repo):
"""Bytes downloaded so far, in GiB: finished files plus ones still being written
(huggingface_hub writes those as blobs/*.incomplete inside the repo's own folder)."""
total = disk_gib(repo) * 2**30
blobs = repo_dir(repo) / "blobs"
if blobs.is_dir():
for f in blobs.glob("*.incomplete"):
try:
total += f.stat().st_size
except OSError:
pass
return total / 2**30
def chat_template(repo):
"""The model's chat template, or "" if it isn't on disk. Newer repos ship it as
chat_template.jinja; older ones keep it inside tokenizer_config.json."""
snaps = repo_dir(repo) / "snapshots"
if not snaps.is_dir():
return ""
for snap in snaps.iterdir():
f = snap / "chat_template.jinja"
if f.exists():
try:
return f.read_text()
except OSError:
pass
cfg = snap / "tokenizer_config.json"
if cfg.exists():
try:
t = json.loads(cfg.read_text()).get("chat_template")
except (OSError, ValueError):
continue
if isinstance(t, str) and t:
return t
return ""
def template_controls(repo):
"""Which reasoning knobs this model's template actually reads. Checked against the
template rather than trusted from the catalog, because passing a kwarg the template
doesn't expect is silently ignored, and an effort level it doesn't know makes it
raise -- so the dashboard must only ever offer what is really there."""
t = chat_template(repo)
return {"thinking": "enable_thinking" in t, "effort": "reasoning_effort" in t}