Adding nodes and edges
Now that your StateGraph() is ready, it's time to add your chatbot's nodes to the graph! The pre-built START and END nodes are directly imported from LangGraph, so you'll have just one chatbot node to create. You'll also define the edges that determine the direction of your chatbot's conversation, from start to finish. Once your nodes and edges are added, you'll compile the graph to get it ready to run with a query.
This exercise is part of the course
Designing Agentic Systems with LangChain
Exercise instructions
- Define the
chatbotfunction by using thellm.invoke()method on the current"messages"instateand return its response. - Use
.add_node()to add a node named"chatbot"to the graph that references thechatbotfunction. - Connect the
STARTnode to the"chatbot"node and"chatbot"to theENDnode using.add_edge()to define the edges for the conversation. - Compile the graph using
.compile()to prepare it for execution.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define chatbot function to respond with the model
def chatbot(state: State):
return {"messages": [llm.____(____["____"])]}
# Add chatbot node to the graph
graph_builder.____("____", ____)
# Define the start and end of the conversation flow
graph_builder.____(____, "____")
graph_builder.____("____", ____)
# Compile the graph to prepare for execution
graph = graph_builder.____()