114 lines
4.9 KiB
Python
114 lines
4.9 KiB
Python
import ipaddress
|
|
import math
|
|
import re
|
|
import tomllib
|
|
from dataclasses import asdict, dataclass
|
|
from pathlib import Path
|
|
from urllib.parse import urlsplit
|
|
|
|
from .errors import LLMError
|
|
|
|
|
|
def positive(value, *, integer=False):
|
|
if isinstance(value, bool) or not isinstance(value, int if integer else (int, float)) or not math.isfinite(value) or value <= 0:
|
|
raise LLMError("LLM_CONFIG")
|
|
return value
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ProviderConfig:
|
|
id: str
|
|
model: str
|
|
base_url: str
|
|
adapter: str = "openai_compatible"
|
|
privacy: str = "local"
|
|
api_key_env: str | None = None
|
|
enabled: bool = True
|
|
timeout_seconds: float = 30.0
|
|
response_format: str = "json_object"
|
|
max_output_tokens: int = 4096
|
|
token_parameter: str = "max_completion_tokens"
|
|
allow_insecure_http: bool = False
|
|
input_cost_per_million: float | None = None
|
|
output_cost_per_million: float | None = None
|
|
cost_currency: str = "USD"
|
|
|
|
def __post_init__(self):
|
|
if not isinstance(self.id, str) or not re.fullmatch(r"[A-Za-z0-9_-]{1,64}", self.id):
|
|
raise LLMError("LLM_CONFIG")
|
|
if not isinstance(self.model, str) or not self.model.strip() or any(ord(c) < 32 for c in self.model):
|
|
raise LLMError("LLM_CONFIG")
|
|
if not isinstance(self.base_url, str) or any(c.isspace() or ord(c) < 32 for c in self.base_url):
|
|
raise LLMError("LLM_CONFIG")
|
|
try:
|
|
url = urlsplit(self.base_url)
|
|
_ = url.port
|
|
except ValueError as error:
|
|
raise LLMError("LLM_CONFIG") from error
|
|
if url.scheme not in {"http", "https"} or not url.hostname or url.username or url.password or url.query or url.fragment:
|
|
raise LLMError("LLM_CONFIG")
|
|
try:
|
|
loopback = ipaddress.ip_address(url.hostname).is_loopback
|
|
except ValueError:
|
|
loopback = url.hostname.lower() == "localhost"
|
|
if self.privacy not in {"local", "self_hosted", "cloud"} or (self.privacy == "local" and not loopback):
|
|
raise LLMError("LLM_CONFIG")
|
|
if url.scheme == "http" and not loopback and not self.allow_insecure_http:
|
|
raise LLMError("LLM_CONFIG")
|
|
if self.api_key_env is not None and (not isinstance(self.api_key_env, str) or not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", self.api_key_env)):
|
|
raise LLMError("LLM_CONFIG")
|
|
if self.response_format not in {"json_schema", "json_object", "none"} or self.token_parameter not in {"max_completion_tokens", "max_tokens"}:
|
|
raise LLMError("LLM_CONFIG")
|
|
if not isinstance(self.adapter, str) or not re.fullmatch(r"[A-Za-z0-9_-]{1,64}", self.adapter):
|
|
raise LLMError("LLM_CONFIG")
|
|
for value in (self.enabled, self.allow_insecure_http):
|
|
if not isinstance(value, bool):
|
|
raise LLMError("LLM_CONFIG")
|
|
positive(self.timeout_seconds)
|
|
positive(self.max_output_tokens, integer=True)
|
|
if not isinstance(self.cost_currency, str) or not re.fullmatch(r"[A-Z]{3}", self.cost_currency):
|
|
raise LLMError("LLM_CONFIG")
|
|
for cost in (self.input_cost_per_million, self.output_cost_per_million):
|
|
if cost is not None and (isinstance(cost, bool) or not isinstance(cost, (int, float)) or not math.isfinite(cost) or cost < 0):
|
|
raise LLMError("LLM_CONFIG")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class GatewayConfig:
|
|
providers: tuple[ProviderConfig, ...]
|
|
max_attempts: int = 10
|
|
retry_delay_seconds: float = 60.0
|
|
blocked_retry_seconds: float = 3600.0
|
|
max_request_bytes: int = 1024 * 1024
|
|
max_response_bytes: int = 1024 * 1024
|
|
privacy_profile: str = "local_only"
|
|
|
|
def __post_init__(self):
|
|
if not isinstance(self.providers, tuple) or not self.providers or any(not isinstance(p, ProviderConfig) for p in self.providers):
|
|
raise LLMError("LLM_CONFIG")
|
|
if len({p.id for p in self.providers}) != len(self.providers):
|
|
raise LLMError("LLM_CONFIG")
|
|
for value in (self.max_attempts, self.max_request_bytes, self.max_response_bytes):
|
|
positive(value, integer=True)
|
|
positive(self.retry_delay_seconds)
|
|
positive(self.blocked_retry_seconds)
|
|
if self.privacy_profile not in {"local_only", "self_hosted", "cloud_allowed"}:
|
|
raise LLMError("LLM_CONFIG")
|
|
|
|
@classmethod
|
|
def load(cls, path: str | Path):
|
|
try:
|
|
with Path(path).open("rb") as stream:
|
|
document = tomllib.load(stream)
|
|
if set(document) != {"llm"}:
|
|
raise LLMError("LLM_CONFIG")
|
|
value = dict(document["llm"])
|
|
value["providers"] = tuple(ProviderConfig(**entry) for entry in value["providers"])
|
|
return cls(**value)
|
|
except (OSError, ValueError, TypeError, KeyError) as error:
|
|
raise LLMError("LLM_CONFIG") from error
|
|
|
|
def snapshot(self):
|
|
# Only configuration references; never resolve credentials for persistence.
|
|
return asdict(self)
|