开源 AI 食谱文档
让多个智能体在多智能体层级结构中协作 🤖🤝🤖
并获得增强的文档体验
开始使用
让多个智能体在多智能体层级结构中协作 🤖🤝🤖
本教程是高级教程。您应首先了解此菜谱中的概念!
在此笔记本中,我们将创建一个多智能体网页浏览器:一个包含多个智能体的智能体系统,它们协作使用网页解决问题!
这将是一个简单的层级结构,使用 ManagedAgent
对象来封装托管的网页搜索智能体。
+----------------+
| Manager agent |
+----------------+
|
_______________|______________
| |
Code interpreter +--------------------------------+
tool | Managed agent |
| +------------------+ |
| | Web Search agent | |
| +------------------+ |
| | | |
| Web Search tool | |
| Visit webpage tool |
+--------------------------------+
让我们来设置这个系统。
运行下面这行命令来安装所需的依赖项
!pip install markdownify duckduckgo-search smolagents --upgrade -q
让我们登录以便调用 HF 推理 API
from huggingface_hub import notebook_login
notebook_login()
⚡️ 我们的智能体将由Qwen/Qwen2.5-72B-Instruct 提供支持,使用 HfApiEngine
类,该类使用 HF 的推理 API:推理 API 允许快速轻松地运行任何 OS 模型。
注意: 推理 API 根据各种标准托管模型,部署的模型可能会在没有事先通知的情况下更新或替换。在此处了解更多信息。
model_id = "Qwen/Qwen2.5-72B-Instruct"
🔍 创建网页搜索工具
对于网页浏览,我们已经可以使用预先存在的 DuckDuckGoSearchTool
工具来提供与 Google 搜索等效的功能。
但随后我们还需要能够查看 DuckDuckGoSearchTool
找到的页面。为此,我们可以导入库的内置 VisitWebpageTool
,但我们将重新构建它以了解其工作原理。
所以让我们使用 markdownify
从头开始创建我们的 VisitWebpageTool
工具。
import re
import requests
from markdownify import markdownify as md
from requests.exceptions import RequestException
from smolagents import tool
@tool
def visit_webpage(url: str) -> str:
"""Visits a webpage at the given URL and returns its content as a markdown string.
Args:
url: The URL of the webpage to visit.
Returns:
The content of the webpage converted to Markdown, or an error message if the request fails.
"""
try:
# Send a GET request to the URL
response = requests.get(url)
response.raise_for_status() # Raise an exception for bad status codes
# Convert the HTML content to Markdown
markdown_content = md(response.text).strip()
# Remove multiple line breaks
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
return markdown_content
except RequestException as e:
return f"Error fetching the webpage: {str(e)}"
except Exception as e:
return f"An unexpected error occurred: {str(e)}"
好的,现在让我们初始化并测试我们的工具!
>>> print(visit_webpage("https://en.wikipedia.org/wiki/Hugging_Face")[:500])
Hugging Face - Wikipedia [Jump to content](#bodyContent) Main menu Main menu move to sidebar hide Navigation * [Main page](/wiki/Main_Page "Visit the main page [z]") * [Contents](/wiki/Wikipedia:Contents "Guides to browsing Wikipedia") * [Current events](/wiki/Portal:Current_events "Articles related to current events") * [Random article](/wiki/Special:Random "Visit a randomly selected article [x]") * [About Wikipedia](/wiki/Wikipedia:About "Learn about Wikipedia and how it works") * [Contac
构建我们的多智能体系统 🤖🤝🤖
现在我们有了所有工具 search
和 visit_webpage
,我们可以使用它们来创建网页智能体。
该智能体应选择哪种配置?
- 网页浏览是一个不需要并行工具调用的单时间线任务,因此 JSON 工具调用非常适合。因此,我们选择
ReactJsonAgent
。 - 此外,由于有时网页搜索需要探索许多页面才能找到正确的答案,我们倾向于将
max_iterations
增加到 10。
from smolagents import CodeAgent, ToolCallingAgent, InferenceClientModel, ManagedAgent, DuckDuckGoSearchTool
model = InferenceClientModel(model_id)
web_agent = ToolCallingAgent(
tools=[DuckDuckGoSearchTool(), visit_webpage],
model=model,
max_iterations=10,
)
然后,我们将此智能体封装到 ManagedAgent
中,使其可以被其管理器智能体调用。
managed_web_agent = ManagedAgent(
agent=web_agent,
name="search_agent",
description="Runs web searches for you. Give it your query as an argument.",
)
最后,我们创建一个管理器智能体,并在初始化时将其管理的智能体传递给其 managed_agents
参数。
由于该智能体负责规划和思考,高级推理将非常有益,因此 ReactCodeAgent
将是最佳选择。
此外,我们希望提出一个涉及当前年份的问题:因此让我们添加 additional_authorized_imports=["time", "datetime"]
manager_agent = CodeAgent(
tools=[],
model=model,
managed_agents=[managed_web_agent],
additional_authorized_imports=["time", "datetime"],
)
就是这样!现在让我们运行我们的系统!我们选择一个需要一些计算的问题和
manager_agent.run("How many years ago was Stripe founded?")
我们的智能体成功高效地协作完成了任务!✅
💡 您可以轻松地将其扩展到更多智能体:一个执行代码,一个执行网页搜索,一个处理文件加载……
🤔💭 甚至可以考虑更复杂的树状层级结构,一个 CEO 智能体管理多个中层经理,每个经理都有多个下属。
我们甚至可以添加更多的中间管理层,每个层级都有多次日常会议,大量的敏捷开发(Scrum 主管),每个新组件都会增加足够的摩擦,确保任务永远无法完成……嗯,等等,不,让我们坚持我们简单的结构。
< > 在 GitHub 上更新