与 ReAct 代理的对话

使用 LangChain 设计 Agentic 系统

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

对话

The area of a rectangle with sides 5 and 7 is 35 square units.
  • 验证答案

  • 用户:"What is the area of a rectangle with sides 5 and 7?"

  • 代理:"The area of a rectangle with sides 5 and 7 is 35 square units."
使用 LangChain 设计 Agentic 系统

对话

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 系统

对话输出

{'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 系统

追问

  • 追问:
    • 用户:"What about one with sides 12 and 14?"
  • 对话历史:
    • 用户:"What is the area of a rectangle with sides 5 and 7?"
    • 代理:"The area of a rectangle with sides 5 and 7 is 35 square units."
    • 用户:"What about one with sides 12 and 14?"
    • 代理:"The area of a rectangle with sides 12 and 14 is 168 square units."
  • 输出
    • 用户:"What about one with sides 12 and 14?"
    • 代理:"The area of a rectangle with sides 12 and 14 is 168 square units."
使用 LangChain 设计 Agentic 系统

追问

{'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 系统

对话历史

from langchain_core.messages import 
HumanMessage, AIMessage












使用 LangChain 设计 Agentic 系统

对话历史

from langchain_core.messages import 
HumanMessage, AIMessage


message_history = messages["messages"]

线性对话设置的起点,包含"消息历史"。

使用 LangChain 设计 Agentic 系统

对话历史

from langchain_core.messages import 
HumanMessage, AIMessage

message_history = messages["messages"]

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

用新问题更新的对话设置。

使用 LangChain 设计 Agentic 系统

对话历史

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 系统

对话历史

# 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 系统

对话历史

# 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 系统

对话历史输出

{'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 系统

让我们练习吧!

使用 LangChain 设计 Agentic 系统

Preparing Video For Download...