Adaptive RAG

Open in ColabOpen in GitHub

Overview

This tutorial introduces Adaptive RAG, which finds information and generates answers in a smarter way. Adaptive RAG is a system that analyzes the nature of questions using AI, selects the most appropriate method such as web search or internal document search to find information, and creates the best possible answer by trying different methods when necessary. In this tutorial, we implement routing between web search and internal document search using LangGraph.

The purpose of this tutorial is to help users understand the concept of Adaptive RAG and learn how to implement it using LangGraph. Through this, users can perform web searches for questions related to recent events and utilize internal document search with self-correcting capabilities for questions related to indexed content.

What We Will Learn

  • Preparing Data (Create Index) Convert documents into a format our system can understand and load them

  • Using AI (LLMs) Use AI to analyze questions and evaluate how good our retrieved documents are

  • Building Web Search Tool (Web Search Tool) Set up tools to search for up-to-date information on the web

  • Designing System Structure (Construct the Graph) Design how our system will work and in what order

  • Completing the System (Compile Graph) Turn our design into a working system

  • Testing in Action (Use Graph) Run our completed system and verify it works properly

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.

[NOTE]

This is not necessary if you've already set the required API keys in previous steps.

Why Adaptive RAG?

Let's say an astrophysicist asks these questions to an AI chatbot:

  1. "Sagittarius A* just released an unprecedented burst of X-rays - why is this significant?"

  2. "Can you analyze how Sagittarius A*'s X-ray emission patterns have changed over the past 5 years?"

While a conventional RAG system would approach information retrieval the same way every time, Adaptive RAG recognizes the different nature of these questions and responds accordingly:

  • Current X-ray burst โ†’ Search the web for real-time observations and urgent analysis from scientists

  • X-ray emission patterns โ†’ Analyze 5 years of observational records from our astrophysics database

  • Ability to self-correct if the answer is inaccurate

Adaptive RAG is a RAG strategy that combines (1) query analysis and (2) Self-Reflective RAG.

The paper "Adaptive-RAG: Learning to Adapt Retrieval-Augmented Large Language Models through Question Complexity" performs routing through query analysis in the following ways:

  • No Retrieval

  • Single-shot RAG

  • Iterative RAG

We implement this using LangGraph. In this implementation, we perform the following routing:

  • Web Search : Used for questions about recent events

  • Self-correcting RAG : Used for questions about indexed content

Image Description

Creating a Basic PDF-based Retrieval Chain

Here we create a Retrieval Chain based on PDF documents. This is the most basic structure of a Retrieval Chain. Note that in LangGraph, we create the Retriever and Chain separately. This allows us to process each node in detail.

Note

  • Since this was covered in the previous tutorial, we'll skip the detailed explanations.

Query Routing and Document Evaluation

The LLMs stage performs query routing and document evaluation . This process is a crucial part of Adaptive RAG , contributing to efficient information retrieval and generation.

  • Query Routing : Analyzes user queries to route them to appropriate information sources. This enables setting the optimal search path based on the query's purpose.

  • Document Evaluation : Assesses the quality and relevance of retrieved documents to enhance the accuracy of final results. This process is essential for maximizing the performance of LLMs .

This stage supports the core functionality of Adaptive RAG and aims to provide accurate and reliable information.

Now let's pass questions to our rag_chain to generate responses.

Retrieval Grader

Using the created retrieval_grader to evaluate the document retrieval results.

Creating RAG Chain for Answer Generation

Now we'll use our rag_chain to generate responses by passing questions to it.

Adding Hallucination Checker for Responses

Evaluate hallucinations in the generated response using our created hallucination_grader .

Query Rewriter

Create an enhanced question by submitting a query to the generated question_rewriter .

Tools

Web Search Tool

The Web Search Tool is a critical component of Adaptive RAG , used to retrieve the latest information. This tool supports users in obtaining quick and accurate answers to questions related to recent events.

  • Setup : Prepare the web search tool to search for the most current information.

  • Perform Search : Search the web for relevant information based on the user's query.

  • Result Analysis : Analyze the retrieved search results to provide the most appropriate information for the user's question.

Run the web search tool and check the results.

Graph Construction

Defining graph states

Define Graph Flows

Graph Flow is defined to clarify the operation of Adaptive RAG . In this stage, the graph's state and transitions are established to enhance query processing efficiency.

  • State Definition : Clearly define each state of the graph to track the progression of the query.

  • Transition Setup : Configure transitions between states to ensure the query follows the appropriate path.

  • Flow Optimization : Optimize the graph's flow to improve the accuracy of information retrieval and generation.

Define Nodes

Define the nodes to be utilized:

  • retrieve : Document retrieval node

  • generate : Answer generation node

  • grade_documents : Document relevance evaluation node

  • transform_query : Question rewriting node

  • web_search : Web search node

  • route_question : Question routing node

  • decide_to_generate : Answer generation decision node

  • hallucination_check : Hallucination assessment node

Define Edges

Graph Compilation

In the Graph Compilation stage, the Adaptive RAG workflow is constructed and brought to an executable state. This process defines the entire query processing flow by connecting each node and edge of the graph.

  • Node Definition : Define each node to clarify the graph's states and transitions.

  • Edge Configuration : Set up edges between nodes to ensure the query follows the appropriate path.

  • Workflow Construction : Build the overall graph flow to maximize the efficiency of information retrieval and generation.

png

[NOTE] Description

Image Description

Here's an explanation of the diagram based on the Graph Compilation context:

This diagram represents an Adaptive RAG (Retrieval-Augmented Generation) workflow, showing how nodes and edges are connected in the query processing flow:

  1. Node Definition:

    • _start_ and _end_ define entry and exit states

    • web_search handles web-based information retrieval

    • Generate manages content generation

    • transform_query processes and adapts queries

    • retrieve fetches relevant information

    • grade_documents evaluates document quality

    • vectorstore manages vector-based information storage

  2. Edge Configuration:

    • Solid arrows represent direct transitions between nodes

    • Dashed arrows indicate conditional paths and feedback loops

    • Labels like 'not relevant', 'hallucination', and 'relevant' define transition conditions

  3. Workflow Construction:

    • Primary path splits into web search and vector retrieval branches

    • Multiple feedback loops ensure quality control

    • Integration between retrieval (web_search, retrieve) and generation (Generate) components

    • Document grading system (grade_documents) provides quality assurance

Graph Utilization

In the Graph Utilization stage, the execution of Adaptive RAG is used to verify query processing results. This process follows the nodes and edges of the graph to generate the final outcome.

  • Graph Execution : Execute the defined graph by following the query's flow.

  • Result Verification : Review the generated results after graph execution to confirm that the query was processed appropriately.

  • Result Analysis : Analyze the generated results to evaluate their alignment with the query's objectives.

Last updated