Part XV — Multimodal Foundation Models: CLIP, Cross-Attention & VLMs · Chapter 9

Retrieval-Augmented Generation (RAG)

Hook

Every model so far answers purely from what it already learned during training — frozen the moment training stopped. What if, right before answering, it could look something up first?

Intuition
Doc: capital of FranceDoc: capital of JapanDoc: capital of AustraliaQ: capital of FranceQ: capital of JapanQ: capital of Australia
retrieved "Doc: capital of Japan" → answer: "Tokyo"

Squares are facts in a small knowledge base; circles are questions. Click a question — it retrieves its nearest fact by embedding distance, exactly Chapter 1's cross-modal search, just within a single modality this time. The retrieved fact is what actually grounds the final answer.

Formalize

Retrieval is nothing more than the nearest-neighbor search from Chapter 1, applied to a document collection instead of images:

retrieve(q)=argminddocs  qd,answer={retrieve(q)’s answergroundedfixed priorungrounded\text{retrieve}(q) = \underset{d \in \text{docs}}{\arg\min}\; \lVert q - d \rVert, \qquad \text{answer} = \begin{cases}\text{retrieve}(q)\text{'s answer} & \text{grounded}\\ \text{fixed prior} & \text{ungrounded}\end{cases}
  • qq — the query: the question being asked, embedded into the same space as the documents.
  • dd — one candidate document in the knowledge base.
  • docs\text{docs} — the full collection of documents being searched.
  • retrieve(q)\text{retrieve}(q) — the document nearest to the query, found by nearest-neighbor search.
  • answer\text{answer} — the final answer: grounded in the retrieved document when retrieval is used, or a fixed generic prior when it isn't.
  1. Generation is simplified to a lookup, on purpose

    Retrieval doesn't change how a model generates — it changes what it has in front of it when it does, and that's the point this simplification is meant to make visible.

  2. A real system still generates fluent text

    It just conditions that generation on retrieved passages instead of — or alongside — its frozen training-time knowledge.

Play
Doc: capital of FranceDoc: capital of JapanDoc: capital of AustraliaQ: capital of FranceQ: capital of JapanQ: capital of Australia
grounded answer: "Canberra"

Toggle retrieval off for the same question. Without it, the model falls back on one fixed, generic answer — the single most common one it might have learned — regardless of what's actually being asked. With retrieval, the answer comes from whichever fact is actually nearest to this question.

Worked example

Three questions, one ungrounded fallback answer ("Paris" — the most famous capital, and this toy model's fixed prior):

  1. Capital of Japan — no retrieval

    The ungrounded answer is "Paris" — wrong. Japan's own fact sits at distance 0.360.36, but without retrieval the model never looks.

  2. Capital of Japan — with retrieval

    The nearest document is "capital of Japan," at distance 0.360.36 — more than seven times closer than the next-nearest fact. Retrieval corrects the wrong prior to "Tokyo."

  3. Capital of France — a coincidence

    Here the ungrounded prior ("Paris") happens to already be correct. Retrieval still finds the same right answer — but the fact that grounding and non-grounding agree here is luck, not evidence that skipping retrieval is safe.

Checkpoint

Every question falls back on the exact same default guess, Paris, whenever retrieval is off. Predict which question that guess is wrong for, turn retrieval off, and pick it.

pick a question
Pick a question and toggle retrieval
Summary
retrieve(q)=argminddocs  qd\text{retrieve}(q) = \underset{d \in \text{docs}}{\arg\min}\; \lVert q - d \rVert

Retrieval-augmented generation is the difference between a model answering from whatever it happened to memorize and a model answering from evidence it just looked up. The nearest-neighbor search itself is nothing new — it's Chapter 1's exact mechanism — but grounding a generator's answer in it is what keeps a model from confidently repeating a stale or simply wrong prior. The capstone puts this same retrieval idea to work on images: type a query, and let the same nearest-neighbor search find the picture that matches.