1. Learn
  2. /
  3. Courses
  4. /
  5. Designing Agentic Systems with LangChain

Connected

Exercise

Add a tool to a graph

You've got your Wikipedia tool ready to go. Now it's time to incorporate it into your chatbot's graph workflow! You'll modify the nodes and edges in the graph workflow to incorporate the tool as an additional node. You'll set up the graph so that the chatbot can use the tool only when needed depending on the user's query. To manage your tool node and its associated edges, the following modules have been imported for you. The wikipedia_tool you built is also available in your environment. Once the graph build is complete, your graph will be visualized for you as a LangGraph diagram.

from langgraph.prebuilt import ToolNode, tools_condition

Instructions

100 XP
  • Use .add_node() to add the "chatbot" node to the graph, linking it to the chatbot function.
  • Create a ToolNode() with wikipedia_tool as tools and use .add_node() to add it to the graph as "tools".
  • Apply .add_conditional_edges() to route from "chatbot" based on the tools_condition.
  • Use .add_edge() to connect "tools" back to "chatbot", START to "chatbot", and "chatbot" to END to complete the workflow.