目录
一、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