A tiny grounded answer pipeline.
Search approved passages, construct a labelled context, and decline when evidence is absent.
Why this workflow exists.
Retrieval-augmented generation is easier to understand when every stage is visible. A question is matched against a small document collection. Only approved passages are eligible. The answer function receives numbered evidence, and no result becomes a supported answer without a source ID.
The local search uses simple word overlap. It is intentionally transparent, not a recommendation for large document collections. The same pipeline boundaries apply when search is replaced by a proper lexical, semantic, or hybrid index.
Code context. Python 3 standard library only. The documents and answer formatter are local stand-ins for a larger retrieval system and model.
The whole example.
import re
DOCUMENTS = [
{"id": "access-1", "text": "Reset your password from the account settings page.", "approved": True},
{"id": "billing-2", "text": "Invoices are available under billing history.", "approved": True},
{"id": "draft-3", "text": "A draft policy under review.", "approved": False},
]
def words(text):
common = {"a", "can", "do", "i", "is", "my", "the", "what", "where"}
return set(re.findall(r"[a-z]+", text.lower())) - common
def search(question):
terms = words(question)
ranked = sorted(
(doc for doc in DOCUMENTS if doc["approved"]),
key=lambda doc: len(terms & words(doc["text"])),
reverse=True,
)
return [doc for doc in ranked if terms & words(doc["text"])]
def answer(question):
hits = search(question)[:2]
if not hits:
return {"status": "declined", "answer": None, "sources": []}
best = hits[0]
return {"status": "candidate", "answer": best["text"], "sources": [best["id"]]}
print(answer("Where can I reset my password?"))
print(answer("What is the holiday policy?"))What each stage does.
words extracts lowercase letter sequences, and search ranks approved documents by overlapping terms. It discards documents with no overlap. answer takes at most two hits, returns the top passage as a candidate, and includes its ID. If no approved passage matches, it returns a decline state.
The first question matches the access passage because reset and password overlap. The second has no matching approved evidence and declines. The unapproved draft never enters the ranking. In a real system, permissions would be per user, and the candidate answer would be generated from and checked against the selected passages.
What the output means.
The first output has a cited candidate answer; the second is a decline with no answer. This demonstrates that evidence availability should control whether the system answers.
Where the example stops.
Word overlap misses synonyms and can return a passage that mentions the words without answering the question. It also does not verify that the first passage is current. Evaluate retrieval recall and source authority before presenting generated answers as supported.