Conversazione con un agente ReAct

Progettare sistemi agentici con LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Conversazione

The area of a rectangle with sides 5 and 7 is 35 square units.
  • Convalida delle risposte

  • Utente: "Qual è l'area di un rettangolo con lati 5 e 7?"

  • Agente: "L'area di un rettangolo con lati 5 e 7 è 35 unità quadrate."
Progettare sistemi agentici con LangChain

Conversazione

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})
Progettare sistemi agentici con LangChain

Risultato della conversazione

{'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.'}
Progettare sistemi agentici con LangChain

Domande di follow-up

  • Follow-up:
    • Utente: "E per uno con lati 12 e 14?"
  • Cronologia conversazione:
    • Utente: "Qual è l'area di un rettangolo con lati 5 e 7?"
    • Agente: "L'area di un rettangolo con lati 5 e 7 è 35 unità quadrate."
    • Utente: "E per uno con lati 12 e 14?"
    • Agente: "L'area di un rettangolo con lati 12 e 14 è 168 unità quadrate."
  • Output
    • Utente: "E per uno con lati 12 e 14?"
    • Agente: "L'area di un rettangolo con lati 12 e 14 è 168 unità quadrate."
Progettare sistemi agentici con LangChain

Domande di follow-up

{'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.']}
Progettare sistemi agentici con LangChain

Cronologia conversazione

from langchain_core.messages import 
HumanMessage, AIMessage












Progettare sistemi agentici con LangChain

Cronologia conversazione

from langchain_core.messages import 
HumanMessage, AIMessage


message_history = messages["messages"]

Inizio della configurazione di una conversazione lineare con "cronologia messaggi".

Progettare sistemi agentici con LangChain

Cronologia conversazione

from langchain_core.messages import 
HumanMessage, AIMessage

message_history = messages["messages"]

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

Configurazione della conversazione aggiornata con nuova query.

Progettare sistemi agentici con LangChain

Cronologia conversazione

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

Configurazione della conversazione aggiornata con invocazione dell'app.

Progettare sistemi agentici con LangChain

Cronologia conversazione

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








Configurazione della conversazione aggiornata con filtro dei messaggi.

Progettare sistemi agentici con LangChain

Cronologia conversazione

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

Configurazione della conversazione aggiornata con stampa dei messaggi.

Progettare sistemi agentici con LangChain

Output cronologia conversazione

{'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.']}
Progettare sistemi agentici con LangChain

Esercitiamoci!

Progettare sistemi agentici con LangChain

Preparing Video For Download...