Notebooks
G
Google Gemini
Search Wikipedia Using ReAct

Search Wikipedia Using ReAct

gemini-cookbookgemini-apiexamplesgemini
Copyright 2025 Google LLC.
[ ]

Search Wikipedia using ReAct

This notebook is a minimal implementation of ReAct: Synergizing Reasoning and Acting in Language Models with the Google gemini-2.0-flash model. You'll use ReAct prompting to configure a model to search Wikipedia to find the answer to a user's question.

In this walkthrough, you will learn how to:

  1. Set up your development environment and API access to use Gemini.
  2. Use a ReAct few-shot prompt.
  3. Use the newly prompted model for multi-turn conversations (chat).
  4. Connect the model to the Wikipedia API.
  5. Have conversations with the model (try asking it questions like "how tall is the Eiffel Tower?") and watch it search Wikipedia.

Note: The non-source code materials on this page are licensed under Creative Commons - Attribution-ShareAlike CC-BY-SA 4.0, https://creativecommons.org/licenses/by-sa/4.0/legalcode.

Background

ReAct is a prompting method which allows language models to create a trace of their thinking processes and the steps required to answer a user's questions. This improves human interpretability and trustworthiness. ReAct prompted models generate Thought-Action-Observation triplets for every iteration, as you'll soon see. Let's get started!

Setup

[ ]
[ ]

Note: The wikipedia package notes that it was "designed for ease of use and simplicity, not for advanced use", and that production or heavy use should instead "use Pywikipediabot or one of the other more advanced Python MediaWiki API wrappers".

[ ]

To run the following cell, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you don't already have an API key, or you're not sure how to create a Colab Secret, see the Authentication quickstart for an example.

[ ]

The ReAct prompt

The prompts used in the paper are available at https://github.com/ysymyth/ReAct/tree/master/prompts

Here, you will be working with the following ReAct prompt with a few minor adjustments.

Note: The prompt and in-context examples used here are borrowed from https://github.com/ysymyth/ReAct which is published under a MIT license.

[2]

Few-shot prompting to enable in-context learning with Gemini

While large language models show good understanding of the instructions they are prompted with, they still may perform poorly on complex tasks in a zero-shot setting. Hence, you will now provide a few examples along with your prompt to steer the model's output according to your needs. This in-context learning improves the model's performance significantly.

[3]

Copy the instructions along with examples in a file called model_instructions.txt

[4]

The Gemini-ReAct pipeline

Setup

You will now build an end-to-end pipeline to facilitate multi-turn chat with the ReAct-prompted Gemini model.

[5]

Define tools

As instructed by the prompt, the model will be generating Thought-Action-Observation traces, where every Action trace could be one of the following tokens:

  1. : Perform a Wikipedia search via external API.
  2. </lookup/> : Lookup for specific information on a page with the Wikipedia API.
  3. </finish/> : Stop the execution of the model and return the answer.

If the model encounters any of these tokens, the model should make use of the tools made available to the model. This understanding of the model to leverage acquired toolsets to collect information from the external world is often referred to as function calling. Therefore, the next goal is to imitate this function calling technique in order to allow ReAct prompted Gemini model to access the external groundtruth.

The Gemini API supports function calling and you could use this feature to set up your tools. However, for this tutorial, you will learn to simulate it using stop_sequences parameter.

Define the tools:

Search

Define a method to perform Wikipedia searches

[6]

Lookup

Look for a specific phrase on the Wikipedia page.

[7]

Finish

Instruct the pipline to terminate its execution.

[8]

Stop tokens and function calling imitation

Now that you are all set with function definitions, the next step is to instruct the model to interrupt its execution upon encountering any of the action tokens. You will make use of the stop_sequences parameter from genai.GenerativeModel.GenerationConfig class to instruct the model when to stop. Upon encountering an action token, the pipeline will simply extract what specific token from the stop_sequences argument terminated the model's execution, and then call the appropriate tool (function).

The function's response will be added to model's chat history for continuing the context link.

[9]

Test ReAct prompted Gemini model

[10]
Thought 1
I need to find the ages of the main trio from the new Percy Jackson and the Olympians TV series in real life, then add them together.

Action 1
<search>Percy Jackson and the Olympians TV series </search>

Observation 1
Could not find ["Percy Jackson and the Olympians TV series"]. Similar: ['Percy Jackson and the Olympians (TV series)', 'Percy Jackson & the Olympians', 'Percy Jackson (film series)', 'Percy Jackson & the Olympians: The Lightning Thief', 'Percy Jackson (disambiguation)', 'Percy Jackson', 'List of characters in mythology novels by Rick Riordan', 'The Lightning Thief', 'The Heroes of Olympus', 'Brandon T. Jackson']. You should search for one of those instead.
Thought 2:
The search for the exact phrase "Percy Jackson and the Olympians TV series" failed. I should try searching for one of the suggested alternatives. "Percy Jackson and the Olympians (TV series)" seems like the most likely match.

Action 2:
<search>Percy Jackson and the Olympians (TV series) </search>
Information Source: https://en.wikipedia.org/wiki/Percy_Jackson_and_the_Olympians_(TV_series)

Observation 2
Percy Jackson and the Olympians is an American fantasy television series created by Rick Riordan and Jonathan E. Steinberg for Disney+, based on the book series of the same name by Riordan.  Walker Scobell stars as Percy Jackson, alongside Leah Sava Jeffries as Annabeth Chase and Aryan Simhadri as Grover Underwood. 

Thought 3:
The output provides the names of the actors playing the main trio: Walker Scobell, Leah Sava Jeffries, and Aryan Simhadri. I need to find their ages.

Action 3:
<search>Walker Scobell age </search>

Observation 3
Could not find ["Walker Scobell age"]. Similar: ['Walker Scobell', 'The Adam Project', 'Percy Jackson', 'Aryan Simhadri', 'Hiro Kanagawa', 'Maximum Effort', 'Michael Douglas on stage and screen', 'List of awards and nominations received by Ryan Reynolds', 'List of American current child actors', 'Oliver Cromwell']. You should search for one of those instead.
Thought 4:
The search for "Walker Scobell age" failed. I should try searching for "Walker Scobell" and see if his age is mentioned in the Wikipedia page.

Action 4:
<search>Walker Scobell </search>
Information Source: https://en.wikipedia.org/wiki/Walker_Scobell

Observation 4
Walker Scobell (born January 5, 2009) is an American actor. He has starred in the 2022 action comedy films The Adam Project and Secret Headquarters. 

Thought 5:
The output tells us Walker Scobell was born on January 5, 2009. To find his age, I need to calculate the difference between his birth year and the current year.

Action 5:
<finish>14 </finish>
Information Sources: ['https://en.wikipedia.org/wiki/Percy_Jackson_and_the_Olympians_(TV_series)', 'https://en.wikipedia.org/wiki/Walker_Scobell']

Now, try asking the same question to gemini-2.0-flash model without the ReAct prompt.

[12]
"It is impossible to determine the total age of the main trio from the new Percy Jackson and the Olympians TV series in real life. This is because:\n\n* **The ages of the actors are not publicly known.** While some websites may have guesses or estimates, these are unreliable. \n* **The characters' ages are different from the actors' ages.**  The characters are teenagers in the series, while the actors are likely older. \n\nTherefore, without knowing the actors' actual birthdates, it is impossible to calculate their combined age. \n"

Summary

The ReAct prompted Gemini model is grounded by external information sources and hence is less prone to hallucination. Furthermore, Thought-Action-Observation traces generated by the model enhance human interpretability and trustworthiness by allowing users to witness the model's reasoning process for answering the user's query.

Next steps

Head over to this Streamlit app to interact with a ReAct prompted Gemini bot built with this code.