ग्राफ में एक टूल जोड़ें
आपका Wikipedia टूल तैयार है. अब इसे अपने chatbot के ग्राफ वर्कफ़्लो में जोड़ने का समय है! आप ग्राफ वर्कफ़्लो में nodes और edges को संशोधित करेंगे ताकि टूल को एक अतिरिक्त node के रूप में शामिल किया जा सके. आप ग्राफ को इस तरह सेट करेंगे कि chatbot, उपयोगकर्ता की क्वेरी पर निर्भर करते हुए, टूल का उपयोग केवल तभी करे जब ज़रूरत हो. आपके टूल node और उससे जुड़े edges को प्रबंधित करने के लिए निम्न मॉड्यूल आपके लिए इम्पोर्ट किए गए हैं. आपने जो wikipedia_tool बनाया है, वह भी आपके environment में उपलब्ध है. जैसे ही ग्राफ बिल्ड पूरा होगा, आपका ग्राफ LangGraph डायग्राम के रूप में विज़ुअलाइज़ हो जाएगा.
from langgraph.prebuilt import ToolNode, tools_condition
यह अभ्यास पाठ्यक्रम का हिस्सा है
LangChain के साथ Agentic Systems डिज़ाइन करना
अभ्यास निर्देश
.add_node()का उपयोग करके ग्राफ में"chatbot"node जोड़ें और उसेchatbotफ़ंक्शन से लिंक करें.wikipedia_toolकोtoolsके रूप में देकर एकToolNode()बनाएँ और.add_node()से उसे"tools"नाम से ग्राफ में जोड़ें.tools_conditionके आधार पर"chatbot"से रूट करने के लिए.add_conditional_edges()लागू करें.- वर्कफ़्लो पूरा करने के लिए
.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()