smolagents 文档
编排一个多智能体系统 🤖🤝🤖
并获得增强的文档体验
开始使用
编排一个多智能体系统 🤖🤝🤖
在这个 Notebook 中,我们将构建一个多智能体网络浏览器:一个由多个智能体协作解决网络问题的智能体系统!
它将是一个简单的层次结构
+----------------+
| Manager agent |
+----------------+
|
_______________|______________
| |
Code Interpreter +------------------+
tool | Web Search agent |
+------------------+
| |
Web Search tool |
Visit webpage tool
让我们来设置这个系统。
运行下面这行命令来安装所需的依赖项
!pip install smolagents[toolkit] --upgrade -q
让我们登录到 HF,以便调用推理提供商
from huggingface_hub import login
login()
⚡️ 我们的智能体将由 Qwen/Qwen2.5-Coder-32B-Instruct 提供支持,使用 InferenceClientModel
类,该类使用 HF 的推理 API:推理 API 允许快速轻松地运行任何 OS 模型。
推理提供商允许访问数百个模型,由无服务器推理合作伙伴提供支持。支持的提供商列表可以在 此处 找到。
model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
🔍 创建一个网页搜索工具
对于网页浏览,我们已经可以使用我们的原生 WebSearchTool 工具来提供等同于 Google 搜索的功能。
但之后我们还需要能够查看 WebSearchTool
找到的页面。为此,我们可以导入库的内置 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,
InferenceClientModel,
WebSearchTool,
LiteLLMModel,
)
model = InferenceClientModel(model_id=model_id)
web_agent = ToolCallingAgent(
tools=[WebSearchTool(), 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 上更新