Build an AI Search Agent using Gemini 2.0 Flash

Share it with your senior IT friends and colleagues
Reading Time: 3 minutes

Large Language Models are pre-trained models. This means they have a knowledge cut-off.

So, if you ask them any question related current event that happened after their training, they will not be able to answer.

For example, if you ask, “Who is the President of the United States right now?” the model might give you an outdated answer.

What’s the solution then?

One solution could be that we can provide LLMs access to Google Search.

So, if a current affairs question is being asked, they can access Google Search using SERP API and provide the answer.

Namaste and Welcome to Build It Yourself.

I am Nikhilesh, and I teach AI, Generative AI, and AI Agents in the simplest way possible.

In this tutorial, we will build an AI Search Agent Using Gemini 2.0 Flash.

If you are a senior IT professional and looking to learn AI + LLM to become an AI leader, check out our most up-to-date courses – https://www.aimletc.com/online-instructor-led-ai-llm-coaching-for-it-technical-professionals/


Build an AI Search Agent Using Gemini 2.0 Flash

Code Notebook – https://github.com/tayaln/AI-Search-Agent-using-Gemini-2.0-Flash

What is an AI Agent?

Before we dive into the code, let’s first understand the core idea.

An AI agent is like a smart assistant powered by a language model (LLM) that can use external tools like Google search to solve problems.

Unlike static chatbots, these agents don’t work in isolation and can access external applications (aka tools) and provide information to the user.


Step 1: Getting Started with Gemini API

We’ll use the Google Generative AI (Gemini) model. Here’s how to set it up:

  1. Go to AI Studio by Google.
  2. Click on “Get API Key”.
  3. Create or choose a project.
  4. Copy the API key.

Once you have it, plug it into your code. This key gives your app access to Gemini models (free with some usage limits).


Step 2: Test Gemini’s Knowledge

Let’s warm up with a basic prompt:

client.model.models.generate_content(
    model_id="gemini-2.0-flash",
    prompt="Explain AI to a 10-year-old in 3 lines"
)

Gemini will say something like:

“Imagine teaching a computer to learn like you do. We give it lots of examples, and it learns to make smart guesses. That’s AI—a computer becoming clever!”

Step 3: Let’s try a real-time question.

"Who is the President of the United States?"

Gemini might still say Joe Biden—even if that’s outdated.

Why?

Because it doesn’t know what’s happened after its training cut-off.


Step 4: Let’s Add the Power of Google Search

Now the fun begins.

We’ll build a Search Agent that combines Gemini with live internet search using the SERP API and a framework called LangChain.

Here’s what we install:

pip install google-generativeai
pip install langchain langchain-community
pip install serpapi

What’s SERP API?

It’s a tool that allows your program to search Google and get back structured results. You can get a free API key by signing in at serpapi.com.


Step 5: Building the Agent

  1. Import your libraries.
  2. Set up your API keys (Gemini + SERP).
  3. Define the tools (Google Search).
  4. Set up the agent using LangChain.
from langchain.agents import initialize_agent, Tool
from langchain_community.tools import SerpAPIWrapper
from langchain_google_genai import ChatGoogleGenerativeAI

search = SerpAPIWrapper()
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)

tools = [Tool(name="Search", func=search.run, description="Search Google")]
agent = initialize_agent(tools, llm, agent_type="zero-shot-react-description", verbose=True)

Let’s ask again:

agent.run("Who is the President of the United States?")

What Happens Behind the Scenes?

This time, instead of guessing, the agent thinks aloud:

  1. “I need to find the current President.”
  2. “I can use a search engine.”
  3. It sends the query to Google.
  4. It reads the results and picks the most relevant one.
  5. Then it responds with something like:
    “Donald Trump is the current president.”

Even more amazing, it can reflect and correct itself if the first result seems outdated!


Why Is This Cool?

You just built an AI that:

  • Knows its limits (“I don’t know, let me search”)
  • Takes action (sends a Google query)
  • Thinks out loud (so you can debug it)
  • Learns on the fly

This is the beginning of more intelligent, autonomous agents—AI that does, not just says.

Stay tuned and happy building!

The most up-to-date AI + Gen AI Coaching for senior IT professionals

In case you are looking to learn AI + Gen AI in an instructor-led live class environment, check out these courses

Are you ready to lead AI in your organisation? Take this 2 minutes quiz


If you have any queries, or suggestions, share them with me on LinkedIn – https://www.linkedin.com/in/nikhileshtayal/

Let’s learn to build a basic AI/ML model in 4 minutes (Part 1)

Share it with your senior IT friends and colleagues
Nikhilesh Tayal
Nikhilesh Tayal
Articles: 97
💬 Send enquiry on WhatsApp