Search⌘ K
AI Features

Implementing Single-Agent Researcher

Explore how to implement a single-agent research assistant that integrates multiple tools for gathering general knowledge, academic papers, and web content. Learn to assemble these into a functioning AI agent with Google ADK, enabling complex research tasks and generating consolidated reports.

With a comprehensive architectural blueprint and a fully prepared project environment, we are now ready to transition from design to execution. This lesson is dedicated to the hands-on implementation of our “Research Assistant.” We will translate the design from our planning phase into functional Python code.

The process will be methodical and incremental. We will begin by building each of the agent’s necessary tools and its specific capabilities one by one. Once these components are in place, we will assemble them into a single, powerful LlmAgent, guided by the master instruction prompt we designed. By the end of this lesson, we will have a complete, runnable agent capable of performing its complex research task from start to finish.

Implementing the wikipedia_tool

First, we will build the tool responsible for gathering general, high-level information. For this purpose, we will use Wikipedia, and create a tool wikipedia_tool that allows our agent to access its vast repository of knowledge using the wikipedia library.

This tool will serve as our agent’s primary resource for foundational knowledge. When the agent needs to understand the basics of a topic, it will use this tool to search for and retrieve a summary from the corresponding Wikipedia article. Let’s see how to implement the wikipedia_tool.

import wikipedia
def wikipedia_tool(query: str) -> str:
"""
Searches Wikipedia for a given query and returns a summary of the top result.
Args:
query (str): The search term to look up on Wikipedia.
"""
try:
# The summary method automatically finds the best-matching page
# and returns a summary of it.
summary = wikipedia.summary(query)
return summary
except wikipedia.exceptions.DisambiguationError as e:
# Handle cases where a query is ambiguous (e.g., "Java")
return f"The query '{query}' is ambiguous. Please be more specific. Options: {e.options[:3]}"
except wikipedia.exceptions.PageError:
# Handle cases where the page does not exist
return f"Sorry, I could not find a Wikipedia page for '{query}'."
except Exception as e:
return f"An unexpected error occurred while searching Wikipedia: {e}"
The wikipedia_tool

Code explanation

  • Line 1: We import the wikipedia library, which provides the functions needed to interact with the Wikipedia API.

  • Line 3: We define a standard Python function named wikipedia_tool that accepts one string argument, query. The -> str is a type hint indicating that this function will return a string.

  • Lines 4–9: This is the function’s docstring. The ADK framework will parse this docstring to create the tool’s description and parameter definitions for the LLM. We clearly state what the tool does and what the query argument represents.

  • Line 10: We use a try...except block to gracefully handle potential errors that can occur when interacting with an external API.

  • Line 13: We call wikipedia.summary(), the core function of the library.

  • Line 14: If the call is successful, we return the retrieved summary string.

  • Lines 15–22: These except blocks handle specific, ...