added guardrails.py to constrain responses and added validation to config.py

This commit is contained in:
2026-06-15 21:57:38 -04:00
parent 2e68c83021
commit f71ae7d2c6
4 changed files with 280 additions and 15 deletions
+18 -2
View File
@@ -3,9 +3,9 @@
from __future__ import annotations
from os import getenv
from typing import Annotated
from typing import Annotated, Self
from pydantic import AliasChoices, Field, field_validator
from pydantic import AliasChoices, Field, field_validator, model_validator
from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
@@ -82,6 +82,8 @@ class EbookSearchConfig(BaseSettings):
vector_candidate_multiplier: int = 4
bm25_candidate_limit: int = 120
rrf_rank_constant: int = 60
min_retrieval_confidence: float = 0.0
validate_citations_enabled: bool = True
bm25_index_dir: str = ".ebook_search_bm25"
bm25_refresh_delay_seconds: int = 60
@@ -99,6 +101,20 @@ class EbookSearchConfig(BaseSettings):
"""Normalize the configured embedding alias to its provider model name."""
return normalize_embedding_alias(value)
@model_validator(mode="after")
def validate_runtime_consistency(self) -> Self:
"""Reject configurations that cannot serve the features they enable."""
if not self.embedding_base_url.strip():
msg = "embedding_base_url must be set"
raise ValueError(msg)
if self.answer_enabled and (not self.vllm_base_url.strip() or not self.chat_model.strip()):
msg = "answer_enabled requires vllm_base_url and chat_model to be set"
raise ValueError(msg)
if self.rerank.enabled and not self.rerank.base_url.strip():
msg = "rerank.enabled requires rerank.base_url to be set"
raise ValueError(msg)
return self
def load_rerank_config() -> RerankConfig:
"""Load reranker config from environment variables."""