02. Build chatbots using LangGraph
first LangGraph Let's make a simple chatbot using. This chatbot will respond directly to user messages. Although simple, LangGraph It will explain the key concepts you build with. At the end of this section, you will be building a basic chatbot.
StateGraph Start by generating. StateGraph The object defines the structure of the chatbot as a "State Machine".
nodes Chattbot can call by adding llm And functions, edges Add to specify how the bot should switch between these functions.
# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv
# Load API key information
load_dotenv() True # LangSmith set up tracking. https://smith.langchain.com
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging
# Enter a project name.
logging.langsmith("CH17-LangGraph") Start tracking LangSmith.
[Project name]
CH17-LangGraph Step-by-Step Understanding the concept!
STEP 1. State definition
STEP 2. Node definition
Next " chatbot "Add a node.
Nodes represent units of work, usually regular Python Function.
STEP 3. Graph definition, add node
Reference
chatbotNode function is currentlyStateInput as input and updated under the key "messages"messagesReturns a dictionary (TypedDict) containing a list.Stateofadd_messagesThe function adds a llm response message to a message that is already in state.
STEP 4. Add graph edge
next, START Add a branch. START Whenever the graph is executed Where to start working is.
Likewise, END Set the point. This represents the end (end point) of the graph flow.
STEP 5. Graph compilation (compile)
Finally, you should be able to run the graph. For this, in the graph builder" compile() Call ". This can be called from the state" CompiledGraph "Is created.
STEP 6. Graph visualization
Now let's visualize the graph.

STEP 7. Graph execution
Let's run the chatbot now!
character! This was the most basic chatbot building.
Below is the full code that organized the previous process.
Full code

Last updated