Introduction

Welcome to the final lesson of the "Introduction to RAG" course! 🎉 In the previous lessons, you've learned the fundamentals of RAG, including how to build a basic RAG pipeline. Now, we'll address a critical issue in Large Language Models (LLMs): hallucinations.

Hallucinations occur when an LLM generates plausible-sounding but factually incorrect information. This can be a major problem in real-world applications where accuracy is paramount. Imagine relying on an LLM for medical advice or financial analysis, only to receive fabricated information! In this lesson, we'll explore how RAG can significantly reduce hallucinations by grounding the LLM in a reliable knowledge base.

RAG: Grounding LLMs in a Knowledge Base

As you learned in the previous lesson, RAG addresses the problem of hallucinations by providing the LLM with relevant context retrieved from a knowledge base. This "grounding" helps the LLM generate more accurate and reliable responses.

To recap, the basic RAG pipeline involves these steps:

  1. Indexing: Organizing documents into a knowledge base.
  2. Retrieval: Identifying the most relevant document(s) from the knowledge base based on the user's query.
  3. Augmentation: Combining the retrieved document(s) with the original query to create a context-rich prompt.
  4. Generation: Feeding the augmented prompt to the LLM to generate a response.
A Financial Knowledge Base
Output Analysis: Accuracy Showdown

Now, let's compare the outputs of the naiveGeneration and ragGeneration functions when answering the same query.

Here's the query we'll use:

const query = `
    Write a short summary of the stock market performance on April 14, 
    2023 for the following symbols: AAPL, MSFT, TSLA.
    Your summary should include:
    For each symbol:
    - The opening price
    - The closing price
    - The highest and lowest prices of the day
    - The trading volume
`;

This query asks for a summary of the stock market performance on April 14, 2023, for AAPL, MSFT, and TSLA, including specific details like the opening price, closing price, and trading volume.

When we run the naiveGeneration function with this query, the LLM generates a response based solely on its pre-trained knowledge. The output might look something like this:

Naive approach:
    On April 14, 2023, the stock market performance for the specified symbols was as follows:

    **AAPL (Apple Inc.)**
    - Opening Price: $167.50
    - Closing Price: $169.00
    - Highest Price: $170.00
    - Lowest Price: $166.80
    - Trading Volume: 55 million shares

    **MSFT (Microsoft Corporation)**
    - Opening Price: $290.00
    - Closing Price: $292.50
    - Highest Price: $293.00
    - Lowest Price: $289.50
    - Trading Volume: 30 million shares

    **TSLA (Tesla, Inc.)**
    - Opening Price: $185.00
    - Closing Price: $188.50
    - Highest Price: $189.00
    - Lowest Price: $184.50
    - Trading Volume: 45 million shares

Notice that the LLM provides plausible-sounding numbers for all the requested information. However, these numbers are absolutely incorrect because the LLM is hallucinating. It's making up the data!

Now, let's run the ragGeneration function with the same query. The output might look something like this:

RAG approach:
    ### Stock Market Performance Summary for April 14, 2023

    **AAPL (Apple Inc.)**
    - **Opening Price:** $161.10
    - **Closing Price:** $162.80
    - **Highest Price:** $163.50
    - **Lowest Price:** $160.50
    - **Trading Volume:** 85 million shares

    **MSFT (Microsoft Corp.)**
    - **Opening Price:** Data not provided.
    - **Closing Price:** Data not provided.
    - **Highest Price:** Data not provided.
    - **Lowest Price:** Data not provided.
    - **Trading Volume:** Data not provided.

    **TSLA (Tesla Inc.)**
    - **Opening Price:** Data not provided.
    - **Closing Price:** Data not provided.
    - **Highest Price:** Data not provided.
    - **Lowest Price:** Data not provided.
    - **Trading Volume:** Data not provided.

    *Note: Information for MSFT and TSLA is not available in the provided data.*

In this case, the RAG approach provides accurate information for AAPL, as it's grounded in the KNOWLEDGE_BASE. However, it states that it doesn't have information for MSFT and TSLA; how can that be, given that our KNOWLEDGE_BASE includes information about those symbols as well?

If you remember the ragRetrieval function from the previous lesson, you might have already guessed why. If you don't, no worries, we'll be exploring that in the practice section!

Conclusion and Next Steps

Congratulations on completing the "Introduction to RAG" course! 🎉 You've learned the core principles of RAG, from its evolution from information retrieval to building a basic RAG pipeline. In this final lesson, you've seen how RAG can significantly improve the accuracy and reliability of LLM responses by grounding them in a knowledge base.

Now, it's time to put your knowledge into practice! Complete the exercises that follow this lesson to solidify your understanding of RAG and its advantages. We hope you enjoyed this course and are excited to continue your journey into the world of RAG!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal