跳转到内容

Google Serper 搜索

该工具用于对指定查询在互联网上进行语义搜索。它使用 serper.dev API
根据用户提供的查询获取并展示最相关的搜索结果。

要有效使用 SerperDevTool,请按以下步骤进行:

  1. 安装包:确认你的 Python 环境中已安装 crewai[tools] 包。
  2. 获取 API Key:在 https://serper.dev/ 获取 serper.dev API key(有免费层)。
  3. 环境配置:将你获得的 API key 存储到名为 SERPER_API_KEY 的环境变量中,方便工具使用。

要将此工具集成到你的项目中,请遵循以下安装说明:

Terminal window
pip install 'crewai[tools]'

下面的示例演示如何初始化工具并使用给定查询执行搜索:

from crewai_tools import SerperDevTool
# 初始化工具,启用网络搜索能力
tool = SerperDevTool()

SerperDevTool 附带多个会传递给 API 的参数:

  • search_url:搜索 API 的 URL 端点。(默认是 https://google.serper.dev/search

  • country:可选。指定搜索结果的国家。

  • location:可选。指定搜索结果的位置。

  • locale:可选。指定搜索结果的语言环境。

  • n_results:要返回的搜索结果数量。默认值为 10

countrylocationlocalesearch_url 的值可在 Serper Playground 中找到。

下面是一个演示如何使用附加参数的示例:

from crewai_tools import SerperDevTool
tool = SerperDevTool(
search_url="https://google.serper.dev/scholar",
n_results=2,
)
print(tool.run(search_query="ChatGPT"))
# 使用工具:搜索互联网
# 搜索结果:标题:Role of chat gpt in public health
# 链接:https://link.springer.com/article/10.1007/s10439-023-03172-7
# 摘要:... ChatGPT in public health. In this overview, we will examine the potential uses of ChatGPT in
# ---
# 标题:Potential use of chat gpt in global warming
# 链接:https://link.springer.com/article/10.1007/s10439-023-03171-8
# 摘要:... as ChatGPT, have the potential to play a critical role in advancing our understanding of climate
# ---
from crewai_tools import SerperDevTool
tool = SerperDevTool(
country="fr",
locale="fr",
location="Paris, Paris, Ile-de-France, France",
n_results=2,
)
print(tool.run(search_query="Jeux Olympiques"))
# 使用工具:搜索互联网
# 搜索结果:标题:Jeux Olympiques de Paris 2024 - Actualités, calendriers, résultats
# 链接:https://olympics.com/fr/paris-2024
# 摘要:Quels sont les sports présents aux Jeux Olympiques de Paris 2024 ? · Athlétisme · Aviron · Badminton · Basketball · Basketball 3x3 · Boxe · Breaking · Canoë ...
# ---
# 标题:Billetterie Officielle de Paris 2024 - Jeux Olympiques et Paralympiques
# 链接:https://tickets.paris2024.org/
# 摘要:Achetez vos billets exclusivement sur le site officiel de la billetterie de Paris 2024 pour participer au plus grand événement sportif au monde.
# ---

SerperDevTool 集成到 Python 项目中后,用户就能直接从应用中执行实时、相关的网络搜索。
更新后的参数可支持更定制化和本地化的搜索结果。遵循这里提供的安装和使用指南后,将此工具集成到项目中会非常直接且简单。