Building a simple chatbot is a great start, but as you've noticed, it has some serious limitations. The biggest one? The model's knowledge is static and incomplete.

This is a major problem for two main reasons:

  1. Private Data: Large Language Models (LLMs) are only trained on publicly available information. They have no access to your company's internal documents, private databases, or any other information that isn't on the public internet. If you ask a simple chatbot a question about your private data, it won't have a clue what you're talking about.
  2. Knowledge Cutoff: The training process for an LLM is incredibly long and expensive, taking months or even years. This means the model's knowledge is "cut off" at a specific date. Any event, discovery, or change that happened after that date is something the model simply doesn't know about.

When an LLM encounters a question about information it wasn't trained on, it often hallucinates 🤯.

This means it confidently generates a response that sounds plausible but is completely made up and factually incorrect. It's not malicious; the model is just trying its best to be helpful by predicting the most likely words in a sequence based on its training data, even if it has no real knowledge to back them up.

Simply changing your prompt won't fix this either. No matter how you rephrase the question, the model is still limited to the same outdated and incomplete knowledge it was originally trained on. You need a way to give the model access to new, external information

The Main Challenge: Choosing the Right Context for Your LLM

image.png

If you only had one or two pages of private or new information for your AI, this would be easy. You could just copy and paste the entire text into every single prompt you send to the model.

But what if you have hundreds or thousands of documents? That’s where the main challenge lies. You have far more information than can fit into a single prompt.

So, how do you solve this "quantity problem"? How do you pick the small, crucial pieces of information from your massive collection of text to help the LLM answer a specific question?

The solution involves a two-step process, which is the foundation of Retrieval-Augmented Generation (RAG):

  1. Indexing: This is the process of getting your documents ready. You'll learn how to preprocess them into a format that a computer can easily understand and search. This is the preparation phase.
  2. Retrieval: Once your documents are indexed, you can retrieve the most relevant pieces of information and use them as context for the LLM.

This chapter focuses on the first step: indexing. Before we dive into the "how," let's explore why your documents need to be preprocessed in the first place. Before we dive into the "how," let's explore why your documents need to be preprocessed in the first place. Let’s assume you would like to use LLMs to analyze the financial performance and risks in Tesla’s 2022 annual report https://www.sec.gov/Archives/edgar/data/1318605/000095017023001409/tsla-20221231.htm , which is stored as text in PDF format. Your goal is to be able to ask a question like “What key risks did Tesla face in 2022?” and get a humanlike response based on context from the risk factors section of the document. Breaking it down, there are four key steps (shown in Figure 2-1) that you’d need to take in order to achieve this goal:

  1. Extract the text from the document.
  2. Split the text into manageable chunks.
  3. Convert the text into numbers that computers can understand.
  4. Store these number representations of your text somewhere that makes it easy and fast to retrieve the relevant sections of your document to answer a given question.