4.4.11 · D1Alignment, Prompting & RAG

Foundations — Retrieval-Augmented Generation (RAG) architecture

1,912 words9 min readBack to topic

This page assumes nothing. If the parent note RAG architecture used a symbol, we build it here from the ground up, in an order where each idea leans only on the ones before it.


0. What is a vector? (the atom of everything here)

Before any RAG formula, you must be comfortable with one picture: a vector is an arrow with a tail at the origin and a tip somewhere in space.

Figure — Retrieval-Augmented Generation (RAG) architecture
  • The picture: says walk 3 right, then 2 up, and draw an arrow from where you started to where you ended.
  • Why RAG needs it: RAG converts every chunk of text and every question into one of these arrows (this conversion is called embedding). Once text is an arrow, "is this document about the same thing?" becomes a geometry question we can actually compute.

1. Length of a vector: the norm

The parent note writes and calls it "magnitude". Let us earn that symbol.

Figure — Retrieval-Augmented Generation (RAG) architecture
  • Why the square root of squares? Look at the figure: the arrow is the hypotenuse of a right triangle whose legs are and . Pythagoras says (leg) + (leg) = (hypotenuse), so the hypotenuse is . The norm is the vector's length via the same triangle you met in school.
  • Example: .
  • Why RAG cares: a longer document tends to produce a longer arrow. The parent note's whole argument for cosine over dot product is "cancel out this length so it can't cheat". You cannot understand that argument without knowing what length means.

2. The dot product

This is the single most important operation in the whole topic. The parent note uses everywhere.

Figure — Retrieval-Augmented Generation (RAG) architecture
  • Why two formulas matter: the left one is what a computer calculates (fast: multiply and add). The right one is what it means (length × length × alignment). RAG needs both: the fast side to run, the meaningful side to justify ranking by it.
  • The picture: the dot product is big when the arrows point the same way, zero when they are perpendicular (90° apart, no shared direction), and negative when they point opposite ways. That "same-way-ness" is precisely "same topic".
  • Why not just use this raw number? Because it multiplies in — the lengths. A long document (big ) scores high just for being long. That is the flaw cosine will fix next.

3. The angle and cosine

The parent's headline formula is . Now it is fully earned.


4. Sum and the retriever/generator split

The parent note's definition uses . Two symbols to unpack.

  • Picture: a stack of index cards ; you flip through each card , score it, and total the scores.
  • Why RAG needs it: RAG never knows which single passage holds the answer, so it weighs all of them and sums their contributions — that is what says. The two pieces:
    • — the retriever: "how likely is passage the right one for query ?"
    • — the generator: "given the query and that passage, how likely is answer ?"
  • reads "probability of the left thing given the right thing is known". The vertical bar means "given / assuming".

5. Turning scores into probabilities: softmax and

The parent's Example 2 uses . Let us define and why it appears.

Figure — Retrieval-Augmented Generation (RAG) architecture

Let us verify the parent's Example-2 arithmetic in the checklist below.


6. How the foundations feed the topic

Vector = arrow in space

Norm = arrow length

Dot product = q1 d1 + q2 d2

Cosine similarity = direction only

Normalise to length 1

Retriever picks top-k chunks

Sum over all passages

p of y given x

Exponential e to the x

Softmax = scores to probabilities

RAG pipeline

Read top to bottom: the vector is the atom; norm and dot product are built from it; cosine is dot product with norms divided out; softmax (built on ) and the sum turn scores into the probability the parent note models. All roads meet at the RAG pipeline.


Equipment checklist

Cover the right side and answer aloud. If any stumps you, re-read that section before tackling the parent note.

A vector — what is its picture and its length?
An arrow: 3 right, 4 up; length .
What does mean and why the square root?
The straight-line length of the arrow; the arrow is the hypotenuse of a right triangle, so Pythagoras gives .
Compute the dot product two ways.
Coordinates: . Meaning: . Same answer.
Why does cosine remove document length?
Dividing by cancels both lengths, leaving only — direction (topic), not size (length).
for two arrows 90° apart — what value, what does it mean?
; the arrows share no direction, so the texts are unrelated.
What does instruct you to do?
For every passage in the pile , compute the term and add them all up.
Read in plain English.
The probability of answer , given that query and passage are known.
Why must softmax exponentiate before normalising?
is always positive (even for negative scores), so the results can be valid probabilities; dividing by the total then makes them sum to 1.
For scores , what is roughly?
.