Download src/rag/safety_validator.py from danishk84/NovaGuide: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/spaces/danishk84/NovaGuide/resolve/main/src/rag/safety_validator.py
- Command line
-
hf download hf://spaces/danishk84/NovaGuide/src/rag/safety_validator.py
-
curl -L -o safety_validator.py https://huggingface.co/spaces/danishk84/NovaGuide/resolve/main/src/rag/safety_validator.py
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} | |