LangGraph-Building-Graphs
Author: Donghak Lee
Peer Review:
Proofread : Chaeyoon Kim
This is a part of LangChain Open Tutorial
Overview
In this tutorial, you will learn how to use LangGraph to create foundational graph structures.
You will learn the following:
The steps to define a graph
How to use conditional edges and different flow variations
Re-search graph structure
Multi-LLM graph structure
Query rewrite graph structure
SQL RAG graph structure
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 check out the
langchain-opentutorialfor more details.
You can set API keys in a .env file or set them manually.
[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.
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.
Steps for Defining a Graph
To define a graph with LangGraph, you need to define State , Node , and Graph , and then compile them.
If necessary, you can flexibly adjust the graph flow by adding conditional edges to nodes using add_conditional_edges().
Define State
State defines the shared state between the nodes in the graph.
It uses the TypedDict format and adds metadata to type hints using Annotated to provide detailed information.
Define Node
Define the nodes that process each step.
These are usually implemented as Python functions, with State as both input and output.
Define Graph
Connect nodes with Edge .
Using conditional edges, you can determine the next Node to execute based on the current State .

Various Graph Structures
In this section, you will learn about different graph structures using conditional edges.
The graph structures you will learn are as follows:
Re-search graph structure
Multi-LLM graph structure
Query rewrite graph structure
SQL RAG graph structure
Re-search Graph Structure
The Re-search Graph inspects the output from the GPT model and selects either re_search or exit. This allows you to obtain more relevant results for the query.
The execution flow is as follows:
A conditional edge is added to the
Aggregation_resultsnode.The
GPT_relevance_checknode checks the relevance of the output from theGPT_requestnode.Based on the result of the relevance check, the
Aggregation_resultsnode decides whether tore_searchorexitusing the State information.

Multi-LLM Graph Structure
The Multi-LLM graph uses various LLM models to generate results.
This allows for obtaining a variety of answers.

Query Rewrite Graph
The Query Rewrite Graph is a structure that adds the rewrite_query node to the Re-search Graph structure.
The rewrite node for the query rewrites the question to obtain more refined results.

SQL RAG Graph Structure
The SQL RAG Graph is a structure that combines Conventional RAG with SQL RAG.
It uses rewrite nodes for the question and query to generate precise results based on the requirements.

Last updated