Retrieval Augmented Generation, explained simply

Large Language Models can be inconsistent. Sometimes they provide precise answers to questions, other times they repeat random facts from their training data. If sometimes they don't seem to know what they're saying, it's because they actually don't know it. LLMs understand how words relate statistically, but not their meaning. Retrieval Augmented Generation (RAG) is an AI framework that improves the quality of the responses generated by LLMs by anchoring the model to external sources of knowledge to integrate the internal representation of the LLM information. In this article, we are going to take a closer look at what they are and how they work to understand what the future of artificial intelligence is.

What you'll find in this article

  • Retrieval Augmented Generation: a brief introduction
  • What is Retrieval Augmented Generation
  • How does Retrieval Augmented Generation work
  • Retrieval Augmented Generation: What are the advantages?
  • Retrieval Augmented Generation: implementation examples
Retrieval Augmented Generation, explained simply

Retrieval Augmented Generation: a brief introduction

Generative artificial intelligence (AI) excels at creating text responses based on large language models (LLM) where AI is trained through a huge number of datapoints.

The good news is that the generated text is often easy to read and provides detailed answers that are broadly applicable to the questions posed by the software, often referred to as prompts.

The bad news is that the information used to generate the response is limited to the information used to train artificial intelligence, often a generalized LLM. The LLM data may not have been updated for weeks, months or years, and in the case of an AI business chatbot, it may not include specific information about the organization's products or services. This can lead to wrong answers that erode the trust that customers and employees place in technology.

This is where retrieval-augmented generation (RAG) comes in. The RAG provides a way to optimize the production of an LLM with targeted information without modifying the model at its base; that targeted information may be more up-to-date than the LLM, but also more precise in the case of specific organizations and sectors. This means that the generative artificial intelligence system can provide more appropriate answers to the prompts, and base those responses on extremely current data.

What is Retrieval Augmented Generation

Retrieval-Augmented Generation (RAG) is a technique that improves the accuracy and reliability of generative artificial intelligence models thanks to information retrieved from specific and relevant data sources. In other words, it fills a gap in the functioning of large language models (LLM).

At their core, LLMs are neural networks, usually evaluated based on the number of parameters they contain. The parameters of an LLM essentially represent the general patterns with which human beings use words to form sentences.

This deep understanding, sometimes called parameterized knowledge, makes LLMs useful in responding to general requests. However, it's not enough for those who need insights into very specific information.

Patrick Lewis, lead author of the 2020 article that coined the term, and his colleagues developed Retrieval-Augmented Generation to link generative AI services to external resources, especially those rich in updated technical details.

The RAG provides models with sources that can be cited, such as footnotes in a scientific article, so that users can verify any statement. This helps build trust.

In addition, the technique can help models clarify any ambiguities present in the user's request. It also reduces the chance that the model will provide a very plausible but incorrect answer, a phenomenon known as hallucination.

Another great advantage of RAG is its relative simplicity.

In a blog, Lewis and three co-authors of the article explain that programmers can implement the process with just five lines of code. This makes the method faster and cheaper than retraining a model with new datasets. And it allows users to dynamically change reference sources.

With retrieval-augmented generation, users can essentially communicate with sets of data, paving the way for new experiences. This means that RAG applications could multiply based on the number of available datasets.

Almost any company can transform its technical or regulatory manuals, videos, or logs into resources called knowledge bases, capable of strengthening LLMs. These sources enable use scenarios such as customer or field support, staff training, and developer productivity.

The difference between RAG and fine-tuning

Currently, most organizations don't train artificial intelligence models from scratch. Instead, customize pre-trained models to suit your needs, often using techniques such as RAG or fine-tuning.

Very often these two are confused by non-technical audiences, so let's take some space for a brief overview of the differences between these two strategies.

The fine-tuning involves modifying model weights, producing a model that is highly customized and optimized for a specific task. It's a good option for organizations that work with codebases written in a specialized language, especially if that language isn't well represented in the model's original training data.

The RAG, on the other hand, does not require changes to the weights.

It retrieves and collects information from various data sources to enrich a prompt, thus allowing the model to generate a more relevant and contextualized response for the end user.

Some organizations start with the RAG and then carry out the fine-tuning of the model to address more specific tasks. Others find that RAG alone is sufficient to customize artificial intelligence according to their needs.

Do you need help bringing Microsoft 365 Copilot to the company?

In collaboration with intranet.ai and Copilot Circle, Dev4Side offers a comprehensive adoption program to maximize your investment in Microsoft 365 Copilot with the continuous support of our experts.

We handle the configuration of your company's digital workplace and provide comprehensive training on Copilot features in Microsoft 365 applications. You can tailor the plan to your specific needs by selecting from the following services:

  • Migrating business data to the cloud of Microsoft 365
  • Assessment of content security and user permissions
  • Training and access to our community of practice
  • Analysis and development of custom extensions

How does Retrieval Augmented Generation work

The RAG works by combining information retrieval models with generative artificial intelligence models, in order to produce more authoritative content. RAG systems query a knowledge base and add more context to the user's prompt before generating a response.

Standard LLM models draw information from their own training datasets. The RAG adds an information retrieval component to the AI workflow, collecting relevant data and providing it to the generative model to improve the quality and usefulness of the responses.

RAG systems follow a five-step process:

  1. The user sends a prompt.
  2. The information retrieval model interrogates the knowledge base to obtain relevant data.
  3. The relevant information is returned from the knowledge base to the integration level.
  4. The RAG system builds an enriched prompt for the LLM, integrating the context of the retrieved data.
  5. The LLM generates an output and returns it to the user.

RAG systems include four main components (plus an extra one) that we see below.

Knowledge base

The first phase in building a RAG system consists of creating a queryable knowledge base. The external data repository can contain information from a multitude of sources: PDFs, documents, guides, websites, audio files and more. A large part of this content will consist of unstructured data, that is, not yet labeled.

RAG systems use a process called embedding to transform data into numerical representations called vectors. The embedding model vectorizes data within a multidimensional mathematical space, organizing the data points according to their similarity. The points judged to be most relevant to each other are placed close together.

Knowledge bases must be constantly updated to ensure the quality and relevance of the RAG system.

Chunking

LLM inputs are limited by context window of the model, that is, the amount of data it can process without losing the logical thread. Divide a document into smaller parts (Chunking) helps to ensure that embedding results do not exceed the limits of the LLM context window within a RAG system.

The size of Chunk is an important hyperparameter for the RAG system. If the chunks are too large, the data points may be too generic and may not precisely match possible user requests. Conversely, if they are too small, the data points may lose semantic consistency.

Retriever

The vectorization of data prepares the knowledge base for Semantic vector search, a technique that identifies points in the database similar to the user's query. The algorithms of Semantic search based on Machine learning they can query large databases and quickly identify relevant information, reducing latency compared to traditional keyword-based searches.

The information retrieval model transforms the user's query into a embedding and search the knowledge base for the most similar embeddings. The results are then returned from the knowledge base.

Integration layer

The level of integration is the core of the RAG architecture: it coordinates processes and manages the flow of data within the system. With the additional data from the knowledge base, the RAG system generates a new prompt for the LLM component. This prompt consists of the user's original query, enriched with the context returned by the retrieval model.

RAG systems use different techniques of Prompt Engineering to automate the creation of effective prompts and help the LLM provide the best possible response. In parallel, LLM orchestration frameworks govern the overall functioning of the artificial intelligence system.

Generator

The generator creates an output based on the enriched prompt it receives from the integration layer. This prompt summarizes the user's input with the retrieved data and instructs the generator to take that information into account in the response. Generators are typically pre-trained language models.

Retrieval Augmented Generation: What are the advantages?

RAG allows organizations to avoid the high costs of retraining when it comes to adapting generative artificial intelligence models to specific industry use cases. Companies can use RAG to fill gaps in the knowledge base of a model of Machine learning, thus allowing him to provide better answers.

But that's not the only benefit; in the next few paragraphs, we'll discuss the main benefits that RAG can offer.

Implementing cost-efficient AI and AI scaling

When implementing artificial intelligence solutions, most organizations start by selecting a Foundation Model, that is, a model of Deep Learning which serves as a basis for the development of more advanced versions. Foundation models usually have general knowledge bases, powered by publicly available training data, such as content on the Internet at the time of training.

The retraining of a foundation model, or the fine-tuning—that is, additional training on a new, smaller dataset specific to a given domain—is a process that requires a lot of resources and is computationally expensive. The model modifies part or all of its parameters to adapt performance to new specialized data.

With RAG, companies can use internal and authoritative data sources, achieving comparable improvements in model performance without having to retrain it. This allows the deployment of AI applications to be scaled as needed, while reducing costs and resource usage.

Access to current, domain-specific data

Generative artificial intelligence models have a knowledge cutoff, i.e. the point at which the training data was last updated. As a model ages beyond its knowledge cutoff, it loses relevance over time. RAG systems link models to additional external data in real time and incorporate updated information into the generated responses.

Companies use RAG to provide models with specific information, such as proprietary customer data, authoritative research, and other relevant documents.

RAG models can also connect to the Internet through application programming interfaces (APIs) and access real-time social media feeds and consumer reviews for a better understanding of market sentiment. Meanwhile, access to real-time news and search engines can lead to more accurate answers, as the models integrate the information retrieved into the text generation process.

Lower risk of hallucinations

Generative artificial intelligence models, such as OpenAI's GPT, work by detecting patterns in their data and using those patterns to predict the most likely outcomes in response to user inputs. Sometimes, models detect patterns that don't actually exist. A hallucination or confabulation occurs when models present erroneous or invented information as if it were factual.

The RAG anchors LLMs on specific knowledge supported by factual, authoritative and updated data. Compared to a generative model that operates only on its own training data, RAG models tend to provide more accurate answers within the contexts of their external data. While RAG can reduce the risk of hallucinations, it cannot make a model error-free.

Increased trust on the part of users

Chatbots, one of the most common implementations of generative artificial intelligence, answer questions posed by human users. For a chatbot like ChatGPT to be successful, users must consider its answers as trustworthy. RAG models may include citations to knowledge sources present in their external data as part of the responses.

When RAG models cite their sources, human users can verify those answers to confirm accuracy, consulting the cited works for clarification and additional information. Archiving business data is often a complex and fragmented maze. RAG responses with citations direct users directly to the materials they need.

Expanded use cases

Access to more data means that a model can handle a wider range of prompts. Companies can optimize models and derive greater value from them by expanding their knowledge bases, thus expanding the contexts in which those models generate reliable results.

Combining generative artificial intelligence with retrieval systems, RAG models can retrieve and integrate information from multiple data sources in response to complex queries.

More control for developers

Modern organizations are constantly processing huge amounts of data, ranging from order inputs to market projections, employee turnover, and more. Effective construction of data pipelines and adequate data storage are critical for a strong implementation of RAG.

At the same time, developers and data scientists can modify the data sources that models have access to at any time. Repositioning a model from one task to another becomes a task of adapting its external sources of knowledge, rather than a process of fine-tuning or retraining. If you need the fine-tuning, developers can give priority to this work, rather than managing the model's data sources.

Increased data security

Because the RAG links a model to external knowledge sources instead of incorporating that knowledge into the model's training data, it maintains a separation between the model and that external knowledge. Companies can use RAG to preserve first-party data, while granting access to it to models—access that can be revoked at any time.

However, companies must be vigilant in maintaining the security of the same external databases. The RAG uses vector databases, which use embedding to convert data points into numerical representations. If these databases are compromised, attackers could reverse the process of embedding vector and access the original data, especially if the vector database is not encrypted.

Retrieval Augmented Generation: implementation examples

RAG systems essentially allow users to query databases using a conversational language. The data-driven question-answering capabilities of RAG systems have been applied in a wide range of use cases, including some of the main ones in this section.

Chatbots, specialized virtual assistants, and research

Companies that want to automate customer support may find that their artificial intelligence models lack the specialized knowledge necessary to properly assist customers. RAG systems link models to internal data to provide customer support chatbots with the latest information about a company's products, services, and policies.

The same principle applies to AI avatars and personal assistants. Linking the underlying model to the user's personal data and referring to previous interactions provides a more personalized user experience.

Capable of reading internal documents and interfacing with search engines, RAG models also excel in searching. Financial analysts can generate customer-specific reports with updated market information and previous investment activities, while medical professionals can interact with patient and institution records.

Content generation, market analysis, and product development

The ability of RAG models to cite authoritative sources can lead to a more reliable content generation. Although all generative artificial intelligence models can commit Hallucination, the RAG makes it easier for users to verify the accuracy of the results.

Business leaders can consult social media trends, competitor activity, industry news, and other online sources to better inform business decisions. Meanwhile, product managers can refer to customer feedback and user behaviors when considering choices for future development.

Knowledge engine and recommendation services

RAG systems can empower employees with internal company information. Streamlined onboarding processes, faster HR support, and on-demand guidance for employees in the field are just a few ways companies can use RAG to improve work performance.

By analyzing previous user behavior and comparing it with current offerings, RAG systems power more accurate recommendation services. An e-commerce platform and a content distribution service can both use RAG to keep customers engaged and encourage purchases.

Conclusions

Artificial intelligence is here to stay and has already demonstrated that it can radically transform the way we work and conceive the tools we use to operate in our daily lives.

Like any new tool, AI also had its own series of initial problems, which, however, the technological community is working hard to solve, even through cutting-edge techniques such as RAG.

How AI will evolve further in the coming years only time can tell us but in the meantime we can begin to take a peek at the future of this technology and, from the premises, it seems to be very promising.

FAQ on Retrieval Augmented Generation

What is Retrieval Augmented Generation (RAG)?

Retrieval Augmented Generation is a technique that makes it possible to improve the responses produced by a generative linguistic model by integrating external and relevant data. This approach helps artificial intelligence overcome the limitations of its training data, making answers more accurate, up-to-date and reliable.

How does RAG improve LLM models?

Traditional LLMs generate outputs based on previously trained data, which may not include recent or contextual information. The RAG fills this gap by adding data retrieved in real time from external sources to the user's prompt, thus offering a richer context that guides the model towards better answers.

What's the difference between RAG and fine-tuning?

The RAG does not modify the parameters of the model, but merely enriches the prompts with updated and relevant information retrieved from external sources. Fine-tuning, on the other hand, involves an internal reworking of the model itself, which is retrained on new data. RAG is therefore more flexible, faster to implement and less expensive, and is suitable for situations where information sources change over time.

How does a RAG system work?

A RAG system receives a question from the user, retrieves relevant data from an external knowledge base through a semantic search system, integrates this data into the original prompt and finally generates a response through the LLM model. The workflow includes a knowledge base, a phase of dividing the contents into manageable portions, a search form, a level of integration, and the final generator that returns the response.

What does chunking mean?

Chunking is the process of dividing longer documents into smaller, semantically consistent chunks. This is necessary because LLM models can only handle a limited amount of text at a time. If the blocks are too large, you risk losing precision; if they are too small, you risk losing meaning.

What are the main advantages of RAG?

Using RAG allows you to avoid expensive model retraining processes, reduces the chances of artificial intelligence providing erroneous or invented information and allows you to easily integrate updated data, even in real time. In addition, it provides greater control to developers who can modify information sources without altering the model and improves data security by separating the model itself from external content.

What are the most common areas of application?

The most common applications of RAG include chatbots and virtual assistants that exploit specific business data, advanced search engines for areas such as finance and healthcare, systems for generating verifiable content, market analysis and product development tools, as well as internal knowledge engines and personalized recommendation platforms.

Does the RAG completely eliminate the errors of AI models?

No, the RAG does not make a model infallible. Although it significantly reduces the risk of generating erroneous or fictitious answers, known as hallucinations, overall accuracy always depends on the quality of the sources used and on the design of the system.

Get in touch with the team

Modern Apps

The Modern Apps team specializes in development and integration across the entire Microsoft 365 ecosystem. We design native applications for Microsoft and Azure platforms, and implement business processes that integrate with and maximize the investment in Microsoft 365.