当前位置: 首页 > article >正文

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

要实现上面结构的功能需要以下几个步骤:

  1. 定义 Visit webpage toolWeb Search tool ,官网示例中后者直接使用了现成的 DuckDuckGoSearchTool
  2. 定义一个 web_agent 并绑定 Visit webpage toolWeb Search tool ;
  3. 定义一个 manager_agent 并绑定 web_agent
  4. 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

在这里插入图片描述


http://www.kler.cn/a/565410.html

相关文章:

  • 边缘计算收益低的三大指标
  • android进阶面试题目
  • MySQL缓存命中率
  • 本地部署DeepSeek-R1(Ollama+Docker+OpenWebUI知识库)
  • ubuntu安装配置docker
  • MySQL---存储过程详解
  • 手机大厂如何处理安卓分屏退出后最近任务显示一半问题?
  • SpringDataJPA使用deleteAllInBatch方法逻辑删除失效
  • 面试基础---JVM 运行时数据区
  • Redis的Spring配置
  • 提升 Spring Boot 系统性能:高效处理实时数据流的 BufferTrigger 使用详解
  • JavaScript系列(92)--前端监控体系
  • Vue 3 + Vite 项目配置访问地址到服务器某个文件夹的解决方案
  • 全面解析:如何查找电脑的局域网与公网IP地址‌
  • reallocate() 和 allocate() 的区别
  • vue实现根据点击或滑动展示对应高亮
  • Django ORM 的常用字段类型、外键关联的跨表引用技巧,以及 `_` 和 `__` 的使用场景
  • 基于EasyExcel封装的Excel工具类,支持高效导出和读取操作
  • Qt for Android下QMessageBox背景黑色、文字点击闪烁
  • JSX 实现列表渲染