Installation
To get started with theGoogle 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 usingYou’re all set! Now you can start using Google Search with AG2.autogen
orag2
, all you need to do is upgrade it using:orasautogen
andag2
are aliases for the same PyPI package.
Imports
Using GoogleSearchTool with Gemini GenAI
TheGoogleSearchTool
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 settinguse_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
- 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
, selectSearch the entire web
if you want global search. - Copy the Search Engine ID from the CSE dashboard (
cx
parameter from the url)
- 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.
- Export engine ID and api key
Agent Configuration
GoogleSearchTool Initialization
CreateGoogleSearchTool
with your search_api_key
and search_engine_id
.