Lab 8 Solution: Creating a "Researcher" Agent with Google Search
Goal
This file contains the complete, step-by-step guide to creating the "Researcher" agent.
Goal
You will build a new agent that can answer questions about current events and topics beyond the LLM's training data by giving it the ability to search the web using the built-in google_search tool.
Prerequisites
- Note on Costs: Enabling the Vertex AI API and using the
google_searchtool may incur small costs on your Google Cloud bill. Please be aware of the pricing for these services.
Step 1: Create the Researcher Agent Project
-
Navigate to your training directory:
Open your terminal and make sure you are in the
adk-trainingdirectory.cd /path/to/your/adk-training -
Create the agent project (Python approach):
Use the
adk createcommand to create a new Python agent namedresearcher_agent.adk create --type=python researcher_agentAlternative (YAML approach):
adk create --type=config researcher_agent
Step 2: Configure the Agent to Use Google Search
To use the google_search tool, you need to enable the Vertex AI API in your Google Cloud project, as this is the service that provides the grounding functionality.
-
Enable the Vertex AI API:
- Go to the Vertex AI API page in the Google Cloud Console.
- Make sure your current project is selected.
- Click the "Enable" button if it's not already enabled.
-
Set up your environment variables:
Navigate into the
researcher_agentdirectory and open the.envfile. For the search tool to work, you must configure your agent to use Vertex AI.Update the
.envfile to look like this, filling in your project ID and desired location:GOOGLE_GENAI_USE_VERTEXAI=1
GOOGLE_CLOUD_PROJECT=<your_gcp_project>
GOOGLE_CLOUD_LOCATION=us-central1- Important: The
google_searchtool does not work with a simple Google AI Studio API key; it requires a full Google Cloud project setup.
- Important: The
-
Define the agent's behavior and add the tool (Python approach):
Open the
agent.pyfile insideresearcher_agentand replace its contents with the following:from google.adk.agents import LlmAgent
from google.adk.tools import google_search
root_agent = LlmAgent(
name="researcher_agent",
model="gemini-2.5-flash",
description="An agent that can research current events using Google Search.",
instruction=(
"You are a helpful research assistant. "
"Your job is to answer the user's questions accurately. "
"If the question is about a recent event, a specific person, or anything that might require up-to-date information, you MUST use the `google_search` tool. "
"Do not rely on your own knowledge for topics that could have changed since your training."
),
tools=[
google_search
],
)Alternative (YAML approach):
Open the
root_agent.yamlfile and replace its contents with the following:# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
name: researcher_agent
model: gemini-2.5-flash
description: An agent that can research current events using Google Search.
instruction: |
You are a helpful research assistant.
Your job is to answer the user's questions accurately.
If the question is about a recent event, a specific person, or anything that might require up-to-date information, you MUST use the `google_search` tool.
Do not rely on your own knowledge for topics that could have changed since your training.
tools:
- name: google_search
Step 3: Test Your New Agent
-
Navigate back to the parent directory:
cd .. -
Start the web server:
From the
adk-trainingdirectory, runadk web.adk web -
Interact with the Researcher Agent:
- Open the Dev UI in your browser.
- Select
researcher_agentfrom the dropdown menu. - Ask a question that the LLM wouldn't know from its training data:
- "Who won the last Super Bowl?"
- "What are the latest headlines about space exploration?"
- Examine the Trace View: Click on the "Trace" tab. You will see a new step in the execution flow:
execute_tool. Expand it to see that thegoogle_searchtool was called, confirming the agent is using its new tool correctly.
Lab Summary
You have successfully given an agent a powerful new capability. You have learned how to:
- Enable the necessary Google Cloud APIs for advanced features.
- Configure an agent to use a built-in tool like
google_search(using both Python and YAML approaches). - Write instructions that guide the agent on when to use its tools.
- Verify that a tool was used by inspecting the Trace View in the Dev UI.