
机器学习算法有哪些
在进入编码部分之前,我们先对本文构建的系统的主要组件有一个清晰的理解。
这个工作流程包含四个 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产品阿颖