Creating a ReAct agent
Now that you've learned the basic components of LangChain, you'll jump right in and create a ReAct agent that can count how many 'r's there are in any word with the tool count_r_in_word.
The following have been loaded for you: tool, ChatOpenAI, create_react_agent, math, and model.
This exercise is part of the course
Designing Agentic Systems with LangChain
Exercise instructions
- Set up the agent
appusingcreate_react_agent()by passing in themodeland thecount_r_in_wordto the list of tools. - Define a
queryvariable that accepts the user's question as a string. - Invoke the
appwith.invoke()and pass a dictionary with a"messages"key, labeling thequeryas"human". - Access the last message in the
responseand print its.contentattribute to get the agent's answer.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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'][____].____)