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

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

在这里插入图片描述

相关文章:

  • 15. C++多线程编程-网络编程-GUI编程(如Qt)学习建议
  • Linux安装jdk,node,mysql,redis
  • 最佳Nginx日志分割
  • InterHand26M(handposeX-json 格式)数据集-release >> DataBall
  • DAV_postgresql_3-schema
  • 初识MySQL · 数据库
  • 大白话Webpack 怎么理解,怎么使用它?
  • c++ 文件及基本读写总结
  • LM studio 加载ollama的模型
  • 【二分答案 C/C++】洛谷P1182 数列分段 Section II
  • 深度学习-15.生成对抗网络
  • 大语言模型学习
  • 图像算法工程师的技术图谱和学习路径
  • 从头开始学SpringBoot—02ssmp整合及案例
  • leetcode 48. 旋转图像
  • 《白帽子讲 Web 安全》之移动 Web 安全
  • SpringCloud系列教程(八):服务网关Gateway
  • 基于JavaWeb开发的Java+SpringBoot+vue+element实现物流管理系统
  • 基于Leaflet和SpringBoot的全球国家综合检索WebGIS可视化
  • python多线程之Event机制笔记
  • 中国情怀:时代记录与家国镜相|澎湃·镜相第三届非虚构写作大赛征稿启事
  • 会谈时间迟迟未定、核心议题存在分歧,俄乌“土耳其谈判”一波三折
  • “大型翻车现场”科技满满,黄骅打造现代化港口和沿海新城典范
  • 俄谈判代表团已抵达土耳其,谈判预计在莫斯科时间10时左右开始
  • 端午假期购票日历发布,今日可购买5月29日火车票
  • “一百零一个愿望——汉字艺术展”亮相意大利威尼斯