smolagents 文档
编排多智能体系统 🤖🤝🤖
并获得增强的文档体验
开始使用
编排多智能体系统 🤖🤝🤖
在此 notebook 中,我们将创建一个多智能体网页浏览器:一个智能体系统,其中多个智能体协作使用网络解决问题!
它将是一个简单的层级结构
+----------------+
| Manager agent |
+----------------+
|
_______________|______________
| |
Code Interpreter +------------------+
tool | Web Search agent |
+------------------+
| |
Web Search tool |
Visit webpage tool
让我们设置这个系统。
运行下面这行代码来安装所需的依赖项
! pip install markdownify duckduckgo-search smolagents --upgrade -q
让我们登录以便调用 HF Inference API
from huggingface_hub import login
login()
⚡️ 我们的智能体将由 Qwen/Qwen2.5-Coder-32B-Instruct 提供支持,使用 HfApiModel
类,该类使用 HF 的 Inference API:Inference API 允许快速轻松地运行任何 OS 模型。
注意: Inference API 基于各种标准托管模型,部署的模型可能会在没有事先通知的情况下更新或替换。在此处了解更多信息 here。
model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
🔍 创建网页搜索工具
对于网页浏览,我们已经可以使用我们预先存在的 DuckDuckGoSearchTool
工具来提供 Google 搜索的等效功能。
但是,我们也需要能够查看 DuckDuckGoSearchTool
找到的页面。为此,我们可以导入库的内置 VisitWebpageTool
,但我们将再次构建它以了解它是如何完成的。
因此,让我们使用 markdownify
从头开始创建我们的 VisitWebpageTool
工具。
import re
import requests
from markdownify import markdownify
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 = markdownify(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])
构建我们的多智能体系统 🤖🤝🤖
现在我们有了所有工具 search
和 visit_webpage
,我们可以使用它们来创建网页智能体。
为此智能体选择哪种配置?
- 网页浏览是一个单时间线任务,不需要并行工具调用,因此 JSON 工具调用非常适合。因此我们选择
ToolCallingAgent
。 - 此外,由于有时网页搜索需要浏览许多页面才能找到正确的答案,我们更倾向于将
max_steps
的数量增加到 10。
from smolagents import (
CodeAgent,
ToolCallingAgent,
HfApiModel,
DuckDuckGoSearchTool,
LiteLLMModel,
)
model = HfApiModel(model_id=model_id)
web_agent = ToolCallingAgent(
tools=[DuckDuckGoSearchTool(), visit_webpage],
model=model,
max_steps=10,
name="web_search_agent",
description="Runs web searches for you.",
)
请注意,我们为这个智能体赋予了属性 name
和 description
,这是使该智能体可由其管理器智能体调用的强制性属性。
然后我们创建一个管理器智能体,并在初始化时将我们管理的智能体传递给它的 managed_agents
参数。
由于此智能体是负责计划和思考的智能体,因此高级推理将是有益的,因此 CodeAgent
将是最佳选择。
此外,我们想问一个涉及当前年份并进行额外数据计算的问题:因此,让我们添加 additional_authorized_imports=["time", "numpy", "pandas"]
,以防智能体需要这些包。
manager_agent = CodeAgent(
tools=[],
model=model,
managed_agents=[web_agent],
additional_authorized_imports=["time", "numpy", "pandas"],
)
仅此而已!现在让我们运行我们的系统!我们选择一个既需要计算又需要研究的问题
answer = manager_agent.run("If LLM training continues to scale up at the current rhythm until 2030, what would be the electric power in GW required to power the biggest training runs by 2030? What would that correspond to, compared to some countries? Please provide a source for any numbers used.")
我们得到这份报告作为答案
Based on current growth projections and energy consumption estimates, if LLM trainings continue to scale up at the
current rhythm until 2030:
1. The electric power required to power the biggest training runs by 2030 would be approximately 303.74 GW, which
translates to about 2,660,762 GWh/year.
2. Comparing this to countries' electricity consumption:
- It would be equivalent to about 34% of China's total electricity consumption.
- It would exceed the total electricity consumption of India (184%), Russia (267%), and Japan (291%).
- It would be nearly 9 times the electricity consumption of countries like Italy or Mexico.
3. Source of numbers:
- The initial estimate of 5 GW for future LLM training comes from AWS CEO Matt Garman.
- The growth projection used a CAGR of 79.80% from market research by Springs.
- Country electricity consumption data is from the U.S. Energy Information Administration, primarily for the year
2021.
看起来如果 扩展假设 继续成立,我们将需要一些大型发电厂。
我们的智能体成功地高效协作以解决任务!✅
💡 您可以轻松地将此编排扩展到更多智能体:一个执行代码,一个进行网页搜索,一个处理文件加载……
< > 在 GitHub 上更新