Konverzace s agentem ReAct

Návrh agentních systémů s LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Konverzace

The area of a rectangle with sides 5 and 7 is 35 square units.
  • Ověřování odpovědí

  • Uživatel: „Jaký je obsah obdélníku se stranami 5 a 7?“

  • Agent: „Obsah obdélníku se stranami 5 a 7 je 35 čtverečních jednotek.“
Návrh agentních systémů s LangChain

Konverzace

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})
Návrh agentních systémů s LangChain

Výstup konverzace

{'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.'}
Návrh agentních systémů s LangChain

Doplňující dotazy

  • Doplňující dotaz:
    • Uživatel: „A co obdélník se stranami 12 a 14?“
  • Historie konverzace:
    • Uživatel: „Jaký je obsah obdélníku se stranami 5 a 7?“
    • Agent: „Obsah obdélníku se stranami 5 a 7 je 35 čtverečních jednotek.“
    • Uživatel: „A co obdélník se stranami 12 a 14?“
    • Agent: „Obsah obdélníku se stranami 12 a 14 je 168 čtverečních jednotek.“
  • Výstup
    • Uživatel: „A co obdélník se stranami 12 a 14?“
    • Agent: „Obsah obdélníku se stranami 12 a 14 je 168 čtverečních jednotek.“
Návrh agentních systémů s LangChain

Doplňující dotazy

{'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.']}
Návrh agentních systémů s LangChain

Historie konverzace

from langchain_core.messages import 
HumanMessage, AIMessage












Návrh agentních systémů s LangChain

Historie konverzace

from langchain_core.messages import 
HumanMessage, AIMessage


message_history = messages["messages"]

Začátek lineárního nastavení konverzace s „historií zpráv“.

Návrh agentních systémů s LangChain

Historie konverzace

from langchain_core.messages import 
HumanMessage, AIMessage

message_history = messages["messages"]

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

Nastavení konverzace aktualizované novým dotazem.

Návrh agentních systémů s LangChain

Historie konverzace

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

Nastavení konverzace aktualizované o vyvolání aplikace.

Návrh agentních systémů s LangChain

Historie konverzace

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








Nastavení konverzace aktualizované o filtrování zpráv.

Návrh agentních systémů s LangChain

Historie konverzace

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

Nastavení konverzace aktualizované o tisk zpráv.

Návrh agentních systémů s LangChain

Výstup historie konverzace

{'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.']}
Návrh agentních systémů s LangChain

Pojďme cvičit!

Návrh agentních systémů s LangChain

Preparing Video For Download...