An answer you cannot verify is worth very little, particularly for the document types that justify running this locally in the first place — contracts, policies, medical notes, case files.
Return sources alongside the answer
Rather than trusting the model to cite correctly, return the retrieved sources programmatically:
def answer_with_sources(question: str, k: int = 5) -> dict:
hits = collection.query(
query_embeddings=[embed_query(question)],
n_results=k,
)
passages = list(zip(hits["documents"][0], hits["metadatas"][0], hits["distances"][0]))
context = "\n\n---\n\n".join(f"[{m['source']}]\n{t}" for t, m, _ in passages)
response = ollama.chat(
model=MODEL,
messages=[{"role": "user", "content": PROMPT.format(context=context, question=question)}],
options={"temperature": 0.1},
)
return {
"answer": response["message"]["content"],
"sources": [
{"file": m["source"], "distance": round(d, 3), "preview": t[:200]}
for t, m, d in passages
],
}
Now every answer ships with the passages it was given, their files and how close each was to the question. The user can check, which is the entire point.
A cheap grounding check
You can catch a useful share of invention without another model. If the answer contains substantial text that appears nowhere in the context, be suspicious:
def grounding_score(answer: str, context: str) -> float:
"""Share of the answer's meaningful words that appear in the context."""
stop = {"the", "a", "an", "is", "are", "was", "were", "of", "to", "and",
"in", "that", "this", "for", "on", "with", "as", "by", "it"}
words = [w for w in answer.lower().split() if w not in stop and len(w) > 3]
if not words:
return 1.0
present = sum(1 for w in words if w in context.lower())
return present / len(words)
A low score does not prove invention — paraphrasing legitimately scores lower. But a score below about 0.4 is worth flagging in a user interface rather than presenting the answer plainly.
Make "I don't know" easy
Models avoid admitting ignorance unless you make it a first-class option. Two things help beyond the prompt rule from the last chapter.
Check the distances. If the closest chunk is far from the question, there probably is no answer in your documents. Short-circuit before calling the model at all:
if hits["distances"][0][0] > 0.6: # tune on your own data
return {"answer": "Nothing in the documents appears to cover that.",
"sources": []}
Show the retrieved passages even when the answer is "not found". Often the user can see that the right document was retrieved and the model simply failed to read it — which is diagnostic information they can act on.
What to log
Log the question, the retrieved chunk IDs and the answer. Not for analytics — for debugging. When someone reports a bad answer, the only useful question is "what did it retrieve", and without the log you cannot reconstruct it.
Keep those logs local. A RAG system built for privacy that ships its prompts to a hosted logging service has quietly undone its own purpose.