4.4.11 · D2Alignment, Prompting & RAG

Visual walkthrough — Retrieval-Augmented Generation (RAG) architecture

2,175 words10 min readBack to topic

We are answering one question, slowly: "Given a question, how does a machine decide which pieces of text are worth reading — and then how does it use them to answer?"


Step 1 — Turn words into arrows

WHAT. A computer cannot compare two sentences directly — it only compares numbers. So we hand each piece of text to an encoder, a trained function that spits out a fixed list of numbers.

We only draw 2 numbers (2D) so we can see it. Real embeddings have hundreds — the geometry is identical, just invisible. See Vector Embeddings for how the encoder is trained.

WHY. Direction is the trick. If "refund policy" and "return within 30 days" are about the same thing, their arrows should aim the same way, even if one arrow is longer.

PICTURE. Two arrows leaving the same point. The angle between them, , is the whole game — small angle means "same topic".

  • ::: the query arrow — the question, turned into numbers.
  • ::: a document arrow — one candidate chunk of text.
  • ::: the angle between them — small angle = similar meaning.

Step 2 — The dot product measures overlap, but it cheats

WHAT. There is one arithmetic operation that already knows about the angle between two arrows: the dot product. For arrows and it is:

  • ::: multiply the two rightward amounts.
  • ::: multiply the two upward amounts, then add.

WHY this tool and not another? Because of a beautiful fact (proved with a right triangle — see Cosine Similarity & Nearest Neighbour Search):

The bars mean length of the arrow (Pythagoras: ). And — the cosine — is just a dial that reads when the arrows point the same way (), when they are perpendicular (), and when opposite ().

The cheat: the dot product multiplies by the two lengths. A long document makes a long arrow, so it scores high just for being long — not for being relevant. That is exactly the wrong thing to reward.

PICTURE. Same short query arrow, two document arrows at the same angle but different lengths. The longer one gets a bigger dot product — unfairly.


Step 3 — Divide the length out → cosine similarity

WHAT. We want the angle only. So take the dot product and cancel the two lengths by dividing by them:

  • numerator ::: the raw overlap from Step 2.
  • denominator ::: the two lengths we want gone.

WHY. Rearrange Step 2: since , dividing both sides by the lengths leaves pure — length has vanished. This number is called the cosine similarity, and it always lands in .

PICTURE. The unit circle. Every arrow is shrunk to length . Put the query along the horizontal axis; then is exactly the projection of the document's unit arrow onto the query's direction — i.e. how much of lies along . (It looks horizontal here only because we lined the query up with the horizontal axis; the real quantity is "shadow onto the other arrow's direction", not onto any fixed axis.)


Step 4 — Rank all chunks, keep the top few

WHAT. Score the query against every stored chunk , sort the scores, and keep the best (say ). This is nearest-neighbour search.

WHY. We do not know in advance which chunk holds the answer, so we test them all and trust the ranking. Keeping only the top throws away the obviously-irrelevant ones so the model isn't drowned in noise.

PICTURE. One query arrow, many document arrows fanned around it. The three closest in angle are highlighted; the far-off ones are greyed out.


Step 5 — Turn scores into probabilities with softmax

WHAT. The scores are just numbers on a line (some negative). To reason about "how likely is chunk the right one", we convert scores into probabilities: positive numbers that add up to . The tool is the softmax:

  • ::: the similarity score of chunk .
  • ::: raise to that score — always positive, and bigger scores blow up faster.
  • ::: add all of them so the whole set sums to exactly .

WHY exponentiate and not just divide by the sum? Two reasons: raw scores can be negative (you can't have a negative probability), and sharpens the winner — a small score lead becomes a big probability lead, so the top chunk dominates. That is precisely why "top- is a fine approximation": the also-rans carry almost no probability.

PICTURE. Bars: flat similarity scores on the left, then the same scores after and normalising — one bar towers over the rest.


Step 6 — Sum over passages: the RAG equation

WHAT. We still don't know the correct passage, so we treat the passage as a hidden (latent) choice and average over all choices, weighting each by how likely it was picked. This is the law of total probability:

  • ::: the query (question).
  • ::: the answer we want.
  • ::: one retrieved passage — the hidden variable.
  • ::: chance the retriever hands us (the softmax from Step 5).
  • ::: chance the LLM writes answer after reading and .

WHY a sum? Because "the right passage" is uncertain. Instead of betting on one, we spread across all of them, each vote weighted by its retrieval probability. In practice we sum only over the top- (Step 4) — using the renormalised weights from Step 5 — because everything else has near-zero weight.

PICTURE. A tree: query at the root, branching to candidate passages (branch thickness = ), each leading to an answer, all merging into the final .


Step 7 — Edge & degenerate cases (never get surprised)

WHAT / WHY / PICTURE, all the corners the formula must survive:

The panels show, left to right: a zero-length arrow (no direction), an opposite arrow (, still tiny nonzero weight), and a fan of equal-angle arrows (flat softmax → "not found").


The one-picture summary

One diagram compresses the whole chain: words → arrows → cosine → top- (renormalised) → softmax weights → weighted sum = the RAG equation. Trace the arrows once and you have re-derived retrieval end to end.

Recall Feynman retelling — the whole walkthrough in plain words

You have a question and a big pile of notes. A machine can't read, so it turns every note and your question into an arrow made of numbers — arrows about the same topic point the same way. To measure "same way", it tries multiplying the arrows (the dot product), but that secretly rewards long notes just for being long. So it divides that length back out, leaving a pure "how aligned are we" score between and — the cosine, which we name . It scores every note this way, keeps the best three, and then softens those scores into confidences that add up to one (softmax, renormalised over just the kept notes), where the best note grabs almost all the confidence. Finally it writes the answer by averaging over those top notes, each weighted by its confidence — that weighted average is the famous RAG equation. And it stays honest at the edges: an empty note has no direction (skip it), an opposite note scores lowest (drop it with a cutoff), and if nothing looks good it should just say "I couldn't find it" instead of making something up.

Recall Quick self-check

Why does cosine beat the raw dot product? ::: It divides out both arrow lengths, so ranking depends on direction (topic), not document length — unless arrows are pre-normalised, when the two are equal. What does mean? ::: It is just a name for , the cosine of the angle between the query and document arrows. Why exponentiate scores in softmax? ::: To make every score positive and force them to sum to 1, while sharpening the lead so the top chunk dominates — justifying the top- shortcut. After truncating to top-, what must you do to the probabilities? ::: Renormalise (divide by the sum of the kept ones) so they sum back to 1 over just those passages. What is in ? ::: A latent (hidden) retrieved passage; we sum because we don't know which single passage is correct. What breaks cosine, and how to fix it? ::: A zero vector (empty chunk) → divide-by-zero; filter empty chunks before indexing.