← Lesson
Citations and Faithfulness
Joeven
Run
Reset
Python loads on first run
import json import re SOURCES = { "billing.md": "Refunds for INV-* take 5-7 days. Never refund in cash.", "runbook.md": "Runner OOM: raise memory limit on the worker, then replay the DLQ.", } RETRIEVED = {"billing.md"} def normalize(s): return re.sub(r"\s+", " ", s).strip().lower() def quote_in_source(quote, source): return normalize(quote) in normalize(SOURCES.get(source, "")) def check_answer(obj): problems = [] for c in obj.get("citations") or []: if c["source"] not in RETRIEVED: problems.append("not retrieved " + c["source"]) continue if not quote_in_source(c["quote"], c["source"]): problems.append("quote not in " + c["source"]) ans = obj.get("answer", "").lower() if "cash" in ans and "never" not in ans and "not" not in ans: problems.append("possible unfaithful cash-refund claim") return {"ok": not problems, "problems": problems} good = { "answer": "Refunds take 5-7 days and cannot be paid in cash.", "citations": [{"source": "billing.md", "quote": "take 5-7 days"}], } bad = { "answer": "We can refund you in cash today.", "citations": [{"source": "billing.md", "quote": "cash today instantly"}], } sneak = { "answer": "Raise memory.", "citations": [{"source": "runbook.md", "quote": "raise memory limit"}], } print("good", json.dumps(check_answer(good))) print("bad ", json.dumps(check_answer(bad))) print("sneak", json.dumps(check_answer(sneak)))
Run to execute this in your browser. Nothing is sent to a server.