aiDhara

What is RAG (Retrieval-Augmented Generation)

RAG, or Retrieval-Augmented Generation, is an AI framework that improves the performance of large language models (LLMs). It works by connecting LLMs to external knowledge sources, like databases or a company’s internal documents, so they can access up-to-date and specific information before generating a response.

Here’s a breakdown of how it works and why it’s useful:

RAG, or Retrieval-Augmented Generation, is an AI framework that improves the performance of large language models (LLMs). It works by connecting LLMs to external knowledge sources, like databases or a company’s internal documents, so they can access up-to-date and specific information before generating a response.

Here’s a breakdown of how it works and why it’s useful:

How RAG Works:

  • Retrieval: When a user submits a query, RAG first uses search algorithms to retrieve relevant information from a designated knowledge base. This external data is outside the LLM’s original training.
  • Augmentation: The retrieved information is then added to the user’s original query, creating an “augmented” prompt.
  • Generation: The LLM receives this augmented prompt and uses both its internal training data and the new, retrieved information to generate a more accurate, contextual, and grounded response.

Why RAG is Important:

  • Factual Accuracy: RAG helps to prevent “hallucinations” or factually incorrect responses that can sometimes occur with LLMs. By providing the model with specific, verifiable information, its answers are more reliable.
  • Up-to-Date Information: LLMs are limited by the data they were trained on, which can become outdated. RAG overcomes this by allowing the model to access and use the latest information from its external sources.
  • Cost-Effective: It is significantly less expensive and time-consuming to update a knowledge base than it is to retrain an entire LLM.
  • Source Citation: RAG enables the model to cite its sources, which builds trust and allows users to verify the information.
  • Contextual Relevance: It helps LLMs maintain context in complex conversations by providing a more comprehensive understanding of the topic.

The RAG process, step-by-step 

The RAG process can be broken down into two main phases: a preliminary indexing phase and a runtime retrieval-and-generation phase. 

Indexing phase 

This phase occurs before a user query is received and involves preparing an external knowledge base. 

  1. Collect data: Relevant data is gathered from various sources, such as documents, websites, or databases.
  2. Chunk the data: The collected data is split into smaller, manageable segments, or “chunks.” This is necessary because LLMs have a limited context window, and chunks must be small enough to be easily retrieved and inserted into a prompt.
  3. Create embeddings: An embedding model converts the text chunks into numerical vector representations. These vectors capture the semantic meaning of the text, allowing for similarity searches later.
  4. Store in a vector database: The embeddings and their corresponding text chunks are stored in a specialized vector database. This allows for fast and efficient retrieval of semantically similar information. 

Retrieval and generation phase 

This phase occurs when a user submits a query to the RAG system. 

  1. Receive query: The user submits a question or prompt to the AI application.
  2. Retrieve relevant information: The system uses a retriever to convert the user’s query into an embedding. It then performs a similarity search in the vector database to find the top-kk𝑘 most relevant text chunks.
  3. Augment the prompt: The system adds the retrieved, relevant information to the user’s original query. This enriched prompt gives the LLM the necessary context to form its response.
  4. Generate response: The LLM receives the augmented prompt and generates a response based on the new, external data, as well as its original training knowledge.
  5. Deliver response: The final, more accurate answer is delivered to the user. Many RAG systems also include citations to the source documents for verification. 

RAG Process in 2 Minutes

Nirmal Dhara Avatar

Published by

Leave a comment