AI

构建用于 LinkedIn 帖子分析的多模态多智能体 AI 系统

构建一个 CrewAI 系统,由多个并行智能体分析 LinkedIn 帖子的文本和图片,并通过 Bright Data 获取帖子数据。
9 分钟阅读
构建用于 LinkedIn 帖子分析的多模态多智能体 AI 系统

在这篇博客文章中,你将了解:

  • 为什么企业级 LinkedIn 帖子分析系统不仅要审查帖子的文本,还要分析其中的图片。
  • 如何使用多智能体工作流设计这样的系统。
  • 如何使用 CrewAI 实现这套用于 LinkedIn 帖子分析(文本 + 图片)的多模态、多智能体 AI 系统,并通过并行执行实现最佳性能和可扩展性。

让我们开始吧!

为什么需要多模态 LinkedIn 帖子分析

大多数用于分析 LinkedIn 帖子的系统和工作流都只关注帖子文本。这种方式存在很大局限,尤其是考虑到图片所发挥的关键作用!

研究和多个来源(此处、此处和此处)表明,带有图片的 LinkedIn 帖子可以获得:

  • 更高的互动率:与纯文本帖子相比,图片可让互动率翻倍。
  • 更多浏览量:加入原创且相关的图片可以显著提升浏览量。
  • 更广的覆盖范围:互动会向 LinkedIn 算法发出内容有价值的信号,帮助帖子覆盖更多受众。
  • 更广泛的吸引力:图片可以在需要频繁滚动的信息流中吸引注意力,促使偏好视觉内容而非文本的用户参与互动。

因此,企业级 LinkedIn 帖子分析系统不应止步于文本分析。它还应该分析帖子所附的图片,从而提供完整的多模态评估。

下一章将介绍如何构建这样的解决方案!

用于 LinkedIn 帖子分析(文本 + 图片)的多模态 AI 智能体:企业级架构

实现一个可同时分析 LinkedIn 帖子文本和图片的 AI 系统是一项复杂的任务。一种实用的方法是采用多智能体系统,让每个智能体专门负责工作流中的不同部分:

  • LinkedIn 帖子检索智能体:使用 Bright Data 的 LinkedIn 帖子抓取工具获取原始 LinkedIn 帖子数据(文本、标题、图片等)。
  • 帖子文本分析智能体:分析帖子的文本和标题,提取主题、关键词和情感,并生成 Markdown 文本分析报告。
  • 帖子图片分析智能体:处理帖子附带的所有图片,生成文字描述,并在 Markdown 图片分析报告中评估图片与帖子内容的相关性。
  • 帖子报告生成智能体:将文本和图片分析智能体的输出合并为一份完整的帖子 Markdown 报告。

其中一些智能体相对通用,可以使用标准 LLM 实现(例如帖子文本分析智能体和帖子报告生成智能体)。其他智能体则需要专门的能力。

例如,LinkedIn 帖子检索智能体必须与可靠的抓取工具交互,以绕过 LinkedIn 的反机器人措施、检索公开帖子数据并以结构化格式返回。Bright Data 能够出色地完成这项任务,同时支持生产级的大规模工作负载。同样,帖子图片分析智能体可能需要借助视觉工具从图片中提取有意义的信息并执行多模态分析。

为节省时间,帖子文本分析智能体和帖子图片分析智能体应并行执行。处理单个 LinkedIn 帖子的 AI 工作流如下图所示:

LinkedIn 多模态、多智能体 AI 分析工作流

然后,为了进一步提高系统速度和可扩展性,你可以针对多个帖子并行运行此工作流(每个多智能体 AI 实例处理一个帖子)。本指南将具体演示这一过程!

由于 CrewAI 非常适合编排多智能体系统,因此我们将采用它作为实现该工作流的 AI 智能体构建框架。通过将其与 Bright Data 集成,你将能够创建上述功能完整的多模态、多智能体 LinkedIn 帖子分析系统。

如何使用 CrewAI 构建用于 LinkedIn 帖子分析的多模态、多智能体 AI 系统

按照以下步骤构建用于多模态 LinkedIn 帖子分析的多智能体 AI 系统。该系统可接收多个 LinkedIn 帖子 URL 并行处理,并将文本和视觉分析整合为结构化的最终报告。

代码示例使用 Python 和 CrewAI 编写,并集成 Bright Data 和 OpenAI,为智能体提供所需的工具和能力。

前提条件

要学习本教程,请确认你具备以下条件:

如果你还没有 Bright Data 账户,也不必担心。后续专门的步骤将指导你完成账户设置。

此外,你还必须充分理解 CrewAI 的主要概念及其工作原理,尤其是通过 YAML 文件进行配置时(这是推荐方式)。了解 Bright Data CrewAI 工具的运行方式也会非常有帮助。

步骤 1:设置 CrewAI 项目

正如官方安装指南所述,安装 CrewAI 需要使用 uv。首先使用 pip 在全局安装 uv:

pip install uv

或者,按照适用于你操作系统的 uv 安装教程操作。

使用 uv 在系统中全局安装 CrewAI:

uv tool install crewai 

现在,使用以下命令创建一个名为 linkedin_post_crew 的新 CrewAI 项目:

crewai create crew linkedin_post_crew

系统会提示你选择 AI 提供商。对于此工作流,请选择 OpenAI(1),选择一个模型(稍后我们会进行更改),然后输入你的 OpenAI API 密钥:

在 CrewAI 模型中配置 AI 集成

完成此步骤后,你的项目文件夹(linkedin_post_crew/)将具有以下结构:

linkedin_post_crew/
├── knowledge/
│   └── user_preference.txt
├── src/
│   └── linkedin_post_crew
│       ├── config/
│       │   ├── agents.yaml
│       │   └── tasks.yaml
│       ├── tools/
│       │   ├── custom_tool.py
│       │   └── __init__.py
│       ├── crew.py
│       ├── main.py
│       └── __init__.py
├── test/
├── .env
├── .gitignore
├── pyproject.toml
└── README.md

如果你不熟悉 CrewAI 生成的文件,请参阅官方安装指南。

使用你偏好的 Python IDE 打开项目,例如 Visual Studio Code(安装 Python 扩展)或 PyCharm。熟悉 CrewAI 生成的文件。

接下来,在终端中进入项目文件夹:

cd linkedin_post_crew

在其中初始化一个 Python 虚拟环境:

uv venv

在 Linux 和 macOS 上,使用以下命令激活虚拟环境:

source .venv/bin/activate

或者,在 Windows 上执行:

.venv/Scripts/activate

完成!你现在已经有了一个空白 CrewAI 项目,可用于多模态、多智能体 AI 开发。

步骤 2:配置 OpenAI 模型

查看 crewai create 命令生成的 .env 文件。该文件包含所选的 OpenAI 模型和你的 OpenAI API 密钥:

MODEL=<SELECTED_OPENAI_MODEL>
OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>

在 CLI 中,系统只提示你选择 GPT-4 模型,但你也可以设置最新的 GPT-5 模型。例如,按如下方式更新 MODEL 环境变量:

MODEL=gpt-5-mini

根据我们的测试,gpt-5-mini 是 GPT-5 系列中最适合处理来自互联网的长输入(例如 LinkedIn 帖子及其相关图片)的模型之一。它具备用于分析图片的视觉能力,并支持通过工具调用与 Bright Data CrewAI 工具交互。这些特点使其非常适合为 Crew 中的每个智能体提供支持。

为了获得更高的灵活性,可以考虑集成多个 AI 连接,以便根据 Crew 中每个智能体处理的具体任务,为其分配最合适的模型。

步骤 3:配置 Crew 以并行执行

为了让此 AI 应用达到企业级要求并具备高度可扩展性,它应能够同时处理多个 LinkedIn 帖子。

换句话说,每个帖子都应由一个单独且并发运行的 Crew 实例处理。此外,为提高灵活性,你应该从配置文件中读取输入的 LinkedIn 帖子。这样一来,每次运行不同分析时都无需修改代码。下面来实现这一行为!

在 src/linkedin_post_crew/main.py 中,首先添加从配置文件(即 input/posts.json)读取 LinkedIn 帖子 URL 的逻辑:

import json
from pathlib import Path

# ...

def load_linkedin_posts(filepath: str) -> list[dict]:
    """
    Read a JSON file containing LinkedIn posts and return the list of posts.
    The file must have the structure: { "posts": [ ... ] }
    """
    path = Path(filepath)
    if not path.exists():
        raise FileNotFoundError(f"File not found: {filepath}")

    with open(path, "r", encoding="utf-8") as f:
        data = json.load(f)

    if "posts" not in data or not isinstance(data["posts"], list):
        raise ValueError("Invalid JSON format: expected an object with a 'posts' array")

    return data["posts"]

在项目顶层创建一个 input/ 文件夹,并在其中添加 posts.json 文件:

项目“input/”文件夹中的 posts.json 文件

使用一些公开的 LinkedIn 帖子 URL 填充该文件,如下所示:

{
  "posts": [
    "https://www.linkedin.com/posts/antonello-zanini_ai-machinelearning-webscraping-activity-7348656708367343617-a8o7",
    "https://www.linkedin.com/posts/antonello-zanini_ai-tools-tech-activity-7338574311957450753-klLg"
  ]
}

最终的 CrewAI 应用将读取这两个 URL,并行处理对应的 LinkedIn 帖子。

现在,为了跟踪 Crew 生成的中间输出,你应该将每项任务的结果导出到同一个文件夹。例如,将该文件夹命名为 output/。为了更好地组织文件,请将每次运行的文件存储在以 LinkedIn 帖子 ID 命名的子文件夹中。这样,每个已分析的帖子都会拥有各自独立且整齐的输出文件夹:

output/
├── 7338574311957450753/
│    └── ...
└── 7348656708367343617/
     └── ...

为此,请添加一个辅助函数,用于从 LinkedIn 帖子 URL 中提取帖子 ID(如果未找到,则默认使用时间戳):

import re
import time

# ...

def extract_linkedin_post_id(url: str) -> str | None:
    """
    Extract the LinkedIn post ID from a LinkedIn post URL.
    If no ID is found, return a timestamp as default.

    Returns:
        The post ID as a string (either extracted or timestamp fallback).
    """

    # Match formats like ...activity-7348656708367343617...
    match = re.search(r"activity-(\d+)", url)
    if match:
        return match.group(1)

    # Default to the current timestamp
    return str(int(time.time()))

对于以下输入 URL:

https://www.linkedin.com/posts/antonello-zanini_ai-machinelearning-webscraping-activity-7348656708367343617-a8o7

extract_linkedin_post_id() 函数将返回:

7348656708367343617

然后,添加逻辑以从 input/posts.json 读取 LinkedIn 帖子,将每个帖子分配给一个 Crew,并以异步方式同时运行:

import asyncio

# ...

async def run_crew(linkedin_post_url):
    # Run the crew on the given LinkedIn post URL
    inputs = {
        "linkedin_post_url": linkedin_post_url,
    }
    linkedin_post_id = extract_linkedin_post_id(linkedin_post_url)

    try:
        await LinkedinPostCrew(
            linkedin_post_id=linkedin_post_id
        ).crew().kickoff_async(inputs=inputs)
    except Exception as e:
        raise Exception(f"An error occurred while running the crew: {e}")

async def run_async():
    # Read all LinkedIn posts to analyze from the configs
    linkedin_posts = load_linkedin_posts("./input/posts.json")
    # Define the crew tasks
    crews = [run_crew(post) for post in linkedin_posts]

    # Run them all in parallel
    await asyncio.gather(*crews)

def run():
    asyncio.run(run_async())  

有关 Crew 异步启动的更多信息,请参阅文档。请注意,在上面的代码片段中,配置的输入文件是 input/posts.json(即之前创建的文件)。

最后,在 crew.py 中,修改生成的 LinkedinPostCrew 类,使其读取 linkedin_post_id 输入,并为每个帖子 ID 创建单独的输出目录:

# ...

@CrewBase
class LinkedinPostCrew():
    # ...

    def __init__(self, linkedin_post_id: str):
        self.linkedin_post_id = linkedin_post_id

        # Create the output directory if it does not exist
        Path(f"./output/{self.linkedin_post_id}").mkdir(parents=True, exist_ok=True)

    # @agent definitions...
    # @task definitions...
    # @crew definition...

这些逻辑共同让你的 AI 系统能够:

  1. 从 JSON 文件读取 LinkedIn 帖子 URL。
  2. 提取唯一的帖子 ID。
  3. 为每个帖子异步运行单独的 Crew 实例,并让每个实例将中间结果导出到 output/ 中的专用子文件夹(按帖子 ID 组织)。

此结构可确保你的多模态、多智能体 LinkedIn 帖子分析器高效并行处理多个帖子,从而满足企业级工作负载的要求!

步骤 4:安装并配置 CrewAI Bright Data 工具

如果没有合适的工具,AI 模型无法自行检索 LinkedIn 帖子内容。此外,即使模型配备了网页爬虫工具,也可能因浏览器指纹识别和 CAPTCHA 等高级反爬虫防御而难以完成任务。

这正是 Bright Data 不可或缺的原因。它提供由企业级云工具支持的 AI 就绪基础设施。其拥有超过 1.5 亿个动态 IP 的代理网络可确保无限扩展能力,而集成的反机器人工具包则能防止封锁和检测。

请注意,CrewAI 通过专用内置工具正式支持 Bright Data,因此集成过程非常简单。在支持的工具中,BrightDataDatasetTool 尤为突出。它可以让你直接访问 Bright Data 丰富的爬虫 API 目录,其中包括专用的 LinkedIn 帖子抓取工具。

在此步骤中,你将开始使用 CrewAI 的 Bright Data 工具,以便让LinkedIn 帖子抓取智能体使用这些工具提取 LinkedIn 数据。

首先,注册 Bright Data 账户。如果已有账户,请直接登录。然后,按照 Bright Data 官方指南创建 API 密钥(需具备管理员权限)。按如下方式将 API 密钥存储在 .env 文件中:

BRIGHT_DATA_API_KEY="<YOUR_BRIGHT_DATA_API_KEY>"

将 <YOUR_BRIGHT_DATA_API_KEY> 占位符替换为你的 Bright Data API 密钥实际值。

接下来,在已激活的虚拟环境中安装 CrewAI Bright Data 工具依赖项:

uv add crewai[tools] aiohttp requests

然后,将 BrightDataDatasetTool Bright Data 工具导入你的 crew.py 文件:

from crewai_tools import BrightDataDatasetTool

CrewAI Bright Data 工具会自动从 BRIGHT_DATA_API_KEY 变量(你已在 .env 文件中设置)读取 API 密钥。具体来说,它使用该密钥对发送至 Bright Data 服务器的 API 请求进行身份验证。

如需查看示例和进一步了解相关内容,请参阅以下 CrewAI + Bright Data 智能体实现:

一切准备就绪!你的智能体现在可以安全连接到 Bright Data,并开始检索 LinkedIn 帖子数据(以及更多内容!)。

步骤 5:构建 LinkedIn 帖子检索智能体

现在,你已经具备构建第一个智能体所需的一切条件。首先构建LinkedIn 帖子检索智能体,它负责根据给定 URL 检索 LinkedIn 帖子数据。

在 crew.py 中,删除 crewai create 生成的所有初始 @agent 和 @task 定义。然后,按如下方式定义 CrewAI 智能体及其主要任务:

@agent
def linkedin_post_scraper(self) -> Agent:
    return Agent(
        config=self.agents_config["linkedin_post_scraper"],
        tools=[
            BrightDataDatasetTool(
                result_as_answer=True,
            )
        ],
    )

@task
def linkedin_post_scraping_task(self) -> Task:
    return Task(
        config=self.tasks_config["linkedin_post_scraping_task"],
        output_file=f"./output/{self.linkedin_post_id}/post.json",
    )

由于此任务需要调用第三方工具,因此必须启用重试逻辑,以防工作流因临时网络问题或工具错误而失败。CrewAI 会自动处理此操作,默认最多重试 3 次(通过 guardrail_max_retries 字段控制)。

另外,请注意,Agent 类直接与 BrightDataDatasetTool 集成。参数 result_as_answer=True 会强制将工具结果作为智能体的输出。这正是我们所需要的,因为该智能体只需检索 LinkedIn 帖子数据,无需进行处理。BrightDataDatasetTool 输出的原始结构化 JSON 非常适合!

请注意,Task 被配置为将此输出(来自 BrightDataDatasetTool 的原始 JSON)存储到 output/ 嵌套文件夹下的 post.json 文件中,如步骤 3 所述。

接下来,在 agents.yaml 配置文件中按如下方式指定LinkedIn 帖子检索智能体:

linkedin_post_scraper:
  role: >
    LinkedIn Post Retriever
  goal: >
    Use Bright Data's Dataset Tool (dataset_type="linkedin_posts") to scrape the LinkedIn post at:
    {linkedin_post_url}.
  backstory: >
    You are an agent capable of scraping LinkedIn posts by connecting to Bright Data's dataset solution.

并在 tasks.yaml 中定义其主要任务:

linkedin_post_scraping_task:
  description: >
    1. Call the Bright Data Dataset Tool (dataset_type="linkedin_posts") to retrieve the content from "{linkedin_post_url}".
    2. Poll until the post has been successfully scraped.
    3. Return the raw JSON response from the tool.
  expected_output: >
    The raw JSON containing the LinkedIn post information returned by Bright Data's Dataset Tool.
  agent: linkedin_post_scraper

可以看到,此任务使用 {linkedin_post_url} 从 CrewAI 输入中读取 URL,它对应于步骤 3 中运行 kickoff_async() 时传递给 Crew 的输入。

而 dataset_type="linkedin_posts" 引用则指向BrightDataDatasetTool 中提供的 LinkedIn 帖子抓取工具数据集:

获取所需 Bright Data 数据集的 ID

简单来说,这些信息会指示 BrightDataDatasetTool 使用标记为 "linkedin_posts" 的数据集。换言之,它会指示工具通过调用 Bright Data 中正确的爬虫 API(即 LinkedIn 帖子抓取工具)来检索 LinkedIn 帖子数据。有关更多详情,请参阅 Bright Data 文档。

该智能体会在对应的输出文件夹中生成一个 post.json 文件,如下所示:

post.json 任务输出文件,其中包含通过 Bright Data 获取的 LinkedIn 帖子数据

该文件包含详细的结构化 LinkedIn 帖子数据,并以 JSON 格式提供,可直接用于分析。它使用 BrightDataDatasetTool 检索而来,数据基于 Crew 输入中指定的 LinkedIn 帖子。

通过与原始 LinkedIn 帖子进行比较,验证数据的准确性:

输入的 LinkedIn 帖子之一

很好!LinkedIn 帖子检索智能体已经完成。现在可以使用类似的逻辑定义工作流中的其余智能体。

步骤 6:定义帖子文本分析智能体

检索 LinkedIn 帖子数据后,下一步是并行分析其文本和图片。我们将从相对简单的文本开始。目标是定义一个能够分析你所需内容的智能体。

在 crew.py 中,使用以下逻辑实现帖子文本分析智能体:

@agent
def post_text_analyst(self) -> Agent:
    return Agent(
        config=self.agents_config["post_text_analyst"],
    )

@task
def post_text_analysis_task(self) -> Task:
    return Task(
        config=self.tasks_config["post_text_analysis_task"],
        context=[self.linkedin_post_scraping_task()],
        markdown=True,
        async_execution=True,
        output_file=f"./output/{self.linkedin_post_id}/post_text_report.md",
    )

请注意,该智能体的主要任务会将抓取任务的输出作为其上下文。换言之,这会将LinkedIn 帖子检索智能体的输出连接到帖子文本分析智能体。此外:

  • markdown=True 确保此任务的输出生成为 Markdown 文件。
  • async_execution=True 允许此任务异步运行。这意味着工作流中的下一个智能体会等待此智能体及所有其他相连的异步智能体完成后再继续执行。简而言之,该机制允许你并发运行文本和图片分析任务,从而节省时间。

接下来,在 agents.yaml 中配置该智能体:

post_text_analyst:
  role: >
    Post Text Analyzer
  goal: >
    Create a detailed report analyzing the input LinkedIn post's text and headline.
  backstory: >
    You are an NLP expert who analyzes text to uncover meaning, themes,
    and main keywords, performs sentiment analysis, and provides insights
    on all these aspects of a LinkedIn post.

并在 tasks.yaml 中指定其主要任务:

post_text_analysis_task:
  description: >
    1. Retrieve the `headline` and `post_text` fields from the given LinkedIn post.
    2. Analyze the `headline` and `post_text` to uncover meaning, themes,
       and main keywords.
    3. Perform sentiment analysis on the post.
    4. Combine the results from the previous steps into a comprehensive
       Markdown report.
  expected_output: >
    A Markdown report containing a detailed and comprehensive analysis of
    the LinkedIn post's text.
  agent: post_text_analyst 

请注意,headline 和 post_text 是 LinkedIn 帖子抓取工具输出架构中的两个字段。实际上,对该 Bright Data API 的每次请求(无论是直接发出,还是像本例一样通过 CrewAI 集成发出)都会生成一个包含这两个字段的结构化 JSON 对象。

在你的 Bright Data 账户中查看 LinkedIn 帖子抓取工具的输出架构。

很好!帖子文本分析智能体现已实现,可以为你的 LinkedIn 帖子生成详细的文本分析报告。

该智能体将生成详细的 post_text_report.md,类似于以下内容:

由 Post Text Analyzer 智能体生成的真实 post_text_report.md 文件示例

步骤 7:创建帖子图片分析智能体

由于这是一个多模态 AI 系统,它不仅会分析帖子的文本,还会分析其中的图片(如果存在)。

CrewAI 通过 multimodal=True 参数支持多模态智能体,该参数会为智能体配备用于处理非文本内容的必要工具。其中一个工具是 AddImageTool,它允许智能体处理图片。

为了改善视觉分析,你还可以提供内置的 VisionTool 工具。该工具可帮助智能体从图片中提取相关信息。

如果不启用多模态模式并添加这些工具,帖子图片分析智能体很可能会生成类似以下内容的消息:

“我无法直接进行像素级访问,因而无法从该 URL 下载或渲染图片文件”

这意味着为智能体提供支持的 LLM 无法直接访问图片,只能根据帖子文本进行推断(这并不是我们想要的!)

请记住,LinkedIn 帖子可以包含多张图片。具体来说,这些图片的 URL 存储在 LinkedIn 帖子抓取工具输出架构的 images 数组中:

请注意包含帖子图片 URL 的“images”数组

帖子图片分析智能体应访问该数组,并逐一处理和分析每张图片。这就是你即将在智能体主要任务中实现的逻辑。

在 crew.py 中,使用以下代码行定义智能体及其任务:

from crewai_tools import VisionTool

@agent
def post_image_analyst(self) -> Agent:
    return Agent(
        config=self.agents_config["post_image_analyst"],
        multimodal=True, # <----------
    )

@task
def post_image_analysis_task(self) -> Task:
    return Task(
        config=self.tasks_config["post_image_analysis_task"],
        context=[self.linkedin_post_scraping_task()],
        tools=[VisionTool()], # <----------
        async_execution=True,
        markdown=True,
        output_file=f"./output/{self.linkedin_post_id}/post_image_report.md",
    )  

请注意,这里使用了 multimodal=True 和 VisionTool(),以帮助智能体正确处理图片。

接下来,在 agents.yaml 中配置该智能体:

post_image_analyst:
  role: >
    Post Image Analyzer
  goal: >
    Create a report analyzing the images of the input LinkedIn post.
  backstory: >
    You are a computer vision expert who can examine an image, provide
    a textual description and evaluate its relevance.

并在 tasks.yaml 中配置其主要任务:

post_image_analysis_task:
  description: >
    1. Retrieve the image URLs from the `images` field of the given LinkedIn post (if present).
    2. For each image:
       2.1 Provide a textual description of the image using the Visual Tool.
       2.2 Evaluate its relevance to the post text.
       2.3 Produce a Markdown analysis containing all this information.
    3. Combine the individual Markdown analyses for each image into a final comprehensive Markdown report.
  expected_output: >
    If the post contains at least one image, return a Markdown report that includes, for each image, a textual description and an evaluation of its relevance to the post text. Otherwise, return a message indicating that the post has no images.
  agent: post_image_analyst

可以看到,这里描述了前面解释的逻辑。

运行此智能体后,它将在正确的 output/ 嵌套文件夹中生成 post_image_report.md:

由 Post Image Analyzer 智能体生成的真实 post_image_report.md 文件

可以看到,该智能体准确识别并描述了图片。作为参考,以下是报告所涉及的 LinkedIn 图片:

由 Post Image Analyzer 智能体分析的 LinkedIn 帖子图片

太棒了!这个 AI 智能体最复杂的部分已经实现。

步骤 8:实现最终帖子报告生成智能体

现在,你已经获得了原始 LinkedIn 帖子数据及其文本和图片分析结果,可以将所有这些信息提供给最终报告生成智能体。

在 crew.py 中添加帖子报告生成智能体:

@agent
def post_report_generator(self) -> Agent:
    return Agent(
        config=self.agents_config["post_report_generator"],
    )

@task
def post_report_generation_task(self) -> Task:
    return Task(
        config=self.tasks_config["post_report_generation_task"],
        context=[self.linkedin_post_scraping_task(), self.post_text_analysis_task(), self.post_image_analysis_task()],
        markdown=True,
        output_file=f"./output/{self.linkedin_post_id}/post_report.md",
    )

请注意,该智能体的任务依赖于前三个智能体的输出。它会将LinkedIn 帖子检索智能体、帖子文本分析智能体和帖子图片分析智能体生成的所有数据合并为一份名为 post_report.md 的最终结构化 Markdown 报告。

接下来,在 agents.yml 中指定该智能体:

post_report_generator:
  role: >
    Post Report Generator
  goal: >
    Create a Markdown report that includes an analysis of both the text and images of a LinkedIn post.
  backstory: >
    You are a content analyst who summarizes all provided information in a clean, structured, and organized Markdown format for further human review.

并在 tasks.yml 中指定其任务:

post_report_generation_task:
  description: >
    1. Access the LinkedIn post data and extract the main information into a table
       (URL, author, headline, text, hashtags, etc.).
    2. Access the text analysis Markdown report.
    3. Access the image analysis Markdown report.
    4. Combine all this information into a final Markdown report that includes:
       - A summary table of the LinkedIn post and its stats
       - The text analysis section
       - The image analysis section
  expected_output: >
    A comprehensive, organized, structured, and detailed Markdown report
    containing the LinkedIn post summary, text analysis, and image analysis.
  agent: post_report_generator

任务完成!多模态 AI 系统中的所有智能体现已全部定义完毕。

步骤 9:完成 Crew 配置

现在 Crew 的所有构建模块都已就绪,请按如下方式完成配置:

@crew
def crew(self) -> Crew:
    return Crew(
        agents=self.agents,
        tasks=self.tasks,
        process=Process.sequential,
        output_log_file=f"./output/{self.linkedin_post_id}/run.txt",
    )

太棒了!crew.py 中的 LinkedinPostCrew 类已经完整实现。该 Crew 会使用步骤 3 中 main.py 定义的逻辑异步执行。

你的多模态、多智能体 AI 系统现已全面投入运行,可以高效处理 LinkedIn 帖子!

步骤 10:测试多模态、多智能体 AI 系统

假设你已完成本教程之前的所有步骤,并按照步骤 3 的说明配置了智能体,现在就可以运行 AI 系统了。

在继续之前,你可能需要在 crew.py 的智能体、任务和 Crew 定义中启用 verbose=True,以便在执行过程中获取详细日志。

然后,在项目文件夹中使用以下命令启动多模态、多智能体 AI 系统:

crewi run

如果启用了详细模式,你应该会在终端中看到类似以下内容的输出:

CrewAI 在 verbose 模式下生成的输出

你会注意到,AI 系统会并行运行每个 Crew。对于每次运行:

  • LinkedIn 帖子检索智能体使用 Bright Data 集成检索帖子的 JSON 内容,并将其作为 post.json 存储在对应的输出文件夹中。
  • 帖子文本分析智能体和帖子图片分析智能体并行运行,以分析之前检索到的 LinkedIn 帖子的文本和图片。完成后,将分别生成 post_text_report.md 和 post_image_report.md。
  • 帖子报告生成智能体将收集到的所有数据聚合为最终的 Markdown 报告。

执行可能需要一些时间,尤其是在处理图片时。完成后,项目根目录中的 output/ 文件夹将如下所示:

AI 系统执行后 output/ 文件夹中的嵌套文件夹和文件

例如,以下是为帖子 ID 7348656708367343617 生成的最终 post_report.md(该帖子在步骤 5 中提及,并且是 posts.json 中的第一项):

最终 Markdown 报告

这是一份超过 1,700 字、内容高度详尽的上下文报告,深入分析了该 LinkedIn 帖子,包括其文本和图片如何影响帖子表现。

注意:你可以通过修改 agents.yaml 和 tasks.yaml 中的描述,轻松自定义每个智能体的行为。例如,你可以专注于分析的特定方面,或添加更多可付诸实践的洞察。

大功告成!借助 CrewAI 中的 Bright Data 集成,你已成功构建一个企业级、高度可扩展的多智能体 AI 系统,它支持并发执行,可用于多模态 LinkedIn 帖子分析。

总结

本文介绍了如何利用 Bright Data 的 AI 集成能力,在 CrewAI 中构建一个用于 LinkedIn 帖子分析(文本 + 图片)的企业级、可扩展、多智能体、多模态 AI 系统。

这里介绍的 AI 工作流非常适合希望优化自身 LinkedIn 帖子,或研究他人帖子以了解其表现优异或不佳原因的公司和个人。

如需构建类似的高级 AI 工作流,请探索 Bright Data 面向 AI 的完整解决方案套件。

立即创建免费的 Bright Data 账户,开始集成我们强大、AI 就绪且可扩展的网页数据工具!

支持支付宝等多种支付方式

Antonello Zanini

技术写作

5.5 years experience

Antonello是一名软件工程师,但他更喜欢称自己为技术传教士。通过写作传播知识是他的使命。

Expertise
Web 开发 网页抓取 AI 集成