Conversație cu un agent ReAct

Proiectarea sistemelor agentice cu LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Conversație

The area of a rectangle with sides 5 and 7 is 35 square units.
  • Validarea răspunsurilor

  • Utilizator: „Care este aria unui dreptunghi cu laturile 5 și 7?"

  • Agent: „Aria unui dreptunghi cu laturile 5 și 7 este de 35 de unități pătrate."
Proiectarea sistemelor agentice cu LangChain

Conversație

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})
Proiectarea sistemelor agentice cu LangChain

Rezultatul conversației

{'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.'}
Proiectarea sistemelor agentice cu LangChain

Întrebări de urmărire

  • Urmărire:
    • Utilizator: „Ce se întâmplă cu unul cu laturile 12 și 14?"
  • Istoricul conversației:
    • Utilizator: „Care este aria unui dreptunghi cu laturile 5 și 7?"
    • Agent: „Aria unui dreptunghi cu laturile 5 și 7 este de 35 de unități pătrate."
    • Utilizator: „Ce se întâmplă cu unul cu laturile 12 și 14?"
    • Agent: „Aria unui dreptunghi cu laturile 12 și 14 este de 168 de unități pătrate."
  • Rezultat
    • Utilizator: „Ce se întâmplă cu unul cu laturile 12 și 14?"
    • Agent: „Aria unui dreptunghi cu laturile 12 și 14 este de 168 de unități pătrate."
Proiectarea sistemelor agentice cu LangChain

Întrebări de urmărire

{'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.']}
Proiectarea sistemelor agentice cu LangChain

Istoricul conversației

from langchain_core.messages import 
HumanMessage, AIMessage












Proiectarea sistemelor agentice cu LangChain

Istoricul conversației

from langchain_core.messages import 
HumanMessage, AIMessage


message_history = messages["messages"]

Începutul configurării conversației liniare cu „istoricul mesajelor".

Proiectarea sistemelor agentice cu LangChain

Istoricul conversației

from langchain_core.messages import 
HumanMessage, AIMessage

message_history = messages["messages"]

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

Configurarea conversației actualizată cu noua interogare.

Proiectarea sistemelor agentice cu LangChain

Istoricul conversației

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

Configurarea conversației actualizată cu invocarea aplicației.

Proiectarea sistemelor agentice cu LangChain

Istoricul conversației

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








Configurarea conversației actualizată cu filtrarea mesajelor.

Proiectarea sistemelor agentice cu LangChain

Istoricul conversației

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

Configurarea conversației actualizată cu afișarea mesajelor.

Proiectarea sistemelor agentice cu LangChain

Rezultatul istoricului conversației

{'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.']}
Proiectarea sistemelor agentice cu LangChain

Să exersăm!

Proiectarea sistemelor agentice cu LangChain

Preparing Video For Download...