Agentic AI: Build a Multi-Agent Application with CrewAI
Short Abstract
Agentic AI: Build a Multi-Agent Application with CrewAI
In this hands-on tutorial, we’ll dive into the fundamentals of building multi-agent systems using the CrewAI Python library. Starting from the basics, we’ll cover key concepts, explore advanced features, and guide you step-by-step through building a complete application from scratch. Along the way, we’ll discuss implementing guardrails, securing interactions, and preventing query injection vulnerabilities.
This session took place in track Generative AI and was classified suitable for novice domain / intermediate python by the speaker.
Transcript (auto)
Auto-generated from the recording utilizing Open-Source AI. Speaker labels (Speaker 1, Speaker 2) reflect diarization, not identity. Timestamps refer to the recording.
Speaker 1 [00:08]
Hello, hello, thank you. Applause for free. All right, so basically, just to clarify, this is not a tutorial, so I will show several things. There will be a presentation at the beginning, but then it's all about you trying to build something. I will help you. You'll also get help from your neighbors if they manage to work on the assignments. and as well we will try to do everything together but yeah once again it's not a tutorial it's all about writing code I know that it's 10 in the morning and no one wants to write Python code at 10 in the morning but let's try to let's try to do it for today all right so let's start with the presentation as I said there is let me get this so there would be the first part of this workshop is A CLASSICAL TALK, PRESENTATION. I WILL SHOW YOU WHY WE ARE TALKING ABOUT AGENTS AND WHAT IS CREW AI ABOUT. WHO KNOWS CREW AI? ANYONE? OKAY, PERFECT. WHO HAS EXPERIENCE WITH AGENTS? WRITING AGENTS? ALL RIGHT, PERFECT. SO AS I SAID, THE FIRST PART IS ABOUT CREW AI. WE WILL SEE THE MAIN COMPONENTS OF CREW AI AND THIS IS ESSENTIALLY WHAT WE ARE GOING in the workshop. So there will be four assignments and we will focus on the components that you see here. And then I'll show you the actual assignments of what we're going to implement during the workshop. I prepared a website for this workshop so you will see that everything is interactive. You don't need to check the slides, but I will also share the slides right after the talk, so in a few minutes. So you have also an interactive way to keep up with whatever we're doing. Just one minute about me. I work as a data scientist. I've been exposed to many domains, right-hand logistics. In a nutshell, one of you, I'm on a stage, but I'm really approachable, so if you have questions, feedback, I'll be happy to answer all the questions you have. All right, let's start with the objectives. Essentially, what I want to do here is show you how to work with agents but from my perspective which is not just saying okay the agents can save the world now a lot of marketing there is a lot of marketing around the agents I see that companies are pushing a lot for agents for me it's more a software engineering view of what you can actually do with agents I like to work on solutions for the problems that I see around me and I saw that working with agents with crew AI and also some other frameworks is actually useful. It's not that agents cannot solve problems. They can definitely solve a class of problems. So we will focus on those. What is agentic AI? Essentially, when we talk about agents, we're basically talking about an entity that can take actions. And the idea is that we have the reasoning implemented through the LLM, and then this agent in that is working in a specific environment with some rules and constraints can take actions for instance to go from A to B or to solve an issue why are we talking about agents now I mean not today but in this period essentially is because of LLMs with LLM we have now the chance so agents is not something you know studying agents autonomous agents also years ago when I was a student so it's not a new topic but what changes now is that we have the LLM that actually gives us the reasoning core of the agent before it was complicated because the agent to in order to let an autonomous agent to work to do something in a in an environment you need to write something that is dynamic and if something changes in the environment and you can imagine that in the real world we have a lot of constraints we have the low physics I I mean, it's very complicated. The agents need something dynamic in order to take actions. And this is why now agents are getting famous again, because we have this sort of reasoning implemented under the hood. When we say reasoning, some people don't like this kind of wording around the agents. It's just a label for me. I don't care about reasoning. I mean, maybe we don't really reason like that. We don't even know how human beings reason. Let's just use this word for convenience. Once again, when can we call it an agent? Imagine you have to go, let's use the example of the Environment. You have to go from a to b and This is a dynamic environment with a lot of rules and Constraints. If you're working with a Solution, you know that there are certain, there are, let's Say, ten rules to go from a to b. And you know how to write that function and you know how to go From a to b. The problem is that when the When the environment is dynamic, you need to adapt with whatever comes next. And if something happens in the middle, you need to react. So you need to make your software, your algorithm dynamic. With agents, we are able, thanks to the LLM, to react and to act to something that you didn't think about, you didn't put into your algorithm, so the agent can take actions. In addition to that, we usually talk about agents in terms of entities that can use tools, so they can use extra tools. You probably saw this when you were using Charge-GPT. Now, when you use Charge-GPT, you're not just using the LLM, the raw LLM, but instead you're using some sort of agents. And every time you see that Charge-GPT is, for instance, doing some math or is doing some, I don't know, programming, data analysis, this, that is actually a tool that is encapsulated in the answer, and the agent, in this case chargeGPT, is able to use this tool to provide extra information. This is one of the key points of the agents. And once again, I mean, finally, if you know how to go from A to B, this is like machine learning. If you know how to write a deterministic function, don't use an agent, don't use machine learning, just write a function. A bit of history, even though it is a bit fascinating that we're talking about history, because this is a new topic, and now when we talk about RUG, we think about something that is already old, even though it's a new topic. So what happened in 2022 was that we started talking about LLMs and using LLMs a lot for many different problems. And in a nutshell, when we use an LLM, we're sending a query, we have a model under the hood, and we generate an answer. That's pretty much it. And if you remember, a lot of people were making fun of Judge APT because Judge APT didn't know who was the president of the United States in, I don't know, some years ago. And they were like, okay, it's so smart, but it doesn't know or she doesn't know the president of the United States. Ha, ha, ha. That was a joke. That's it. But instead, it was actually by design because, of course, if you're not training the LLM with those information, the LLM doesn't know anything. And this comes into the game, now comes into the game, the RUG. With retrieval augmented generation, we're able to give more information that have not been part of the training set to the LLM in order to generate an answer that is a mix of knowledge that the LLM has plus the RUG. And finally, we move to the agents. The agent is a kind of third step in history, let's say, and this is pretty much what you see here. So we still have the LLM, we have some tasks that are assigned to the agents, some tools that the agents can use, and we don't use the direct output that we get from the LLM, but instead we have some sort of reasoning. There is this loop that you see here that is implementing the reasoning process. So the LLM is helping the agent to reason about what comes next and perhaps adjust the output in order to provide something that fits the problem that you want to solve. LLM, Crew AI is not the only one. There are also some other alternatives, just to be clear. We have Langchain, Langraph. I try them all. I really love to use Crew AI when it comes with agents because it's simple. It goes straight to the point and also in terms of if you're coming from Python and you're coming from the interfaces where you used to see crew AI does a very good job with that. All right, so what is this package about? Essentially, we have a package that is implementing a lot of interesting and complex things under the hood, so we don't have to take care about it. When I say complex things, I'm talking about, for instance, orchestration and some of the core components like agents, tasks, and crews that we're going to see in the next slides. With this package, we have the reasoning under the hood and we have the orchestration because essentially with crew AI, we're basically working with a team. What we do with crew AI is once we select the problem that we want to solve, the next step is to create a crew of agents that are going to work for you on that problem. One example is, let's say that I want to work on a marketing report. And I know that there are several steps, and writing a huge prompt gets very complicated whenever you have a lot of parameters, you have a lot of constraints, and then you end up with a huge string that the next person has to maintain. Instead of doing that, you can create a team of agents, and each agent is specialized on something. For instance, you have an agent that is creating the final report. You have an agent that is doing some data analysis, or you have an agent that is browsing the web in order to enhance your query. So this is an example of three agents that will be part of a crew, And this crew is your actual application that is about solving this marketing problem we're talking about. All right. Let's dive into the coding part. I hope this is clear. I will show the slides in a few seconds. And as I said, it will be a website. So even if you don't read the code very well, it's okay. We start with the first component. This is the interface for the agent. When you import agent from crew AI, you basically have this entity that is one of those components I was talking about. So this is one of the components of the team. You are probably familiar with this way of also talking to the LLM. How many prompt engineers we have in these rooms? No one wants to say that, right? I'm a prompt engineer. Just kidding. Anyhow, when you write a prompt, one of the things that we do is, especially when we start the prompt is, you are an expert on this field and you studied computer science for 10 years. Please do that. This is pretty much what I just said. So we're trying to define the role of the agent. We're trying to say, okay, you are about doing this, this is what you are, but I'm not telling you what to do. There's nothing about the task here. We're not telling the agent, this specific agent, the agent two, what to do. We're just saying, okay, you are this entity and what comes next is what you're going to do. And this is the interface that we use to tell the agent what to do, the task. With the task, we have a description, which is exactly what is going to happen. We have an expected output, which is a very powerful Way to force the output. How many of you struggled with The output of an llm? you say 100 times, please give Me a json, please, please, please, please. It's something else. You get an image, maybe. So i feel you, this is a huge problem, and with interfaces Like this, with frameworks like this, what usually works really Well is that you can force the output. You can use the expected Output, or as we're going to see in the assignments, you can use And this task is going to be assigned to the sales agent, so the agent is going to work on this, the sales agent is going to work on this one. Then we put everything together, all our tasks, all our crews in the crew. The crew is the final kind of application that puts together all the agents, so this is basically our team that is going to solve something specific. As you can see here, we have the list of agents, tasks, we have a process. The process is how we're going to execute the tasks and how the agents are going to work on the problem. In this case, we're saying the process is sequential, so we're going to tackle the tasks one by one. There is also another type of process, we'll see it soon. We can say something about the output and some extra logging. Speaking of the process, we have essentially two different ways of executing the tasks and working with the agents. The first one is sequential. As we said, we tackle the tasks one by one. The second one, which is one probably the most interesting, is hierarchical. Here we have the team working as an actual team of people lead by a manager. So we will have another agent that It is acting as a manager and is orchestrating the work of the agents. This is a very powerful concept because one example of how to use a manager is, let's say that you're planning your vacation and you're using a crew to do so. The manager can be sure that one specific constraint is going to be met. One example is you want the total budget to be above 200 euros. The manager will let the agents work, but it will keep in mind that you have this constraint, so the output will be kind of lead by the manager. And if something goes wrong, if one agent finds an auto that costs 2,000 euros, the manager will say, no, you have to go back, work again on your task, and give me something that meets the constraint. Then we have the tools. This is also one of the most powerful topics in agentic AI, because here we're basically giving the possibility to the agent to use something that is kind of not part of the LLM. And we're saying, okay, this specific research agent is able to use the Serper DevTool. Who knows this Serper DevTool? Okay, this is part of the workshop anyhow. The separate dev tool is a very powerful tool. You will enjoy it because it gives you the possibility to browse the web. So you give this tool to the agent, and the agent, if it's necessary, will browse the web to get more information, to get more information. And you don't have to do anything on top of this apart from telling the agent, hey, you use this tool, separate dev tool, that's it. And based on the task you're giving to the agent, The agent will decide whether to use the tool or not. One more thing about the tool, by the way. If you want, and we will see it in the assignments, you can also write your own tools. Let's say you have some custom, you have some domain within your organization, and you know that in order to create, there is an agent that is creating an email for the people you're going to onboard in your organization. That logic is something that belongs to you. If you want, you can write a custom function and give it to the agent in this specific way, essentially like this. But instead of server dev tool, you're using your own function, and the agent will be able to use it. One more important topic is flow. This is also something, of course, that we're going to see in the assignments, and essentially is a very nice way to take care of LLM hallucinations. Do you know what is it? I think we all know what is LLM hallucinations. Essentially, it's when the LLM is giving you something that is completely unrelated, not correlated to the input you're giving to the LLM. So you get an answer that is completely all over the place. With this flow from Crew AI, we can create some sort of different paths into our application, And we can create essentially some different flows to say, okay, the crew is doing this, but if that specific action happens, I want to perhaps recreate a new output, call another crew, and so forth and so on. The nice thing about flow is that you get it out of the box as part of crew AI, and in some situations it's really helpful. For instance, when you want to ride guardrails, and we will see how to do it next. Then we have conditional tasks. This is also another way to create some dynamic conditions that can be leveraged by the agents. Essentially, we have seen the tasks before. This is another type of task. Essentially, the interface is almost the same, but what's different here is that we can say, okay, we have a task that is dependent on a specific event, AND IF THIS EVENT HAPPENS, IF IT'S TRUE, FOR EXAMPLE, WE WILL DECIDE WHETHER TO LET THE AGENT WORK ON THIS TASK OR NOT. THEN WE HAVE DIFFERENT WAYS TO HANDLE THE INPUT AND THE OUTPUT. THIS IS A VERY TECHNICAL THING. USUALLY WHEN I DO THE CLASSICAL TALK, I DON'T REALLY EXPLAIN THE INPUT-OUTPUT, BUT IN THIS CASE IT WILL BE USEFUL FOR THE ASSIGNMENTS. THERE ARE DIFFERENT WAYS YOU CAN ESSENTIALLY PASS THE OUTPUT FROM THE CREW TO THE AGENTS AND TO THE TASKS. THESE ARE TWO EXAMPLES. WHAT YOU NEED TO KNOW ESSENTIALLY IS THAT WHEN YOU DEAL WITH INPUT AND OUTPUT, YOU'RE BASICALLY DEALING WITH SOMETHING THAT YOU CAN CONTROL VERY WELL, AND YOU CAN SAY, FOR EXAMPLE, OKAY, THIS SPECIFIC TASK GET THIS EXPECTED OUTPUT WHICH IS AN IMAGE URL, AND AS YOU CAN SEE HERE, I'M BASICALLY Passing and basically referring to the image url as a key value pair so it's it's it's really What you would expect from from a dictionary and in addition to that as i said We can, also use by dantic to force the output to be a specific class or instance of something This is an example so it's very long because, we have basically a full application here, we have a crew we have agents we have everything essentially here what we're doing is creating an agent this is the weather agent and then we have the task that is going to be assigned to the agent then we have the crew which only has one agent and one task and when we call crew kickoff we're basically we're basically calling our application this is a full working application and then if we want to access the output we have different ways and this is what i was referring to before it's a Very powerful concept because when you access the output you Have, you can use Pydentic, you can access it as a json dictionary, you can get The raw content, so there are different ways, and usually, of Course, i like to use pydentic because it's quite powerful in This context. Then we have asynchronous crew Kickoff. If you want, you can call Different crews at the same time in an asynchronous way. Who is familiar with asynchronous functions calls? Okay, good enough. If you want to know more during the workshop, we can talk about what an asynchronous function is eventually. Here, essentially, thanks to Crew AI, we can easily kick off the crew in an asynchronous way against different outputs, and we will see in the assignments that in some cases it's really helpful. All right, so we're getting close to the hands-on. What is this going to be about? So essentially what I want to do is just write, these are not rules, more like guidelines, whatever, just to give you an idea of what we're going to do. So, first of all, there will be a website that I made for this workshop. You will find all the information, all the code that you need, all the help that you need to write the code. Once again, feel free to ask me anything, also questions for which I don't know the answer. That's completely fine. Share your ideas with your neighbors, please, because if someone already had a solution, if someone doesn't want to write code, or if you want to work in a group, just please do it. Try to help each other. And get creative. I wrote the assignments, but if you want to do more, if you want to change something in the assignment, or you say, okay, I really don't want to work in this direction, I want to do something that is more interesting to me because I'm working on this actual problem, do it. The assignment is just to give you some guidelines of what to do, but if you want to change it, feel free to go in a different direction now I created who knows who knows this reference no one knows about this symbol yeah I'm old it's okay it's okay no worries I'm gonna leave now all right so this is this is a website I made please access the website this is from this symbol is whoever tells me from this in this symbol from where it Comes from? Get a prize. Yeah, but you just said yes. He gave the answer. You get an extra t-shirt. That's what i can do. All right. So it's coding time. This is the website. Please access the website. If it doesn't work, the workshop ends here. Really? I hope so. Let's check. You know, as i usually say, it works for me. Can you access the website? Maybe the link was wrong. I don't know. Separate has a problem i someone already told me that's not a big Issue because this is an extra tool so if we want we just don't Use it it's completely fine it's completely fine so if we want we Just can we we can avoid the tool the you using the tool i'll Show you how to do it is just as simple as not including it in The list of tools that's it if you manage to get a separate dev Tool open key that's that's nice But you you won't you won't really see the difference apart From seeing the agent browsing the web all right let me explain You how this website works essentially we have All right essentially what i made here is a way to make this Thing interactive because showing the slides is Complicated for this kind of things you have these api keys There is also a list, there is also a message about what to do if you don't or if you don't have an open AI key. Crew AI is LLM agnostic. That means that you can pretty much use anything depending on what Crew AI provides in terms of interfaces. If you have an interface for a specific LLM in Crew AI, you can use it. Now, the very tough question. who doesn't have access to an LLM I'm talking about having a key that gives it okay only three people are we sure three four five okay it's 20 okay it's okay no worries so this is a very important step so let's try to get a key you can use open ai you can use the trial you can use one of those the only the most important thing is if you want to use one of those uh the most important probably um you you will probably have an interface in crew ai if you want to use one of those just make sure that you go here on the website and you check llms just to be sure that the llm that you want to use is supported by here Provided configuration is supported by crew ai best thing Is just get an open ai key. If someone has an open ai key, An open ai account and you already have a key, maybe you Can, i can also do it if you want, just come to me, you can Create a temporary key for someone else. It will probably be, it will probably cost one dollar doing doing this exercise so it's not the probably less 20 cents um so if you need a key you eventually can also come to me we will we will can we can work uh we can work that out uh but at the same time please have a look at what is provided by crew ai because probably among all these services you can get something uh for free trial or whatever yeah if you have suggestions Okay if you find a solution my Suggestion is can you write it on Discord since no one is using it let's Make a good use of discord and no one Answers my questions on discord so let's Go to the room what's the name of this Room who knows yeah thank you we go to go to the room and you leave whatever you find this is a good suggestion as i said make sure that you have uh the provider supported by crew ai all right the second step once you get the key is separate dev tool doesn't work it's okay is installing crew ai i'm leaving here the steps in case you're not familiar with it but this is a classical creating a directory installing the dependencies i'm leaving the poetry file who is familiar with pi project toml okay so please ask your neighbors if you want to know how to use it Essentially, inside the PyProject.toml, if we ever look at it, we have something like this. So we have the dependencies, and you don't really have to look into this. You just have to use poetry install, and that's it. If you don't have poetry, please install poetry. If you don't like poetry, you can use UV, whatever you want. If you want to do everything from scratch, if you want to use virtualenv, just get this. This all 114 all version of crew AI and for this python you just you have to use this python version. You can create a new virtual environment and then you're ready to go. So there are many ways. And if you need more clarification on this, please do it as soon as possible because this should take. Two minutes. But if you need clarification, please just raise your hand. I will not help you with the microphone. I'm coming to you, so we keep everything secret. If you say, hey, I don't know what the tunnel is, please help me. Okay, it stays between us. More about this website. once you are set up and you have the poetry you you did run poetry and you have your crew AI we will start looking into the assignments here we have four assignments one example is your first assignment we create a simple agent you have all the information that you need here you have the links useful links and then you have a section with code examples we which in most of the cases is essentially almost what you have to do so it should be pretty straightforward and then in addition to this there will be for some of these assignments let's have a look at another example we have this not here we have this fire bonus feature so if you look at it you're gonna have some bonus feature if you're really good at 2ai and you manage to go through The assignments you can simply work on the bonus feature who already completed All the assignments if someone completes all the assignments in very very fast I Can create another one on the fly I have another one in mind all right so I don't Want to go through the first assignment for now because I give you a couple of minutes to set up your machines and I'm just kind of walking here to see if someone needs help for whoever is watching this remotely because I think we're streaming this right please send a message in discord and I will check if you if you need help if you need support all right who needs help with the installation of packages or anything when you download this toml file I didn't write this here but that's a good question you get a .toml download I guess close to it yeah you have to get rid of this you have to save it as pyproject.toml otherwise when you run poetry install it won't be recognized by poetry that's it who doesn't have access to the website okay I'm glad someone was trying to yeah I'm coming someone was trying to install crew AI in Google collab notebook that's also possible if you don't have it installed just run exclamation point peep install crew AI and then you have it available in your Google Colab notebook all good shall we move to the first assignment okay who doesn't want to move to the first assignment only me all right first assignment your first agent so this is essentially a very simple assignment to see that whatever you did until now is working is correct you have an API key that is letting you access the LLM and essentially here what I want to do as you can see here for each assignment we have an overall an overview of what you have to implement the steps of your application in this case we have we get the input this is an overview I mean this is not kind of you don't have to exactly implement these steps this is an overview in most of the cases is exactly like this but it's just for you to understand the flow you have the input the user prompt the user prompt and the target LLM essentially what we're doing here is creating a multi-I agent that focuses on prompt engineering optimization. Essentially we're doing prompt announcement. This is what happens when you use, for instance, charge APT. The LLM doesn't really get your prompt, but it gets a prompt that has been announced by charge APT itself by another agent. And this is what we're going to do. So we get the prompt that you want to send to the LLM, the target LLM. The target LLM is not the LLM that using is just a string like this this crew is about getting a prompt getting a string and then the goal of the crew the goal of this application is given the prompt and given the potential lm where you want to execute this prompt we're going to do some prompt engineering optimization some enhancement so this target llm is not the llm that you're using under the hood it's just okay this prompt optimize it for charge GPT for deep seek so far so on then we have the actual improving structure so this will be an agent it is that focuses on improving the structure of the prompt then we have another agent that is about improving for the target model and then the final output the agent that you have to implement are as we said the prompt structure agent this is an agent that get the prompt improve the structure and then the optimization for the LLM this is another agent that given an out given this well structured prompt that is prepared by the first agent is making it making this better for the target LLM that's pretty much it please read through all the things that wrote here you have useful links like agents because we're going to implement an agent the task which are the tasks that we're going to assign to the agents the crew so the final wrap of all the agents and all the tasks process sequential because we want to run the process in a sequential mode and LLM agnostic check the LLM that you're using because of course when I do it I can show you an example assignment one here I have just the agent implementation and the get crew okay this is a function for getting the tasks and the main function that is calling kicking off the crew and I'm not saying anything about the LLM I'm just saying I'm just using this out of the box I'm not saying I'm using open AI why not saying anything here because if you want you can tell crew ai please use this specific lm and as you can see here in this example here you have all the things that you need otherwise if in your system you have this open ai this is what i have now open api key saved this is the default model that is used by crew ai so i don't have to do anything on top of it otherwise you can do things like this you say this is the LLM that I'm using you don't need this extra parameter you just need the model and then whenever you want to work with this LLM you just assign it to the crew let me show you an example this is also how you can use crew.ai documentation of course they're using crew.ai for this all right you have an agent you have a second agent what's interesting is that for this specific example we're using this LLM and for the second example we use another LLM this is an example where two agents are basically using two different LLMs that's not what really what we want to do we want to use the same element LM for all the agents my advice is to write something like this you write the LLM you tell the LLM object what's the key the base URL of the LLM you get this from the LLM documentation and the model that's it the temperature is optional and then whenever you work with the agents you always define this you say okay this is the LLM that I'm using and it will be the same for all the assignments all right now you have everything to start working on the first assignment and in a few minutes we'll go through the solution, and I'll show you what I did for this. Questions? All right, so a crew AI unit, or in general, crew AI CLI is not part of the workshop, so going to use the agents, the tasks, and all the classes directly. CRU AI CLI is useful if you want to set the project in a specific way that eventually lets you write the agents and the tasks in a YAML file, which is also useful for when you deploy CRU AI on CRU AI Enterprise, but that's not part of the workshop. If you want you can use in it in my advice is not to use the crew aicli for now and to focus on writing the actual agents and tasks in python and also the structure of the process project is more clear because one crew is essentially this here i have some extra input parameters but this is one crew i mean we're talking about 100 lines of codes and a lot of comments that's it with crew ai in it you get At so many things Okay if you have questions just I'll come to you no worries You have a few minutes for this This is too easy I didn't even want to Include it into the workshop Right it's too easy And if you don't Want to write code which is completely fine Because it's too early in the morning Please Pay with someone else Try to work with someone else Also with me If you want to come here We can write code together I'm not chatting I'm checking Discord Who is still stuck at Configuring The packages Poetry Shall we all gather in that part of the whole so yeah let's let's gather there let's do a quick workshop in the workshop a couple of things first in this very tiny link because I'm not a good front-end developer there is the there is the presentation so if you click on this tiny this presentation I'm sorry I'm a data scientist you get I get what I deserve this presentation you go to the presentation so you have all the things that we have seen this is very useful because you can just go here and see okay agents tasks crews and the other components in addition to this someone okay a couple of things that I learned from you use a Google call up if your environment is broken you don't know how to fix it you have problem with certificates so far so on work in a google call up the way you do it is that you open the google call up notebook you do pip install you install crew ai and you're ready to go in order to export the openai key and to have it ready usually you can if you're working into the terminal you can just do export open ai key is not really called like this i think it's uh okay there's a different name but please check the documentation and then you put your secret here when you export you have this available for crew ai which recognizes the key automatically if it's OpenAI. If you're using something else, we go with what we have seen before. So we create this object LLM and we tell it where the key is and which model you want to use. All right, we have... How is going on? Okay, someone has a question. How is going on with the first assignment? Who needs help with the first assignment? Okay, anyone else? Just yeah, yeah, yeah, up, up, up, no worries, I also need help, let's get, okay, all right, one useful feature that you can use in the Python project TOML is this, package mode faults, if you have this, you should not complain about the workshop folders and I was I'm pretty sure that this was part of the toml file yeah so one minute and we will check the solution all right let's check the solution of the first assignment of course I don't think we will finish all the assignments today but that's why I have the website so you can do it also later and I will provide the solutions in the same repository all right so what I did for the first assignment as we said we want to have this prompt announcement feature application what I'm doing here is creating two different agents is it shall I zoom in can you all read the code shall I decrease the lights can I can try all right that starts in the meantime so basically I have two agents as we said the first one is about prompt structure engineering so we want to get the prompt I just came up with this thing right so you decide how to you can also do it do everything in one agent but for the sake of the exercise I want to split this so the first agent is about improving the prompt structure of the prompt the second agent takes the prompt that has been improved and it will optimize it for the LLM. What I'm saying here is prompt structure expert, improve the structure of the prompt clarity blah blah blah. A goal for the LLM optimization agent, optimize specialist LLM prompt, your specialist optimizing prompt for different LLMs models. Okay, I tend to use verbose especially for this workshop so we can see the full output and the reasoning behind that is done by QAI and then what I like to do is wrapping tasks into functions this is a lot of comment okay so just a few lines of code instead of just writing tasks and passing the output as I showed you also in the assignment I'm basically wrapping the two tasks into a function and I'm returning these tasks i'm in this case i'm returning the crew why am i doing this because i have to input parameters one parameter is the prompt and another the other one is the target llm the target model and i want to put these two strings because essentially we're talking about strings into the description of the task this is what i put into the task can be also way simpler than this and as you can see here i'm using the python formatting to put the prompt into the string and i'm doing the same for the task for the optimization task optimize the enhance prompt for the target model let's have a look at the first task what i'm doing is giving the instruction I will apply this to the prompt that I get as input and the agent that I'm assigning this task to is the prompt structure agent and the expected output is something very relaxed. I'm not working with PyIdentic, with dictionaries, I'm just saying an enhanced version of the prompt with improved structure and clarity. How does it work in terms of sequential tasks? This task will be connected to the second one, so the second one has access to what we see here, so the output of this task. If you want to be, let's say, diligent, you have to do what I showed also in the input-output slide and you say something like improved prompt something all right then the second task gets whatever you get from this task plus something else this is a string I'm not doing anything with crew.ai and this is a string that I get from this function by doing this I have the two tasks initialized with what I needed inside the description and I can give the tasks to the crew I have the two agents of course the agents are here we saw them before and then I have the two tasks and i'm using process process sequential the process sequential is needed because we want to execute these two tasks one after the other and that's it we're returning the crew if you wrap everything enough into a function it's definitely easier for handling input outputs and as i said encapsulating more strings into their descriptions in the main i'm just get in the crew with the function that we just saw I am, so in this case this is the prompt, write a story about a robot the target model is GPT-4, can be anything else and in this case I'm just writing some extra stuff to show the output do you want do you do you want me to run this shall we or yeah I wasn't expecting you to be so excited about running it all right Perfect. All right. So this is very nice because when you use verbose, you get a lot of extra information. This seems not super useful, but when you write a big application, this is the final output. When you write a big application, the verbals can be very useful to understand what is happening behind. Sometimes we are dealing with a lot of non-deterministic processes, thanks to the LLMs. And understanding what crew AI is kind of thinking, it helps us to see, okay, especially when we have hierarchical process. okay this task was assigned to this agent but the manager decided to go in a different direction and call another agent why and then you can see the reasoning behind because all these agents are explaining everything about the product kind of thinking process all right then this is the final prompt about the story of a robot yes robot all right um probably have questions about this I will answer them but for now let me introduce the second yeah try to pair up please if you can let me go to the second assignment and then I will stick around to answer questions about the first one who needs the solution of the first assignment right now okay I'll think about it all right content creation guardrails this second assignment yeah the second assignment is about creating content with guardrails this is a very common thing we do with LLM essentially what we're doing here is creating an agent that creates content for you but on top of this we're basically using the flow so we have these steps content creation safety check output we can have one crew with only one agent it can be very simple or multiple agents that's up to you and the way we call the crew you have the code here is basically this one we have this flow that helps us to call the crew, check the output or whatever we get from the crew and then eventually decide, it decides to recreate the output if something goes wrong, for instance the content contains violence or continue and return the final output of the application. If you check this example we have generate vacation plan task crew we kick off and then we get a result we save the result into the state this is the state that we're using and then in the second step we're using these decorators to take advantage of the flow In the second step we're checking if what we get out of the LLM contains violence or not. If it's not valid we move to recreating the content through the crew or we move to the final output. What you see here is essentially what you have to use, so you can take this code and use it as it is. the only thing is that you have to adapt it to your crew so you cannot use task vacation plan but you're gonna write something like okay your crew is creating some content all right in the meantime I'll try I will share the solutions and I'll answer questions we have a few minutes but I think we can get five more if you wanna all right okay for you to know now if you go here to the github page of the workshop you have the website and you have a folder called solutions so you have all the solutions here Um yeah also if you want to run another if you want to run a Workshop if you want you can use this website hopefully it will Be useful for someone else after the pycon um yeah so the Solution of the first assignment is there but also the other Ones if you're stuck um and now i'll stick around for questions all right we have we're running out of time but we're checking if what comes next so if you want to stay for five minutes more we can use the Q&A kind of ask questions, or I can help you with whatever you're doing now. So, we have a couple of questions, Alessandro. Can we start with questions? Okay, well, thank you everyone for the great participation, and thank you, Alessandro, for such a wonderful tutorial so before we wrap up we have a couple of questions which are coming in Slido so I'll be representing them on behalf of all the attendees so come first thing why are we we are moving to MCP does QA also support this instead of writing my custom tools I don't know, honestly. I didn't use it, and I'm not aware. I suggest you check the documentation. I really don't know. Okay, next question is, can you brief about CRU-AI versus Autogen? Versus Autogen? Yeah. So versus Autogen or also versus Landgraf, I think that what I like about CRU-AI is that it has a simple interface. So I like the interface. It's, in my opinion, simpler compared to many other frameworks that I see around. So especially if you're starting out with agents, it's not a big step to use crew AI. So that's, in my opinion, one of the most interesting things. And I like the separations between agents, tasks, crews. That's a nice concept. Okay, thank you. So the next question is, could you recommend crew AI in production? What should I consider as a developer? Yeah, that's a question I get all the times is yes and no. I mean, there are so many. It depends on what you do with it. If you're working with a simple application, you get a lot of nice things that you don't get from using the raw APIs of an LLM, definitely. You can play with the outputs. With the outputs, you have separations of tasks, crews, agents, so I like that. But at the same time, I found myself also contributing to crew AI for bugs that I wasn't expecting to see in the library because it's a new library. So, yeah, yes and no. It depends on what you want to do and what kind of application you want to have in production. Well, so the next question is, Super.dev gives me bad requests when I try to sign up. Any clues? Server dev tool? Yeah. Yeah. Apparently today is not working. So bad for them. They could have gained so many customers. Okay. So one last question. How does Crew AI address the problem that even a 90% success rate on a single call over multiple sequential calls compounds to much lower overall success rate? Can you repeat? Maybe I'll also read it. It's a long question. Can I help? You know, that's, I'm not sure about maybe this anonymous person can write something more about this. I didn't really get it So when you say steps you're talking about Having multiple, for instance, tasks in the same crew. Yeah. Well, this is where the framework Comes into help, i guess, because you have some control On top of it. You're not just, like, calling An end point, an open ai end point as it is, and then Whatever you get is the final output. have some, for instance, we have seen before the guardrails, whenever you are calling a crew and something goes wrong, because it does, it happens, what you say it is, is what we're working with in terms of generative AI. When something goes wrong, you have some guardrails and you can force the output, you can force the crew to recreate the output and kind of play with the flow of the execution so you do have some some tools that comes that can help you that have that are provided by crew AI yeah and another another thing that I have in mind is orchestration hierarchical whenever you have this agent that is taking care because sometimes you have agents that are working on something that has a very low success rate maybe you're asking the agent to find a solution of some universal I don't know some kind of super specific problem but if you use hierarchical you have another agent that is on top of all the agents it is overseeing the work of the underlying agents as a manager and that helps to maybe I don't know say okay you cannot find a solution I'll try with another agent I can write a report about why we didn't make it things like this And also, if you know, if you are aware of the problem you're solving, you can also build a clue in a way that if there is no possible solutions, for instance, with flow or guardrails, you can force application to say, okay, there is no solution. Just say it. That is not possible. Yeah. But that's a good question. It was complicated. It felt like a light code from Google that that's it. Okay. That's all the report. Maybe you can take this question offline. So thank you so much, everyone, for being this tutorial interactively. And thank you so much, Alessandro, once again, for delivering such a wonderful workshop. Thank you. Thank you, everyone. Thank you. Yeah, whoever has questions, I'll stay here for five minutes more. So feel free to approach me.