🗒️跟着吴恩达学AI多智能体-3/17-创建多AI智能体
00 分钟
2024-5-25
2024-6-29
type
status
date
summary
slug
tags
category
password
URL
icon
😀
这里写文章的前言:
通过这段资料,我们学习了如何使用Crew AI框架构建一个多智能体系统。我们详细介绍了智能体的创建、任务的分配以及如何将它们结合在一起形成一个团队。这个过程展示了多智能体系统的强大功能和灵活性,特别是在自动化内容创作方面。通过这种方式,用户可以解放双手,实现高效自动化操作。这一课程还提供了更多关于多智能体系统的应用场景,激发了进一步探索和应用的兴趣。

课程内容

你已经学到了很多关于智能体的知识。现在你对它们的工作原理和用途有了更多了解。接下来,让我们动手实际构建我们的第一个多智能体系统。在这个案例中,我们将构建一个能够进行研究并为我们撰写文章的团队。你将看到这些智能体如何协同工作,通过多智能体合作创建有价值的内容。请继续关注,你一定会喜欢这一部分。让我们开始吧。
我对此非常兴奋。我们将深入讨论如何实际构建多智能体系统。我坚信这是未来的发展方向,将成为企业建设的关键部分,工程师们在工作和职业生涯中将发挥巨大的影响力。
首先,我们将学习如何构建我们的第一个多智能体系统,并尽量保持简单。我会从复制一些代码开始,确保过程中没有出现警告。我们将从Crew AI中导入三个主要组件:Agent(智能体)、Task(任务)和Crew(团队)。这三个类是构建多智能体系统的核心。
导入这些组件后,我们需要选择一个作为所有智能体“大脑”的LLM。在Crew AI中,你可以选择不同的LLM,默认情况下,它们都使用GPT-4,但在这个案例中,我们将使用GPT-3.5 Turbo。你可以通过设置环境变量openAI_model_name来实现。
接下来,我们开始创建智能体。智能体是构建多智能体系统的基石。在这个案例中,我们将创建三个不同的智能体:计划者(Planner)、撰写者(Writer)和编辑者(Editor)。这三个智能体将协同工作,帮助我们撰写出优秀的技术文章。
创建智能体非常简单,基本上是创建Agent类的实例。在创建实例时,有几个关键属性需要设置,即角色(role)、目标(goal)和背景故事(backstory)。例如,对于计划者,我们设置角色为内容规划者(Content Planner),目标是计划一个引人入胜且准确的内容。背景故事为智能体提供了更多的上下文信息,帮助其更好地执行任务。
然后,我们创建撰写者智能体和编辑者智能体,它们的角色、目标和背景故事类似,但具体内容会有所不同。撰写者负责根据计划者的研究撰写内容,编辑者则负责审阅内容,确保格式正确,质量高。
有了智能体后,我们需要为每个智能体创建任务。创建任务同样简单,就是创建Task类的实例。任务需要描述、预期输出和分配的智能体。例如,计划任务的描述是“研究最新趋势,查找关键人物和值得注意的新闻”,预期输出是一个全面的内容计划,包含大纲、受众分析、SEO关键词和资源。
最后,我们需要将智能体和任务结合起来,这就是Crew的作用。Crew将智能体和任务组织在一起,使它们作为一个单元运行。在这个例子中,我们设置了三个属性:智能体列表、任务列表和详细模式(verbose mode),以便查看执行过程中的日志。
运行多智能体系统非常简单,只需调用crew.kickoff方法,并传入一个输入字典,其中包含所有任务和智能体中用到的变量。在这个案例中,我们使用的主题是“人工智能的兴起”。
运行后,我们可以看到内容规划者开始工作,研究人工智能的最新趋势,并生成文章大纲。接着,内容撰写者利用这些信息撰写Markdown格式的博文,最后编辑者审阅内容,确保其质量。
notion image
通过这个过程,我们构建了一个简单但功能强大的多智能体系统。你也可以根据自己的需求尝试创建不同主题的内容。

原文字幕

All right, so you learned a lot about agents already. You now understand a little more about how they work, what they're meant for. Want to make them teak? Now let's get our hands on and actually build our first multi agent systems ourselves. For this one, we're going to be building a crew that is able to do research and write an article for us. You're going to see how this agents work together and how you're able to create something that's super valuable through multi agent collaboration. So you stick around, you're going to really like this one. Let's dive into it. All right, I'm super excited about this. We're going to be talking more about how to actually build multiple agent systems. I honestly believe this is the future. It's going to be a key part of how businesses are going to be build and how engineers can drive an immense impact on your jobs and your career.
{ 0:56 }
We're going to start by looking at how we can build our first multi agent system and we're going to try to do things simple. I'm going to start by just copying some code in here, making sure that we don't get any warnings along the way. So we're going to start by importing the three main things from crew AI. We're going to import agent, task and crew. Those three classes are the main important part from Korea and how we are going to be building those multi agent systems. Now that we have imported them, we got to choose what's going to be the LLM. That is going to be the brain for all of our agents. So in Korea, you can choose your agents to be run from different LMS and by default, they all run from GPT 4. But in this case, let's make sure that they're running from GPT 3.5 Turbo. And you can do that by descending environment variable called open AI_model_name. And in there, you can add the name of the model.
{ 1:58 }
You're going to see down the road that you can connect your agents to any LMS out there that are not necessarily open AI. You can use local models, You can use any other major providers out there. So for this is let's make sure that we are using GPT 3.5. So let's start by creating our agents. Agents are the corner piece of how we're going to build the multi agent systems. So for this use case, we're going to build three different agents. We're going to have a planner, a writer and an editor. These three agents together are going to help us come up with amazing technical articles. So how do you create an agent? Well, it's pretty straightforward. You basically create an instance of the Agent class. When creating that instance, there are a few attributes that you really care about, namely the role, the go, and the back story. So let's look into that. For our planner, we want to make sure that we set a role that matches what we want it to do.
{ 2:57 }
So we're going to keep it simple and we're going to say it's a content planner. You're going to notice that in the following up lessons, we might do more complex roles and goals, but for this one, let's keep things simple. Now let's set up a goal for this agent. The goal for this planner is basically plan, engaging, and factually accurate content on a given topic. You notice here how we are interpolating the variable topic within the goal. You can do that in the row, you can do that in the go, and you can also do that in the back story. And I'm gonna show you how that plays a role on actually running these agents down the road. So now let's set a back story to the splitter agent. So if you look at this, you might be wondering why are we going to the trouble of setting up the role that go in the back story. One thing that you're going to learn in future lessons is the fact that agents perform better when role-playing. In this case, you can see that the agents playing as a content planner.
{ 3:56 }
So as much context as we can set in terms of like what kind of agent it is, the better because it's going to conform to what we expect. So now I'm going to set up a couple other attributes, namely when that say that this agent is not allowed to delegate work to any other agent, and also making sure that it's verbose so we can see how this agent runs in its inner thoughts. Don't worry about those two attributes for now. We're going to get into them later. All right, so now, but we have our planner here, we can create this agent and there we go. We have our planner agent ready to go. Now let's move on and create our writer agent that's gonna be responsible to actually write the content based on top of the research that the planner did. So in here you can see that we are doing a similar thing. This agent also has a role, also has a goal, and also has a back story.
{ 4:53 }
And you can see interpolation happening on multiple fronts, but all the interpolation that is happening is around the same variable called topic. And we're going to see how that plays a role in here by the end of it. So let's create our writer agent. Now let's create our final agent for this crew. And this agent is an editor. The editor is basically responsible to review the content, make sure that things are correctly formatted. That's good and it's ready to go. So let's go ahead and create that editor agent as well. With that done, we have three agents, but we have no tasks and nothing to guide them. So let's now create a task for each one of these agents. Create the task is as simple as creating the agent. You need to create an instance of the task class. So here you can see that you can just type task and the task expect at least three attributes, the description, the expected output, an agent. So the description is actually what do you expect the task to be? What do you expect the agent to do?
{ 5:59 }
So in this case, this is the plan task. So we expect our planner agent to look at the latest trends and look at the key players and making sure to create like a content or a research that we can use to actually write it down. The other argument that we expect in here is the expected output. And expected output here defers for the description because it kind of acts as a forcing function to force you to think exactly 1 you want to get out of the task. 1:00 you want this agent to produce as a final outcome. And here you can see that they're being very descriptive about it. We're saying that we want a comprehensive content plan with outline, audience analysis, SEO, keywords and resources. And then the final thing that we need to do is actually assign an agent to this task. So in this case, the agent that we're going to be assigning to this is our planner agent.
{ 6:58 }
And with that, we created our first task. Now let's move on and create our right task. It's very similar, but again, different. Different arguments, a different description and a different expected output. And here you can see that the expected output for the right task is actually the actual blog post. And we want it to be writing as markdown, having multiple sections and each section having between two or three paragraphs. So with that, we are ready to go because we have our two initial tasks. Now let's create our final task. This task is a little bit shorter because it's the added task. This task is where the actor comes in and kind of proofread the blog post, make sure that the content is right, making sure that we have the progress that we need and that The thing is high quality. So we can also go ahead and create that task. So right now we have the agents and we have the tasks.
{ 7:58 }
We just need one way to mash those together and that's where the third element of crew AI comes in, and that is the crew. Crew basically puts together agents and tasks so they can operate as a unit. So in here you can see that we set three attributes. The first attribute is a list of the agents, the second one is a list of the tasks. And then we have a verbose mode that allows us to see how this crew is executing and look at some of its logs. One thing that I want to call out is that by the full, a crew operates sequentially, meaning that one task is going to be executed after the next. Because of that, the order of the tasks when you create to accrue, it's important given that the output of 1 task is going to be sent as part of the input for the next. So you really want to make sure that you get that right.
{ 8:56 }
We're going to learn down the road that that's not always the case, and you can have a wayfinder control on what order you want those tasks to be executed. If you want tasks to be executed in parallel, or if you want them to be executed higher or typically, it's all up to you. For this initial use case though, this is going to work great. So how do we actually get our multi agent system run? One thing that I want to highlight here is this verbose setting being sacked to. Depending on how you're setting up our crew, you might want to be able to see a certain level of granular logs. So in this case, you can set your verbose node between 1:00 and 2:00, and that will define how much verbosity you're gonna see on your logs. If you're gonna see all the logs that are possible, or just a subset of them. In this case, because we really want to see what is happening behind the scenes, we're gonna set the highest number possible, in this case number two. So now we have our agents and we have our tasks.
{ 9:58 }
So how do we get it run? How do we execute this multi agent system? It's pretty straightforward. We use the crew dot kickoff method. So you can do this by calling kickoff and then passing an input dictionary into the kickoff method. And this input will have all the variables that you interpolated throughout your tasks and agents. So this is where the topic thing came into play. So you remember how we interpolated this topic variable throughout our agents when we first declared them up here? Well, now when we're kicking off our crew, we can actually set what is the word that we want to actually replace in all those different places. So now let's kick off our crew and see how that goes.
{ 10:48 }
So from the get go, you can see that the content planner starts off and it's starting its first task that is prioritized at the latest trend chief players and Noteworth news on artificial intelligence because that's the topic that we sat. So we can see here that it already did. That gave us an outline of what should be this article about what should be the main topics that should include on it. So now we can see that our new agent kicks in, our content writer, and it uses the previous information, the research that was made in order to put together a markdown blog post. So you can see in here how it's writing it all down for us now, the editor coming in proofreads and review and make sure that it aligns with our brain's voice and that it looks great. And by the end of it, we should have a full-fledged post for doing that. I'm going to be using this markdown helper. So once that you do that, you can plot the markdown here and we can see what this article looks like.
{ 11:57 }
It's called the rise of artificial intelligence, a transformative force in today's world. And you can see that it has subtopics and each subtopics has some nice content into it. All right, so now we have our agents doing the work for us, and you can do it too. So I invite you to come in here and write the topic that you want your agents to write about. And you can execute this code and see they come alive and write this blog for you. So congratulations, this is your first multi agent system and works great. Now let's talk about other use cases. So throughout this course, we're going to use Crew AI for all the examples. And you're going to probably find yourself looking at our docs at some point. So let me go ahead and tell you how the documentation that you find on Crew AI is actually written by a crew. So this is another great practical use cases that showcases how multi agent systems can help you with engineering work as well.
{ 12:55 }
Where we don't have anyone on our team writing docs anymore because now we have a multi agent system that does that for us. So I would definitely recommend you to think about all those different use cases that you could apply those multi agent systems into. Let's do a quick recap about everything that we learned so far before we move on to the next lesson. So we now understand what are agents There be LMS that can like talk to itself. They have an inner taught process and they can receive tasks and use tools in order to give way more complex answers that they would be able to do otherwise. We also learned that you can have multiple agents hooked up together so that one agent can pass tasks to another agent and by doing that you can have a specific agents that are great in one single thing. We learned the three main pieces and the corner pieces of crew AI. We learned about agents, tasks, and crews and we now know how to put them together. But there are way more things that we learned throughout this.
{ 13:59 }
1st, we learned that agents perform better when role-playing. Two, you should focus on goals and expectations. Whenever you're creating your tasks and your agents, you need to make sure that you're setting up in a way they know what they're doing and they know what is expected of them. Three, one agent can do multiple tasks if you want to. In our use case, we had one agent for every task, but we could go beyond that. We also learned that tasks and agents should be Grambler. You can see how we focus each agent on one very specific thing. And we also know that tasks can be executed in different ways. We talked about tasks being executed in parallel or sequentially or higher artistically, and we are going to dive into all that in future lessons. And we also talked about how it's pretty easy to create multi agent systems with crew AI.
{ 14:54 }
So thank you so much for sticking around and learning more about multi agent systems in this lesson. I'm looking forward to seeing you in the next one where we're gonna dive even deeper into how to build these agents and making sure that we're taking full advantage of them and their tools.
 
💡
有关Notion安装或者使用上的问题,欢迎您在底部评论区留言,一起交流~
上一篇
跟着吴恩达学AI多智能体-4/17-AI智能体的关键要素
下一篇
跟着吴恩达学AI多智能体-2/17-AI智能体