You get a bonus - 1 coin for daily activity. Now you have 1 coin

Retrieval-Augmented Generation (RAG)

Lecture



Retrieval-augmented generation ( RAG ) is a technique that allows large language models (LLMs) to retrieve and incorporate new information. With RAG, LLMs do not answer user queries until they have consulted a specified set of documents. These documents supplement the information from the LLM's existing training data. This allows the LLM to use domain-specific and/or up-to-date information that is missing from the training data. For example, this helps LLM-based chatbots access a company's internal data or generate answers based on authoritative sources.

RAG improves large language models (LLMs) by including information retrieval before generating responses. Unlike traditional LLMs, which rely on static training data, RAG retrieves relevant text from databases, uploaded documents, or web sources. According to Ars Technica, "RAG is a way of improving LLM performance by essentially combining the LLM process with a web search or other document-retrieval process to help the LLM stick to facts." This method helps reduce AI hallucinations, which cause chatbots to describe non-existent policies or recommend non-existent court cases to lawyers looking for citations to support their arguments.

RAG also reduces the need to retrain LLMs on new data, which saves computational and financial costs. In addition to improving efficiency, RAG also allows LLMs to include sources in their responses so that users can verify the cited sources. This provides greater transparency, since users can double-check the resulting content for accuracy and relevance.

The term RAG was first introduced in a 2020 research paper from Meta.

Limitations of RAG and LLMs

LLMs can provide incorrect information. For example, when Google first demonstrated its LLM tool "Google Bard," the LLM provided incorrect information about the James Webb Space Telescope. This error caused the company's stock value to drop by 100 billion dollars. RAG is used to prevent these errors, but it does not solve all the problems. For example, LLMs can generate misinformation even when using factually correct sources, if they misinterpret the context. MIT Technology Review gives an example of an AI-generated response: "There has been one Muslim president of the United States, Barack Hussein Obama." The model retrieved this from an academic book with the rhetorical title "Barack Hussein Obama: America's First Muslim President?" The LLM did not "know" or "understand" the context of the title, which led to a false statement

LLMs with RAG are programmed to prioritize new information. This method is called "prompt stuffing." Without prompt stuffing, the LLM's input is generated by the user; with prompt stuffing, additional relevant context is added to this data, guiding the model's response. This approach gives the LLM key information right from the start of the prompt, prompting it to prioritize the provided data over the knowledge it already has from training.

Process

Retrieval-augmented generation (RAG) improves large language models (LLMs) by incorporating an information-retrieval mechanism that allows the models to access and use additional data beyond their original training set. AWS states: "RAG allows an LLM to retrieve relevant information from external data sources to generate more accurate and contextually relevant responses" ("indexing"). This approach reduces reliance on static datasets, which can quickly become outdated. When a user submits a query, RAG uses a document retriever to find relevant content from the available sources before incorporating the retrieved information into the model's response ("retrieval"). Ars Technica notes that "when new information becomes available, instead of retraining the model, all that is required is to augment the model's external knowledge base with the updated information" ("augmentation"). Through the dynamic integration of relevant data, RAG allows LLMs to generate more informed and contextually grounded responses ("generation"). IBM states that "in the generation phase, the LLM uses the augmented prompt and its internal representation of its training data to synthesize an engaging answer tailored to the user at that moment."

Key stages of RAG

Indexing

Typically, the data that needs to be referenced is converted into LLM embeddings — numerical representations in the form of a large vector space. RAG can be used for unstructured (usually text), semi-structured, or structured data (for example, knowledge graphs). These embeddings are then stored in a vector database to enable document retrieval

Retrieval-Augmented Generation (RAG)

Overview of the RAG process, which combines external documents and user input into an LLM prompt to produce a tailored result

Retrieval

When a user query is received, a document retriever is first invoked to select the most relevant documents to be used to augment the query. This comparison can be performed using various methods, which depend in part on the type of indexing used.

Augmentation

The model passes this relevant retrieved information to the LLM through prompt engineering of the original user query. Newer implementations (as of 2023) may also include special augmentation modules with capabilities such as expanding queries across multiple domains and using memory and self-improvement to learn from previous retrievals.

Generation

Finally, the LLM can generate output based on both the query and the retrieved documents. Some models include additional steps to improve the output, such as re-ranking the retrieved information, context selection, and fine-tuning.

Improvements

Improvements to the basic process described above can be applied at various stages of the RAG pipeline.

Encoder

These methods focus on encoding text as dense or sparse vectors. Sparse vectors, which encode word identity, typically have a length equal to the vocabulary size and contain mostly zeros. Dense vectors, which encode meaning, are more compact and contain fewer zeros. Various improvements can enhance the way similarity is computed in vector stores (databases).

  • Performance is improved by optimizing the computation of vector similarity. Dot products improve similarity scoring, and approximate nearest neighbor (ANN) search improves retrieval efficiency compared to K-nearest neighbor (KNN) search.
  • Accuracy can be improved through late interaction, which allows the system to compare words more precisely after retrieval. This helps refine document ranking and improve retrieval relevance.
  • Hybrid vector approaches can be used to combine dense vector representations with sparse one-hot vectors, taking advantage of the computational efficiency of sparse dot products compared to dense vector operations.
  • Other retrieval methods aim to improve accuracy by refining the way documents are selected. Some retrieval methods combine sparse representations, such as SPLADE, with query-expansion strategies to improve retrieval precision and recall.

Retriever-oriented methods

These methods aim to improve the quality of document retrieval in vector databases:

  • Pre-training the retriever using the Inverse Cloze Task (ICT), which helps the model learn retrieval patterns by predicting masked text in documents.
  • Progressive data augmentation, used in Diverse Augmentation for Generalizable Dense Retrieval (DRAGON), improves retrieval density by sampling hard negative examples during training.
  • Supervised retriever optimization aligns retrieval probabilities with the likelihood distribution of the generator model. This involves retrieving the top-k vectors for a given prompt, estimating the difficulty of the generated response, and minimizing the KL divergence between the retriever's selections and the model's likelihoods to refine retrieval.
  • Re-ranking methods can improve retriever performance by prioritizing the most relevant retrieved documents during training.

Language model

Retrieval-Augmented Generation (RAG)

The Retro language model for RAG. Each Retro block consists of Attention, Chunked Cross Attention, and Feed Forward layers. Black rectangles denote the data being modified, and blue denotes the algorithm performing those modifications.

By redesigning the language model with the retriever in mind, a network 25 times smaller can achieve a level of perplexity comparable to much larger counterparts. Since training happens from scratch, this method (Retro) entails high costs for training runs, which the original RAG scheme avoided. The hypothesis is that, by providing domain knowledge during training, Retro focuses less on the domain and can devote its smaller weight resources solely to the semantics of language. The redesigned language model is presented here.

Retro has been reported to be non-reproducible, so changes were made to make it so. The more reproducible version is called Retro++ and includes contextual RAG.

Chunking

Chunking data involves using various strategies to split data into vectors so that the retriever can find details within them.

Retrieval-Augmented Generation (RAG)
Different data styles have patterns that can be used to chunk them appropriately.

Three types of chunking strategies:

  • Fixed length with overlap. This is fast and simple. Overlapping consecutive chunks helps maintain semantic context between them.
  • Syntactic chunking can split a document into sentences. Libraries such as spaCy or NLTK can help with this as well.
  • Chunking by file format. Some file types have built-in chunks, and it is better to take these into account. For example, code files are best chunked and vectorized as whole functions or classes. In HTML files, <table> elements or base64-encoded <img> elements should be left untouched. Similar considerations should be taken into account for PDF files. Libraries such as Unstructured or Langchain can help with this method.

Knowledge graphs

Instead of using documents as a source for vectorizing and retrieving data, knowledge graphs can be used. One can start with a set of documents, books, or other text corpora and convert them into a knowledge graph using one of many methods, including language models. Once the knowledge graph is built, subgraphs can be vectorized, stored in a vector database, and used for retrieval, just as in ordinary RAG. The advantage is that graphs have a more recognizable structure than lines of text, and this structure can help extract more relevant facts for generation. This approach is sometimes called GraphRAG.

Hybrid search

Sometimes, searching vector databases may miss key facts needed to answer the user's question. One way to address this problem is to perform a traditional text search, add the retrieved results to the text chunks associated with the retrieved vectors, and feed the resulting hybrid text into the language model for generation.

Evaluation and benchmarks

RAG systems are typically evaluated using benchmarks designed to test retrievability, retrieval accuracy, and generation quality. Popular datasets include BEIR, a set of information-retrieval tasks across various domains, as well as Natural Questions or Google QA for testing data quality in open domains.

Quality metrics

  • Context relevance — assesses the accuracy and specificity of the retrieved context, ensuring it matches the question and minimizing the cost of processing extraneous content.
  • Answer faithfulness — ensures that the generated answer corresponds to the factual information in the retrieved context, ruling out contradictions.
  • Answer relevance — requires a direct relationship between the answer and the question.

Challenges

RAG is not a complete solution to the problem of hallucinations in models with limited grounding. According to Ars Technica, "this is not a direct fix, since hallucinations related to the source material can still occur in response to LLM exposure."

Although RAG improves the accuracy of large language models (LLMs), it does not eliminate all problems. One limitation is that RAG, while reducing the need for frequent model retraining, does not eliminate it entirely. In addition, LLMs may have difficulty recognizing cases where they lack sufficient information to provide a reliable answer. Without special training, models may generate answers even when they should indicate uncertainty. According to IBM, this problem can arise when a model is unable to assess the limits of its own knowledge.

RAG systems can retrieve sources that are factually correct but misleading, leading to errors of interpretation. In some cases, an LLM may extract a statement from a source without taking its context into account, leading to an incorrect conclusion. In addition, when faced with contradictory information, RAG models may have difficulty determining which source is accurate. The worst outcome of this limitation is that the model may combine data from multiple sources, producing answers that merge outdated and current information in a misleading way. According to MIT Technology Review, these problems arise because RAG systems can misinterpret the data they retrieve.

Applications

Since RAG systems are an extension of traditional search systems, their fields of application overlap. RAG systems are used in areas such as customer service, medical information analysis, legal research, education, scientific research, financial analysis, log data evaluation, and all other applications where information retrieval matters.

See also

  • Artificial intelligence
  • Generative artificial intelligence
  • LLM

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Approaches and directions for creating Artificial Intelligence"

Terms: Approaches and directions for creating Artificial Intelligence