Диалог с агентом 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...