用途
本技能用于为 AG2 智能体(agents)注册并使用联网搜索与网页内容提取工具,使智能体能够搜索网页并读取页面正文内容。
主要能力
该 web-search 工具为 AG2 智能体提供两个函数:
- web_search(query, max_results):通过 DuckDuckGo 搜索网页(无需 API Key),返回带有标题、URL 和摘要的格式化结果。
- fetch_page(url):抓取网页,并使用 BeautifulSoup 提取干净、可读的文本内容。
工具参数细节
web_search
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| query | str | — | 搜索查询字符串 |
| max_results | int | 5 | 返回的最大结果数(1–20) |
返回值:格式化字符串,包含编号结果,每项含标题、URL 与摘要。
fetch_page
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| url | str | — | 要抓取的页面 URL |
返回值:提取的文本内容(最多约 8000 个字符)。会去除脚本、样式、导航、页眉和页脚,以输出干净内容。
安装方式
安装所需依赖:
pip install httpx>=0.27 beautifulsoup4>=4.12
使用方式
向 AG2 智能体注册工具
import autogen
from tools.web_search import fetch_page, web_search
# Configure the LLM
config_list = [{"model": "gpt-4o", "api_key": "YOUR_KEY"}]
assistant = autogen.AssistantAgent(
name="research_assistant",
llm_config={"config_list": config_list},
)
user_proxy = autogen.UserProxyAgent(
name="user",
human_input_mode="NEVER",
code_execution_config=False,
)
# Register both tools with the agent
assistant.register_for_llm(description="Search the web using DuckDuckGo")(web_search)
assistant.register_for_llm(description="Fetch and extract text from a URL")(fetch_page)
user_proxy.register_for_execution()(web_search)
user_proxy.register_for_execution()(fetch_page)
完整可运行示例
import autogen
from tools.web_search import fetch_page, web_search
config_list = [{"model": "gpt-4o", "api_key": "YOUR_KEY"}]
assistant = autogen.AssistantAgent(
name="web_researcher",
system_message=(
"You are a helpful research assistant. Use the web_search tool to "
"find information, then use fetch_page to read the most relevant "
"results. Summarize your findings clearly."
),
llm_config={"config_list": config_list},
)
user_proxy = autogen.UserProxyAgent(
name="user",
human_input_mode="NEVER",
max_consecutive_auto_reply=10,
code_execution_config=False,
)
# Register tools
assistant.register_for_llm(description="Search the web using DuckDuckGo")(web_search)
assistant.register_for_llm(description="Fetch and extract text from a URL")(fetch_page)
user_proxy.register_for_execution()(web_search)
user_proxy.register_for_execution()(fetch_page)
# Start the conversation
user_proxy.initiate_chat(
assistant,
message="Search the web for 'AG2 multi-agent framework' and summarize what you find.",
)
配合 ConversableAgent 与函数注册使用
import autogen
from autogen import register_function
from tools.web_search import fetch_page, web_search
config_list = [{"model": "gpt-4o", "api_key": "YOUR_KEY"}]
assistant = autogen.ConversableAgent(
name="assistant",
llm_config={"config_list": config_list},
)
user_proxy = autogen.ConversableAgent(
name="user",
human_input_mode="NEVER",
code_execution_config=False,
)
register_function(
web_search,
caller=assistant,
executor=user_proxy,
name="web_search",
description="Search the web using DuckDuckGo and return results",
)
register_function(
fetch_page,
caller=assistant,
executor=user_proxy,
name="fetch_page",
description="Fetch a web page and extract its text content",
)
适用场景
适用于需要让 AG2 智能体具备联网检索与网页正文读取能力的研究型任务。示例中展示的场景为:智能体先使用 web_search 查找信息,再用 fetch_page 阅读最相关的结果,最后清晰地汇总发现。
限制与注意事项
- 网页搜索无需 API Key(使用 DuckDuckGo HTML 搜索)。
- fetch_page 遵守 15 秒超时,并会跟随重定向。
- 内容会在约 8000 字符处截断,以适配 LLM 上下文限制。
其余如定价、平台支持范围等信息暂未提供。