07. Adaptive RAG

Adaptive RAG

This tutorial covers the implementation of Adaptive RAG (Adaptive Retrieval-Augmented Generation). Adaptive RAG is a strategy that combines query analysis with active/self-modifying RAGs to retrieve and generate information from various data sources. This tutorial uses LangGraph to implement routing between web browsing and self-correcting RAGs.

The purpose of this tutorial is for users to understand the concept of Adaptive RAG and learn how to implement it through LangGraph. This allows users to perform web searches for questions related to the latest events, and utilize self-correcting RAGs for questions related to indexes.

Mainly covered

  • Create Index : Index creation and document loading

  • LLMs : Routing queries and document evaluation using LLM

  • Web Search Tool : Web search tool settings

  • Construct the Graph : Graph status and flow definition

  • Compile Graph : Graph compilation and walkflow building

  • Use Graph : Graph execution and results verification


Adaptive RAG has RAG As a strategy, (1) Query analysis and (2) Self-Reflective RAG Combine.

Thesis: Adaptive-RAG: Learning to Adapt Retrieval-Augmented Large Language Models through Question Complexity In, the following routing is performed through query analysis.

  • No Retrieval

  • Single-shot RAG

  • Iterative RAG

Implement this using LangGraph.

In this implementation, we perform the following routing:

  • Web search : Used for questions related to the latest events

  • Magnetic modification RAG : Used for questions related to index

Preferences

Basic PDF-based Retrieval Chain creation

Here, we create a Retrieval Chain based on PDF documents. Retrieval Chain with the simplest structure.

However, LangGraph creates Retirever and Chain separately. Only then can you do detailed processing for each node.

Reference

  • As covered in the previous tutorial, we omit the detailed description.

Query routing and document evaluation

LLMs In phase Query routing and Document evaluation Perform. This course Adaptive RAG As an important part of, it contributes to efficient information retrieval and creation.

  • Query routing : Analyze the user's query and route it to the appropriate source of information. This allows you to set the optimal search path for your query's purpose.

  • Document evaluation : Evaluate the quality and relevance of the searched documents to increase the accuracy of the final result. This course LLMs It is essential to maximize the performance of.

This step Adaptive RAG It supports the core functions of and aims to provide accurate and reliable information.

The following tests the results of query routing and then checks the results.

Search Evaluator (Retrieval Grader)

Created retrieval_grader Use to evaluate document search results.

Create RAG chain to generate answers

Now created rag_chain Pass the question to create an answer.

Add Hallucination checker to answer

Created hallucination_grader Use to evaluate the hallucinations of the generated answers.

Query Rewriter

Created question_rewriter Pass the question to create an improved question.

Web search tools

Web search tools has Adaptive RAG An important component of, used to retrieve the latest information. This tool helps users get quick and accurate answers to questions related to the latest events.

  • Settings : Set up a web search tool to prepare you for the latest information.

  • Search : Search for relevant information on the web based on your query.

  • Results analysis : Analyze the searched results to provide the best information for your questions.

Run the web search tool to check the results.

Graph configuration

Graph status definition

Graph flow definition

Graph flow By defining Adaptive RAG Clarify how it works. At this stage, you set the state and transition of the graph to increase the efficiency of query processing.

  • Status definition : Clearly define each state of the graph to track the progress of the query.

  • Switch settings : Set the transition between states so that the query proceeds along the appropriate path.

  • Flow optimization : Optimize the flow of graphs to improve the accuracy of information retrieval and creation.

Node definition

Edge definition

Graph compilation

Graph compilation In phase Adaptive RAG Build your workflow and make it viable. This process defines the overall flow of query processing by connecting each node and edge of the graph.

  • Node definition : Define each node to clarify the state and transition of the graph.

  • Edge setting : Set the edge between the nodes so that the query proceeds along the appropriate path.

  • Build a walkflow : Build

    • the entire flow of graphs to maximize the efficiency of information retrieval and creation.

Using graph

Using graph In phase Adaptive RAG Check the query processing results through the execution of. This process processes the query along each node and edge of the graph to produce the final result.

  • Graph execution : Run the defined graph to follow the flow of the query.

  • Check results : Review the results generated after running the graph to ensure that the query was properly processed.

  • Results analysis : Analyze the generated results to evaluate whether they meet the purpose of the query.

Last updated