Overview

3 Grounding Outputs with RAG

Retrieval-Augmented Generation (RAG) is presented as a practical way to make AI systems more reliable by grounding language model outputs in current, authoritative information. Instead of depending only on an LLM’s pretrained knowledge, a RAG system retrieves relevant external context at query time and passes it to the model before generating an answer. This helps conversational systems handle dynamic domains such as e-commerce, telecom, healthcare, finance, and legal support, where product details, inventory, policies, regulations, or recommendations may change frequently. The chapter emphasizes that RAG can reduce hallucinations, improve specificity, and make answers more traceable, while also noting that it introduces costs, latency, infrastructure complexity, and its own failure modes.

The chapter explains the core RAG architecture through the interaction of retrievers, generators, knowledge sources, and orchestration tools. The retriever searches indexed data sources such as documents, product catalogs, APIs, databases, FAQs, manuals, or policy repositories; the generator then uses the retrieved passages and the user’s query to produce a fluent response. A major focus is reliable indexing: loading data, splitting it into useful chunks, embedding those chunks into vectors, and storing them in a searchable vector index such as FAISS. The chapter also shows how tools like LangChain and Haystack can simplify common RAG tasks, including document loading, text splitting, embedding, retrieval chains, metadata extraction, and question-answering pipelines for applications like an e-commerce chatbot.

The chapter also covers how to evaluate, optimize, and extend RAG systems for production reliability. It discusses evaluator LLMs, curated test datasets, retrieval precision, answer relevance, factual accuracy, human evaluation, and RAGAS-style metrics such as faithfulness, context precision, context recall, and answer correctness. To reduce hallucinations, it recommends filtering retrieved passages by semantic relevance, using metadata-based filtering, tuning chunk sizes and embedding models, and keeping context concise so key facts are not buried. More advanced techniques include fine-tuned embeddings, hybrid retrieval, query rephrasing, query routing, GraphRAG for relationship-aware multi-document reasoning, and agentic RAG for systems that plan multiple retrieval steps before answering. Overall, the chapter frames RAG as a powerful but engineering-intensive pattern that requires careful data preparation, retrieval design, evaluation, and continuous refinement.

Basic prompting through interacting with LLM directly. [1]
Asking an LLM (Claude) about a specific question related to inventory systems
The RAG System architecture with the Retrievers, Knowledge Source, and the Generator.
The indexing process consists of four steps: (1) loading the data; (2) splitting, or segmenting, the data; (3) embedding, or converting the data into vectors; and (4) storing the embeddings in a structured repository.
The query, retrieval, generation phases in a Retrieval-Augmented Generation (RAG) system
The different stages in RAG for answering a user’s query for the Clara chatbot
The role of evaluator LLMs in the RAG System
The limits of the context window across different LLMs
The steps needed to build a RAG system in LangChain

Summary

  • Retrieval Augmented Generation (RAG) combines large language models with real-time information retrieval, addressing limitations of standalone LLMs such as outdated knowledge and hallucinations.
  • RAG systems consist of two main components: retrievers, which fetch relevant information from external sources, and generators, which use this information to produce responses.
  • The RAG process involves query processing, information retrieval, context integration, and response generation, enabling more accurate and up-to-date answers.
  • RAG significantly reduces hallucinations by grounding responses in retrieved information, enhancing accuracy, consistency, and adaptability of AI systems.
  • Effective data indexing is crucial for RAG, involving steps like loading, splitting, embedding, and storing data in searchable formats using tools like FAISS (an in-memory library) or managed vector databases like Pinecone and Weaviate.
  • Techniques like semantic similarity filtering and metadata-based categorization optimize retrieval relevance and reduce hallucination risks.
  • Evaluation of RAG systems requires multi-faceted approaches, including metrics like retrieval precision, answer relevance, factual accuracy, human evaluation scores, and RAGAS evaluation.
  • Advanced RAG techniques include fine-tuning embeddings for domain-specific relevance, implementing hybrid retrieval methods, and enhancing queries through rephrasing and augmentation.
  • Tools like LangChain simplify the implementation of RAG systems by providing modular components for retrieval, prompt management, and response generation.
  • Practical challenges in RAG implementation include balancing chunk sizes, choosing appropriate embedding models, and maintaining data quality and freshness.
  • Continuous evaluation and refinement are essential for RAG systems, involving the use of diverse test datasets, evaluator LLMs, and iterative improvement cycles.
  • The future of RAG involves further advancements in retrieval techniques, more sophisticated integration with LLMs, and broader applications across various domains.

FAQ

What is Retrieval-Augmented Generation (RAG)?

Retrieval-Augmented Generation (RAG) is an approach that combines a large language model with an information retrieval system. Instead of relying only on the LLM’s pretrained knowledge, a RAG system first retrieves relevant information from external sources, such as documents, databases, product catalogs, APIs, or web pages, and then uses that retrieved context to generate a more accurate and grounded response.

Why is RAG useful compared to using a stand-alone LLM?

Stand-alone LLMs can produce fluent responses, but they often struggle with current, domain-specific, or frequently changing information. RAG helps overcome this limitation by retrieving up-to-date and authoritative information at query time. This is especially useful for areas like e-commerce, healthcare, finance, and legal support, where stale or incorrect answers can reduce trust or create risk.

How does RAG reduce hallucinations?

RAG reduces hallucinations by grounding the model’s answer in retrieved evidence rather than relying only on the model’s internal memory. For example, instead of guessing a return policy, a RAG system can retrieve the exact policy clause and use it to generate a precise answer. However, RAG does not eliminate hallucinations entirely; retrieval can miss relevant documents, sources can be incomplete, and the generator may still misinterpret the context.

What are the main components of a RAG architecture?

A typical RAG architecture includes two core components: the retriever and the generator. The retriever searches external knowledge sources for information relevant to the user’s query. The generator, usually an LLM, receives both the original query and the retrieved context, then produces a fluent response grounded in that information. In more advanced systems, orchestration tools such as LangChain or Haystack help connect these components into a pipeline.

What is the typical flow of a RAG system?

A RAG system usually follows this flow: first, the user submits a query. Next, the query is embedded or otherwise processed so it can be matched against an indexed knowledge base. The retriever finds the most relevant passages or documents. Then the retrieved context is passed, along with the original query, to an LLM. Finally, the LLM generates an answer that incorporates the retrieved information.

What role does indexing play in a RAG system?

Indexing is the backbone of effective retrieval. It organizes knowledge sources into a searchable structure so the retriever can quickly find relevant information. The chapter describes four main indexing steps: loading data from sources such as files, JSON, URLs, or databases; splitting long documents into manageable chunks; embedding those chunks into vector representations; and storing the embeddings in a searchable repository such as FAISS or a managed vector database.

What are embeddings, and why are they important for RAG?

Embeddings are numerical representations of text that capture semantic meaning. In RAG, both documents and user queries can be converted into embeddings so the system can compare them by similarity. For example, embeddings help the system recognize that “dog” and “puppy” are semantically closer than “dog” and “cat.” This enables a chatbot to retrieve relevant passages even when the user’s wording differs from the wording in the knowledge base.

How can LangChain help build RAG systems?

LangChain is an open-source Python library that simplifies building LLM applications. In RAG systems, it can help with document loading, text splitting, embeddings, vector stores, retrievers, prompt templates, memory, and retrieval QA chains. The chapter first uses LangChain as a helper toolkit for loading and splitting data, then later demonstrates how to use its higher-level abstractions to build an end-to-end e-commerce RAG chatbot.

How should RAG systems be evaluated?

RAG systems should be evaluated across both retrieval and generation quality. Practical metrics include retrieval precision, answer relevance, factual accuracy, and human evaluation scores. The chapter also discusses RAGAS, which provides RAG-specific metrics such as faithfulness, context relevancy, context precision, answer relevancy, context recall, answer correctness, and answer similarity. Evaluator LLMs can also be used to score generated responses for factual correctness, contextual appropriateness, and hallucination indicators.

What techniques can improve RAG performance and reliability?

Several techniques can improve RAG systems. These include semantic relevance filtering, metadata-based filtering, careful chunk-size tuning, choosing better embedding models, hybrid retrieval that combines keyword and vector search, query rephrasing, query augmentation, query routing, and fine-tuning embeddings for domain-specific relevance. More advanced approaches include GraphRAG, which uses knowledge graphs to connect information across documents, and agentic RAG, where an AI agent controls multiple retrieval steps before synthesizing an answer.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Reliable AI Systems ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Reliable AI Systems ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Reliable AI Systems ebook for free