Nodes और edges जोड़ना
अब जबकि आपका StateGraph() तैयार है, समय है कि आप अपने चैटबॉट के nodes ग्राफ में जोड़ें! पहले से बने START और END nodes सीधे LangGraph से इम्पोर्ट होते हैं, इसलिए आपको सिर्फ एक चैटबॉट node बनाना होगा. आप वे edges भी परिभाषित करेंगे जो आपके चैटबॉट की बातचीत की दिशा तय करते हैं—शुरुआत से अंत तक. जैसे ही आप nodes और edges जोड़ लेंगे, आप ग्राफ को compile करेंगे ताकि यह query के साथ रन करने के लिए तैयार हो जाए.
यह अभ्यास पाठ्यक्रम का हिस्सा है
LangChain के साथ Agentic Systems डिज़ाइन करना
अभ्यास निर्देश
chatbotफंक्शन को परिभाषित कीजिए:stateमें मौजूदा"messages"परllm.invoke()मेथड चलाकर response लौटाएँ..add_node()का उपयोग करके"chatbot"नाम का node ग्राफ में जोड़ें, जोchatbotफंक्शन को रेफर करे.- बातचीत के लिए edges तय करने हेतु
.add_edge()सेSTARTnode को"chatbot"node से, और"chatbot"कोENDnode से कनेक्ट करें. - execution के लिए तैयार करने के लिए
.compile()का उपयोग करके ग्राफ को compile करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# 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.____()