KORP AI — anti-hallucination guardrail snippets (2026-06-01)
Working code companions to the post
AI Chatbot หลอน (Hallucination): ป้องกัน 7 ชั้น.
MIT licensed — adapt freely.
- output-guardrail.js — Layer 5. Regex-first screen that BLOCKs forbidden promises (full refund / 100% guarantee / free-shipping-no-min) and vague price guesses before the reply reaches the customer. No LLM call.
- no-invent-price.md — Layer 2. System-prompt fragment + deterministic injection pattern so the LLM phrases verified numbers but never invents a price/stock/policy.
- confidence-handoff.py — Layer 4. Make "I'm not sure" a valid answer: low retrieval similarity or out-of-scope → hand off to a human instead of guessing.
- citation-enforcer.py — Layer 3. Every factual claim must carry a
[src:id#n] tag; un-grounded claims are suppressed (Thai-aware, one-claim-per-line contract).
- audit-logger.py — Layer 7. Append-only, hash-chained conversation log with PII redaction (phone/email/Thai ID) for PDPA ม.30 record-of-processing.
- hallucination-eval.py — measure it before you ship: trap-question harness that scores hallucination / over-promise rate. Run weekly.
← all snippets · blog · korpai.co