smolagents学习笔记系列(九)Examples - Orchestrate a multi-agent system
这篇文章锁定官网教程中 Examples
章节中的 Orchestrate a multi-agent system
文章,主要介绍了如何设计一个多Agent系统。
- 官网链接:https://huggingface.co/docs/smolagents/v1.9.2/en/examples/multiagents;
通常情况下将一个任务拆分成不同部分让多个模块合作完成,这样的框架设计是比较合理的,那么Agent也是一样。如果你有一个多模态任务,例如:浏览网页后将主题以音频文件的方式播报给你,那么最合理的设计是使用三个Agent:
- Agent 1:检索网页并总结出精炼文本内容;
- Agent 2:使用合适的TTS功能将文本转换为音频;
- Agnet 3:调度使用这两个Agent,将Agent 1 的输出传递给 Agent 2;
smolagents也提供了对多Agent的支持,官网以网络检索功能为例提供了下面这个结构示例:
+----------------+
| Manager agent |
+----------------+
|
_______________|______________
| |
Code Interpreter +------------------+
tool | Web Search agent |
+------------------+
| |
Web Search tool |
Visit webpage tool
需要安装以下依赖:
$ pip install markdownify duckduckgo-search smolagents --upgrade -q
要实现上面结构的功能需要以下几个步骤:
- 定义
Visit webpage tool
和Web Search tool
,官网示例中后者直接使用了现成的DuckDuckGoSearchTool
; - 定义一个
web_agent
并绑定Visit webpage tool
和Web Search tool
; - 定义一个
manager_agent
并绑定web_agent
; - 向
manager_agent
进行询问;
完整代码如下:
import re
import requests
from markdownify import markdownify
from requests.exceptions import RequestException
from smolagents import tool
from smolagents import (
CodeAgent,
ToolCallingAgent,
HfApiModel,
DuckDuckGoSearchTool,
LiteLLMModel,
)
#-------------------------------------------------------------------------#
# Step1. 定义 visit_webpage 工具
@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)}"
#-------------------------------------------------------------------------#
# Step2. 定义 web_agent 并绑定 visit_webpage 和 DuckDuckGoSearchTool 工具
model = HfApiModel()
web_agent = ToolCallingAgent(
tools=[DuckDuckGoSearchTool(), visit_webpage],
model=model,
max_steps=10,
name="search",
description="Runs web searches for you. Give it your query as an argument.",
)
#-------------------------------------------------------------------------#
# Step3. 定义 manager_agent 并绑定 web_agent
manager_agent = CodeAgent(
tools=[],
model=model,
managed_agents=[web_agent],
additional_authorized_imports=["time", "numpy", "pandas"],
)
#-------------------------------------------------------------------------#
# Step4. 向Manager Agent询问
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.")
运行结果如下,这个运行耗时可能比较长,因为实际需要按照 manager_agent -> web_agent -> manager_agent
信息流进行运行,考虑到每个 agent 都会根据LLM回答决定是否需要二次提问,所以这里可能会出现很多轮问答:
$ python demo.py