Delimiters, Grounding & Anti-Hallucination
The single most effective way to stop a model from making things up is grounding: give it the source material, fence it with clear delimiters, and instruct it to answer only from that material, with an explicit escape hatch when the answer is not there. This is the core of every retrieval-augmented system, and it is all prompt construction.
Build grounded_prompt(question, context_chunks) that assembles:
- A
CONTEXT:section with each chunk numbered and fenced, like[1] ...,[2] .... - The question after the context.
- An exact instruction to answer only from the context, ending with the escape hatch: if the answer is not in the context, reply exactly
I don't know. - A trailing open answer turn so the model knows to respond.
Then build the checker. check_grounded(answer, context_chunks) returns True when the answer is grounded: either it is exactly the I don't know. sentinel, OR every significant word in the answer appears in the joined context. It returns False when the answer contains invented words the context never mentioned.
ctx = ["The capital of France is Paris."]
check_grounded("The capital is Paris.", ctx) # True
check_grounded("I don't know.", ctx) # True (honest)
check_grounded("The capital is Berlin.", ctx) # False (invented)Ignore tiny stopwords (the, is, a, of...) when checking significant words, so normal phrasing is not punished, but a content word like Berlin that never appears in the context correctly fails the check. Press Run to grade.
Be honest about what this catches: containment flags invented tokens (words the context never mentioned), but it cannot catch a false claim recombined from real context words. "Refunds take France business days" would pass here even though it is nonsense, because every word is in the context. Real faithfulness checking needs an LLM-judge or an NLI (entailment) model that reasons about meaning, not just word membership.
Write grounded_prompt(question, context_chunks) that numbers and fences the chunks under CONTEXT:, appends the question, demands an answer only from the context with the exact escape hatch I don't know., and ends on an open answer turn. Write check_grounded(answer, context_chunks) returning True for the sentinel OR when every significant (non-stopword) answer word is in the joined context, else False.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.