ReAct एजेंट के साथ बातचीत

LangChain के साथ Agentic Systems डिज़ाइन करना

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

बातचीत

5 और 7 भुजाओं वाले आयत का क्षेत्रफल 35 वर्ग इकाई है.
  • उत्तर जाँचना

  • User: "5 और 7 भुजाओं वाले आयत का क्षेत्रफल क्या है?"

  • Agent: "5 और 7 भुजाओं वाले आयत का क्षेत्रफल 35 वर्ग इकाई है."
LangChain के साथ Agentic Systems डिज़ाइन करना

बातचीत

tools = [rectangle_area]
query = "What is the area of a rectangle with sides 14 and 4?"


# Create the ReAct agent app = create_react_agent(model, tools)
# Invoke the agent with a query and store the messages response = app.invoke({"messages": [("human", query)]})
# Define and print the input and output messages print({ "user_input": query, "agent_output": response["messages"][-1].content})
LangChain के साथ Agentic Systems डिज़ाइन करना

बातचीत का आउटपुट

{'user_input': 'What is the area of a rectangle with sides 14 and 4?',
 'agent_output': 'The area of a rectangle with sides 14 and 4 is 56 
 square units.'}
LangChain के साथ Agentic Systems डिज़ाइन करना

फॉलो-अप प्रश्न

  • फॉलो-अप:
    • User: "12 और 14 भुजाओं वाले का क्या?"
  • बातचीत इतिहास:
    • User: "5 और 7 भुजाओं वाले आयत का क्षेत्रफल क्या है?"
    • Agent: "5 और 7 भुजाओं वाले आयत का क्षेत्रफल 35 वर्ग इकाई है."
    • User: "12 और 14 भुजाओं वाले का क्या?"
    • Agent: "12 और 14 भुजाओं वाले आयत का क्षेत्रफल 168 वर्ग इकाई है."
  • आउटपुट
    • User: "12 और 14 भुजाओं वाले का क्या?"
    • Agent: "12 और 14 भुजाओं वाले आयत का क्षेत्रफल 168 वर्ग इकाई है."
LangChain के साथ Agentic Systems डिज़ाइन करना

फॉलो-अप प्रश्न

{'user_input': 'What about one with sides 12 and 14?',

'agent_output': ['HumanMessage: What is the area of a rectangle with sides 5 and 7?', 'AIMessage: The area of a rectangle with sides 5 and 7 is 35 square units.', 'HumanMessage: What about one with sides 12 and 14?', 'AIMessage: The area of a rectangle with sides 12 and 14 is 168 square units.',
'HumanMessage: What about one with sides 12 and 14?', 'AIMessage: The area of a rectangle with sides 12 and 14 is 168 square units.']}
LangChain के साथ Agentic Systems डिज़ाइन करना

बातचीत इतिहास

from langchain_core.messages import 
HumanMessage, AIMessage












LangChain के साथ Agentic Systems डिज़ाइन करना

बातचीत इतिहास

from langchain_core.messages import 
HumanMessage, AIMessage


message_history = messages["messages"]

"message history" के साथ रैखिक बातचीत सेट-अप की शुरुआत.

LangChain के साथ Agentic Systems डिज़ाइन करना

बातचीत इतिहास

from langchain_core.messages import 
HumanMessage, AIMessage

message_history = messages["messages"]

new_query = "What about one with sides 4 and 3?"

नई क्वेरी के साथ बातचीत सेट-अप अपडेट हुआ.

LangChain के साथ Agentic Systems डिज़ाइन करना

बातचीत इतिहास

from langchain_core.messages import 
HumanMessage, AIMessage

message_history = messages["messages"]

new_query = "What about one with sides 4 and 3?"
# Invoke the app with the full message history messages = app.invoke({"messages": message_history + [("human", new_query)]})

ऐप इनवोकेशन के साथ बातचीत सेट-अप अपडेट हुआ.

LangChain के साथ Agentic Systems डिज़ाइन करना

बातचीत इतिहास

# Extract the human and AI messages
filtered_messages = [msg for msg in 
                    messages["messages"] if 
                    isinstance(msg, 
                    (HumanMessage, 
                    AIMessage)) 
                    and msg.content.strip()]








मैसेज फ़िल्टरिंग के साथ बातचीत सेट-अप अपडेट हुआ.

LangChain के साथ Agentic Systems डिज़ाइन करना

बातचीत इतिहास

# Extract the human and AI messages
filtered_messages = [msg for msg in 
                    messages["messages"] if 
                    isinstance(msg, 
                    (HumanMessage, 
                    AIMessage)) 
                    and msg.content.strip()]


# Format and print the final result print({ "user_input": new_query, "agent_output": [f"{msg.__class__.__name__}: {msg.content}" for msg in filtered_messages]})

मैसेज प्रिंटिंग के साथ बातचीत सेट-अप अपडेट हुआ.

LangChain के साथ Agentic Systems डिज़ाइन करना

बातचीत इतिहास आउटपुट

{'user_input': 'What about one with sides 4 and 3?',

'agent_output': ['HumanMessage: What is the area of a rectangle with sides 14 and 4?', 'AIMessage: The area of a rectangle with sides 14 and 4 is 56 square units.', 'HumanMessage: What about one with sides 4 and 3?', 'AIMessage: The area of a rectangle with sides 4 and 3 is 12 square units.',
'HumanMessage: What about one with sides 4 and 3?', 'AIMessage: The area of a rectangle with sides 4 and 3 is 12 square units.']}
LangChain के साथ Agentic Systems डिज़ाइन करना

अभ्यास करते हैं!

LangChain के साथ Agentic Systems डिज़ाइन करना

Preparing Video For Download...