與 ReAct 代理的對話

Designing Agentic Systems with LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

對話

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

  • 使用者:「邊長為 5 和 7 的長方形面積是多少?」

  • 代理:「邊長為 5 和 7 的長方形面積是 35 平方單位。」
Designing Agentic Systems with LangChain

對話

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})
Designing Agentic Systems with LangChain

對話輸出

{'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.'}
Designing Agentic Systems with LangChain

追問問題

  • 追問:
    • 使用者:「那邊長為 12 和 14 的呢?」
  • 對話紀錄:
    • 使用者:「邊長為 5 和 7 的長方形面積是多少?」
    • 代理:「邊長為 5 和 7 的長方形面積是 35 平方單位。」
    • 使用者:「那邊長為 12 和 14 的呢?」
    • 代理:「邊長為 12 和 14 的長方形面積是 168 平方單位。」
  • 輸出
    • 使用者:「那邊長為 12 和 14 的呢?」
    • 代理:「邊長為 12 和 14 的長方形面積是 168 平方單位。」
Designing Agentic Systems with LangChain

追問問題

{'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.']}
Designing Agentic Systems with LangChain

對話紀錄

from langchain_core.messages import 
HumanMessage, AIMessage












Designing Agentic Systems with LangChain

對話紀錄

from langchain_core.messages import 
HumanMessage, AIMessage


message_history = messages["messages"]

以「message history」開始線性對話設定。

Designing Agentic Systems with LangChain

對話紀錄

from langchain_core.messages import 
HumanMessage, AIMessage

message_history = messages["messages"]

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

加入新問題後的對話設定。

Designing Agentic Systems with LangChain

對話紀錄

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

呼叫應用程式後更新的對話設定。

Designing Agentic Systems with LangChain

對話紀錄

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








套用訊息過濾後的對話設定。

Designing Agentic Systems with LangChain

對話紀錄

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

印出訊息後的對話設定。

Designing Agentic Systems with LangChain

對話紀錄輸出

{'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.']}
Designing Agentic Systems with LangChain

一起來練習吧!

Designing Agentic Systems with LangChain

Preparing Video For Download...