Skip to main content
The Google Search integration in AG2 allows users to perform real-time web searches within the AG2 framework. This is especially useful for retrieving up-to-date information that may not be available in static datasets.

Installation

To get started with the Google Search integration in AG2, follow these steps: Install AG2 with the google-search extra. Since our examples also use openai and gemini, install them as well:
Note: If you have been using autogen or ag2, all you need to do is upgrade it using:
or
as autogen and ag2 are aliases for the same PyPI package.
You’re all set! Now you can start using Google Search with AG2.

Imports

Using GoogleSearchTool with Gemini GenAI

The GoogleSearchTool enables search functionalities in AG2 and can be configured to use Gemini GenAI for an enhanced search experience. This section covers agent configuration and tool initialization.

Agent Configuration

GoogleSearchTool Initialization

For Gemini GenAI, you can use the GenAI prebuilt Google Search tool by setting use_internal_llm_tool_if_available to True.
Note: You cannot combine other tools with the prebuilt Google Search tool. If you need multiple tools, refer to the AG2 implementation section.

Start the Conversation

With the setup complete, you can now use the assistant to fetch live web search results.

Output

GoogleSearchTool with AG2 Google Search implementation

For all other LLM providers or if you need to combine Google Search with other tools, follow this section to configure AG2’s implementation.

Setup Google Search Engine and API Key

  1. Create a Google Custom Search Engine (CSE):
    • Go to Google Programmable Search Engine
    • Click Get Started and create a search engine.
    • Under Sites to Search, select Search the entire web if you want global search.
    • Copy the Search Engine ID from the CSE dashboard (cx parameter from the url)
  2. Get a Google API Key:
    • Go to Google Cloud Console
    • Create a new project.
    • Navigate to APIs & Services > Library, search for Custom Search API and enable it.
    • Go to APIs & Services > Credentials, click on Create Credentials > API key and copy it.
  3. Export engine ID and api key

Agent Configuration

GoogleSearchTool Initialization

Create GoogleSearchTool with your search_api_key and search_engine_id.

Start the Conversation

With the setup complete, you can now use the assistant to fetch live web search results.

Output