我用不到 50 行代码编写了这个 AI 工具,可以自动制作 PPT 幻灯片🔥🚀

2025-05-24

我用不到 50 行代码编写了这个 AI 工具,可以自动制作 PPT 幻灯片🔥🚀

TL;DR

每个人都讨厌制作演示文稿;如果你不讨厌,那就应该做。但是,嘿,世界不是这样的。无论你是学者还是业内人士,PowerPoint 演示文稿都是不可避免的。

但它仍然很无聊。正如睿智的德怀特·施鲁特先生所说。

Dwight Schrute 在 Powerpoint 上

因此,我构建了一个由人工智能驱动的工具,可以从数据集自动创建 PPT。

它的作用如下。

  • 该工具可以访问 Google Sheets 来检索数据。
  • 使用 Python-pptx 库生成图表和关键点,将原始数据转化为可操作的见解。
  • 根据数据自动创建具有视觉吸引力的幻灯片。

技术栈

以下是我们完成此任务所需的库。

  • Composio:用于集成 Google Sheets 和代码解释器。
  • CrewAI:构建自动化 AI 应用程序的框架。

Composio - AI 应用的工具平台

以下是 Composio 的简要介绍。

它是一个开源平台,提供构建 AI 应用程序的工具解决方案。它允许您集成 GitHub、Jira、Linear 和 Slack 等第三方应用程序,以创建 AI 软件工程师、GitHub PR 代理等。

Composio 拥有超过 100 种工具和集成,允许开发人员构建全面的 AI 自动化软件。

在海滩上挣扎的家伙

请帮我们加一颗星。🥹

这将有助于我们创作更多这样的文章💖

为 Composio 代码库加星标 ⭐


CrewAI——构建人工智能自动化的框架

CrewAi 是一个开源框架,可轻松构建强大的 AI 代理。它支持所有流行的 LLM 推理提供商,例如 OpenAI、Anthropic、Google、Groq 等。

在此处了解有关 CrewAI 的更多信息


工作流程概述

以下是代理将要做的事情。

  • 从 Google 电子表格中获取数据。
  • 了解数据集的上下文。
  • 通过使用代码解释器执行代码来创建图表和图形。
  • 最后,它使用该python-pptx库创建 PowerPoint 幻灯片。

Powepoint(PPT)代理工作流程


让我们开始吧!

首先创建一个虚拟环境,就像任何 Python 项目一样。

python -m venv ppt-agent
cd ppt-agent
source bin/activate
Enter fullscreen mode Exit fullscreen mode

接下来,安装以下依赖项。

composio-crewai
langchain-openai
python-dotenv
Enter fullscreen mode Exit fullscreen mode
  • Composio-core:用于将工具和应用程序与AI应用程序集成的核心库。
  • langchain-openai:用于访问 OpenAI 模型。
  • python-dotenv:用于将.env变量加载到本地shell环境。

接下来,创建一个 .env 文件并为 OpenAI API 密钥添加环境变量。

OPENAI_API_KEY=your API key
Enter fullscreen mode Exit fullscreen mode

要创建 OpneAI API 密钥,请转到官方网站并在仪表板中创建 API 密钥。

OpenAI API 密钥


设置 Composio

接下来,您需要使用 Composio 登录并进行身份验证。

composio login
Enter fullscreen mode Exit fullscreen mode

这将打开一个标签,要求您登录。您可以使用 GitHub、Gmail 或任何电子邮件 ID 登录。

Composio 登录页面

登录后,将出现一个带有钥匙的屏幕。

Composio 身份验证密钥

将其复制并粘贴到终端。

现在,更新应用程序。

composio apps update
Enter fullscreen mode Exit fullscreen mode

接下来,集成 Google Sheet。

composio add googlesheet
Enter fullscreen mode Exit fullscreen mode

系统将提示您授予权限。批准后,一切就绪。

Google Sheet 权限页面

添加集成后,您可以从 Composio 仪表板对其进行监控。

用于集成的 Composio 仪表板


编写 AI 代理

现在一切就绪,让我们开始编码部分。

正如我所说,这个单文件代码只有不到 50 行。那么,让我们开始吧。

首先,导入库和模块并将.env变量加载到shell环境中。

from tabnanny import verbose
from urllib import response
from composio_crewai import ComposioToolSet, App, Action
from crewai import Crew, Agent, Task, Process
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
from pathlib import Path
import os
load_dotenv()
Enter fullscreen mode Exit fullscreen mode

实例化 GPT-4o。

llm = ChatOpenAI(model='gpt-4o')
Enter fullscreen mode Exit fullscreen mode

获取您要使用的 Google Sheet 的 ID。您可以从 Sheet 的 URL 中找到该 ID。

Google 表格 ID

GOOGLE_SHEET_ID = '1i8OwCM_o2E4tmpZ18-2Jgu8G42ntPWoUgGhfbcyxnoo'
Enter fullscreen mode Exit fullscreen mode

现在,初始化 Composio Toolset 实例以及来自代码解释器工具和 Google Sheets 的所有操作。

composio_toolset = ComposioToolSet(output_dir=Path("/Users/composio/Desktop/sample-projects/ppt_builder"))
tools = composio_toolset.get_tools(actions=[Action.CODEINTERPRETER_EXECUTE_CODE,
                                            Action.CODEINTERPRETER_GET_FILE_CMD,
                                            Action.CODEINTERPRETER_RUN_TERMINAL_CMD,
                                            Action.GOOGLESHEETS_BATCH_GET])
Enter fullscreen mode Exit fullscreen mode

以下是行动的摘要。

  • Action.CODEINTERPRETER_EXECUTE_CODE:执行并解释代码以进行计算或数据转换。
  • Action.CODEINTERPRETER_GET_FILE_CMD:从指定位置或目录检索并处理文件。
  • Action.CODEINTERPRETER_RUN_TERMINAL_CMD:直接在系统上执行终端或 shell 命令。
  • Action.GOOGLESHEETS_BATCH_GET:从 Google 表格中批量获取数据以供进一步处理或分析。

接下来,使用 CrewAI 定义 PPT 代理。

ppt_agent = Agent(
    role="Google Sheets Assistant",
    goal="Read Google Sheets Data",
    backstory=f"""
            You are an AI assistant specialising in creating PowerPoint presentations using the Python-PPTX library. 
            Your task is to analyze the Google Sheets data from the provided spreadsheet ID: {GOOGLE_SHEET_ID}. 
            Extract critical insights and generate relevant charts based on this data. 
            Finally, create a well-structured presentation that includes these charts and any necessary images, ensuring the formatting is professional and visually appealing. 
            Only the spreadsheet ID should be passed as input parameters when utilising the Google Sheets tool.
            NOTE: The user usually passes small sheets, so try to read the whole sheet at once and not via ranges.
    """,
    tools=tools,
)
Enter fullscreen mode Exit fullscreen mode

代理会根据角色、目标和背景故事等参数进行拒绝。这些附加信息有助于 LLM 在任务执行期间做出更好的响应。它提供了更多信息。

接下来,定义任务。

agent_task = Task(
    description=f"""
    Create a ppt on the google sheets: {GOOGLE_SHEET_ID}. 
    Create a sandbox
    First, retrieve the sheet's content, and then pip installs the python-pptx using a code interpreter.
    After that, run the code to create graphs from the data.
    Then, write the code using Python-pptx to create a PowerPoint.

    Ensure that the ppt is detailed and has proper formatting 
    that makes it look good. The graphs in it should be factual. 
    NOTE: Mostly, the user passes small sheets, so try to read the whole sheet at once and not via ranges.
    """,
    expected_output="Sheet was read, graphs were plotted, and Presentation was created",
    tools=tools,
    agent=ppt_agent,
    verbose=True,
)
Enter fullscreen mode Exit fullscreen mode

任务是程序中最关键的部分。以下是每个参数的含义。

  • 描述:这是一份关于LLM模式及其功能的详细指南。它逐步指导了LLM完成工作所需采取的所有行动。
  • 预期输出定义为 LLM 的预期最终输出。
  • 工具是我们之前定义的所有动作,用于帮助模型完成任务。
  • 代理就是我们在上一节中定义的PPT代理。
  • Verbose设置为 True,以输出详细的流程输出。

最后定义Crew并运行。

crew = Crew(
    agents=[ppt_agent],
    tasks=[agent_task],
    process=Process.sequential,
)

response= crew.kickoff()
Enter fullscreen mode Exit fullscreen mode

Crew 已将 PPT 代理、任务和流程设置为顺序。

把所有东西放在一起

from tabnanny import verbose
from urllib import response
from composio_crewai import ComposioToolSet, App, Action
from crewai import Crew, Agent, Task, Process
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
from pathlib import Path
import os
load_dotenv()

llm = ChatOpenAI(model='gpt-4o')
#Settings.llm = Groq(model="llama3-groq-70b-8192-tool-use-preview")
#llm = Groq(model="llama3-groq-70b-8192-tool-use-preview")

GOOGLE_SHEET_ID = '1i8OwCM_o2E4tmpZ18-2Jgu8G42ntPWoUgGhfbcyxnoo'
#GOOGLE_SHEET_LINK + 'https://docs.google.com/spreadsheets/d/1i8OwCM_o2E4tmpZ18-2Jgu8G42ntPWoUgGhfbcyxnoo/edit?gid=0#gid=0z'
composio_toolset = ComposioToolSet(output_dir=Path("/Users/composio/Desktop/sample-projects/ppt_builder"))
tools = composio_toolset.get_tools(actions=[Action.CODEINTERPRETER_EXECUTE_CODE,
                                            Action.CODEINTERPRETER_GET_FILE_CMD,
                                            Action.CODEINTERPRETER_RUN_TERMINAL_CMD,
                                            Action.GOOGLESHEETS_BATCH_GET])

ppt_agent = Agent(
    role="Google Sheets Assistant",
    goal="Read Google Sheets Data",
    backstory=f"""
            You are an AI assistant specialising in creating PowerPoint presentations using the Python-PPTX library. 
            Your task is to analyze the Google Sheets data from the provided spreadsheet ID: {GOOGLE_SHEET_ID}. 
            Extract critical insights and generate relevant charts based on this data. 
            Finally, create a well-structured presentation that includes these charts and any necessary images, ensuring the formatting is professional and visually appealing. 
            Only the spreadsheet ID should be passed as input parameters when utilising the Google Sheets tool.
            NOTE: The user usually passes small sheets, so try to read the whole sheet at once and not via ranges.
    """,
    tools=tools,
)

agent_task = Task(
    description=f"""
    Create a ppt on the google sheets: {GOOGLE_SHEET_ID}. 
    Create a sandbox
    First, retrieve the sheet's content, and then pip installs the python-pptx using a code interpreter.
    After that, run the code to create graphs from the data.
    Then, write the code using Python-pptx to create a PowerPoint.

    Ensure that the ppt is detailed and has proper formatting 
    that makes it look good. The graphs in it should be factual. 
    NOTE: Mostly, the user passes small sheets, so try to read the whole sheet at once and not via ranges.
    """,
    expected_output="Sheet was read, graphs were plotted, and Presentation was created",
    tools=tools,
    agent=ppt_agent,
    verbose=True,
)

crew = Crew(
    agents=[ppt_agent],
    tasks=[agent_task],
    process=Process.sequential,
)

response= crew.kickoff()
Enter fullscreen mode Exit fullscreen mode

现在,运行代码并查看代理的运行情况。

python main.py
Enter fullscreen mode Exit fullscreen mode

在终端日志中观察 AI 代理创建最终 PPT 的步骤。

  • 代理使用该GOOGLESHEETS_BATCH_GET操作从 Google 电子表格中检索数据。
  • 它通过分析检索到的信息来了解数据上下文。
  • 代理利用CODEINTERPRETER_EXECUTE_CODE动作根据数据绘制图表。
  • 最后,它利用该python-pptx库自动创建 PowerPoint 幻灯片并将其保存到指定路径。

完整代码可以 在 GitHub 上找到

这是代理运行的一个示例。👇


下一步是什么?

在本文中,您构建了一个 AI 工具,可以从电子表格自动创建 PowerPoint 演示文稿。然而,这些只是现实世界中一些简单的 PPT 幻灯片。为了构建更强大的代理,您可以对其进行迭代,添加更多工具和多种数据格式(PDF、CSV、网页等)。

这本身就是一款很棒的产品,而且我可以看到人们在实际工作中使用类似的东西。

如果您使用 Composio 进行构建,请在社交媒体上标记我们;我们很乐意在拥有超过 10,000 名读者的食谱和时事通讯中推荐您

开始使用 Composio

文章来源:https://dev.to/composiodev/tired-of-making-ppt-slides-see-how-i-automated-it-in-under-50-lines-of-code-4gbo
PREV
使用 Three.js 的太阳系
NEXT
我厌倦了通过 GitHub 解决问题,所以我创建了自己的 AI 机器人……🤔