LangGraph-Building-Graphs

Open in ColabOpen in GitHub

Overview

In this tutorial, you will learn how to use LangGraph to create foundational graph structures.

You will learn the following:

  1. The steps to define a graph

  2. How to use conditional edges and different flow variations

  3. Re-search graph structure

  4. Multi-LLM graph structure

  5. Query rewrite graph structure

  6. 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-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can check out the langchain-opentutorial for 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 .

png

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:

  1. Re-search graph structure

  2. Multi-LLM graph structure

  3. Query rewrite graph structure

  4. 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_results node.

  • The GPT_relevance_check node checks the relevance of the output from the GPT_request node.

  • Based on the result of the relevance check, the Aggregation_results node decides whether to re_search or exit using the State information.

png

Multi-LLM Graph Structure

The Multi-LLM graph uses various LLM models to generate results.

This allows for obtaining a variety of answers.

png

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.

png

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.

png

Last updated