Understanding RAG with a Simple Example
Most explanations of Retrieval Augmented Generation get too complicated with technical diagrams. Let me show you how it works using basic math and a simple example.
Let’s say we have these documents: “Fry some eggs”, “Scramble eggs”, “Fix a flat tire”
Breaking Down the Text
First we split everything into pieces:
Text0: "Fry some eggs"
Text1: "Scramble eggs"
Text2: "Fix a flat tire"
Converting to Vectors
Next, we turn each text into numbers using a neural network. Think of it like GPS coordinates but for meaning. Here are some example 4D vectors (real ones are much bigger):
Vec0 = [0.85, 0.15, 0.05, 0.12] # "Fry some eggs"
Vec1 = [0.82, 0.18, 0.03, 0.11] # "Scramble eggs"
Vec2 = [-0.25, 0.35, 0.75, 0.15] # "Fix a flat tire"
Making Them Standard Size
We adjust all vectors to have the same length:
Vec0_norm = [0.979, 0.173, 0.058, 0.138]
Vec1_norm = [0.968, 0.212, 0.035, 0.130]
Vec2_norm = [-0.289, 0.405, 0.867, 0.174]
Storing Everything
We put these vectors in a database and remember which text goes with which number.
Finding Similar Content
When someone asks “What’s the best way to prepare eggs?”, we:
- Convert their question to a vector:
[0.975, 0.165, 0.02, 0.145] - Compare it to all stored vectors using dot products
- Pick the most similar ones
For example:
- Question × Vec0_norm = 0.987 (very similar)
- Question × Vec1_norm = 0.982 (very similar)
- Question × Vec2_norm = -0.156 (not similar)
So we grab the egg cooking instructions and feed them to the AI to generate a good answer.