LangGraph-Add-Query-Rewrite

Open in ColabOpen in GitHub

Overview

In this tutorial, we will cover the process of restructuring the original question by incorporating a Query Rewrite step. When a user's question is received, it enables more effective searching. While using Naive RAG as a foundation, this process aims for us to add supplementary mechanisms through query rewriting and web search. The procedure is as follows:

  1. Perform Naive RAG

  2. Conduct Groundedness Check for Retrieved Documents (Groundedness Check)

  3. Web Search

  4. (This Tutorial) Query Rewrite

[Note] As this builds upon a previous tutorial, there may be overlapping content. For any missing explanations, please refer to the previous tutorial.

image.png

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.

Creating a Basic PDF-based Retrieval Chain

Here, we create a Retrieval Chain based on PDF documents. This is the simplest structure of a Retrieval Chain.

However, in LangGraph, the Retriever and Chain are created separately. This allows for detailed processing at each node.

[Note]

Detailed explanations for this step can be found in the previous tutorial.

Defining GraphState

The GraphState object defines the shared state between nodes in the Langgraph.

Generally, using a TypedDict is to ensure type safety and maintain a clear structure for the graph's state management.

In this case, we'll add a field for relevance check results to the state.

[Note]

Here, we define question as a list to accomodate the rewritten queries that are additionally stored.

Defining Nodes

Nodes represent individual processing steps. They are typically implemented as Python functions. Both input and output are state values.

[Note]

Each Node takes a state as input, performs its designated logic, and returns an updated state.

Adding the Query Rewrite Node

The Query Rewrite Node rewrites the original questions, using a prompt designed for query reformulation.

This node utilizes a question_rewriter function to rewrite the question.

Defining Edges

Edges determine the next Node to execute based on the current state.

There are different types such as regular edges and conditional edges.

Let's visualize the compiled graph structure.

png

Running Graph

  • config parameter: Passes configuration settings required for running the graph.

  • recursion_limit: Sets a limit on the number of recursions when running the graph.

  • inputs: Passes the input information required for running the graph.

If the relevance_check of the initial search results fails, it performs web search and provides web search results.

The evaluator summarized above follows this.

Last updated