Building a Basic Chatbot with LangGraph
Author: r14minji
Peer Review:
Proofread : Chaeyoon Kim
This is a part of LangChain Open Tutorial
Overview
In this document, we explore how to build a simple chatbot using LangGraph, a framework that allows the creation of state machine-based chatbots. The process is broken down into manageable steps, covering key concepts like defining states, creating nodes, adding edges, compiling the graph, visualizing the chatbot structure, and finally executing the chatbot.
Table of Contents
References
Environment Setup
Setting up your environment is the first step. See the Environment Setup guide for more details.
[Note]
The langchain-opentutorial is a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials.
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.
Step-by-Step: Understanding the Concepts
By following these steps, you will have a functional and clearly structured chatbot built with LangGraph!
STEP 1. Defining the State
We start by defining the state of the chatbot using Python's
TypedDict. The state keeps track of messages, which are updated as the conversation progresses.
STEP 2. Defining the Nodes
Add a "chatbot" node.
Nodes represent individual units of work and are typically implemented as regular Python functions.
STEP 3. Defining the Graph and Add Nodes
Create a graph and include the defined nodes.
The "chatbot" node function takes the current State as input and returns a dictionary (
TypedDict) containing the updated list of messages under the key "messages".The
add_messagesfunction in the State appends the LLM's response to the existing messages in the state.
STEP 4. Adding Graph Edges
Define the flow of the graph by adding edges:
START: Specifies the entry point where the graph execution begins.
END: Marks the termination point of the graph's flow.
STEP 5. Compiling the Graph
To execute the graph, call the
compile()method using the graph builder.This generates a
CompiledGraph, which can be invoked during runtime to process states.
STEP 6. Visualizing the Graph
Visualizing the graph helps you understand its structure and how different nodes and edges are connected.

STEP 7. Running the Graph
Let's run the chatbot with a sample question!
Complete Code
This code provides the full process of creating a simple chatbot using LangGraph, including defining the state, nodes, edges, compiling the graph, visualizing it, and running it with a user query.

Last updated