LangGraph-Add-Query-Rewrite
Author: Sunworl Kim
Design: LeeYuChul
Peer Review:
Proofread : Chaeyoon Kim
This is a part of LangChain Open Tutorial
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:
Perform Naive RAG
Conduct Groundedness Check for Retrieved Documents (Groundedness Check)
Web Search
(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.

Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor 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
GraphStateThe 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.

Running Graph
configparameter: 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