Two endpoints over forge's Ollama gpt-oss:120b: /local-optimistic/v1 forge, else cloud gpt-5.4-mini /local-strict/v1 forge, else a clean 503 the caller handles Discovery, not configuration: each instance probes an ordered candidate list (stable DNS name, then the Lounge and future Serenity literals) concurrently but in priority order, so the happy path is one round trip and silicon's move to Serenity needs no rewrite. /healthz runs a real 1-token completion — forge can be up with Ollama stopped. Liveness/readiness are deliberately NOT forge-gated: otherwise k8s would evict the pods during an outage and callers would get a connection failure instead of the promised 503, and Kuma could not tell "forge down" from "router gone". max_tokens/max_completion_tokens are translated per backend: Ollama accepts only the former, gpt-5.4-mini rejects it outright. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
97 lines
4.0 KiB
Python
97 lines
4.0 KiB
Python
"""Runtime configuration for the router.
|
|
|
|
Everything is environment-driven so that one image serves every site. The only
|
|
value that encodes *identity* is ``FORGEWAY_NAME``; it is read here once and
|
|
referenced from everywhere else, because the final DNS name is still Rick's call.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
def _env(key: str, default: str) -> str:
|
|
value = os.environ.get(key)
|
|
return default if value is None or value.strip() == "" else value.strip()
|
|
|
|
|
|
def _env_int(key: str, default: int) -> int:
|
|
try:
|
|
return int(_env(key, str(default)))
|
|
except ValueError:
|
|
return default
|
|
|
|
|
|
def _env_float(key: str, default: float) -> float:
|
|
try:
|
|
return float(_env(key, str(default)))
|
|
except ValueError:
|
|
return default
|
|
|
|
|
|
def _env_bool(key: str, default: bool) -> bool:
|
|
return _env(key, "true" if default else "false").lower() in {"1", "true", "yes", "on"}
|
|
|
|
|
|
def _env_list(key: str, default: str) -> list[str]:
|
|
raw = _env(key, default)
|
|
return [item.strip().rstrip("/") for item in raw.split(",") if item.strip()]
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Config:
|
|
# --- identity -------------------------------------------------------
|
|
# The working name. The final DNS name is NOT decided yet, so this is the
|
|
# single place the service learns what it is called.
|
|
name: str = field(default_factory=lambda: _env("FORGEWAY_NAME", "forgeway"))
|
|
site: str = field(default_factory=lambda: _env("FORGEWAY_SITE", "unknown"))
|
|
|
|
# --- discovery ------------------------------------------------------
|
|
# Ordered candidate list. The router probes these to find forge; it never
|
|
# assumes which site forge currently lives at. Put the stable DNS name
|
|
# first so a host move is followed automatically by DNS alone.
|
|
upstreams: list[str] = field(
|
|
default_factory=lambda: _env_list(
|
|
"FORGEWAY_UPSTREAMS",
|
|
"http://forge.dmz.falkvinge.net:11434",
|
|
)
|
|
)
|
|
local_model: str = field(default_factory=lambda: _env("FORGEWAY_LOCAL_MODEL", "gpt-oss:120b"))
|
|
|
|
# --- cloud fallback (local-optimistic only) -------------------------
|
|
cloud_base_url: str = field(
|
|
default_factory=lambda: _env("FORGEWAY_CLOUD_BASE_URL", "https://api.openai.com/v1").rstrip("/")
|
|
)
|
|
cloud_model: str = field(default_factory=lambda: _env("FORGEWAY_CLOUD_MODEL", "gpt-5.4-mini"))
|
|
|
|
# --- health ---------------------------------------------------------
|
|
# Health is gated on a REAL completion, never a ping: forge can be up with
|
|
# Ollama stopped, or Ollama up with the model evicted.
|
|
health_ttl: float = field(default_factory=lambda: _env_float("FORGEWAY_HEALTH_TTL", 5.0))
|
|
health_fail_ttl: float = field(default_factory=lambda: _env_float("FORGEWAY_HEALTH_FAIL_TTL", 3.0))
|
|
health_timeout: float = field(default_factory=lambda: _env_float("FORGEWAY_HEALTH_TIMEOUT", 45.0))
|
|
discovery_timeout: float = field(default_factory=lambda: _env_float("FORGEWAY_DISCOVERY_TIMEOUT", 4.0))
|
|
# When false, a probe that finds the model not resident reports DOWN instead
|
|
# of triggering a cold load. Used to keep monitoring from re-loading
|
|
# gpt-oss:120b during an ai-toolkit run (they collide on forge's card 1).
|
|
health_cold_load: bool = field(default_factory=lambda: _env_bool("FORGEWAY_HEALTH_COLD_LOAD", True))
|
|
|
|
# --- proxying -------------------------------------------------------
|
|
request_timeout: float = field(default_factory=lambda: _env_float("FORGEWAY_REQUEST_TIMEOUT", 600.0))
|
|
connect_timeout: float = field(default_factory=lambda: _env_float("FORGEWAY_CONNECT_TIMEOUT", 5.0))
|
|
port: int = field(default_factory=lambda: _env_int("FORGEWAY_PORT", 8080))
|
|
log_level: str = field(default_factory=lambda: _env("FORGEWAY_LOG_LEVEL", "INFO").upper())
|
|
|
|
@property
|
|
def cloud_api_key(self) -> str:
|
|
"""Read lazily and never stored, logged, or echoed."""
|
|
return os.environ.get("OPENAI_API_KEY", "").strip()
|
|
|
|
@property
|
|
def cloud_enabled(self) -> bool:
|
|
return bool(self.cloud_api_key)
|
|
|
|
|
|
CONFIG = Config()
|