開始使用免費開始

將工具加入圖形

你已經準備好 Wikipedia 工具。現在要把它整合進你聊天機器人的圖形工作流程!你將修改圖形工作流程中的節點與邊,把該工具加入為額外的節點。你會設定圖形,讓聊天機器人能依使用者的查詢需要,才使用這個工具。為了管理你的工具節點及其相關邊,以下模組已替你匯入。你先前建立的 wikipedia_tool 也已可在你的環境中使用。當圖形建置完成後,系統會以 LangGraph 圖示為你視覺化顯示。

from langgraph.prebuilt import ToolNode, tools_condition

本練習屬於課程

Designing Agentic Systems with LangChain

檢視課程

練習說明

  • 使用 .add_node()"chatbot" 節點加入圖形,並連結到 chatbot 函式。
  • wikipedia_tool 作為 tools 建立一個 ToolNode(),並使用 .add_node() 將其以 "tools" 名稱加入圖形。
  • 套用 .add_conditional_edges(),根據 tools_condition"chatbot" 進行路由。
  • 使用 .add_edge()"tools" 接回 "chatbot"、將 START 接到 "chatbot",以及將 "chatbot" 接到 END,以完成整個工作流程。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Add chatbot node to the graph
graph_builder.____("____", ____)

# Create a ToolNode to handle tool calls and add it to the graph
tool_node = ____(tools=[____])
graph_builder.____("tools", ____)

# Set up a condition to direct from chatbot to tool or END node
graph_builder.____("____", ____)

# Connect tools back to chatbot and connect START and END nodes
graph_builder.add_edge("____", "____")
graph_builder.add_edge(____, "chatbot")
graph_builder.add_edge("chatbot", ____)

graph = graph_builder.compile()

try:
    display(Image(graph.get_graph().draw_mermaid_png()))

except Exception:
    print("Plot generation failed... falling back to cached asset.")
    display_fallback()
編輯並執行程式碼