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.
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:
- Indexing: Organizing documents into a knowledge base.
- Retrieval: Identifying the most relevant document(s) from the knowledge base based on the user's query.
- Augmentation: Combining the retrieved document(s) with the original query to create a context-rich prompt.
- Generation: Feeding the augmented prompt to the LLM to generate a response.
Let's define our KNOWLEDGE_BASE
for this lesson. This is a dictionary containing information about stock prices for AAPL, MSFT, and TSLA on April 13th and 14th, 2023.
Python1KNOWLEDGE_BASE = { 2 "AAPL": { 3 "title": "AAPL Stock (April 2023)", 4 "content": ( 5 "On 2023-04-13, AAPL opened at $160.50, closed at $162.30, with a high of $163.00 and a low of $159.90. " 6 "Trading volume was 80 million shares. " 7 "On 2023-04-14, AAPL opened at $161.10, closed at $162.80, with a high of $163.50 and a low of $160.50. " 8 "Trading volume was 85 million shares." 9 ) 10 }, 11 "MSFT": { 12 "title": "MSFT Stock (April 2023)", 13 "content": ( 14 "On 2023-04-13, MSFT opened at $285.00, closed at $288.50, with a high of $290.00 and a low of $283.50. " 15 "Trading volume was 35 million shares. " 16 "On 2023-04-14, MSFT opened at $286.00, closed at $289.00, with a high of $291.50 and a low of $284.70. " 17 "Trading volume was 40 million shares." 18 ) 19 }, 20 "TSLA": { 21 "title": "TSLA Stock (April 2023)", 22 "content": ( 23 "On 2023-04-13, TSLA opened at $185.00, closed at $187.00, with a high of $189.00 and a low of $184.50. " 24 "Trading volume was 50 million shares. " 25 "On 2023-04-14, TSLA opened at $186.00, closed at $188.50, with a high of $190.00 and a low of $185.50. " 26 "Trading volume was 55 million shares." 27 ) 28 } 29}
This KNOWLEDGE_BASE
is a dictionary where keys are stock symbols (AAPL
, MSFT
, TSLA
) and values are dictionaries containing the title and content of a document related to that stock. The content includes the opening price, closing price, highest and lowest prices, and trading volume for April 13th and 14th, 2023.
The rest of the codebase is the same as the one in the previous lesson, so we won't discuss that again here.
Now, let's compare the outputs of the naive_generation
and rag_generation
functions when answering the same query.
Here's the query we'll use:
Python1query = ( 2 "Write a short summary of the stock market performance on April 14, " 3 "2023 for the following symbols: AAPL, MSFT, TSLA.\n" 4 "Your summary should include:\n" 5 "For each symbol:\n" 6 "- The opening price\n" 7 "- The closing price\n" 8 "- The highest and lowest prices of the day\n" 9 "- The trading volume" 10)
This query asks for a summary of the stock market performance on April 14, 2023, for AAPL
, MSFT
, and TSLA
, including specific details like opening price, closing price, and trading volume.
When we run the naive_generation
function with this query, the LLM generates a response based solely on its pre-trained knowledge. The output might look something like this:
1Naive approach: 2 On April 14, 2023, the stock market performance for the specified symbols was as follows: 3 4 **AAPL (Apple Inc.)** 5 - Opening Price: $167.50 6 - Closing Price: $169.00 7 - Highest Price: $170.00 8 - Lowest Price: $166.80 9 - Trading Volume: 55 million shares 10 11 **MSFT (Microsoft Corporation)** 12 - Opening Price: $290.00 13 - Closing Price: $292.50 14 - Highest Price: $293.00 15 - Lowest Price: $289.50 16 - Trading Volume: 30 million shares 17 18 **TSLA (Tesla, Inc.)** 19 - Opening Price: $185.00 20 - Closing Price: $188.50 21 - Highest Price: $189.00 22 - Lowest Price: $184.50 23 - 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 rag_generation
function with the same query. The output might look something like this:
1RAG approach: 2 ### Stock Market Performance Summary for April 14, 2023 3 4 **AAPL (Apple Inc.)** 5 - **Opening Price:** $161.10 6 - **Closing Price:** $162.80 7 - **Highest Price:** $163.50 8 - **Lowest Price:** $160.50 9 - **Trading Volume:** 85 million shares 10 11 **MSFT (Microsoft Corp.)** 12 - **Opening Price:** Data not provided. 13 - **Closing Price:** Data not provided. 14 - **Highest Price:** Data not provided. 15 - **Lowest Price:** Data not provided. 16 - **Trading Volume:** Data not provided. 17 18 **TSLA (Tesla Inc.)** 19 - **Opening Price:** Data not provided. 20 - **Closing Price:** Data not provided. 21 - **Highest Price:** Data not provided. 22 - **Lowest Price:** Data not provided. 23 - **Trading Volume:** Data not provided. 24 25 *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 rag_retrieval
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!
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!
