Creare un agente ReAct
Ora che hai visto i componenti di base di LangChain, passiamo subito alla pratica e crea un agente ReAct che sappia contare quante "r" ci sono in qualsiasi parola usando lo strumento count_r_in_word.
Sono già stati caricati per te: tool, ChatOpenAI, create_react_agent, math e model.
Questo esercizio fa parte del corso
Progettare sistemi agentici con LangChain
Istruzioni dell'esercizio
- Configura l'agente
appusandocreate_react_agent()passandomodele includendocount_r_in_wordnell'elenco degli strumenti. - Definisci una variabile
queryche accetti la domanda dell'utente come stringa. - Richiama
appcon.invoke()e passa un dizionario con la chiave"messages", etichettando laquerycome"human". - Accedi all'ultimo messaggio in
responsee stampa il suo attributo.contentper ottenere la risposta dell'agente.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Create the agent
____ = ____(model=____, tools=[____])
# Create a query
____ = "How many r's are in the word 'Terrarium'?"
# Invoke the agent and store the response
response = app.____({"messages": [("human", ____)]})
# Print the agent's response
print(____['messages'][____].____)