Get startedGet started for free

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

View Course

Exercise instructions

  • Set up the agent app using create_react_agent() by passing in the model and the count_r_in_word to the list of tools.
  • Define a query variable that accepts the user's question as a string.
  • Invoke the app with .invoke() and pass a dictionary with a "messages" key, labeling the query as "human".
  • Access the last message in the response and print its .content attribute 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'][____].____)
Edit and Run Code