Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Designing Agentic Systems with LangChain

Cursus bekijken

Oefeninstructies

  • Define the chatbot function by using the llm.invoke() method on the current "messages" in state and return its response.
  • Use .add_node() to add a node named "chatbot" to the graph that references the chatbot function.
  • Connect the START node to the "chatbot" node and "chatbot" to the END node using .add_edge() to define the edges for the conversation.
  • Compile the graph using .compile() to prepare it for execution.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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.____()
Code bewerken en uitvoeren