Skip to content
CrewAI 文档版本 跟随上游 源快照 b7d67f92 更新于 2026-10-18

Google Serper 搜索

SerperDevTool 旨在搜索互联网并返回最相关的结果。

SerperDevTool

描述

此工具旨在针对指定查询,在互联网上的文本内容中执行语义搜索。它利用 serper.dev API 根据用户提供的查询获取并显示最相关的搜索结果。

安装

要有效使用 SerperDevTool,请按以下步骤操作:

  1. 包安装:确认你的 Python 环境中已安装 crewai[tools] 包。
  2. 获取 API 密钥:在 https://serper.dev/ 获取 serper.dev API 密钥(提供免费套餐)。
  3. 环境配置:将获取的 API 密钥存储在名为 SERPER_API_KEY 的环境变量中,以便该工具使用。

要将此工具纳入你的项目,请遵循下面的安装说明:

shell
pip install 'crewai[tools]'

示例

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

python
from crewai_tools import SerperDevTool

# Initialize the tool for internet searching capabilities
tool = SerperDevTool()

参数

SerperDevTool 附带若干将传递给 API 的参数:

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

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

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

  • locale:可选。指定搜索结果的语言区域。

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

country、location、locale 和 search_url 的取值可以在 Serper Playground 上找到。

带参数的示例

以下示例演示了如何使用带附加参数的工具:

python
from crewai_tools import SerperDevTool

tool = SerperDevTool(
    search_url="https://google.serper.dev/scholar",
    n_results=2,
)

print(tool.run(search_query="ChatGPT"))

# Using Tool: Search the internet

# Search results: Title: Role of chat gpt in public health
# Link: https://link.springer.com/article/10.1007/s10439-023-03172-7
# Snippet: … ChatGPT in public health. In this overview, we will examine the potential uses of ChatGPT in
# ---
# Title: Potential use of chat gpt in global warming
# Link: https://link.springer.com/article/10.1007/s10439-023-03171-8
# Snippet: … as ChatGPT, have the potential to play a critical role in advancing our understanding of climate
# ---
python
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"))

# Using Tool: Search the internet

# Search results: Title: Jeux Olympiques de Paris 2024 - Actualités, calendriers, résultats
# Link: https://olympics.com/fr/paris-2024
# Snippet: Quels sont les sports présents aux Jeux Olympiques de Paris 2024 ? · Athlétisme · Aviron · Badminton · Basketball · Basketball 3x3 · Boxe · Breaking · Canoë ...
# ---
# Title: Billetterie Officielle de Paris 2024 - Jeux Olympiques et Paralympiques
# Link: https://tickets.paris2024.org/
# Snippet: 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 项目中,用户可以直接在自己的应用里对互联网进行实时、相关的搜索。 更新后的参数可以带来更定制化、更本地化的搜索结果。遵循所提供的安装与使用指南,将该工具纳入项目的过程简洁而直接。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。