Part IV · Ch. 20 — RAG and Vector Search

Part IV · Chapter 20 of 21

RAG and Vector Search

Embeddings at work: giving the model a library card


A question the model can't answer

“What's our refund policy?” Training may never have included our private documents. A fluent model can still invent a plausible policy. We need to supply evidence.

Where should changing facts live?
ApproachCost or limit
Retrain or fine-tuneChanges weights; slow to refresh and unreliable for exact recall
Paste every documentUses context space and attention computation
Retrieve relevant passagesKeeps documents outside the model; depends on search quality

Vector search finds candidates. RAG supplies those passages to the model. We can build both with Chapter 8's embeddings and Chapter 2's cosine similarity.

Whole passages become arrows

An embedding model represents a whole passage with one vector. Query and document encodings must share a compatible space. Training makes relevant pairs easier to retrieve; similarity is evidence of relevance, not proof.

“How do I get my money back?” and “Items may be returned within 30 days for a full refund” can match despite different wording. Our arrows are invented for teaching, as in Chapter 8; real vectors usually have many more dimensions.

Two text chips on the left — 'How do I get my money back?' and 'Items may be returned within 30 days for a full refund.' — with the annotation 'zero important words in common' beneath them. Both feed thin arrows into a single box labeled 'embedding model, a smaller sibling transformer'. On the right, a small pair of axes shows an arrow toward direction (3,4) labeled 'question' and an arrow toward (4,3) labeled 'refund passage', with an arc between them labeled cos theta = 0.96.
Related wording can point the same way.
$$\mathbf{q} = \begin{bmatrix} 3 \\ 4 \end{bmatrix} \qquad \mathbf{v}_{\text{refund}} = \begin{bmatrix} 4 \\ 3 \end{bmatrix}$$

In words: the question points to the spot (3, 4) on the plane and the refund passage points to (4, 3), almost the same direction, with the across- and up-parts swapped.

Our question is $\mathbf q=[3,4]$. The refund passage is $[4,3]$. Both have length 5. We deliberately reused the cat–dog arithmetic so we can check the result by hand.

A library of arrows: nearest-neighbor search

We split documents into chunks, embed each, and store its text, source, and vector in a vector database. This preparation is called indexing.

At question time, nearest-neighbor search ranks candidates. Our measure is cosine:

$$\cos\theta = \frac{\mathbf{q} \cdot \mathbf{v}_{\text{refund}}}{\|\mathbf{q}\| \, \|\mathbf{v}_{\text{refund}}\|} = \frac{3 \times 4 + 4 \times 3}{5 \times 5} = \frac{24}{25} = 0.96$$

In words: multiply matching slots and add to get 24, then divide by both lengths. Their cosine is 0.96, a directional similarity score.

Worked example

Five passages, five dot products over the same $5 \times 5 = 25$, then sort:

Five passages scored against the question
Passage (its arrow)q · passagecos θreading
refund policy [4, 3]3×4 + 4×3 = 240.96the winner
warranty claims [4.8, 1.4]3×4.8 + 4×1.4 = 200.80related
holiday hours [−3, 4]3×(−3) + 4×4 = 70.28a little
shipping times [4, −3]3×4 + 4×(−3) = 00.00perpendicular
careers page [−3, −4]3×(−3) + 4×(−4) = −25−1.00the anti-match

Look at the shipping row. $12 - 12 = 0$ means the shipping passage sits at a right angle to the money-back question: unrelated in the Chapter 2 sense, neither agreeing nor opposing.

A full-plane plot with equal aspect. Five dots mark the library passages: refund at (4,3), warranty at (4.8,1.4), holiday hours at (−3,4), shipping times at (4,−3), careers at (−3,−4). An arrow runs from the origin to (3,4) for the question, labeled 'money back? q = [3,4]'. A solid connector to the refund dot is labeled 0.96 and a dashed one to warranty is labeled 0.80. Annotations note that shipping times sits exactly perpendicular at cos 0.00 and careers points perfectly away at cos −1.00.
Cosine ranks the toy library.

These are hand-placed geometry scores, not probabilities of relevance. A negative cosine is an opposite direction; in a learned embedding space it need not mean an opposite meaning.

Approximate nearest-neighbor search inspects promising regions instead of comparing every vector. The tradeoff is speed versus missed candidates. Library size, vector width, hardware, and filtering determine when approximation helps.

RAG: the full loop

Prepare the document index first; model weights stay fixed.
StepAction
1. Embed the questionUse the library’s embedding space.
2. Retrieve passagesRank matches and enforce access rules.
3. Assemble the promptCombine sources, question, and instructions.
4. Generate and checkAnswer from context; verify claims against cited text.
Index documents once, then embed a question, retrieve passages, assemble the prompt, and generate a cited answer with fixed weights.
Index once; retrieve for each question.

The assembled prompt (step 3, made visible)

Context from company documents:
— Items may be returned within 30 days for a full refund.
— Warranty claims: defects covered for one year.
Question: How do I get my money back?
Answer using only the context above.

The passages enter as ordinary context tokens. Attention reads them; training weights stay fixed. Source identifiers let the answer point back to evidence. Access restrictions must apply before unauthorized passages reach the prompt.

Where retrieval can fail
FailureWhat we check
Relevant text never retrievedChunk boundaries, query encoding, and search coverage
Exact name or part number missedCombine keyword and vector search
Outdated or unauthorized sourceRefresh the index and filter permissions
Answer distorts the retrieved textCompare claims with citations; allow an insufficient-evidence answer

Grounding helps us check answers. It cannot guarantee them. Hybrid search covers complementary strengths. Re-embed changed chunks when documents change; changing the embedding model usually requires rebuilding compatible document vectors.

See it move

The eleven-passage widget lets us move the question vector and inspect the resulting prompt. It exposes the retrieval arithmetic; production systems add indexing, permissions, and evaluation.

Why the LLM cares

Weights contain learned skills and factual associations. Context supplies the current working material. Retrieval selects material from an external library. Fine-tuning and RAG can complement each other: adapt response behavior, then retrieve current evidence. The same vectors also support search without generation, duplicate detection, clustering, and recommendations.

Three homes for information

Retrieval moves library evidence into the context the model reads.
HomeContainsUpdated by
WeightsLearned facts and skillsTraining; relatively slow
Context windowCurrent working materialPrompt assembly; immediate
LibraryDocuments, vectors, sourcesEditing and re-embedding; typically minutes

Worked example: change the policy

Our stored policy says 30 days. If the approved document changes to 45 days, update its text and embedding, then retrieve that new passage. The assembled prompt now says 45 days while model weights remain unchanged. Check that the answer cites the current source.

What you now know

  • Embedding models represent passages and questions in compatible vector spaces.
  • Our refund passage wins with cosine 0.96.
  • RAG retrieves passages, adds them to context, then generates.
  • Index updates change available evidence without changing model weights.
  • Retrieval and generation can both fail; sources make checking possible.

Where we're headed

We have built the last mechanism. Next we place it on the wider map of machine learning.