/ module 05

Retrieval-Augmented Generation

LLMs only know what they were trained on. RAG lets a model answer about your private or fresh documents by retrieving relevant chunks first and stuffing them into the prompt.

concept

The pipeline in 4 steps

1. Chunk documents into passages. 2. Embed each chunk into a vector. 3. Retrieve the top-k by similarity to the query. 4. Generate with the chunks prepended to the prompt.

Production systems use neural embeddings + a vector database. The lab below uses classic TF-IDF cosine, and the math feels identical.

embedding
A vector that captures meaning. Similar text → close vectors.
top-k
How many retrieved chunks to send to the LLM.
grounding
Forcing answers to come from retrieved context, reducing hallucinations.
Live lab · RAG retriever
doc #612.7%
Plants release oxygen during photosynthesis, which animals breathe to survive.
doc #10.0%
Photosynthesis converts sunlight, water and carbon dioxide into glucose and oxygen inside chloroplasts.
doc #20.0%
The mitochondria is the powerhouse of the cell, producing ATP through respiration.
doc #30.0%
Newton's second law states that force equals mass times acceleration (F = ma).
doc #40.0%
DNA is a double helix made of nucleotides: adenine, thymine, cytosine and guanine.
doc #50.0%
The speed of light in a vacuum is approximately 299,792 kilometers per second.
doc #70.0%
Black holes have an event horizon beyond which not even light can escape.
Generated prompt sent to the LLM
You are a helpful assistant. Use ONLY the context below.

Context:
[1] Plants release oxygen during photosynthesis, which animals breathe to survive.
[2] Photosynthesis converts sunlight, water and carbon dioxide into glucose and oxygen inside chloroplasts.
[3] The mitochondria is the powerhouse of the cell, producing ATP through respiration.

Question: how do plants make food?

Answer: