🤖🤝🤖 编排 Multi-Agent 系统:协作解决问题的网络浏览器

在这个教程中,我们将构建一个多代理(multi-agent)系统,模拟一个能够协作完成复杂任务的网络浏览器。通过结合代码解释、网页搜索和网页访问工具,我们的系统将能够搜索信息、处理数据并回答复杂问题。


🌟 系统概览

我们设计的系统结构如下:

              +----------------+
              | Manager agent  |
              +----------------+
                       |
        _______________|______________
       |                              |
  Code interpreter   +--------------------------------+
       tool          |         Managed agent          |
                     |      +------------------+      |
                     |      | Web Search agent |      |
                     |      +------------------+      |
                     |         |            |         |
                     |  Web Search tool     |         |
                     |             Visit webpage tool |
                     +--------------------------------+

🚀 安装依赖

首先,运行以下命令安装所需的依赖包:

!pip install markdownify duckduckgo-search smolagents --upgrade -q

🔐 登录 Hugging Face Hub

为了调用 Hugging Face 的 Inference API,我们需要登录:

from huggingface_hub import login

login()

⚡️ 使用 Hugging Face 模型

我们将使用 Hugging Face 的 Qwen/Qwen2.5-Coder-32B-Instruct 模型,这是一款强大的代码生成和推理模型,支持长上下文(最多 128K tokens)。模型 ID 如下:

model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"

🔍 创建网络搜索工具

我们将使用 DuckDuckGoSearchTool 作为搜索工具,并重新实现一个 VisitWebpageTool 来访问网页内容。以下是 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:
    """访问指定 URL 的网页并返回其内容的 Markdown 格式。

    Args:
        url: 要访问的网页 URL。

    Returns:
        转换为 Markdown 的网页内容,或错误信息。
    """
    try:
        # 发送 GET 请求
        response = requests.get(url)
        response.raise_for_status()  # 检查状态码

        # 将 HTML 转换为 Markdown
        markdown_content = markdownify(response.text).strip()

        # 删除多余的换行符
        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])

🤖 构建 Multi-Agent 系统

1️⃣ 创建 Web Agent

我们将 DuckDuckGoSearchToolvisit_webpage 工具组合成一个 Web Agent:

from smolagents import (
    ToolCallingAgent,
    HfApiModel,
    DuckDuckGoSearchTool,
)

model = HfApiModel(model_id)

web_agent = ToolCallingAgent(
    tools=[DuckDuckGoSearchTool(), visit_webpage],
    model=model,
    max_steps=10,  # 增加最大步骤数以支持复杂搜索
)

2️⃣ 封装为 Managed Agent

web_agent 封装为 ManagedAgent,使其可以被其他代理调用:

from smolagents import ManagedAgent

managed_web_agent = ManagedAgent(
    agent=web_agent,
    name="search",
    description="Runs web searches for you. Give it your query as an argument.",
)

3️⃣ 创建 Manager Agent

我们创建一个 Manager Agent 来管理整个系统。由于它负责高级推理,我们选择 CodeAgent,并允许其导入额外的 Python 包(如 timenumpypandas)以支持复杂计算:

from smolagents import CodeAgent

manager_agent = CodeAgent(
    tools=[],
    model=model,
    managed_agents=[managed_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.

1. 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.

2. 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.

💡 扩展思路

你可以轻松扩展这个系统:

  • 添加更多工具,例如代码执行工具、文件加载工具等。
  • 增加更多代理,分工协作完成更复杂的任务。

通过这种方式,我们可以构建一个功能强大的多代理系统,解决从搜索到推理的一系列问题!✅

评论

发表回复

人生梦想 - 关注前沿的计算机技术 acejoy.com 🐾 步子哥の博客 🐾 背多分论坛 🐾 知差(chai)网