Trò chuyện với một ReAct agent

Thiết kế Hệ thống Agentic với LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Cuộc hội thoại

Diện tích hình chữ nhật có các cạnh 5 và 7 là 35 đơn vị vuông.
  • Xác thực câu trả lời

  • Người dùng: "Diện tích hình chữ nhật có các cạnh 5 và 7 là bao nhiêu?"

  • Agent: "Diện tích hình chữ nhật có các cạnh 5 và 7 là 35 đơn vị vuông."
Thiết kế Hệ thống Agentic với LangChain

Cuộc hội thoại

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})
Thiết kế Hệ thống Agentic với LangChain

Kết quả hội thoại

{'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.'}
Thiết kế Hệ thống Agentic với LangChain

Câu hỏi tiếp theo

  • Câu hỏi tiếp theo:
    • Người dùng: "Thế còn hình có các cạnh 12 và 14?"
  • Lịch sử hội thoại:
    • Người dùng: "Diện tích hình chữ nhật có các cạnh 5 và 7 là bao nhiêu?"
    • Agent: "Diện tích hình chữ nhật có các cạnh 5 và 7 là 35 đơn vị vuông."
    • Người dùng: "Thế còn hình có các cạnh 12 và 14?"
    • Agent: "Diện tích hình chữ nhật có các cạnh 12 và 14 là 168 đơn vị vuông."
  • Đầu ra
    • Người dùng: "Thế còn hình có các cạnh 12 và 14?"
    • Agent: "Diện tích hình chữ nhật có các cạnh 12 và 14 là 168 đơn vị vuông."
Thiết kế Hệ thống Agentic với LangChain

Câu hỏi tiếp theo

{'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.']}
Thiết kế Hệ thống Agentic với LangChain

Lịch sử hội thoại

from langchain_core.messages import 
HumanMessage, AIMessage












Thiết kế Hệ thống Agentic với LangChain

Lịch sử hội thoại

from langchain_core.messages import 
HumanMessage, AIMessage


message_history = messages["messages"]

Bắt đầu thiết lập hội thoại tuyến tính với "message history".

Thiết kế Hệ thống Agentic với LangChain

Lịch sử hội thoại

from langchain_core.messages import 
HumanMessage, AIMessage

message_history = messages["messages"]

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

Thiết lập hội thoại được cập nhật với truy vấn mới.

Thiết kế Hệ thống Agentic với LangChain

Lịch sử hội thoại

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)]})

Thiết lập hội thoại được cập nhật khi gọi app.

Thiết kế Hệ thống Agentic với LangChain

Lịch sử hội thoại

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








Thiết lập hội thoại được cập nhật với bước lọc thông điệp.

Thiết kế Hệ thống Agentic với LangChain

Lịch sử hội thoại

# 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]})

Thiết lập hội thoại được cập nhật với bước in thông điệp.

Thiết kế Hệ thống Agentic với LangChain

Đầu ra lịch sử hội thoại

{'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.']}
Thiết kế Hệ thống Agentic với LangChain

Cùng luyện tập nào!

Thiết kế Hệ thống Agentic với LangChain

Preparing Video For Download...