/{mode}/api/{tags,chat,generate,version,show,ps} beside the OpenAI surface.
forge path is a raw proxy (forge is Ollama); the cloud fallback is translated
both ways — NDJSON frames byte-compatible with Ollama's, tool calls round-trip
with synthesised ids, format mapped, think→reasoning_effort (forced to none
with tools: OpenAI rejects the combination on gpt-5.4-mini). Tags never wait
on a model load (HA's list() has a 5 s timeout). local-strict still 503s in
Ollama's error shape; the OpenAI dialect is untouched.
Home Assistant (Lounge, 2026.9.2) configured through its REST config flow and
proven with a real conversation. HA's VLAN cannot reach the k8s ingress, so
it uses forgeway.dmz.lounge.falkvinge.net through the entrance HAProxy
(RFC1918-fenced vhost); the .services. alias needs a pfSense rule from Rick.
22/22 in-cluster checks on both sites. scripts/ha_setup_ollama.py is
idempotent and ends with a real answer; local-smoke-ollama.sh uses the exact
client HA pins.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
100 lines
4.3 KiB
Python
100 lines
4.3 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))
|
|
# Reported by /{mode}/api/version when forge has never answered (clients may
|
|
# gate features on it); replaced by forge's real version once seen.
|
|
ollama_version_fallback: str = field(default_factory=lambda: _env("FORGEWAY_OLLAMA_VERSION_FALLBACK", "0.34.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()
|