type
status
date
summary
slug
tags
category
password
URL
icon
<Multi AI Agent Systems with crewAI>是吴恩达大佬与CrewAI的创始人兼CEO合作新开的一门课程,该课程的目标是通过理论和实际案例,让学员掌握构建智能体系统的技能,最终能自主构建复杂的智能体团队。该课程是以视频教学的形式进行的,为了便于大家快速学习,我这边提供了图文形式。官方链接放在了左下角,大家点击原文链接即可查看官方视频课程。该课程共分为17节,本文是第八节,后续我会每天更新一节,大家有问题可以随时私信或者写在评论区。以下是第八节的图文内容。
课程内容
为了开始,我们需要一些基础代码,并且开始构建我们的智能体组(crew)。首先要确保你再次导入智能体、任务和crew类。一旦导入这些类,你就可以开始了。
现在我们已经导入了智能体、任务和crew类,我们可以设置要使用的模型。对于这个项目,我们将使用GPT-4。你之前一直使用的是GPT-3.5,但现在我们要处理更多的数据,因此需要更大的上下文窗口来处理所有信息。让我们设置环境变量。
另一个你会注意到的是,我们在这里使用了Serper API。Serper基本上是一个允许你搜索谷歌的API,你会获得一个密钥供你使用。你也可以访问他们的网站注册,获得免费积分。当然,还有许多其他的搜索工具可以使用,但在这个例子中我们选择使用Serper。现在让我们执行这一部分。
接下来,我们需要确保使用一些QAI工具,同时创建一个简单的自定义工具来展示其使用方法。让我们开始创建我们的智能体。这次我们将创建两个智能体。第一个是销售代表智能体,因为我们这次要进行一个销售用例。销售代表有一个目标,那就是识别与我们理想客户档案匹配的高价值潜在客户(通常称为ICP)。销售是多智能体系统的一个重要用例,因此你可能在互联网上看到许多相关的案例。在背景故事中,我们设置了一些上下文,说明销售代表在Crew.ai工作及其任务。这个智能体不允许委派工作或提问,只需执行指示。我们还将创建一个新的智能体,即销售主管智能体。
销售主管智能体建立在之前的基础上,具有不同的目标。这次的目标是找到实际的个性化内容,并与潜在客户建立个性化的沟通。这个智能体组的最终目标是找到与潜在客户沟通的方式,使其对Crew AI感兴趣。
我们创建了两个智能体,现在是时候看看我们要使用的工具了。我们将使用CREAI工具包。这次我们将使用三个工具:目录读取工具、文件读取工具和Serper开发工具。目录读取工具允许你的智能体读取任何目录,或者你可以限制它读取特定目录。文件读取工具也是类似的。Serper工具允许你搜索互联网。让我们实例化这些工具。
在目录读取工具中,我们指定它只能读取本地的instructions目录。这个目录中有一系列关于如何处理不同规模公司(大公司、小公司和中型公司)的说明文件。让我们的智能体可以读取这些文件,以获取所需的信息。
我们现在有三个工具:目录读取工具、文件读取工具和搜索工具。我们可以创建一个自定义工具来展示其使用方法。每个框架都会提供创建自定义工具的模式,Crew.ai也不例外。我们将从Creator AI中继承一个类,并创建自己的工具类。在这个例子中,我们的类将名为SentimentAnalysisTool。这个工具将进行情感分析,但这可以是任何功能,例如调用外部API。每个工具需要一个名称和描述,在这里我们将名称设置为“情感分析工具”,描述为“分析文本情感”。
为了实现工具,只需在类中创建一个run函数。这个函数可以包含任何代码,例如调用API或发送邮件。在这个例子中,我们简单地返回“positive”表示情感分析结果为正面。
现在我们创建了这个工具,我们可以实例化它,并像上面的工具一样使用它。然后将这些工具传递给智能体和任务。
我们创建的第一个任务是潜在客户分析任务。这个任务分析给定的潜在客户,了解其行业、规模等信息。我们为其提供了目录读取工具、文件读取工具和搜索工具,以便智能体在执行任务时能够读取指示文件,并使用Serper API搜索互联网获取更多信息。
我们需要一个新任务,帮助创建个性化的外展信息。这个任务使用的工具有所不同,因为它不再需要读取目录,而是专注于情感分析和进一步搜索,以确保与客户进行适当的沟通。
创建了两个任务后,我们可以创建智能体组。智能体组设置与之前相同,传入智能体和任务,并设置详细模式和记忆功能。然后启动智能体组,传入输入参数。
这些输入参数将被插入到任务和智能体定义中。在这个例子中,我们模拟潜在客户名为“Deep Learning AI”,行业为“在线学习平台”,关键决策者是“Andrew”,他是公司的CEO,主要里程碑是产品发布。
智能体组开始工作,销售代表智能体首先进行深入分析,了解“Deep Learning AI”公司,并进行互联网搜索,获取更多信息。然后,销售主管智能体利用前一个智能体的研究成果,制定与潜在客户沟通的策略,并生成个性化的外展邮件。
通过这个过程,我们展示了如何创建和使用多智能体系统来自动化销售流程,包括识别潜在客户、进行研究和生成个性化沟通内容。这是多智能体系统在销售领域的一个强大应用。
翻译原文:
All right, to get things started, let's get some of the boilerplate code in here. And let's start build our crew. So first thing that you want to do is make sure that you import the agent, task, and the crew class again. Once that you import that, you're good to go and ready to get started. All right, so now that you imported the agent, the task and the crew class, we can set up what model we're gonna be using. So for this one, we're gonna be using GPT-4. So you have been using GPT-3.5 up to this point, but we're moving to 4 now because we're gonna be handling with more data. So you wanna make sure that you have that extra context window to handle all the information that we're going to be sending to this agent. So let me just copy this and paste it here. Another thing that you're going to notice is that we're using Surfer API here. So Surfer is basically an API that allows you to search Google and you're going to be provided with a key that you can use. But you can also go into their website and sign up and they offer free credits. But honestly, there's a lot of other like search tools that we could use as well. We just choose to use Serper in this example. So let's go ahead and just execute that. All right, so now we're ready to dive into tools. We basically want to make sure that we use some of the QAI tools and we also want to make sure that we create a simple custom tool to show you how that looks. So let's go ahead and start creating our agents. We're going to have two agents for this. The first one is going to be a sales rep agent because we are going to be doing a sales use case this time. The sales rep has one goal, and that is identify high-value leads that match our ideal customer profile. People like to call that ICP. And sales is a big use case for multi-agent systems. So probably you're going to see a lot of that around the internet as well. You can see that in the backstory, we set some context around the fact that the sales agent works at Crew.ai and some of the mission around it and everything. You're also going to notice that this agent is not allowed to delegate any work or ask any questions. It's just supposed to do what it's told. We're also going to create a new agent, and that's going to be a lead sales rep agent. So let's execute this and then create our new one. So this lead sales rep agent, you're going to see that it builds on top of our previous one. It has a different goal. This goal is kind of find the actual personalized content and build a personalized communication with this lead that we're going to be working on. So the idea of this crew at the end of the day is giving a lead, find out a way to communicate with them that helps them to get an interest on the Crew AI. So let's see how that's going to play out. So let's first execute this agent. All right. So now we have those two agents created. Now it's time for us to look at the tools that we are going to use. We're going to be using the CREAI tools package again. So you can go ahead and type that out and import those tools. You're going to see that we are using three tools this time. We are using a directory read tool, we're using a file read tool, and we are using a server dev tool. So the file read tool, and we're using a SERPer dev tool. So the directory read tool, you allow your agent to read any directory that it wants. Or again, you can kind of like confine its options by saying a specific directory that you want it to run. Then the file read tool, the same thing. And the SERP tool, we already talked about that in another lesson. It allows you to search the internet. So let's go ahead and instantiate those three tools. So you can create a directory read to, the file read to, and the search to. And the one thing that you're going to notice is that on the directory read to, we're specifying that we want it to be able to read only this local instructions directory. So in this directory, there are a series of instructions as markdown files on how to handle companies from different sizes. So you're going to have big companies, small companies, and medium companies instructions on how to handle them and we're letting our agents being able to read these files by setting up this tool that allow them to actually look at that folder so as you can see we now have three tools that we're using here our directory read to or file read to and our search tool and you can see they're like all similar but the directory read to actually is passing one attribute called directory this attribute is forcing this tool to only allow the agent to read this one single directory in this case is a local directory called instructions so this local directory basically holds a set of instructions on how to handle some of those customers, if it's a big customer or a small customer or whatever it might be. Let's look at those files real quick. So here you can see that there are three files inside that directory. A file that has a set of instructions to how to handle a small business, on how to handle tech startups, and on how to handle like more enterprise deals. And you can see here there's like a specific template, there's key strategic points to address, there's introduction. So everything that our agent might need in order to actually engage with like a specific company. So now that we have our tools here, we can create a custom tool just to see what that would look like. So every framework out there is going to give you a pattern to create a custom tool. And Crew.ai is no different. It offers a couple of different ways that you can use to create a custom tool. So in this case, the way that we are going to be using is by inheriting from a class inside Creator AI. We can import this class. It's called base tool. And now that we imported it, we can create our own class that's going to be our tool that inherits from that. So in this case, our class will be called SentimentAnalysis2. So you can envision that this tool would do some sort of sentiment analysis, but this could be anything. This could be an external API call or whatever you want. But in this case, we're just going to fake it a little bit to make sure that we build a custom tool and we learn how to do it. So one thing that every tool needs to have in query AI is a name and a description. So in here we are setting the name of this tool to sentiment analysis tool and the description tells the agent when it should use this tool and what is its purpose. So in here we're saying that it analyzes the sentiment of the text. And then in order to actually implement the tool, all you gotta do is create this run function inside that class. So as long as you have this underscore run function in there, you can add any code that you want. So in here is where you would call an API or where you would take an action or send an email or anything you wish your tool to do. In this case, we're keeping it very simple because this is just an example and we're just returning the word positive. So this is a sentiment analysis tool that always returns that the sentiment is positive. So now that we create this tool, we can actually instantiate it so that we can use it the same way that we did with the tools above, with the directory, the file, and the search tool. So you can go ahead and just do that, and now you have the sentiment analysis tool, and you can pass that to your agents and tasks. So now that we instantiate our tools, let's create our tasks and assign them properly. Here we're going to be able to see how we can focus in specific and clear goals on what we want to achieve by using these tools. So the first task that we're going to create is a lead these tools. So the first task that we're going to create is a lead profiling task. So the lead profiling task basically conduct an analysis on a given lead in order to understand its sector, how big it is, information that might be useful for us to engage with this potential customer. So in here you can see that we are providing it with a set of tools, our directory read tool, our file read tool, and our search tool. So with these tools this agent when performing this task are going to be able to look at those instruction files that we saw before, read them, and then using the SERPR API search the internet for more information about this lead. So let's go ahead and create this task. Now we're going to need a new task. This new task is going to help with creating a personalized message to outreach for this specific lead. So I can paste it in here and you're going to see that this task has a different set of tools. It doesn't need to read the directory any longer because by now it already knows if this is a big or small company and already has done some research on them. This task is focused specifically on doing a sentiment analysis and doing any further search that might be necessary in order to make sure that we talk with this customer in an appropriated manner. So you can go ahead and also create this. Okay, so now that we created the two tasks, you can create your crew. The crew is pretty straightforward, the same way that we have been doing so far. We pass in the agents, we pass in the tasks, you can set the verbose mode to two, and you also set the memory so that the screw is using the memory functionality and features. You don't necessarily need to if you don't need, but I think it might be a good use case. So let's go ahead and run this. Now you can kick off the screw, and kicking it off should be pretty straightforward. So in this case, we're passing a set of inputs. Remember that these inputs are going to be interpolated in any place that we use them, both in the tasks and in the agents. So these inputs are going to be used into any place in the tasks descriptions or the agents definitions. Anywhere where we try to interpolate these variables, we can now pass their values in here. So in this case, we're simulating that the lead name is deep learning and AI. And their industry is online learning platform. The key decision maker is Andrew, and he is the CEO of the company and the basic milestone that they have is a product launch. So we can go ahead now and kick off this crew. So everything starts with our sales representative and it's going to start by conducting this in-depth analysis of the deep learning AI company and basically trying to learn more about it. So you can see here that it started by searching the internet, trying to understand more about Leap Learning AI. So in here, you can see all the search results that it got from Google. And if you scroll further, you're going to see that it by mistake decided to try to read one of the websites as a file. And you know that we just talked about how tools need to gracefully fail. This is a great example of this. The file content read to is not meant to read a website, but actually a file from your file system. So if you didn't plan for that, you would draw an exception and your execution would stop. But Query AI automatically handles that so that it fails gracefully. So you can see that despite of it drawing an exception, it tells the agent that it got this error. So the agent kind of like now understands that it can directly access the external links using the read files content. So it's learning from the tool that it tried to use. So it then goes back into search the internet to try to find more information about deep learning AI. So if you're building your own thing or depending on your choices or framework, you might notice that if your tool finds an exception, the decision there is to let that exception actually blow up. And when that exception blow up, it stops the execution of your agent. So that means that hours that was completed so far is gone and your agent execution stops, so you can't move forward. That means that you would then need to add an extra layer so that you can treat for that. Either kind of like kick off your agent from the scratch or do something else with that error. So for crew it was a conscious decision early on that we would prevent exceptions from stopping execution. But you might find other frameworks that choose it otherwise and choose to let exceptions actually stop executions. And that is a conscious choices that you can make. And if you're doing something from the scratch, because again, you don't need to be using a framework. You can be building these agents yourself. It's just going to be a lot more coding, but it's something that you need to be mindful of. How you want to handle exceptions when using tools is a big thing of agents. So you can see that again, it finds a bunch of different researches and put together a deep learning AI company profile. a deep learning AI company profile with a background, what are the key personnel, recent business development, potential needs engagement strategy, everything that we need to know about the company. Now, our next agent kicks in, the lead sales representative, where using the research that your agent did before, it's going to try to come up with a good way to engage with deep learning. So here you can see that, again, it's doing some research online. Keep in mind that this is where some of that caching strategy that we talked about comes in. If this agent tries to do the same research or the same search online that another agent did it's gonna hit a caching layer instead of actually doing the search so you're gonna be saving on rate limits and on the requests and basically it's gonna just work faster and here you can see that it come up with an email where we can send to andrew the subject is Enhase Deep Learning's AI Educational Impact with Crew AI Advanced Analytics. And then it goes on and explaining. And you can see that even references the batch. The batch is actually a newsletter from Deep Learning AI. And we never told this agents anything about it. They learn about it automatically through the research that they made. So now we have an email that we could engage and we actually have a few other options here. So you can see that it gives another option now a little bit more focused on AI drive-tos and another one talking more about a partnership. So you can choose what email you want to use to actually engage with this company. This is such a great use case. Throughout our interactions with the community that has been building multi-agent systems, independently of what framework they are using, we realized that there's a few verticals that are very common. And sales is a big one. Because a lot of the sales process involves reporting, involves researching, involves engaging, and all that are things that can now be automated through agents. So it's going to be common for you going out there and talking with other people that are building agents to see that they are doing things in these verticals. And this is honestly a very interesting space in general.
- 作者:luxinfeng
- 链接:https://www.luxinfeng.top/article/%E5%AE%A2%E6%88%B7%E5%A4%96%E5%B1%95%E6%B4%BB%E5%8A%A8%E7%9A%84%E5%B7%A5%E5%85%B7
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。