38 RAG
Retrieval playground: finding the right page
A frozen model has never read your documents. RAG hands it the right passage at the right moment: embed the question, score it against every document by cosine similarity, keep the top few, and paste them into a grounded prompt. Below is the book's exact five-document Cascara Coffee corpus and its hash embedder, reimplemented in the browser — ask it anything.
Cascara Coffee Roasters · 5 documents
Embedder:
1 · Embed the question
Each meaningful word is hashed into one of 4,096 slots (stopwords dropped, plurals stemmed). The query vector is those filled slots.
2 · Cosine score against every document
3 · Assemble the grounded prompt (top-k = 2)
What to notice
- The default question scores returns 0.445, everything else far behind — these are the exact numbers from the book, because this is the same hash embedder.
- Now switch to "Who started the company?" Under the hash embedder it limps in at 0.149 — it matches words, and the document says "founded", not "started"; "roasters", not "company".
- Flip to the "learned" embedder and the same question soars to ~0.66. That is the entire reason embedding models exist: they match meaning, not spelling. (See the honest footnote on how this demo fakes "learned".)
- Retrieval is just
index @ query— one matrix–vector multiply — followed by a sort and a paste. The learned magic lives entirely in one swapped function.
This is the RAG pipeline from Chapter 41 of Large Language Models from the Ground Up. ← Back to all labs