Tutorials · Chapter D (4/4) · ~11 min
Build a mini RAG
Play → read → next
Retrieve a snippet, then answer from it — a RAG path you can walk through out loud.
Code Lab
Build a mini RAG
Retrieve a doc chunk, then print it as the grounded answer.
Recap
What you just did
You built the builder’s favorite pattern: search your stuff → prompt with evidence → generate. Even a toy keyword or embedding search counts. The showable win is the cited snippet sitting next to the answer, not a floating claim.
Teach
How it works
Toy RAG in a few moves:
docs = {
"hours": "Cafe open 8am–6pm weekdays.",
"wifi": "Wi‑Fi password is painted by the register.",
}
q = "when do you open?"
# retrieve: pick the best matching doc
hit = docs["hours"]
prompt = f"Use only this note:\n{hit}\n\nQ: {q}\nA:"
# then send `prompt` to the LLM
- Index your notes/chunks
- Retrieve top matches for the question
- Augment the prompt with those chunks
- Generate an answer that should stick to them
Mental model: open-book quiz, not closed-book celebrity trivia.
Use it
When you'd use this
- FAQ bots over your handbook
- Support answers that must quote policy text
- Personal study buddy over your lecture notes
Watch out
Watch out
If retrieval misses, the model invents. Always surface “I used this chunk” (or “no chunk found”). Don’t stuff the whole company drive into one prompt — chunk and rank.
Try next
Try this next
Ask a question your docs don’t cover. Prefer an honest “not in notes” over a confident guess.