▣~/runthismodel▍
local AI explorer00:00:00Z
← Local AI: Zero to Hero

CHAPTER 07 OF 12

Answer from your documents

Retrieve sources, preserve their identity and check citations and answers separately.

In this chapter

Add knowledge without changing weights

Retrieval selects useful source material and includes it in the model's input. This is often a practical way to answer questions about documents that change. The weights remain the same; the answer depends on which passages the application retrieves and how the model uses them.

A retrieval system has several independently testable stages: importing documents, splitting them into useful units, finding candidate passages, selecting a bounded context, generating an answer and checking its support. If the answer is wrong, inspect each stage. Tuning the generation prompt cannot recover a fact that was never retrieved.

The book's first retriever uses word overlap weighted by how common a term is across the small corpus. This makes its decisions easy to inspect. It is not semantic search. Synonyms, paraphrases and unfamiliar terminology can defeat it. Embedding search is another option: encode queries and passages into compatible vectors, then rank by a similarity measure. That adds an embedding model, index version, distance choice and additional evaluation requirements.

Keep the document boundary visible

A useful retrieved item retains an identifier, text and a link or location in the original source. In a larger system, also retain document version, access permissions and the chunk's location. Updating a document should invalidate or replace its old index entries; otherwise a correct retrieval algorithm can still answer from stale information.

Documents may contain instructions, intentionally or accidentally. Treat them as quoted evidence. A sentence in a retrieved file must not grant the model permission to send email, run commands or read other files. Prompt wording helps express the boundary, but does not reliably enforce permissions. The application must enforce those independently.

Prerequisites

Complete chapters 4 to 6. The companion documents.json contains three fictional projects. This exercise reads only that fixture; it does not ingest your filesystem. Keep the model service running.

Steps

  1. Read the three documents. For the question "When is the Atlas code freeze?", record the expected fact and source ID before running the model.

  2. Run retrieval and generation:

python3 rtm_lab.py rag --output retrieval-run.json
  1. Inspect retrieved. Confirm that the relevant document is present and that the score is labeled as a retrieval score, not answer confidence.

  2. Inspect parsed, validShape and passed. The required answer value is Tuesday at noon, and the source must be atlas. A correct source ID with a wrong deadline still fails.

  3. Compare the initial report and distractor report with the current report. The first output added a sentence around the deadline. An example clarified the format, but the model then borrowed the other project's deadline. The current generator receives only the highest-ranked source; selectedSourceIds records that choice. Both retrieved candidates remain in the report for inspection.

  4. In a copy of the fixture, change the Atlas deadline and rerun. Keep the old fixture and result so the change is traceable.

Expected results

The retriever should include the Atlas document among its two selected sources. The current reference run's strict task result is passed. The raw report is authoritative if your result differs. The prompt and source-selection revisions are development work, not a held-out evaluation of retrieval quality. Selecting one source limits the available context; questions requiring several documents need a different design and fresh tests.

A citation is useful only if it resolves to a real source and the source actually supports the claim. Checking that the citation string exists is necessary but insufficient. For a real application, test unanswered questions and conflicting sources as well as simple positive cases.

Common failures

Chunks can split a fact from its qualification. Large chunks can bury the relevant sentence or exceed context. Repeated boilerplate can dominate word matching. An old index can preserve deleted information. A model can obey a schema while inventing an answer. Diagnose these as separate failures.

Never combine documents from different users before enforcing access permissions. Local execution does not remove the risk of returning another person's private text.

Exercise

Add two conflicting versions of a fictional project note, each with a date and source ID. Define which version should win and how the answer should expose that choice. Then add a question that no document answers. Do not accept a guessed deadline as successful retrieval.

Sources

The original RAG research paper provides background on retrieval-augmented generation. The companion implements a deliberately simpler lexical retriever. Ollama embeddings is a possible extension, not an executed part of this edition.