多智能体协作之 AutoGen Studio

目录

一、AutoGen Studio 安装

二、AutoGen Studio 使用

2.1 Skills

2.1.1 generate_and_save_images

2.1.2 generate_and_save_pdf

2.2 Models

2.3 Agents

2.4 Workflows

2.5 Playground


本文主要对 AutoGen Studio 进行介绍。

一、AutoGen Studio 安装

通过 pip 进行安装,默认会安装 AutoGen。

pip install autogenstudio

如果无法通过 pip 安装,可以下载 autogenstudio 安装包,手动安装(点击下载安装包)。

pip install autogenstudio-0.1.4-py3-none-any.whl

指定端口,运行 AutoGen Studio。 

autogenstudio ui --port 8081 --host 0.0.0.0

运行界面如下所示,目前是 Beta 版,有些 bug。

 运行参数说明。

--host <host> :指定主机地址,默认是本机地址(localhost);
--appdir <appdir> : 指定存储应用程序文件(例如:数据库和生成的用户文件)的目录。默认情况下,设置为用户主目录中的 .autogenstudio 目录;
--port <port> : 指定端口号。默认情况下为 8080;
--reload :用于在代码发生更改时启用服务器的自动重新加载。默认情况下,它设置为 False;
--database-uri :指定数据库 URI,示例值包括 SQLite 的 sqlite:///database.sqlite 和 PostgreSQL 的 postgresql+psycopg://user:password@localhost/dbname。如果未指定,数据库 URIL 默认为 --appdir 目录中的 database.sqlite 文件。

二、AutoGen Studio 使用

下面对 AutoGen Studio 具体使用进行说明。

2.1 Skills

这个部分是定义 Agent 能够调用的 python 函数。

默认定义了两个函数,一个是根据问题生成图片,一个是根据问题生成 pdf 报告。

2.1.1 generate_and_save_images

左侧是具体代码,右侧是函数名称和描述等。

上图左侧代码部分。 


from typing import List
import uuid
import requests  # to perform HTTP requests
from pathlib import Path

from openai import OpenAI


def generate_and_save_images(query: str, image_size: str = "1024x1024") -> List[str]:
    """
    Function to paint, draw or illustrate images based on the users query or request. Generates images from a given query using OpenAI's DALL-E model and saves them to disk.  Use the code below anytime there is a request to create an image.

    :param query: A natural language description of the image to be generated.
    :param image_size: The size of the image to be generated. (default is "1024x1024")
    :return: A list of filenames for the saved images.
    """

    client = OpenAI()  # Initialize the OpenAI client
    response = client.images.generate(model="dall-e-3", prompt=query, n=1, size=image_size)  # Generate images

    # List to store the file names of saved images
    saved_files = []

    # Check if the response is successful
    if response.data:
        for image_data in response.data:
            # Generate a random UUID as the file name
            file_name = str(uuid.uuid4()) + ".png"  # Assuming the image is a PNG
            file_path = Path(file_name)

            img_url = image_data.url
            img_response = requests.get(img_url)
            if img_response.status_code == 200:
                # Write the binary content to a file
                with open(file_path, "wb") as img_file:
                    img_file.write(img_response.content)
                    print(f"Image saved to {file_path}")
                    saved_files.append(str(file_path))
            else:
                print(f"Failed to download the image from {img_url}")
    else:
        print("No image data found in the response!")

    # Return the list of saved files
    return saved_files


# Example usage of the function:
# generate_and_save_images("A cute baby sea otter")

2.1.2 generate_and_save_pdf

左侧是具体代码,右侧是函数名称和描述等。

上图左侧代码部分。

import uuid
import requests
from fpdf import FPDF
from typing import List, Dict, Optional
from pathlib import Path
from PIL import Image, ImageDraw, ImageOps
from io import BytesIO

def generate_and_save_pdf(
    sections: List[Dict[str, Optional[str]]], 
    output_file: str = "report.pdf", 
    report_title: str = "PDF Report"
) -> None:
    """
    Function to generate a beautiful PDF report in A4 paper format. 

    :param sections: A list of sections where each section is represented by a dictionary containing:
                     - title: The title of the section.
                     - level: The heading level (e.g., "title", "h1", "h2").
                     - content: The content or body text of the section.
                     - image: (Optional) The URL or local path to the image.
    :param output_file: The name of the output PDF file. (default is "report.pdf")
    :param report_title: The title of the report. (default is "PDF Report")
    :return: None
    """

    def get_image(image_url_or_path):
        if image_url_or_path.startswith("http://") or image_url_or_path.startswith("https://"):
            response = requests.get(image_url_or_path)
            if response.status_code == 200:
                return BytesIO(response.content)
        elif Path(image_url_or_path).is_file():
            return open(image_url_or_path, 'rb')
        return None

    def add_rounded_corners(img, radius=6):
        mask = Image.new('L', img.size, 0)
        draw = ImageDraw.Draw(mask)
        draw.rounded_rectangle([(0, 0), img.size], radius, fill=255)
        img = ImageOps.fit(img, mask.size, centering=(0.5, 0.5))
        img.putalpha(mask)
        return img

    class PDF(FPDF):
        def header(self):
            self.set_font("Arial", "B", 12)
            self.cell(0, 10, report_title, 0, 1, "C")
            
        def chapter_title(self, txt): 
            self.set_font("Arial", "B", 12)
            self.cell(0, 10, txt, 0, 1, "L")
            self.ln(2)
        
        def chapter_body(self, body):
            self.set_font("Arial", "", 12)
            self.multi_cell(0, 10, body)
            self.ln()

        def add_image(self, img_data):
            img = Image.open(img_data)
            img = add_rounded_corners(img)
            img_path = Path(f"temp_{uuid.uuid4().hex}.png")
            img.save(img_path, format="PNG")
            self.image(str(img_path), x=None, y=None, w=190 if img.width > 190 else img.width)
            self.ln(10)
            img_path.unlink()

    pdf = PDF()
    pdf.add_page()
    font_size = {"title": 16, "h1": 14, "h2": 12, "body": 12}

    for section in sections:
        title, level, content, image = section.get("title", ""), section.get("level", "h1"), section.get("content", ""), section.get("image")
        pdf.set_font("Arial", "B" if level in font_size else "", font_size.get(level, font_size["body"]))
        pdf.chapter_title(title)

        if content: pdf.chapter_body(content)
        if image:
            img_data = get_image(image)
            if img_data:
                pdf.add_image(img_data)
                if isinstance(img_data, BytesIO):
                    img_data.close()

    pdf.output(output_file)
    print(f"PDF report saved as {output_file}")

# # Example usage
# sections = [
#     {
#         "title": "Introduction - Early Life",
#         "level": "h1",
#         "image": "https://picsum.photos/536/354",
#         "content": ("Marie Curie was born on 7 November 1867 in Warsaw, Poland. "
#                     "She was the youngest of five children. Both of her parents were teachers. "
#                     "Her father was a math and physics instructor, and her mother was the head of a private school. "
#                     "Marie's curiosity and brilliance were evident from an early age."),
#     },
#     {
#         "title": "Academic Accomplishments",
#         "level": "h2",
#         "content": ("Despite many obstacles, Marie Curie earned degrees in physics and mathematics from the University of Paris. "
#                     "She conducted groundbreaking research on radioactivity, becoming the first woman to win a Nobel Prize. "
#                     "Her achievements paved the way for future generations of scientists, particularly women in STEM fields."),
#     },
#     {
#         "title": "Major Discoveries",
#         "level": "h2",
#         "image": "https://picsum.photos/536/354",
#         "content": ("One of Marie Curie's most notable discoveries was that of radium and polonium, two radioactive elements. "
#                     "Her meticulous work not only advanced scientific understanding but also had practical applications in medicine and industry."),
#     },
#     {
#         "title": "Conclusion - Legacy",
#         "level": "h1",
#         "content": ("Marie Curie's legacy lives on through her contributions to science, her role as a trailblazer for women in STEM, "
#                     "and the ongoing impact of her discoveries on modern medicine and technology. "
#                     "Her life and work remain an inspiration to many, demonstrating the power of perseverance and intellectual curiosity."),
#     },
# ]

# generate_and_save_pdf_report(sections, "my_report.pdf", "The Life of Marie Curie")

2.2 Models

创建可以在代理和工作流中重复使用的模型配置,默认提供了四个模型的配置例子,如下所示。

模型配置参数包括模型名称、URL、 API Key 等,如下所示。

2.3 Agents

配置可以在 Agent 工作流程中重复使用的 Agent,可以创建一个 Agent 组(群聊),可以有多个 Agent。

Agent 配置参数。 

2.4 Workflows

工作流其实是一种编排,默认有两个工作流,一个是默认工作流(两个Agent),一个是旅行计划工作流(一个用户代理Agent,一个群聊Agent)。

 当然,可以自行设计工作流。

2.5 Playground

创建 Session,选择工作流,如下所示。

参考链接:

[1] https://github.com/microsoft/autogen

[2] User Guide | AutoGen

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
AutoGen Studio是一款用于嵌入式系统开发的集成开发环境(IDE),它提供了丰富的工具和功能来简化嵌入式开发过程。以下是使用AutoGen Studio的基本步骤: 1. 下载和安装:从AutoGen Studio官方网站下载最新版本的软件,并按照安装向导进行安装。 2. 创建项目:打开AutoGen Studio,在菜单栏中选择“File(文件)”->“New Project(新建项目)”来创建一个新项目。根据你的需求选择项目类型,比如C/C++项目或者嵌入式项目。 3. 配置目标平台:在创建项目时,你需要选择目标平台的硬件架构和操作系统类型。根据你的目标平台选择合适的配置。 4. 编写代码:在AutoGen Studio中,你可以使用内置的文本编辑器来编写代码。创建源文件、头文件等,并编写你的应用程序逻辑。 5. 构建项目:在菜单栏中选择“Build(构建)”->“Build Project(构建项目)”来编译你的项目。如果有错误,需要解决这些错误,直到项目成功编译通过。 6. 调试和测试:AutoGen Studio提供了强大的调试功能,可以帮助你调试和测试你的嵌入式应用程序。使用调试器来设置断点、监视变量和跟踪程序执行流程。 7. 部署和运行:完成调试后,你可以将生成的可执行文件部署到目标硬件上运行。根据目标平台的要求,将可执行文件烧录到硬件设备中,并验证应用程序的功能。 除了以上基本步骤外,AutoGen Studio还提供了其他功能,如版本控制、代码自动完成、代码生成器等,可以根据你的需求进一步探索和使用。 此外,AutoGen Studio官方网站上提供了详细的使用教程和文档,你可以参考这些资源来深入了解和学习AutoGen Studio的各种功能和用法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值