Phase 05 · GenAI & LLM ApplicationsLesson 1 of 3
RAG (Retrieval-Augmented Generation) Explained
Ground an LLM's answers in your own documents.
The problem
Ask a general chatbot about your company's leave policy and it will either admit it doesn't know or, worse, invent something plausible. LLMs only know what was in their training data. Retrieval-Augmented Generation (RAG) fixes this by fetching the relevant parts of your documents and handing them to the model along with the question.
The concept
The idea in one sentence#
Retrieve the most relevant information, augment the prompt with it, then let the model generate an answer grounded in that information.
Part 1: Indexing (done once, ahead of time)#
- Load your documents: PDFs, web pages, Notion pages, support tickets.
- Chunk them into smaller passages (for example, a few hundred words each).
- Embed each chunk: an embedding model turns text into a list of numbers (a vector) that captures its meaning.
- Store the vectors in a vector database such as Chroma, Pinecone, pgvector or Qdrant.
Part 2: Answering (every time a user asks)#
- Embed the question with the same embedding model.
- Search the vector database for the chunks whose vectors are closest in meaning.
- Build the prompt: the question plus the top few chunks as context.
- Generate: the LLM writes an answer using that context, ideally citing its sources.
"How many leave days do new joiners get?"
│ embed
▼
vector search ──► 3 most similar chunks from HR-policy.pdf
│
▼
prompt = instructions + chunks + question ──► LLM ──► grounded answer + source
Why embeddings work#
Embeddings place text with similar meaning close together, even when the words differ. "Annual leave for freshers" and "vacation days for new employees" end up near each other, so the search finds the right chunk even without matching keywords.
A minimal prompt template#
Answer the question using ONLY the context below.
If the answer is not in the context, say "I don't know".
Context:
{retrieved_chunks}
Question: {user_question}
RAG vs fine-tuning#
| RAG | Fine-tuning | |
|---|---|---|
| Teaches the model | New knowledge (facts, documents) | New behaviour (style, format, task skills) |
| Updating information | Just re-index the documents | Retrain the model |
| Can cite sources | Yes | No |
| Cost to start | Low | Higher |
For "answer questions about our documents", RAG is almost always the right first choice.
Try it
Your task · about 10 minutes
Pick five pages of notes you own. Write down how you would chunk them and what questions a RAG bot over them should answer.
Use the interactive visual above to explore it step by stepRecap & next
Key takeaways
- LLMs don't know your private or latest data. RAG fetches relevant information and adds it to the prompt.
- Indexing: split documents into chunks → turn each into an embedding → store in a vector database.
- Answering: embed the question → retrieve the most similar chunks → the LLM writes an answer using them.
- RAG reduces hallucinations and lets answers cite sources, without retraining the model.
Quick check
2 questions · instant feedback
Go deeper
The full course on GenAI & LLMs
Udemy course
Claude Ecosystem - AI, Code, Cowork, M365 & Certification
View course (opens Udemy in a new tab)
Want to train your team on this? Talk about corporate training
Finished this lesson?
Mark it complete to track your progress and keep your streak going.