hi there Tony from CRE AI in this video I'm going to walk you through how to get structured consistent outputs from your cre AI tasks if you're not familiar with tasks in crew AI it's essentially another primitive of crew Ai and what it is is essentially a unit of work that's given to an agent to perform and actually get an output from so think of it as you as an employee or maybe you're not an employee but just imagine an employee they need a job description and they also need kpis to actually measure it that they actually Meeting those goals in a sense so same thing with AI agents concept so tasks are things that are given to an agent to perform and take action on and actually provide an output after that so let's get to the code and show you exactly how this works all right so let's get to it uh in this code that I have here I have a crew that essentially goes and gathers the latest AI developments in on the web so it goes using Ser Dev is going to scrape all the links of the latest AI given a specific Topic in this case I give it I'm going to give you a topic of AI agents in 2024 since that's really something that's really interesting at the moment and I'm going to give you a date of today which is going to use the daytime function there and it's just supposed to go out there gather some report of AI agents uh topic for example you can pick a different topic there if you want but for this case I'm using AI agents supposed to go do some research get that research and generate a final report that I then can actually consume uh in a much nicer format in in a sense it will be in a markdown file at the end of it so quick let's just get started here with going over my agents so I have three agents here and the first agents is a researcher and like it it says here it's a senior data researcher and the goal for it is to uncover cutting age developments in a given topic which is in this case I'll give it as AI agents in 2024 and the backstory is this is more like the job description so your season researcher with an knack for uncovering the latest developments in a given topic known for your ability to find the most relevant information and presented in a clear and concise manner and then we have the reporting uh reporting analyst here it's just going to take the information that the first generated which is from the research and then it's going to create a analysis based out of that information and then finally pass it to the formatter who's going to just essentially put it in a format that I want and just bring it back to me in a markdown file in a specific order that I need and then we have uh tasks now so each agent needs a task so for my research agent is this is their task and their description here is conduct a thorough research about a specific topic that I give it to it and they expected out with that I need from this agent is a list with 10 bullet points of the most relevant information about the given topic that I'm going to give it to in this case and then the agent that will do this job is of course the researcher and then the output file for once it finishes their task I want them to Output the output that they've generated from that task in an output folder under research. Json so I want that to be in Json format and then we have a reporting task which is essentially going to be assigned to the research uh through the reporting analyst and it's going to do the reporting kind of gather insights from that research and then generate a a report with the main topics and and uh full section of information here I've just tell I've told it to format as markdown without the signs but I want it to be in Json so this line doesn't really matter in this case so the output file I want that to be in Json as well and then we have the formatting task which is the final task that we need to give it to the final format agent and essentially what it does here I've given it some couple I think about 12 instructions here on what it needs to do and this is the one that is going to generate the Final Markdown file that it's going to be in a structure that I want it to be so for example here you can see I've told it use proper markdown headers and I've given it an example there just more like oneshotting it as well and then include emojis and of course who doesn't love emojis and then format key findings into bullets at proper spacing uh just regular extra information that I want to give it to here and then the expected output that I need from it's uh from this agent is a beautifully formatted markdown Report with proper structure spacing citations and visual elements that enhance readability and presentation yeah keep in mind that we want citations on it as well so that way we can track if it's hallucinating on some of the report for sure and then we're assigning that to the form agent and then for context also we wanted to take the context of of the first two tasks so the first task which is does a research and then the analyst so we wanted to keep the context of those and use that within its final task here and what it does and as as as say as well it's going to generate a report. MD file here as well so let's look at our crew.
py file on how we actually enforcing so if we were to run that just without having any structured output like using pantic models in this case to make sure that it comes out in a structure that we want if I run the agent the first time it would generate maybe in a different format that I I wanted maybe it would add a topic findings relevance and Source but then the next time it would not do that at all it would not add the to that so how do we force it to make sure that it actually generates exactly as we want this is where the output pantic and output Json comes in this is the structured output section of it so here we would create within your crew you would create uh two uh a couple models depending on how much you want to go in depth in terms of how your information you want it to be but in this case I've generated I want to say one two three four four models in this case uh of pantic output so we have the first one is the research point and for this one essentially I just want the topic of that research that it gathered I want the findings that came out of that and I also want the relevance um why this finding is relevant or important in this case why should I care in a sense and then the sources I wanted to have a description which is you know sours with title and URL and so I want that to have each Source attached to each um source that that that it produces as well and then we have research output essentially kind of inherits this first model here as well and a list of research findings and then a brief some sario or overall findings of all of this report that you generated and then we have the final executive report section and this one is the one that adds or we make sure that it adds the Emojis adds title as the content uh section has the key insights and the final recommendations at the end as well and also making sure that it has the sources are added in there as well so that covers that as well and then we have the executive report generator at the end of it so this is the one that's going to generate the markdown at the way I just kind of making sure that whatever outputs come from the agents from the task themselves they get validated against these models to make sure that they IDE to this structure and that's why it keeps it more of like a consistent structure that you can depend on as well and so we have the extra spaces here as well you know report sections we have the next steps and then we have the sources for each of the section and after that our regular code comes in so we have our crew base your class which um you know has all the agents that we need we have the research agent we have the reporting agent uh formatter agent and then when we cut to the tasks and this is where how do we use the models that we've defined up there you would use it within your tasks so if you come to each task here for example for the research task because that's the one we wanted to do research we're assigning that the research output which we built up here already which is this research task output and so for the first task we assigned that here and you would come here and do output. output Json Json and so we siding that and we're telling it hey once you do the research formatted in the structure we gave you up there and make sure that it it's validated against that to make sure that it ad the to that and then output it's going to Output that into uh an output folder that we'll see on the side as well there and then we have the reporting task same thing but this time we wanted to just output by dantic and we already defined that up there as well so just output that as well and now here we have uh the formatting which it's already we don't need to Define it for it because it's already it's going to inherit from this from this one up here as well as the final done file and here we Define our crew which is essentially a crew you can think of he this is my crew it's usually your buddies or something of the sort so same thing here assembling all of them together and getting ready to run them now we go to the main file here which is of course importing the crew itself daytime and then here is just the running portion so on inputs this is where I'm adding the topic of which I wanted to do research on which is AI agents in 2024 and then we have date it's just going to get that date time there and this is really to make sure that it gives me a report for I know which report was for which day for example and we just run it so let's go ahead and give a spin and see what it outputs for us and just for your awareness as well I'm using gp4 or mini so it might take a while a little bit for some of it to just go through it but let's run it and as this starts up and run we should monitor here on the side we'll see eventually once it start it finishes each specific task for example you've seen here it's going and done the research it's it this is what it input into the Ser Dev search and it actually searched a bunch of results we see some results that is generated here now the next step right now what it's doing once this outputs are are are returned from the age from the from the task itself it's actually going and validating against our pantic model and making sure that it matches that uh format that we want and now it actually finished the research so the researchers has finished their job now it's moving to the next agent to actually get the analyst to do the analysis there so if we look at what the researcher has already done this is the output that is generated so we can actually just close the side and I'll show you a snippet of it so this is the Json and it that uses Json so it's consistently a good Json file that is a valid Json so if you didn't have the pantic sometimes it would generate a Json sometimes it wouldn't or it would generate Json but then it would add extra text at the top that would just just then break the Json at that point so this is why having structured output is very important and you can see it structured it the way we want it so you see the topic and then you have the findings you have the relevance why this is important and then the sources you have the title and the link to the source itself so this is really cool so it's doing the the research Gathering all the websites and all the details going through each and then generating this final output here as well and let's see what else has been generated so we have the research. Json uh we have the report now so the report agent has finished your report and since this is also another Json file I don't think it's it's structured the way we want it so that's easy just do option if you're on Mac option shift F and it should format that file as you need or you can just go ahead and click on the file right click and then click on format and here you can see it's already you know generated it so we have the report title generation date which is today executive summary and then uh key findings as well on that side we have key finding key finding key finding key findings for each and then we have the report section it just continues and it also has the the links as well so that looks pretty good now let's go ahead and see if it's yeah so it's generated the final report here so it's in and it's in markdown so we can actually preview this and see how it looks like in properly structured markdown so if you already have that extension within your IDE you can actually see it here so here it's generated the final output and let's see how it looks like so in this case we have ai agents 2024 Transformations and future directions and it's added xray Emoji there and we have table of contents that we has asked it to have so we have executive summary key findings recommendations market growth independence of AI agents generative AI advancements productivity enhancements modular AI systems interactive knowledge synthesis so a bunch of stuff here of course right and then we have the next steps and then we have sources at the end so let's just look through this scan really quick so it has some information so executive summary uh in 2024 a agents are experienc significant advancements okay that seems accurate of course and then key findings AI agents Market is projected to grow from 5.
1 billion US in 2024 to 47. 1 billion by 2030 with a CER of 44. 8 During the period so and it also cites the source here so if you go to this Source you can see it's saying it's Global newswire.