MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Designing Agentic Systems with LangChain

Lihat Kursus

Petunjuk latihan

  • 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.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# 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.____()
Edit dan Jalankan Kode