ReActエージェントとの会話

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平方単位です。」
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})
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.'}
LangChainで設計するエージェント型システム

フォローアップ質問

  • フォローアップ:
    • ユーザー: 「辺が12と14のものは?」
  • 会話履歴:
    • ユーザー: 「辺が5と7の長方形の面積は?」
    • エージェント: 「辺が5と7の長方形の面積は35平方単位です。」
    • ユーザー: 「辺が12と14のものは?」
    • エージェント: 「辺が12と14の長方形の面積は168平方単位です。」
  • 出力
    • ユーザー: 「辺が12と14のものは?」
    • エージェント: 「辺が12と14の長方形の面積は168平方単位です。」
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.']}
LangChainで設計するエージェント型システム

会話履歴

from langchain_core.messages import 
HumanMessage, AIMessage












LangChainで設計するエージェント型システム

会話履歴

from langchain_core.messages import 
HumanMessage, AIMessage


message_history = messages["messages"]

「メッセージ履歴」を使った線形会話設定の開始。

LangChainで設計するエージェント型システム

会話履歴

from langchain_core.messages import 
HumanMessage, AIMessage

message_history = messages["messages"]

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

新しいクエリを追加した会話設定。

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

アプリ呼び出しを追加した会話設定。

LangChainで設計するエージェント型システム

会話履歴

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








メッセージフィルタリングを追加した会話設定。

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

メッセージ出力を追加した会話設定。

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.']}
LangChainで設計するエージェント型システム

さあ、練習しましょう!

LangChainで設計するエージェント型システム

Preparing Video For Download...