detect.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """Detection of running llama-server instances.
  2. Probes well-known local roots and fingerprints genuine llama-server via /props (build_info + model
  3. fields — Ollama and LM Studio answer /v1/models but not /props). Detection never needs a key, but
  4. honors one if the probed server requires it (401 -> detected, auth_required=True).
  5. """
  6. from __future__ import annotations
  7. import json
  8. import urllib.error
  9. import urllib.request
  10. from dataclasses import dataclass
  11. # Always 127.0.0.1 — resolving localhost costs ~2s/request on Windows.
  12. DEFAULT_PROBE_PORTS = (8080,) # llama-server default; managed port comes from config
  13. @dataclass
  14. class DetectedServer:
  15. base_url: str # OpenAI-compatible /v1 root
  16. build_info: str # e.g. "b10290-c8e03ce81"
  17. model_path: str # currently loaded model (may be empty in router mode)
  18. n_ctx: int | None
  19. router_mode: bool # GET /models answered -> router management available
  20. auth_required: bool
  21. def _get(url: str, timeout_s: int = 3) -> tuple[int, dict | None]:
  22. try:
  23. with urllib.request.urlopen(url, timeout=timeout_s) as r:
  24. raw = r.read()
  25. return r.status, (json.loads(raw) if raw else None)
  26. except urllib.error.HTTPError as exc:
  27. return exc.code, None
  28. except (urllib.error.URLError, OSError, TimeoutError, json.JSONDecodeError):
  29. return 0, None
  30. def probe_port(port: int) -> DetectedServer | None:
  31. """One port: /props fingerprint, then /models for router capability."""
  32. root = f"http://127.0.0.1:{port}"
  33. status, props = _get(f"{root}/props")
  34. if status == 401:
  35. return DetectedServer(f"{root}/v1", "", "", None, router_mode=False, auth_required=True)
  36. if status != 200 or not isinstance(props, dict):
  37. return None
  38. build = str(props.get("build_info", ""))
  39. if not build:
  40. return None # answers /props but isn't llama-server
  41. dgs = props.get("default_generation_settings")
  42. models_status, models = _get(f"{root}/models")
  43. return DetectedServer(
  44. base_url=f"{root}/v1", build_info=build, model_path=str(props.get("model_path", "")),
  45. n_ctx=dgs.get("n_ctx") if isinstance(dgs, dict) else None,
  46. router_mode=models_status == 200 and isinstance(models, dict) and "data" in models,
  47. auth_required=False)
  48. def detect_server(extra_ports: tuple[int, ...] = ()) -> DetectedServer | None:
  49. """First hit across default + extra ports (managed port, config port)."""
  50. for port in dict.fromkeys((*DEFAULT_PROBE_PORTS, *extra_ports)):
  51. hit = probe_port(port)
  52. if hit:
  53. return hit
  54. return None