- Runtime Execution: Triggered once document embeddings are stored in the database.
- Acronym Breakdown:
- Retrieval (R): Queries the database to fetch context matching the user’s question.
- Augmentation (A): Merges the question and retrieved context into a single prompt template.
- Generation (G): Sends the compiled prompt to the LLM to synthesize the final answer.
- 1. Step 1: Retrieval
- 2. Step 2: Augmentation
- 3. Step 3: Generation
- 4. End-to-End Code: Combined Ingestion & RAG Flow
- 5. Types of RAG Architectures
- 6. Practice Exercises
1. Step 1: Retrieval
- Under the Hood:
- Query Embedding: The user’s text query is converted into a numerical vector using the exact same embedding model configured during the Ingestion stage.
- Similarity Matching: The database calculates a numerical similarity score between the query vector and each stored chunk vector using a distance metric (like Cosine Similarity).
- Top-K Filtering: The database sorts all chunks by their similarity scores in descending order and extracts the top
khighest-ranked chunks.
- Top-K Parameter (
k): A configuration parameter specifying the number of chunks to retrieve.- Common Choice:
k=3tok=5is preferred to balance context completeness and token efficiency. - Evaluation & Selection: Determined empirically by running evaluations over different values:
- Low
k(e.g. 1-2): Reduces token costs and latency, but risks low Context Recall (missing critical facts). - High
k(e.g. 10+): Maximizes recall, but introduces noise, increases API costs, and slows generation times. - Selection: The final
kis selected where Context Recall stabilizes without inflating token overhead.
- Low
- Common Choice:
2. Step 2: Augmentation
- Goal: Combines the user’s question and retrieved text chunks into a unified prompt.
- Mechanism: Injects context directly into structured system/human templates, preventing LLM hallucinations.
3. Step 3: Generation
- Goal: Synthesizes a factual response grounded in the provided context.
- Pipeline Mechanism: Chains the retriever context mapping, prompt template, LLM, and string parser using LCEL.
4. End-to-End Code: Combined Ingestion & RAG Flow
Here is the complete, self-contained script combining Phase 1 (Ingestion) and Phase 2 (Retrieval, Augmentation, and Generation) into a single execution flow:5. Types of RAG Architectures
As pipelines scale, they transition through three architectural paradigms:- Naive RAG: A standard linear pipeline (Ingest ➔ Embed ➔ Retrieve ➔ Generate). High risk of retrieving noise or hallucinating.
- Advanced RAG: Adds pre-retrieval and post-retrieval optimizations like Hybrid Search (vector + keyword search), Reranking (using cross-encoders to sort relevance), and Metadata Filtering (restricting database search scope).
- Agentic RAG: Uses LLMs as autonomous agents that determine when to query databases, rewrite queries, and evaluate retrieval relevance.
6. Practice Exercises
Practice 1: Multi-Document RAG Chain
Expand the RAG pipeline to query over multiple policy files. Set up the retriever to return the top 2 matches (k=2), format them using the format_docs helper, and invoke the chain.
Solution
Solution
Summary
- RAG Steps: RAG runtime flow consists of Retrieval (fetching documents), Augmentation (injecting documents into a template), and Generation (synthesizing LLM response).
- LCEL RAG Pipeline: Chains retriever data mapping, prompt augmentation, LLM inference, and parsing using the
|operator. - RAG Architectures: Scales from Naive RAG (simple linear lookup) to Advanced RAG (hybrid search, metadata filters, rerankers) and Agentic RAG (autonomous decision loops).