Operation
Retrieval-Augmented Generation
While Large Language Models (LLMs) are powerful in many respects, they have inherent limitations when it comes to generating verifiable responses based on real-world data. Their knowledge is confined to their training data. This makes them susceptible to nonsensical, biased, out-of-context, and sometimes outdated responses. RAG introduces fact retrieval from external sources for LLMs to mitigate this.
What is RAG?
Retrieval-Augmented Generation (RAG) is a technique for optimising the output of an LLM by referencing an authoritative or predefined knowledge base before generating a response. This knowledge base could be a reputable online source such as NASA, the CDC, or an organisation's internal database. RAG aims to connect LLMs with up-to-date, factual information from the real world to make their output more reliable, trustworthy, and accurate.
Without RAG, a user provides a text input (prompt) to an LLM, and the model delivers a response based on its training data. Whilst this is fine for general use cases, it becomes a problem when:
You need a specific response from your LLM-based solution that is based on up-to-date data. Training data can become outdated, leading an LLM to give you an incorrect answer. For example, in 2023, astronomers discovered that Saturn has more moons than Jupiter. If a model was trained on data from 2022, it will give you the wrong answer.
You need a specific response from the LLM based on your company data.
So how does RAG mitigate this? It adds a fact-retrieval component to this process. The user's input is used to retrieve information from a relevant source (trusted online sources or enterprise data). This information and the user's prompt are passed to the LLM. Finally, the LLM uses both to construct an accurate response.
How Retrieval-Augmented Generation Works
Implementing RAG into the LLM workflow involves 6 key steps. Another AI technique, vector embeddings, plays a crucial role in this process. Embeddings are primarily important for retrieving relevant information based on document-query similarity. They help LLMs identify relevant information from your chosen source, whether it is a public online source or a company's internal database. Here is how RAG works.
[caption id="" align="alignnone" width="949"]

Source: Google Cloud Blog [/caption]
1: Preparing and Storing Data in a Vector Database (Preliminary Step)
Before this workflow begins, you prepare the data that the LLM will use for reference. For example, you can obtain this data via APIs from available sources. Alternatively, you can also retrieve it from the company's internal database.
To do this, an embedding model converts this information into vector representations. Put simply, text data is transformed into numerical data that can be easily searched, matched, and retrieved from the vector database. A type of AI model called an embedding model is used to complete this process.
2: User Provides Input
A user submits a query with a question or instruction. Taking a company's AI-powered chatbot as an example: an employee might ask a question like "How much annual leave do I have left?". The following steps illustrate what happens in the background to generate an accurate response.
3: Retrieving Relevant Information
The chatbot analyses this question and forwards it to the embedding model. The query is converted into its vector representation, which is used to perform a semantic search in the vector database. Once relevant information has been identified, the system retrieves this information from the knowledge base.
4: Generating an Augmented Query
The retrieved information is then combined with (augmented into) the user's original query and passed to the LLM. This creates a richer, more informative context for the LLM to draw upon during generation.
5: Response Generation
The LLM uses the knowledge gained from the training data together with the prompt augmented by internal data to generate a response. Based on this combined knowledge, the LLM incorporates the retrieved facts and addresses the user's intent.
6: Source Attribution
Although sometimes optional, RAG implementations allow the LLM to cite the sources of the retrieved information within its response. These could be links to specific paragraphs or footnotes, similar to academic papers.
The Benefits of RAG
Implementing RAG into the AI content generation of LLMs brings several benefits, including the following:
Improved Factual Accuracy
RAG grounds an LLM's output in real-world, factual information. It enables LLMs to access and utilise information from trusted online sources or a company's knowledge base. This ensures that the generated outputs are factually accurate and up to date, making the LLM less prone to hallucinations.
More Reliable Output
LLMs are trained on massive amounts of data, but this data may not always be accurate or complete. When an LLM encounters a question outside its training data, it might fabricate information to fill the gaps. This also happens when a user's prompt lacks sufficient context.
RAG helps LLMs generate reliable outputs in this way:
These models can verify and retrieve facts from reliable sources before responding.
After retrieving the facts, it augments the user's prompt with this information to provide the LLM with rich context for understanding user intent.
It Provides Insight into the LLM's Reasoning
As LLMs are increasingly deployed for critical tasks, understanding their reasoning is vital to ensure they make reliable and trustworthy decisions. Although these models are complex and therefore not easily interpretable, implementing RAG takes you halfway towards understanding how they arrive at their outputs.
With RAG, LLMs can cite the information sources from which their output is derived. Users can verify the information presented and understand the reasoning behind an LLM's response.
Ability to Leverage a Wide Variety of Knowledge Sources
You can use RAG to connect virtually any LLM to any external information source. This includes globally trusted sources like the WHO or a company's internal database. This makes LLMs useful for a wide range of specific applications.
Furthermore, RAG is a cost-effective method to ensure that an LLM's output remains relevant, accurate, and useful across different contexts. You do not need to expend massive computational resources to retrain or fine-tune the LLM to achieve this.
The Challenges of RAG Implementation
Although RAG is designed to make LLM outputs more reliable, relevant, and factually accurate, reaping these benefits is not guaranteed. Several challenges can hinder this, and understanding them is the first step towards proactively addressing them and using RAG effectively. Here are some challenges in implementing RAG:
Outdated or Biased Information in Knowledge Bases
The LLM's output is only as good as the knowledge base that provides it with facts and context. If this source is outdated or contains biased information, the quality of the output will be low. It is likely to generate a biased response or one that is no longer correct because the information is outdated.
It Relies on the Success of the Retrieval System
RAG depends on its retrieval system to find accurate information relevant to the user's search query and efficiently provide it for the LLM to use. If this fails, RAG is ineffective. It may retrieve factually correct information, but if it is irrelevant or misses the context, it will not be helpful to the user.
Imagine an AI-powered app that helps prospective customers discover new restaurants. Suppose a user asks: "What is the best pizza restaurant in the city?" and the RAG system retrieves a list of the best pizza restaurants for the entire country.
Even though this is factually correct information, the answer will not be helpful to the user. The system failed to retrieve the best pizza restaurants for that specific city.
Implementation Complexity
Connecting the LLM to a knowledge base can be complicated. For example, you may need to build endpoints for a SaaS app to access specific data and consistently feed it into the LLM. Or you might develop a scraper to periodically scrape text from a website and add it to the LLM. In either case, this process is complex and requires significant technical resources.
Use Case Example: A Generative AI Chatbot for Bank Customer Support
Suppose a bank has a customer service chatbot based on an LLM. A customer might ask the bot a question about their eligibility for a specific loan product. The chatbot could struggle with specific details if it relies solely on its training data. With RAG, however, the chatbot can retrieve relevant, up-to-date information about the loan product.
It combines this with the customer's credit score and banking policies retrieved from the bank's internal database to provide the customer with an accurate response.
Summary
RAG enables LLM-based applications to retrieve up-to-date real-world information and augment a user's prompt with it before generating a response. This improves the accuracy, reliability, and trustworthiness of the results. RAG reduces the tendency of an LLM to hallucinate information to fill knowledge gaps.
This opens up various use cases for LLM-based apps that extend beyond what the model was originally trained for. Because of these advantages, RAG implementation is becoming standard practice for generative AI-based applications.



