Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Understanding RAG: Preventing Hallucinations in LLMs

Hello! Welcome to the first lesson in our module on building RAG systems.

In previous modules, we've explored how Large Language Models work, from their transformer architecture to the way they are trained. We've also covered the crucial concepts of text embeddings and vector databases, which allow us to represent and search for text based on semantic meaning.

Today, we'll bring these ideas together to tackle one of the most significant challenges with LLMs: hallucination. This lesson introduces Retrieval-Augmented Generation (RAG), a powerful architectural pattern that makes LLMs more factual, reliable, and trustworthy. By the end of this lesson, you will be able to explain the RAG pattern and how it serves as a primary strategy to mitigate LLM hallucinations.

1. The Problem: LLM Hallucinations

Before we can solve a problem, we need to understand it. Large Language Models, despite their impressive capabilities, have a tendency to "hallucinate." This doesn't mean they are having visions; it means they generate text that sounds plausible but is factually incorrect, nonsensical, or not grounded in reality.

To understand why this happens, please read the following brief article. It defines hallucination and explains its root causes.

Reducing LLM Hallucinations: A Developer's Guide

This article from Zep, a company focused on LLM memory, provides a clear and concise explanation of what LLM hallucinations are and why they occur.

Please read the introduction and the section titled 'Why LLMs Hallucinate'. Focus on understanding the core reasons, such as training data limitations and the probabilistic nature of text generation.

As the article explains, LLMs are fundamentally pattern-matching systems. They predict the next most likely word based on the vast amount of text they were trained on. They don't have an internal concept of "truth" or a mechanism to fact-check their own statements. This can lead to several types of errors, as categorized below.

Types of LLM Hallucinations
This diagram categorizes LLM hallucinations. They can conflict with the user's prompt (input-conflicting), with previous parts of the conversation (context-conflicting), or with established world knowledge (fact-conflicting). RAG is primarily designed to combat fact-conflicting hallucinations.

The core issues leading to hallucination are:

  • Stale Knowledge: An LLM's knowledge is frozen at the time of its training. It knows nothing about events, data, or discoveries that have occurred since.
  • Lack of Domain-Specific Knowledge: A general-purpose model won't have deep knowledge of your company's internal documentation, a niche scientific field, or a specific legal case.
  • No Source Verification: A standard LLM provides an answer without citing sources, making it impossible to verify the information's origin or accuracy.

2. The Solution: Retrieval-Augmented Generation (RAG)

So, how can we ground an LLM in reality and provide it with up-to-date, specific information? The answer is Retrieval-Augmented Generation (RAG).

At its core, the idea is simple and intuitive: instead of relying solely on the LLM's internal (and potentially outdated) memory, we first retrieve relevant information from a trusted external knowledge source and then provide that information to the LLM as part of the prompt to generate a factually grounded answer.

The following video from IBM Technology provides an excellent high-level introduction to this concept.

What is Retrieval-Augmented Generation (RAG)?

This video uses a great analogy to explain the core idea behind RAG: instead of answering a question from memory, you first look it up in a reliable source.

Watch the video from the beginning until the 5:41 mark. Pay attention to the two main LLM challenges identified (outdated information and lack of sources) and how the RAG framework directly addresses them.

3. The RAG Pattern in Action

Now that you have the high-level concept, let's look at the mechanics of the RAG pattern. Your background as a software engineer will be helpful here, as you can think of RAG as a system architecture that couples a retrieval system with a generative model.

The diagram below shows the typical flow of a RAG system.

Retrieval Augmented Generation (RAG) Engines Conceptual Flow
This diagram illustrates the RAG pipeline. The process consists of three main stages: 1) **Retrieve**: Find relevant context from a knowledge base (vector database). 2) **Augment**: Combine the original query with the retrieved context into a prompt. 3) **Generate**: Use an LLM to produce an answer based on the augmented prompt.

Let's break down this flow:

Phase 1: Ingestion (Offline Process)
This is the preparatory step where you build your knowledge base.

  1. Load Data: You start with a corpus of documents (e.g., PDFs, web pages, text files) containing the information you want the LLM to use.
  2. Chunk Documents: The documents are broken down into smaller, manageable pieces or "chunks." We will explore strategies for this in the next lesson.
  3. Embed Chunks: Each chunk is passed through an embedding model (as we discussed in Module 4) to create a numerical vector representing its semantic meaning.
  4. Index Embeddings: These embeddings and their corresponding text chunks are loaded into a vector database, which indexes them for efficient similarity search.

Phase 2: Retrieval and Generation (Online/Runtime Process)
This happens every time a user submits a query.

  1. User Query: The system receives a query from the user.
  2. Embed Query: The query is converted into an embedding using the same model from the ingestion phase.
  3. Search: The vector database is searched to find the text chunks whose embeddings are most similar to the query embedding (e.g., using cosine similarity). These are the "retrieved documents" or "context."
  4. Augment Prompt: The original user query is combined with the content of the retrieved documents into a single, comprehensive prompt for the LLM. The prompt essentially instructs the LLM: "Using only the following information, answer this question."
  5. Generate Response: The LLM receives the augmented prompt and generates an answer that is grounded in the provided context.

The following video provides a more hands-on example that walks through this process, showing how RAG helps avoid incorrect answers.

Retrieval Augmented Generation (RAG) Explained in 8 Minutes!

The ritvikmath channel gives a very practical walkthrough of RAG, demonstrating how it solves a real-world problem (a chatbot for a YouTube channel) and detailing the retrieval step.

Watch from the beginning to the 7:14 mark. Notice how the system's response changes when provided with retrieved context. The explanation of the retrieval step (starting around 5:27) will be a good refresher on the concepts of embeddings and cosine similarity from our previous module.

4. How RAG Mitigates Hallucinations

By now, the benefits of RAG in fighting hallucinations should be becoming clear. Let's explicitly connect the RAG pattern to the problems we identified earlier.

Reducing LLM Hallucinations: A Developer's Guide

Let's return to the Zep article, which clearly summarizes the key benefits of using RAG.

Please read the section titled 'Retrieval-Augmented Generation (RAG) and External Data Grounding'. Pay close attention to the 'Key benefits of RAG' and the example prompt structure provided.

To summarize, RAG directly mitigates hallucinations in three key ways:

  1. Provides Factual Grounding: By forcing the LLM to base its answer on provided text, RAG prevents the model from inventing details. The LLM's role shifts from a "knower" to a "reasoner and summarizer" of the retrieved facts.
  2. Enables Up-to-Date and Domain-Specific Knowledge: You can update the knowledge base in your vector database at any time without having to retrain the multi-billion parameter LLM. This allows you to feed the model information about recent events or proprietary data dynamically.
  3. Increases Transparency and Trust: Since the answer is based on specific retrieved documents, the system can cite its sources. This allows users to verify the information for themselves, which is crucial for building trust in your application. If a user asks a question and no relevant documents are found, the system can be instructed to respond "I don't have enough information to answer that," which is far better than providing a confident but wrong answer.

5. A Word of Caution: RAG is Not a Silver Bullet

While RAG is incredibly effective, it's not foolproof. The quality of a RAG system is highly dependent on the quality of its retrieval step. If the retriever fails to find the correct documents—or if the documents themselves contain errors—the LLM will still produce a poor or incorrect response. This is a classic "garbage in, garbage out" scenario.

The following short reading discusses this limitation.

Understanding RAG Part VIII: Mitigating Hallucinations in RAG

This article from Machine Learning Mastery explains that hallucinations can still occur in RAG systems and briefly touches on the reasons why.

Read the section titled 'Why Hallucinations Still Occur in Hallucinations in RAG Systems'. This will give you a more nuanced understanding of RAG's limitations and set the stage for later lessons on improving retrieval quality.

Conclusion

In this lesson, we've established a foundational understanding of Retrieval-Augmented Generation.

Key Takeaways:

  • LLM Hallucination is the tendency of models to generate confident but factually incorrect or nonsensical output, often due to static training data and a lack of real-world grounding.
  • RAG is an architectural pattern that mitigates hallucination by combining a retrieval system with a generative model.
  • The RAG pipeline involves retrieving relevant information from an external knowledge base, augmenting the user's prompt with this information, and having the LLM generate an answer grounded in the provided facts.
  • RAG makes LLMs more reliable by providing them with up-to-date, domain-specific, and verifiable information at query time.

Now that you understand the "what" and "why" of RAG, we can move on to the practical "how." In our next lesson, we will begin building our own RAG system by focusing on the very first step: developing effective document chunking strategies to prepare our knowledge base for ingestion.

Can't find a good explanation? Sign up and we'll make it for you

Sign up