cd .. / course
Part 3 · Lesson 7/8

RAG: retrieve the facts, then prompt with them

The weights froze the day training ended, so a running model can't learn a new fact. RAG gets knowledge in the only way that's left — it retrieves the closest passages by cosine similarity and prepends them to the prompt. The model just reads them as context.

⏱ ~6 min 🎚 cosine retrieval, live math: some

After training, the weights are frozen (Part 1: "train vs run"). A live model can't absorb a new fact on its own. There are only three ways to get knowledge in: prompting (paste the text yourself), fine-tuning (retrain the weights — permanent, costly, then frozen again), and RAG.

RAG is automated prompting. Keep your documents as embedding vectors in a library; at query time, embed the question and retrieve the closest passages by cosine similarity — the exact operation of Part 1 lesson 2 — then prepend them to the prompt. Retrieval finds the text; prompting injects it.

So retrieval quality gates answer quality. If the top match is strong, the model answers from real text. If nothing is close, RAG injects nothing and you're back to the frozen weights guessing — the honest fix for hallucination (lesson 14).

Prompting
one-off facts

You already have the text. Paste it into the context. Instant, but bounded by the window — and you must supply it every time.

RAG
a changing knowledge base

Retrieval fetches the right passages automatically by embedding similarity, then prompts with them. Update the library, not the model.

Fine-tuning
changing behaviour / format

Actually retrain the weights on new data. Permanent and expensive — best for teaching a style or skill, not fresh facts.

Poke it · pick a query, cosine-retrieve, watch it prepend
the query (embedded to a vector)
top-N to retrieve
the passage library — cos(query, passage) over all 6, live
top match
P2
cos 1.00
passages injected
2
prepended to prompt
retrieval confidence
strong
top cos ≥ 0.9
the assembled prompt · retrieved text prepended above the question

TRY → Pick a different query and every bar re-runs cos(query, passage) and re-ranks — the retrieved cards and the assembled prompt rebuild. Bump top-N to inject more. Nothing here is stored: it's the Part 1 lesson 2 cosine looped over the library on every click.

Show me the actual numbers

Retrieval is literally the cosine of Part 1 lesson 2, run over a document library. Each passage is a 4-dim vector on the axes [animals, weather, cooking, space]; each query is a vector too. Take "biggest planet" = [0, 0.1, 0, 1] against Jupiter P3 = [0, 0.2, 0, 1]:

cos(q, P3) = (0·0 + 0.1·0.2 + 0·0 + 1·1) / (√(0.1²+1²) · √(0.2²+1²)) = 1.02 / (√1.01 · √1.04) = 1.02 / (1.005 · 1.020) = 0.995

Every other passage scores near zero for that query — the next best, storm-cloud P1, is only 0.198. So retrieval cleanly returns Jupiter. The other verified queries: "cats sleep" [1,0,0,0] → P0 cos 0.995 (then P4 0.944); "cook onions" [0,0.1,1,0] → P2 cos 1.00, then P5 cos 0.989.

An embedding model turns each passage into a vector once, offline; at query time you embed the question and rank by cosine — that's the entire retrieval step. Fine-tuning would instead bake the facts into the weights (and re-freeze them); prompting is you doing the retrieval by hand. RAG shines when the knowledge changes faster than you can retrain, and it gives the softmax real text to lean on instead of plausibility — the honest fix for the lesson-14 hallucination.

MythRAG teaches the model new knowledge — the retrieved facts get learned.
RealityThe weights never change. RAG retrieves relevant text by embedding similarity and pastes it into the prompt — the model reads it as context, exactly as if you had typed it.
Takeaway

Frozen weights can't learn a fact — so RAG retrieves it by cosine and prepends it to the prompt. Retrieval is Part 1's cosine; injection is prompting. The model just reads.

In RAG, how are the right documents found?
Why: retrieval ranks passages by cosine similarity in embedding space and injects the top ones as context; the frozen weights are never touched.