AI Foundations · Module 04
Modern RAG Architecture
The three phases of a RAG system (indexing, retrieval and generation), what each component contributes, and how RAG designs grew from naive to agentic. The final assignment is the source's own: it uses AWS (S3, Bedrock) and a policy PDF that this course does not supply.
Học xong bạn làm được
- Describe the indexing phase: loading, chunking, embedding, storing
- Explain similarity search, hybrid search and re-ranking
- Prepare context and write a grounded prompt with citations
- Say what breaks when one component is removed
- Pick a RAG architecture for a use case
- Build a complete RAG pipeline in the final assignment
Bài học
- Modern RAG Architecture - Phase 1: Indexing (loading and chunking)
A standard RAG system runs in three phases - Indexing, Retrieval and Generation. Indexing works like ETL - it loads documents of many formats into plain text with metadata, which enables pre-filtering, and splits them into chunks, by fixed size or recursively, with overlap between neighbouring chunks.
- Phase 1: Indexing (advanced strategies, embedding, vector store)
Small chunks are good for search and large chunks are good for the LLM. Parent-child (small-to-big) indexing searches small child chunks but returns their large parent, and summary indexing searches an LLM summary but returns the original. Indexing ends by embedding the chunks into dense vectors and storing them with ids and metadata in a vector store.
- Phase 2: Retrieval (query processing and similarity search)
Retrieval decides whether a RAG system succeeds - if it finds wrong or missing information, the LLM cannot answer. The question is embedded into a query vector; Multi-Query and HyDE make short or vague questions searchable; similarity search uses cosine similarity or Euclidean distance with ANN algorithms such as HNSW, and is weak on exact keywords.
- Phase 2: Retrieval (hybrid search and re-ranking)
Hybrid search runs dense vector search and sparse keyword search (BM25, TF-IDF) in parallel and merges them with Reciprocal Rank Fusion. Re-ranking then re-scores the candidates with a cross-encoder, which reads question and document together - retrieve many fast with a bi-encoder, re-rank few accurately.
- Phase 3: Generation (context preparation)
Once retrieval returns documents, the LLM synthesises an answer, but the context needs preparing first. Context stuffing simply joins the top-K documents, at the cost of tokens, latency and noise. Context reordering (U-shape) works around "lost in the middle", and context compression summarises the context before the main LLM sees it.
- Phase 3: Generation (prompt engineering, generation and attribution)
Good context with a bad prompt can still give a wrong answer. Zero-shot prompting uses a fixed RAG template, few-shot learning adds one or two Context-Question-Answer examples, and chain-of-thought asks the model to reason step by step. Finally the LLM generates the answer, ideally with citations of the source documents.
- Extension: Component Role Analysis
Removing one component at a time shows what each does. Without the embedding model the system falls back to lexical retrieval and misses synonyms; without the vector store and ANN index it becomes an unscalable prototype; without the LLM it becomes a semantic search system that returns excerpts instead of answers.
- The Evolution of RAG Architectures
RAG moved through four generations - naive (embed, retrieve, generate), advanced (query transformation, hybrid search, re-ranking), modular (a router over swappable retrievers) and agentic (an agent that decides, executes, evaluates and loops). Choose the simplest one that meets your quality bar.
- Basic AI Fundamentals - Final Assignment
RAG Agent for FPT Policy Document Q&A. Your task is to build a RAG-based Q&A system that allows employees to ask natural language questions and receive accurate answers based on the policy documents.