Shortcuts

GoogleSearch

class agentlego.tools.GoogleSearch(api_key='env', timeout=5, search_type='search', max_out_len=1500, with_url=False, k=10, toolmeta=None)[source]

A tool to search on Google.

Code is modified from lang-chain GoogleSerperAPIWrapper (https://github.com/langchain-ai/langchain/blob/ba5f baba704a2d729a4b8f568ed70d7c53e799bb/libs/langchain/ langchain/utilities/google_serper.py)

To get an Serper.dev API key. you can create it at https://serper.dev

Parameters:
  • api_key (str) – API key to use for serper google search API. Defaults to ‘env’, which means to use the SERPER_API_KEY in the environ variable.

  • timeout (int) – Upper bound of waiting time for a serper request. Defaults to 5.

  • search_type (str) – Serper API support [‘search’, ‘images’, ‘news’, ‘places’] types of search, currently we only support ‘search’. Defaults to search.

  • max_out_len (int) – The maximum length of the total search result. Defaults to 1500.

  • with_url (bool) – Whether to include the url of search results. Defaults to False.

  • k (int) – select first k results in the search results as response. Defaults to 10.

  • toolmeta (None | dict | ToolMeta) – The additional info of the tool. Defaults to None.

Default Tool Meta

  • name: GoogleSearch

  • description: The tool can search the input query text from Google and return the related results.

  • inputs:

    • query (str)

  • outputs:

    • str

Examples

Use the tool directly (without agent)

from agentlego.apis import load_tool

# load tool
tool = load_tool('GoogleSearch')

# apply tool
res = tool('Highest mountain in the earth')

With Lagent

from lagent import ReAct, GPTAPI, ActionExecutor
from agentlego.apis import load_tool

# load tools and build agent
# please set `OPENAI_API_KEY` in your environment variable.
tool = load_tool('GoogleSearch').to_lagent()
agent = ReAct(GPTAPI(temperature=0.), action_executor=ActionExecutor([tool]))

# agent running with the tool.
ret = agent.chat(f'What is the highest mountain in the earth?')
for step in ret.inner_steps[1:]:
    print('------')
    print(step['content'])

Set up

Before using the tool, please confirm you have a Serper API key, and set it in environment variables.

export SERPER_API_KEY='Your API key'

Reference

The Google Search API comes from Serper