
如何用AI進行情感分析
這個工作流程包含四個 Agent,每個 Agent 都有專門的技能。
我們都知道這兒的任務是指一個整體任務,意味著 LLM 被要求完成一個單一的目標,即數據提取。
這種方法在處理更復雜、多步驟的任務時的局限性變得很明顯。下面列舉了其中的一些局限性:
在本節中,我們將探討如何利用智能體工作流程來創建一個系統,該系統在對用戶主題進行深入研究后,撰寫博客文章、LinkedIn 內容、Twitter 帖子。代碼結構如下圖所示:
project
|
|---Multi_Agents_For_Content_Creation.ipynb
|
data
|
|---- blog-post.md
|---- linkedin-post.md
|---- tweet.md
、
現在我們已經探討了每個 Agent 的角色,接下來我們看看如何實際創建它們,以及它們的角色和任務。
在此之前,讓我們設置一些前提條件,以便更好地實現這些角色和任務。
前提條件代碼是在 Google Colab notebook 中運行的,我們的用例只需要兩個庫:openai 和 crewai[tools],可以通過以下方式安裝它們。
%%bash
pip -qqq install 'crewai[tools]'
pip -qqq install youtube-transcript-api
pip -qqq install yt_dlp
成功安裝庫后,下一步是導入以下必要的模塊:
import os
from crewai import Agent
from google.colab import userdata
from crewai import Crew, Process
from crewai_tools import YoutubeChannelSearchTool
from crewai import Task
每個 Agent 都利用了 OpenAI 的 GPT-4o 模型,我們需要通過以下方式設置訪問模型的權限:
OPENAI_API_KEY = userdata.get('OPEN_AI_KEY')
model_ID = userdata.get('GPT_MODEL')
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
os.environ["OPENAI_MODEL_NAME"] = model_ID
環境變量及其值
通過 Agent 類,我們可以通過提供以下屬性來創建一個 Agent:role,goal,backstory 和 memory。
只有 Agent 1 或 Video Analyzer 具有額外的屬性:tools 和 allow_delegation。
大多數屬性是自解釋的,但我們還是來了解它們的具體含義。
現在,讓我們開始創建所有 Agent。
但是,在此之前,我們設置需要由第一個 Agent 使用的工具,以探索我的個人 YouTube 頻道。這是通過提供句柄 @techwithzoum 的 YouTubeChannelSearchTool 類實現的。
# The tool used by the topic researcher
youtube_tool = YoutubeChannelSearchTool(youtube_channel_handle='@techwithzoum')
1、Agent1 — 主題研究員
topic_researcher = Agent(
role='Topic Researcher',
goal='Search for relevant videos on the topic {topic} from the provided YouTube channel',
verbose=True,
memory=True,
backstory='Expert in finding and analyzing relevant content from YouTube channels, specializing in AI, Data Science, Machine Learning, and Generative AI topics.',
tools=[youtube_tool],
allow_delegation=True
)
在當前 Agent 定義中,后續 Agent 的定義與此相似,只是屬性值有所不同。一旦理解了這一點,后續 Agent 的定義就不再需要解釋。
blog_writer = Agent(
role='Blog Writer',
goal='Write a comprehensive blog post from the transcription provided by the Topic Researcher, covering all necessary sections',
verbose=True,
memory=True,
backstory='Experienced in creating in-depth, well-structured blog posts that explain technical concepts clearly and engage readers from introduction to conclusion.',
allow_delegation=False
)
# LinkedIn Post Agent
linkedin_post_agent = Agent(
role='LinkedIn Post Creator',
goal='Create a concise LinkedIn post summary from the transcription provided by the Topic Researcher.',
verbose=True,
memory=True,
backstory='Expert in crafting engaging LinkedIn posts that summarize complex topics and include trending hashtags for maximum visibility.',
allow_delegation=False
)
twitter_agent = Agent(
role='Twitter Content Creator',
goal='Create a short tweet from the transcription provided by the Topic Researcher that capture key points and insights',
verbose=True,
memory=True,
backstory='Specializes in distilling complex information into concise, impactful tweets that resonate with a tech-savvy audience.',
allow_delegation=False
)
我們注意到最后三個智能體都不再委托任務,這是因為它們的結果不需要被其他 Agent 處理。
完美!現在我們的 Agent 已經準備好了解對它們的期望了,這是通過 Task 類來實現的。
任務
人類在接到任務指示后會執行任務來交付結果。
同樣,Agent 也需要執行任務并交付結果,所需的屬性如下:
接下來,我們來看這些任務的 Python 實現。1、Agent 1 — 主題研究員
research_task = Task(
description="Identify and analyze videos on the topic {topic} from the specified YouTube channel.",
expected_output="A complete word by word report on the most relevant video found on the topic {topic}.",
agent=topic_researcher,
tools=[youtube_tool]
)
2、Agent 2 — 博客撰寫者
blog_writing_task = Task(
description=""" Write a comprehensive blog post based on the transcription provided by the Topic Researcher.
The article must include an introduction , step-by-step guides, and conclusion.
The overall content must be about 1200 words long.""",
expected_output="A markdown-formatted of the blog",
agent=blog_writer,
output_file='./data/blog-post.md'
)
3、Agent 3 — LinkedIn 帖子創建者
linkedin_post_task = Task(
description="Create a LinkedIn post summarizing the key points from the transcription provided by the Topic Researcher, including relevant hashtags.",
expected_output="A markdown-formatted of the LinkedIn post",
agent=linkedin_post_agent,
output_file='./data/linkedin-post.md'
)
4、Agent 4 — Twitter 帖子創建者
twitter_task = Task(
description="Create a tweet from the transcription provided by the Topic Researcher, including relevant hastags.",
expected_output="A markdown-formatted of the Twitter post",
agent=twitter_agent,
output_file='./data/tweets.md'
)
對于每個 Agent,屬性都是自解釋的,其結果為:
最后,我們協調 Agent 團隊,按照如下方式執行任務:
my_crew = Crew(
agents=[topic_researcher, linkedin_post_agent, twitter_agent, blog_writer],
tasks=[research_task, linkedin_post_task, twitter_task, blog_writing_task],
verbose=True,
process=Process.sequential,
memory=True,
cache=True,
max_rpm=100,
share_crew=True
)
在協調了 Agent 之后,是時候通過 kickoff 函數觸發它們了,該函數接收一個輸入字典作為參數。在這里,我們搜索的是我錄制的關于 GPT3.5 Turbo 微調與圖形界面的一個視頻。
topic_of_interest = 'GPT3.5 Turbo Fine-tuning and Graphical Interface'
result = my_crew.kickoff(inputs={'topic': topic_of_interest})
成功執行上述代碼將生成我們上面指定的三個 Markdown 文件。
以下是每個文件內容的結果顯示。作為錄制視頻的人,這些內容與教程中涉及的內容完全一致。
博客 Agent 未能提供百分之百的分步指南,用戶在執行代碼時可能會遇到問題,但它提供了對教程范圍的極好理解。
對于 LinkedIn 和 Twitter 帖子,結果非常出色!
你可以在下面鏈接中查看所有文件的內容。
https://github.com/keitazoumana/LLMs/tree/main/ai_agents_outputs
我對 Agent 的上述評估基于我對內容的個人熟悉程度。然而,在投產之前,需要更客觀且可擴展的評估方法。
以下是一些有效評估這些 AI Agent 的策略。
可以利用多個框架來執行這些評估,其中包括:
本文簡要介紹了如何利用 AI Agent 有效完成高級任務,而不是讓一個大語言模型單打獨斗。
本文章轉載微信公眾號@AI產品阿穎