NovaGuide / src /rag /safety_validator.py
DKLPDV15\eB
Deploy NovaGuide with versioned runtime configuration
7ca0188
Raw History Blame Contribute Delete
1.88 kB
"""Validate deterministic answers for unsupported live and personal claims."""
from __future__ import annotations
import re
from typing import Any
class SafetyValidator:
"""Detect claims that require unavailable live or personal evidence."""
LIVE_PATTERNS = (
r"\b(?:is|was|has been)\s+delayed\b", r"\b(?:is|was|has been)\s+cancelled\b",
r"\bboarding\s+(?:is\s+)?now\b", r"\bgate\s+(?:has\s+)?changed\b",
r"\byour\s+baggage\s+is\s+located\b", r"\bsecurity\s+incident\b",
r"\bemergency\s+evacuation\b",
)
PERSONAL_PATTERNS = (r"\byour\s+booking\b", r"\byour\s+passport\b")
def contains_prohibited_live_claims(self, answer: str) -> bool:
lowered = answer.lower()
return any(re.search(pattern, lowered) for pattern in self.LIVE_PATTERNS)
def contains_personal_data_claims(self, answer: str) -> bool:
lowered = answer.lower()
return any(re.search(pattern, lowered) for pattern in self.PERSONAL_PATTERNS)
def validate_response(
self, answer: str, context: dict[str, Any] | None = None, route: str | None = None
) -> dict[str, Any]:
issues: list[str] = []
if not answer.strip():
issues.append("Empty response")
api_success = bool((context or {}).get("flight_context", {}).get("success"))
confirmed_warning = "official ber" in answer.lower() or "your airline" in answer.lower()
if self.contains_prohibited_live_claims(answer) and not (api_success and confirmed_warning):
issues.append("Unsupported live operational claim")
if self.contains_personal_data_claims(answer):
issues.append("Unsupported personal-data claim")
notes = [] if not issues else ["Unsafe draft must be replaced with a fallback."]
return {"valid": not issues, "issues": issues, "safety_notes": notes}