03. 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.
Copy
# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv
# Load API key information
load_dotenv()Copy
True Copy
# 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")Copy
Step-by-Step Understanding the concept!
STEP 1. State definition
Copy
STEP 2. Node definition
Next " chatbot "Add a node.
Nodes represent units of work, usually regular Python Function.
Copy
STEP 3. Graph definition, add node
Copy
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.
Copy
Likewise, END Set the point. This represents the end (end point) of the graph flow.
Copy
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.
Copy
STEP 6. Graph visualization
Now let's visualize the graph.
Copy

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