← govind p lal
AI and ML Bootcamp · Lecture 10 · lecture notes

RAG from the Ground Up - Part 1

In short — LLMs are frozen after training, so they miss new, private and personal data. RAG (Retrieval Augmented Generation) fixes this by fetching relevant documents and adding them to the prompt. This lecture builds the basic pipeline: chunking, embedding and storing data.">ingestion (parse, chunk, embed, store), retrieval (semantic, keyword, hybrid, contextual, re-ranking) and generation.

Why RAG: LLMs are frozen in time

builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026, Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026

A trained LLM is just static weights on a server. It is frozen in time.

Example

A legal-tech company hires lawyers to build precise case documents. The LLM never saw them, so RAG feeds the right documents into the prompt.

Basic idea: new info goes into the prompt. RAG = retrieve data, then generate with it. -> Retrieval Augmented Generation

similar docsUser questionRetrieve documentsPrompt + docsLLM answer
Basic RAG flow.

When to use RAG, prompt or fine-tuning

Ingestion 1: Document loading and parsing

Ingestion - bringing your data into the system. Pipeline: parse -> chunk -> embed -> store.

!

Clean data is the first and most important step of any RAG system.

Ingestion 2: Chunking

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026, Lecture 4: Week 3 - AI and ML | Transformers- Part 1 | 31/01/2026
Problem

One vector per 1000-page book compresses everything into one point. A question about chapter 2 retrieves the whole book.

Solution

Chunking - split data into parts and embed each part. Retrieve only the 10 chunks of chapter 1 when asked about chapter 1.

TypeHow it splits
FixedFixed number of words, lines or paragraphs per chunk
RecursiveFollows natural boundaries: paragraph, line, sentence, character
LLM-basedLLM decides where to cut (costlier, usually better)
ASTFor code, split by abstract syntax tree parts
Structure / paragraph / parent-childOther strategies for specific use cases

Why fixed is bad: it cuts info mid-way. One chunk has half the information, the LLM sees only that half and hallucinates the rest.

Example

Paper-cutting: cut three papers at random into four pieces each and mix them. Finding a character name split across two pieces is hard. Same for chunks.

Ingestion 3: Embeddings and vector storage

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026, Lecture 4: Week 3 - AI and ML | Transformers- Part 1 | 31/01/2026

Each chunk -> embedding -> stored in a vector database. Similar meaning sits close together, so search is fast vector math.

Parse docsChunkEmbedVector DB
Ingestion pipeline.
Example

Query: what was Acme's total revenue in Q3 2024? Semantic-only ranked a Q2 2024 report first (distance about 0.2). Hybrid with BM25 matched the exact date and quarter and retrieved correctly. Naive RAG is bad here.

Contextual retrieval

Idea from Anthropic. Chunk alone may say only 'revenue grew by 3%'.

Re-ranking and compression

builds on Lecture 5: Week 4 - AI&ML | Transformers: Part 2 | 07/02/2026

First pass is fast and rough (semantic/hybrid search, returns many chunks). Second pass is slow and precise (re-rank, keep top 3-5). -> Re-ranking

Example

Hiring: 10,000 resumes, keywords give 200. A human reviews them and prefers 3 years in a startup over 7 years in management. Order changes after the second look.

Full pipeline and tips

builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026
Ingestionparse, chunk, embed, storeRetrievalquery transform, search,…GenerationLLM answers with context
Three stages of a basic RAG pipeline.

Glossary

RAG
Retrieval Augmented Generation: retrieve relevant data and pass it to the LLM with the prompt.
Ingestion
Loading, parsing, chunking, embedding and storing data.
Chunking
Splitting documents into smaller parts before embedding.
Vector database
Store of embeddings that returns the nearest matches to a query.
BM25
Keyword-based sparse search for exact terms.
Hybrid search
Combining semantic and keyword search.
Cross-encoder
Transformer that scores a query and document together for re-ranking.
Re-ranking
Second precise pass that reorders retrieved chunks.