I score each document against the question by shared words, keep the top three, and print the matches so only the relevant documents reach the model.
facts = [
"Company refund policy is 30 days.",
"Office hours are 9 to 5.",
"Parking is free on Mondays.",
"The warranty covers two years.",
]
def similarity(query, doc):
q = set(query.lower().split())
d = set(doc.lower().split())
return len(q & d)
query = "how do I get my money back and what is the warranty"
sc = [(doc, similarity(query, doc)) for doc in facts]
sc = [s for s in sc if s[1] > 0]
sc.sort(key=lambda x: x[1], reverse=True)
top = sc[:3]
for i, doc in enumerate(top, 1):
print("Retrieved doc %d: %s" % (i, doc))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
Retrieved doc 1: The warranty covers two years.
Retrieved doc 2: Company refund policy is 30 days.
Retrieved doc 3: Parking is free on Mondays.
The words do not need to match exactly; shared meaning is enough. Retrieving the closest documents before answering is what keeps answers grounded in your data instead of guesses.