1. End-to-End RAG Synthesis Pipeline
We compose the retriever, augmented prompt template, and Chat Model using LangChain Expression Language (LCEL):2. Types of RAG Architectures
As RAG applications scale, they transition through three architectural paradigms:2.1 Naive RAG
The standard pipeline: Ingest Embed Retrieve Generate.- Limitations: Low retrieval precision (retrieving noise), poor recall (missing details), and model hallucinations if the context is too long.
2.2 Advanced RAG
Introduces optimizations before and after retrieval to improve answer relevance:- Hybrid Search: Combines keyword search (BM25) with vector search (semantic) to find both exact term matches (e.g. product IDs) and conceptual synonyms.
- Reranking: Uses a secondary Cross-Encoder model to calculate exact relevance scores for the top-N retrieved documents, sorting the most important context to the top before sending it to the LLM.
- Metadata Filtering: Restricts searches to specific tags (e.g.
{"department": "HR"}or{"year": 2026}), preventing the retriever from pulling irrelevant documents.
2.3 Agentic RAG
Uses LLMs as agents that decide when to query databases, reformulate queries, and self-correct answers if the retrieved data is insufficient.3. RAG Evaluation Metrics
To measure RAG performance (using frameworks like Ragas or TruLens), systems are evaluated across two halves of the pipeline:3.1 Retrieval Metrics
- Context Recall: Measures if the retriever found all the necessary facts needed to answer the question.
- Context Precision: Measures if the retrieved chunks are highly relevant, or if they contain too much irrelevant noise.
3.2 Generation Metrics
- Faithfulness (Groundedness): Measures if the LLM’s response is based only on the retrieved context. A high score means no hallucinations.
- Answer Relevance: Measures if the generated response directly answers the user’s question, rather than talking about unrelated topics.
4. Practice Exercises
Practice 1: Identifying RAG Failure Modes
Identify which evaluation metric is failing in the following scenarios:- The LLM answers a query by making up facts that were not in the retrieved documents.
- The user asks about sick leave policies, but the database retriever returns documents about office lunch hours, leading to a blank answer.
Solution
Solution
- Faithfulness (Groundedness) is failing, because the model is hallucinating facts outside of the provided context.
- Context Recall is failing (and consequently Context Precision is low), because the retriever failed to find the correct sick leave documents.