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.
  • 답 검증

  • 사용자: "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으로 에이전트형 시스템 설계하기

대화

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으로 에이전트형 시스템 설계하기

후속 질문

  • 후속 질문:
    • 사용자: "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으로 에이전트형 시스템 설계하기

후속 질문

{'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"]

"message history"로 선형 대화 설정 시작.

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으로 에이전트형 시스템 설계하기

Let's practice!

LangChain으로 에이전트형 시스템 설계하기

Preparing Video For Download...