Sub topic 13
Grounding answers in supplied documents
This chapter assumes you have used a chat-based assistant, can read short Python, and know that models learn from large text collections. Earlier chapters covered why models guess when they lack knowledge. Here you will see how to give the model the source text, so it has less reason to guess.
What grounding means
Grounding means placing the relevant document text in the prompt and asking the model to answer from that text. The model no longer has to recall a fact from training. It can read the fact in front of it.
Grounding reduces fabrication, but it does not remove it. The model can still ignore the document, misread it, or fill a gap with a plausible guess. Grounding works best when you also make it easy to check the answer against the text.
A basic grounded setup
A grounded prompt has four parts:
- The instruction: answer only from the document.
- The document, clearly marked.
- A fallback: what to say if the answer is not there.
- A request for a supporting quotation.
def build_prompt(document, question):
return (
"Answer the question using only the document below.\n"
"If the document does not contain the answer, reply exactly: "
"NOT IN DOCUMENT.\n"
"After your answer, give a direct quotation from the document "
"that supports it, on a line starting with QUOTE:.\n\n"
"DOCUMENT:\n" + document + "\n\n"
"QUESTION: " + question
)
doc = "The report was published in March and covers 2021 data."
print(build_prompt(doc, "Which year does the report cover?"))The fallback phrase matters. Without it, a model that cannot find the answer often invents one, because a fluent answer is the most likely continuation. A fixed phrase also lets your code detect the "not found" case.
Checking the quotation
The quotation request gives you something to test. If the model claims a quote comes from the document, you can check that the text really appears there.
def quote_supported(quote, source_text):
def norm(s):
return " ".join(s.lower().split())
return norm(quote) in norm(source_text)
source = "The report was published in March and covers 2021 data."
print(quote_supported("covers 2021 data", source))
print(quote_supported("covers 2023 data", source))The first call prints True and the second prints False. An invented quote fails the check.
Common errors in grounded setups
- A real quote that does not support the claim. The text exists, but the answer misreads it. The string check cannot catch this, so a human still needs to read the quote beside the answer.
- Retrieving the wrong passage. When a system searches a large collection and passes in only the best-matching passages, the answer may be absent from them. A good model then says NOT IN DOCUMENT. A weaker setup guesses.
- Mixing in training knowledge. The model may blend what it remembers with what the document says. Telling it to use only the document helps, but does not guarantee it.
- Long or cluttered documents. Relevant details buried in a very long prompt are easier to miss. Pass in only the sections that matter.
- False premises in the question. A question such as "Why did the report drop the 2022 data?" assumes something the document may not say. Ask neutral questions where you can.
When to ground
Ground the answer when the facts must come from a specific source: contracts, policies, internal reports, product manuals, or recent events the model could not have learned. For general explanations where an error costs little, grounding adds effort without much gain.
Exercise
A grounded assistant answers "The refund window is 30 days" and quotes "refunds are accepted within 30 days of purchase". Your string check confirms the quote appears in the document. Name one remaining way the answer could still be wrong.
Show answer
The quote may belong to a different product or condition than the one asked about, for example the document may say 30 days only for unopened items. The string check proves the text exists, not that it supports the claim in context. A person must read the quote with its surrounding text.
Exercise
Why should a grounded prompt specify an exact phrase such as NOT IN DOCUMENT for missing answers?
Show answer
Without a permitted way to decline, the model tends to produce a fluent guess. An exact phrase gives it an acceptable alternative and lets your code detect missing answers automatically.