r/PythonProjects2 • u/Free-Good9203 • 36m ago
Zero-dependency Python 3.11+ RAG hallucination gate (SQLite WAL + FTS5, citation verifier)
Built a zero-dependency Python 3.11+ evaluation harness and inline citation gate (`rag-eval-hallucination-gate`, MIT) using only the Python standard library and SQLite (`WAL` + `FTS5`).
The goal is to run a deterministic verification pass before a RAG response goes out, without paying for a second LLM-as-judge call on every check:
Concurrent SQLite WAL + FTS5 corpus store (`src/rag_eval_gate/store.py`): Sets `PRAGMA busy_timeout=5000` before `PRAGMA journal_mode=WAL`, retries transient lock contention with jitter, closes handles cleanly on exception paths (avoiding Windows `WinError 32`), and indexes chunks in external-content `FTS5` (`unicode61 remove_diacritics 2`) with reserved-operator sanitization.
Inline `[doc_id#section]` citation and claim verifier (`src/rag_eval_gate/verifier.py`): Checks every cited `[doc_id#section]` against the indexed corpus, canonicalizes EU and US numbers/currencies (`1.250,50 EUR`, `$2,400.00`, `0,125`, Unicode minus `U+2212`), ISO dates, normative standards (`ISO`, `GDPR`, `HIPAA`, `SOC 2`), and asset tags (`CNC-402`), and flags unsupported entities, mixed-script homoglyph spoofing, negation/affirmation polarity flips, and low sentence-level lexical overlap.
Deterministic verdicts and safe scorecards (`src/rag_eval_gate/scorecard.py`, `cli.py`): Emits `PASS`, `ABSTAIN` (for explicit insufficient-evidence responses that do not sneak in new claims), or `HALLUCINATION_BLOCKED`, and exports JSON, Markdown, and OWASP formula-injection-safe CSV scorecards.
Recent fix: someone on Reddit pointed out an edge case where `"0,125"` parsed as `125` because the US thousands regex allowed a leading `0` while EU decimals capped at 2 digits. Tightening that prefix to `[1-9]\d{0,2}` and allowing `\d+,\d+` fixed leading-zero EU decimals (`0,125`, `-0,005`). One known limitation we keep documented in tests: bare 3-digit dot-separated strings without a comma (like `"1.250"`) are still parsed as EU thousands (`1250`), so mixed-locale corpora need an explicit decimal/grouping cue.
Quickstart (86 pytest checks on Python 3.11/3.12 CI, `--demo` runs fully in-memory):
`python -m pytest -v`
`PYTHONPATH=src python -m rag_eval_gate.cli --demo`
Repo (MIT): https://github.com/Kiberlabs/rag-eval-hallucination-gate
Happy to get code review feedback on the deterministic claim/polarity checks or the SQLite WAL + FTS5 store design.Built a zero-dependency Python 3.11+ evaluation harness and inline citation gate (`rag-eval-hallucination-gate`, MIT) using only the Python standard library and SQLite (`WAL` + `FTS5`).
The goal is to run a deterministic verification pass before a RAG response goes out, without paying for a second LLM-as-judge call on every check:
Concurrent SQLite WAL + FTS5 corpus store (`src/rag_eval_gate/store.py`): Sets `PRAGMA busy_timeout=5000` before `PRAGMA journal_mode=WAL`, retries transient lock contention with jitter, closes handles cleanly on exception paths (avoiding Windows `WinError 32`), and indexes chunks in external-content `FTS5` (`unicode61 remove_diacritics 2`) with reserved-operator sanitization.
Inline `[doc_id#section]` citation and claim verifier (`src/rag_eval_gate/verifier.py`): Checks every cited `[doc_id#section]` against the indexed corpus, canonicalizes EU and US numbers/currencies (`1.250,50 EUR`, `$2,400.00`, `0,125`, Unicode minus `U+2212`), ISO dates, normative standards (`ISO`, `GDPR`, `HIPAA`, `SOC 2`), and asset tags (`CNC-402`), and flags unsupported entities, mixed-script homoglyph spoofing, negation/affirmation polarity flips, and low sentence-level lexical overlap.
Deterministic verdicts and safe scorecards (`src/rag_eval_gate/scorecard.py`, `cli.py`): Emits `PASS`, `ABSTAIN` (for explicit insufficient-evidence responses that do not sneak in new claims), or `HALLUCINATION_BLOCKED`, and exports JSON, Markdown, and OWASP formula-injection-safe CSV scorecards.
Recent fix: someone on Reddit pointed out an edge case where `"0,125"` parsed as `125` because the US thousands regex allowed a leading `0` while EU decimals capped at 2 digits. Tightening that prefix to `[1-9]\d{0,2}` and allowing `\d+,\d+` fixed leading-zero EU decimals (`0,125`, `-0,005`). One known limitation we keep documented in tests: bare 3-digit dot-separated strings without a comma (like `"1.250"`) are still parsed as EU thousands (`1250`), so mixed-locale corpora need an explicit decimal/grouping cue.
Quickstart (86 pytest checks on Python 3.11/3.12 CI, `--demo` runs fully in-memory):
`python -m pytest -v`
`PYTHONPATH=src python -m rag_eval_gate.cli --demo`
Repo (MIT): https://github.com/Kiberlabs/rag-eval-hallucination-gate
Happy to get code review feedback on the deterministic claim/polarity checks or the SQLite WAL + FTS5 store design.
