hey guys and welcome to this crew aai flows crash course where you're going to learn how to use crew ai's most powerful new feature with flows you can combine the power of code with the power of crew AI to build powerful event driven workflows that can automate anything for example you could build a flow that combines multiple Crews together to go off and write a 20,000 work book or you could build another flow that goes off and is continuously looping and checking your emails and autogenerating responses for you or you could even build another flow
that takes in a single newsletter and then branches off and builds a new post for YouTube LinkedIn and X really at this point with flows your imagination is really the only limiting factor and in this tutorial we're going to be walking through everything step by step together we'll eventually cover six different examples when it comes to flows where we're going to start off by creating a super simple flow just to get our bearings and we're slowly going to add in complexity to we off creating a new flow that is combining multiple Crews to go off
and write that 20,000 word book on our behalf and the best part which I'm so excited for you guys to see is creating flows is so easy doesn't matter if you're a beginner or an advanced developer you can start creating your first flows today so if you're excited to level up as an AI developer and you're tired of those other complicated AI Frameworks this is the video for you but enough talking let's go ahead get our hands dirty and dive into creating some flows oh and I almost forgot the best part all the source code
in this video is completely for free and you can download it by checking out the link down description below and if you want some additional support and to meet some fellow AI nerds go ahead and check out the free school Community I created for you guys we have over 3,000 members and going strong and we'd love for you to be a part of the group but enough of all that back to flows all right guys so welcome to the first example where our whole goal is to build and run your first flow super excited for
you guys here's a quick overview of everything that we're about to do together first things first we need to actually set up your environment to run these flows this is going to be super simple I have some like step-by-step instructions for you guys to then we're going to actually dive into start stubbing out our flow and then running it and along the way we're going to cover a few important topics that you need to know when it comes to building flows such as the flow class as a whole and then what the heck are these
start and listen decorators we're going to see these in action and explain what's exactly going on so you guys will actually click and understand a few of the key important parts of working with flows so let's go ahead and start running into step number one which is setting up your environment to run these flows all right guys so what welcome to cursor this is where we're going to be coding out all of the different examples over here inside the tutorial and what we need to do before actually creating that first flow is we need to
set up your local environment now here's what we need to do inside of the source code that you can download for this project you will find a read me this read me will walk you through the two steps that you need to do to set up your virtual environment on your computer and the reason why we're trying to do this is we basically when working with python want to create a virtual environment each time we work on a project that way we don't have different packages is mixing up and conflicting with each other so what
you'll see inside of this read me is I give you guys the option to create environments in two different ways you can use cond if you've already installed cond on your computer or you can go down the path of creating a virtual environment this way now I'm a huge fan of using cond so I'm just going to go with approach number one so let's walk through this together so first things first we're going to open up our editor and we're going to go ahead and paste in this command to create our new virtual environment this
will take a second our already have the virtual environment so it's going to ask me do I want to remove it and I'll say yes and it'll go through the process of going ahead deleting it and installing all the local packages so I'm going to say yes I do want to create this virtual environment then once it's done it'll say hey cool you have installed a new virtual environment for python 3.10 do you want to activate it and start using it the answer is yes and here's the command we're just going to do cond activate
crew aai flows this will go ahead and update our current virtual environment to say we are using crei flows and this is where we can now work safely and not mess up and interfere with other different python versions on our computer cool now what are we going to do next well now that we have our virtual environment we want to go off and install all the requirements for this project so we can just follow this Command right here once we're in our virtual environment and what it's going to do is go off and install all
the different dependencies in our requirements. text where is this at well it's right here requirements.txt if you open it up you'll see that we're just using two right now we might add more later such as cre a i and then python. EnV this is how we're going to go off and actually like load our environment variables more on that later on so what we're going to do go ahead and install it and this is going to add in sorry wrong directory you need to make sure that you're in the root CIF flows but once you
do that you can go ahead and type in PIP install - R requirements and this will go off and install crei and python. EMD awesome so we have now successfully set up our environment and installed our dependencies which means we can now start working on actually creating our first flow so here is a super basic flow where we're going to strictly focus on the Three core components we talked about earlier flow listen and start and we're going to see how they all tie together in just a basic flow outline understand what they do and then
we'll actually start implementing in functionality so you can see everything in action so first things first whenever you want to create a flow what you need to do is create a new class that inherits from flows okay super straightforward now let's dive into our two different decorators start and listen so let's start with start start does exactly what you think it does this is the starting function that gets kicked off whenever you start running a new flow so in this example whenever we kick off this example flow the first thing we're going to do is
call the generate City function and in this example we only have one start function but you could easily have 5 10 doesn't matter you can have as many starting points inside of a flow as you like now now what we want to do next is dive into our listen decorator and the way our listen decorator works is it does pretty much also what you would expect it to do it is going to listen for whenever the generate City function finishes and as soon as that function finishes it's then going to go off and run this
function so this is super nice because we make it very easy to listen to whenever things get done so that you can start taking action on them so let's walk through this flow start to finish so you can understand exactly what's happening whenever a user kicks off this flow first things first we are going to go generate a city this city function is going to then use AI to go off and generate a random City then we are going to return that City and what's so nice about this is inside of flows is whenever you
return a value from one function what you can do is access that return value elsewhere so in our case we're go ahead and generate a random City here and then for all of our listeners they're going to be able to access that generated random City so you can see right here in our listener we're going to be able to access that return value right here so super cool and so basically what we're doing is going okay go ahead and generate me a random City return it and then all of our listeners will go hey I'm
waiting for Generate City to be done so whenever this is finished it goes oh okay you're done oh and I see you generated a random City for me New York awesome I'm now going to go off and use AI to generate a fun fact about this city so all around pretty straightforward and as you can see it makes it so nice to just pass information between different events inside of your flows okay so cool now just to wrap things up we've talked about kicking off your flow but how the heck do you actually do that
well super easy just like you normally do with Crews where you would go off and create a crew and then you would run kickoff that's exactly what you do with a flow you create your new flow and then you just run flow. kickoff and what'll happen is the final function whatever is the last running function inside of your flow if this was to return something so return fun fact about New York this will get returned and this will actually become the result so then we can actually go off and log the final result from our
flows so now that you've seen big picture and high level implementation of how things are structured in a flow let's go ahead and actually go look at a real world example of this in action so you can see oh that is exactly how information is getting passed around and we're going to start using and adding in some AI so you can actually see us generating random cities and generating fun facts so let's go ahead clear things out and look at the final result and we'll start from the top and work ourselves all the way down
to the bottom so the first thing that we have changed is we've updated our Imports to add in two additional Imports one load. this is going to help load all of our environment variables and the second one is going to be from light llm light llm will come included whenever you install crew aai but what it does is it's a super awesome library that makes it very easy to start interfacing with all the most common llm providers out there in our case we're just going to use light LM completions to go off and start making
some requests to to open AI to generate that random City and fun fact for us that's all we're trying to do now it is super important in order to do this example that you go off and create and update your environment variables to include and basically make sure your EMV file includes your open AI key so make sure hop over to open a grab your open a key paste it in your environment variables so that this will properly run for you cool now once we've done that let's go ahead and dive into our flow now
that we have our environment set up and using open AI so what are we doing in here first things first we need to set a property inside of this class in our case we're going to say hey I just want you to use GPT fl0 mini when it comes to accessing and generating anything inside of open AI cool so that's our model now here's where we're actually going to start working with our two different steps inside of our flow so step one which is going to be all about generating that City let me show you
what we're going to do first off we are going to say okay you are my starting point when I start to generate a city and to do that I'm going to use the completions library from light llm and what I'm going to do is just hard code and say yes this is my model that I like to use and for the purpose of this example all I want to do is just pass in a single message saying hey please go off and return me the name of a random city in the world super straightforward from
there what we're going to do is you know if you've used open AI before you know there's like a ton of like responses grab the First Response get the message get the content so this is just all straightforward working with open Ai and we're just going to go ahead and grab that random City and like we talked about earlier we give you the ability inside of flows to return the value from one step to the next so that's all we're doing here we're going to go okay cool I just made that random City I'm now
going to return that City so that everyone that's listening to the generate City function to get finished will all have access to this random City that we just generated in this example all we have is one single listener listening for the generate City to wrap up so what we're going to do is this function will get kicked off immedi medely next once we finish and it will take in that random City and all we're going to do in our case is say okay cool I got it I got the random City now I would like
to make a new completion and the whole point of this completion is to say hey open AI please tell me a fun fact about an our case New York San Francisco whatever random City got generated cool once again we're going to pull out that fun fact from our response and then now what we're going to do is we're going to return that fun fact so that this is the final event that's happening in side of our flows which means it will now be the final result that we can end up showing to our user so
this fun fact will come out right here and this is how we can access it after running the kickoff Okay cool so now that you've kind of seen this in action and we've walked through everything from top to bottom let's go ahead and actually kick this flow off together so that you can see an action so go ahead open up your terminal let's clear things out and start running this flow so first things first you need to make sure that you are actually using your proper environment now the the easiest way to do that is
whenever you just type in which python I think we might have talked about this earlier but just make sure your environment right here you'll click enter interpreter path and you'll just go ahead and copy and paste this interpreter path up here inside a vs coder cursor if you're on a PC you'll just type in where python and do the exact same thing to make sure that you are actively working in the environment that we set up earlier so big note on that one cool now that we have that set up let's go ahead and start
running this example that we put together what we can do is just type in Python and we are in the first example set so we'll do one basic flow and this is the basic flow python class so let's go ahead and run it then the first time it usually takes a few seconds for CI to find everything but what's pretty cool is you can see it did exactly what we asked it to do so first things first it's going to say I'm starting the flow and what's cool is like obviously this is our starting point
so we would expect this to be the first thing to get printed exactly what happened open AI please go make that random City for me print it out looks great and just to prove to you guys that information is getting passed from one step in our flow to the next you can see right down here when we go off to generate our fun fact we are receiving a random City and that's exactly what you'll see right here then we can start taking action on this in a very step oriented approach and then what we can
do is go cool I got your city I'll generate a fun fact now let's return it and that's what you'll see right here whenever we go off and generate a fun fact this is coming from our result which was the you know whatever the last function to run inside of our flow will become the result so super awesome and I hope you guys see like this is a very straightforward event where we have one function spinning result to the other but with flows we have so much capabilities that you'll see later on in further on
examples to where you can have one initial step and then three follow-up steps all running in parallel you could have three starting functions and then you'll wait for all them to get done and then you'll take in all that information and aggregate it into one follow-up step there's so much that you guys can do so I'm super excited to get into this as we progress throughout this course so let's go ahead and head over to the next example where we're going to start going off and working with a little bit more state so we're going
to add in some a little bit more complexity and show you guys just how awesome flows can be so see you guys in the next example all right guys welcome to example number two and in this example and the follow-up example we're going to talk all about State Management inside of your flows so what the heck is State Management well inside of our flows we are giving you access to a global state property and which so nice about this state property is that it is one shared dictionary that you can access between all of your
different steps inside of your flows because if you remember in the last example we set it up so that you know basically step number one would go off and do its work and then it would return the value immediately down whoever's listening to it well with State what you can do is go oh okay well I'm just going to update This Global dictionary that way I don't have to worry about passing information around it's always right here and I can just just go ahead and say like hey you know I want to set this state
I want to set a property to it or later on I want to get a specific value from it so very straightforward but let's go ahead and look at examples so we can see state in action and to start we're going to focus on an unstructured State and this will become more clear as we compare and contrast it to structured state in the next example so what is unstructured State well what we're doing is we're saying okay by default self. state is just an empty dictionary we it doesn't have any Keys any values nothing it's
just a blank dictionary and it's up to us as the developers to say okay cool well in this case for our inst structured example all I really want to do is just work on changing two things I want to update my message and I want to slowly work with a counter so in this example here's what we're going to do first things first we're going to use our start decorator to kick off our first method and all we're going to do is say okay let me just check out what the current state looks like before
we run everything and once we've checked the state I'm just going to go ahead and set our initial message to hello from unstructured flows and then from there what I want to do is set our initial counter to zero then what you'll notice is once this function is done we're going to listen for whenever it's done and then we're going to kick off our second method our second method we're going to once again log state so we can see what's happening and then all we're going to do is start appending and updating the values inside
of our state and what you'll notice is we're just going to add a little bit to message and bump up our counter and just to like bring it home we're going to do it one more time we're now going to listen for whenever our second method finishes and then we're going to once again just make a few changes update our message and state just so you can see it's Global it's we can access it anywhere throughout our entire flow cool so let's go ahead and run this example so that you guys can see unstructured Flows
In State Management in action so open up your terminal once again and this time we're going to do Python and this is example number two and we're going to go ahead and call unstructured Flow so what we would expect to see is it for it to go method 1 2 3 and along the way we would like to see our state gradually be incremented and updated as we go and that's exactly what's happening inside of our code so let's dive through this part by part and actually look and compare it to our code just to
make sure it's working so whenever we start out we said all right whenever we're in first method we went to start the flow and we went to look at what the current float is before the current state before we go off and actually start executing and updating our state management and you can see yeah right out the gate before we do anything state is an initial it's just an initial empty dictionary there's nothing in it it's up to us to update it once we go ahead start making changes in it you can see in our
second method like oh yeah we did we were able to go ahead and update message encounter and then in our third method we were able to continually keep adding in additional values now what's so nice is that not only when you're working with flows and you have access to State Management what's so cool is you can actually after you've run your entire flow you can access that state so let's just say you had a bunch of different events going off and parallel doing work over here doing work over here and once you're done you just
want to go hey what was this piece of information and what was this piece of information inside of our state well you can do that you'll just after running your flow you'll just go ahead and access the state property on your flow and you can go ahead and look at it so in our case hey what was the final state after running my flow oh well it was our message that we updated numerous times and we updated the counter okay super cool now now that we've kind of seen unstructured Flows In Action I want to
go ahead and hop over to structured flows which give you some more guard rails and some type safety and are all around just a little bit more safe so let's go ahead and hop over to example number three so you can see structured Flows In Action all right guys welcome to example number three where we're going to start looking at structured State Management inside of our flows now like I said in the previous video structured flows give us some type safety and guard rails as we're going off and updating our state and accessing different properties
in addition to that they also provide autocomplete so now that I've given like the key benefits let's talk about like how the heck do I start using these structured flows and what's different so first things first the major difference when it comes to working with structured flows is you need to pass in an initial State into your flow so you can see in our case we're just going to say hey our example State includes a counter and a message and in our case case counters a number message it's just an empty string now once you
defined this initial State as a class what you can go ahead and do and it is real fast just important to mention that this initial state that you're creating you need to inherit from pedantic base model so make sure super important that whenever you're creating your initial state to inherit from that awesome now once you've created this initial default class with default values what you can see is whenever we're creating our flow we're now using square brackets to say okay here is my default State and what we're doing is passing it in right here and
this is different because remember in unstructured flows we didn't do any of this square bracket nonsense so what we're doing now is just saying okay to use a structured flow I just need to provide structure to my flow so it understands what the heck am I working with so now that you've seen how we set it up let's actually show you some of the benefits in action so benefit number one like I said it provides for some guard rails and autocomplete so now whenever I'm working with State you can see that I now have access
to oh I have a message that I can work with with and then I also have a counter that I can work with so this is all because of the fact that I passed in my initial State cuz if I hop back over to our unstructured method and I start working with State it's basically just going to be like hey dude you're an empty dictionary I have no idea what you want me to work with that's one of the key benefits and then type safety the other thing that's super important is these default values that
you've passed in so right now you can see when our updated structured State Management flow you can see right out the gate we're not just setting the counter we're actually interacting with it and updating it and the reason we're able to do this is because well we have a default value so in our case counter by default is zero so what I'm going to do is now I'm able to increment it if you had tried to do something like this over an unstructured flow land right out the gate tried updating values this would cause a
major error because if you remember by default our state is just an empty dictionary so there's no way that you can update the counter property in a dictionary where it doesn't does exist yet this would just throw some major errors so these default values are super powerful and just give you a really good starting place inside of your flows then all around this is pretty much the exact same as the other function that we just looked at except now we're just using more structured State Management so let's go ahead and run this so you can
see it in action it should be like I said just the exact same but so let's go ahead clear it and then let's run in our case this is example number three structured flow and structured flow so let's go ahead and run it sorry misspelled python so now whenever I run this you can go ahead and see okay cool right now whenever I'm going to go ahead and first log our initial State you can see oh by default I'm not an empty dictionary anymore by default before I've made a single change I can see oh
yeah I have a counter and I have a message so this is just like hey this is how structured State Management really is beneficial and now you've seen it in action you'll see it much more in action later on as we work with some of our bigger examples okay I want to drop a few pieces of information that I think you guys will find helpful as you're going off building your own flows piece of advice number one you cannot mix and match when it comes to flows you can't do half structured and half unstructured meaning
you can't do selfa state and then we'll call it dummy value equals two if you try and do something like this it's going to go hey this doesn't exist on your structured state so I'm throwing an error I have no idea what you're trying to get me to do this is on purpose because what we want to do is add in some guardrails so if you're going to go structured stick to structured if you're going to do unstructured stick to unstructured cool now what else is super nice well when it comes to working with our
state what I like to do with structure State advice number two is I when I'm first building out my applications using flows I like to stick to unstructured just so I can quickly crank one of these flows out if I need to you know have Global State Management throughout my flow so once I get all of this working and you know I'm using the square bracket approach on to access and set properties in my dictionary once I have this working I'll usually take a step back and then go ahead and Implement a structured flow that
way going forward as I make changes I have guard rails and I don't end up accidentally doing something and doing basically like a misspell of like counter you know like basically let auto complete help me okay cool so hopefully that makes sense and hopefully you understand everything now when it comes to working with structured flows and unstructured flows so now that we worked on passing information in our basic example from function one to function two you've seen two different types of St State Management unstructured and structured let's go ahead and now hop into what my
favorite part of working with flows is which is how we can actually conditionally manage the way we structure different steps inside of our flows so let's go ahead and hop over to example number four where we have three different mini modules to show you guys all the different capabilities that you can do with flows so see you guys in the next example all right guys so now it's time for us to dive into section four where you're going to look at three different small examples so you can see how you can conditionally control our flows
inside of our application so let's go ahead and dive into the first example which is going to be talking all about or and one of my favorite Parts about working flows is the keywords that we're learning do exactly what they sound like so let me show you what I'm talking about so in this example we're going to focus on or and what or allows us to do is update our listeners so here's exactly what's going on in our case we are going to be listening in our case we have a logger and our logger is
going to be listening to either the start method or keyword or the second method and when either of these functions finish we're going to go ahead and Trigger this right here so this is a super nice way to go ahead and you know if you need to react to multiple things going on in our case a logger which would just you know go ahead and print the result we can go ahead and say oh yeah this makes sense whenever start method gets done working I want my logger to print and save the result whenever my
second method is finished I want to print and log the result so that's exactly what's happening inside of our code and the key here is just make sure you import or underscore from crei flows flows and what you need to do is just inside of your listener before you go off and start adding your methods just add in the OR function to say hey I just want to listen to these and you can just keep adding on more and more conditions as many as your heart desires Okay cool so now that you've seen how ores
work and look like in the code let's go ahead and run it so you can see them in action and what we'll do real fast is let's go ahead and clear this up and start running our first example inside of our conditional flows module so do python 4 and this is the first example which is all about or so let's go ahead and run it and talk about what we would like to see so to start we should see starting flow then we should see that this function finishes and it should immediately trigger off two
things one start method should immediately trigger off the second function cuz hey it's listening to our start method but also our logger is listening to the start method so when we come down here and look at our logs you can see okay this got triggered then this got triggered and at the exact same time in parallel our logger got triggered so you can kind of see down here starting flow was triggered up here then because these two were getting triggered in parallel we should instantly see our second method runs check and our logger should run
check and you can also see that our logger is not only listening but it's accepting whatever result was kicked out of this function all around super cool then what would we expect next well whenever our second method finishes we are listening to it once again with our logger so we would expect to see one final log going okay hey second method finishes up what do I want to do I want to log the result and that's exactly what we're doing right here so I'm hoping you guys can kind of see the power of like okay
cool I now know how I can make sure when I'm working inside of my flows if I ever have this situation where I need to listen to multiple things happening this is the approach to go anytime any of these functions finish whether start method or second method or keyword I'll go ahead and take an action so all around this is super cool now what we're going to do next is hop over to learning about and so let's go ahead and hop over to the next example inside of conditional flows all right guys so now it's
time to dive into and and and does exactly what you think it should do inside of a listener we can now update it to say hey I only want you to trigger this function when the start method and the second method finish so wait until both of these methods finish off and when they're done then go off and Trigger our logger so what's going to happen in this example is our start method is going to get triggered right out the gate then we're going to say all right well who's listening for start method in our
case that's going to be our second method is listening and our logger okay okay then what's going to happen next is well this gets triggered off because it's only listening to One thing so start method then second method now once our second method is done okay taing we have now met all the criteria because we've now completely finished the start method and the second method they're both done now we can go off and Trigger our logger so what we would expect to see is a print statement another print statement and then when we finally go
ahead and update our logger we're just going to print state so we should be able to see both of these pieces of information okay so let's go ahead and run it so that you can see and in action so we're going to go do python this is the fourth module and we're just going to look at example two focused on M so print print and then State that's exactly what we'd expect to see so start method check second method check and then in the final one that should only get triggered when both of the previous
functions are done this is now where we're going to see the state from that we updated in both of the two previous steps so I hope hope this is starting to make sense and and provide you guys a lot of flexibility because in the world where you ever want to go off and like let's say you wanted to set up a flow to like hey I'm going to go scrape LinkedIn I'm going to scrape Twitter and I'm going to go ahead and scrape another social media site and I only want to go off and write
a summary once I have the inputs from all of my previous scrapings this is exactly how you would do it so just a quick recap if this was scrape YouTube scrape LinkedIn and another one was scraped go ahead and scrape X you can go ahead and say hey I don't want to take action until I'm done scraping all of my different surfaces that are running in parallel and once they're done I can then synthesize all the data in a follow-up step that will only get triggered once all of these different methods are finished and once
again you could continually add in as many ands as you want in here and this function will not get triggered until all of the previous functions finish running so hopefully that makes sense and now that you've seen the two different types or and and let's go ahead and dive into the most exciting a little bit more complicated but most exciting part which is going to be working with the router which will allow you to dynamically go ahead and change completely different paths inside of your floats so let's go ahead and hop over to example number
three inside of this conditional flows all right guys so now it's time for us to start looking at routers and routers are nothing more than a new decorator that we can start adding inside of our flows and and all they do is Route you down different branches inside of your flows the best example I like to give when it comes to working with routers is to think of it as like if you're building a chatbot and this chat bot was to go ahead and its whole purpose was to send people to the proper representative in
the organization so if a customer asked hey this broke can you please give me support oh we need to send them down the support pipeline for help oh the customer wants to buy a new product we need to send them down the sales path to meet with the sales representative so they can buy from us so as you can see based on a different category however we categorize the initial question that was asked to us we send them down path a for sales p b for support so forth and so forth well that's exactly what
routers allow you to do we dynamically allow you to Branch down different parts of your flows depending on different conditions so let's look at this example in action walk through it so you can see like oh yeah routers are branching me inside of our floats so to start to go ahead and show you what's happening whenever we kick off our router flow all we're going to do is say okay to simulate making a decision we're just randomly going to generate true or false then once we have randomly generated that value we're going to save it
to our state as the success flag now here's where routers come in we're going to go okay cool inside the router whenever start method finishes we're going to trigger this function and the whole purpose of the router is to say I'm going to go down path a path b or path C so here's what's going to happen we're going to go okay cool well was the success flag true if it was we want to send people down the success Branch now this is a new term because everything up until now if you return something it
was just a value passed down to the next step inside of our flows but with a router you're not returning a value you're returning a whole new path to go down so you can see now we're going oh okay if things were successful I now want to go down the success branch and the way I'm going to go down the success branch is by updating a following on listener to listen for this new Branch so in the past everything has been a you know our listeners have always had a function name in here well with
routers you're going to go down to different branches and it's up to you to create those branches different Pathways so this is where you'll go oh okay I now start need to use strings so that's what we're going to say we're going to go down the success branch and okay cool we're going down that Branch I want to listen whenever we go down the success Branch just start triggering off these functions okay likewise if we are not successful and we fail okay I want to go down the failed Branch so now I'm going to start
listening for the failed Branch to get triggered when it does cool I'll start going down the rest of this flow logic so hopefully this is starting to make sense where we're going to start like start off do some work then we're going to have our router go okay I'm listening for work and based on some conditions I'll either go down path a or path b or path C you can have unlimited different branches it's up to you to Define them okay so hopefully that makes sense I do just want to quickly recap something that's very
important you need to make sure that this Branch this string this return value matches this Branch here if you accidentally forgot an s or something at the very end well this will never get triggered because you know this never gets returned so please please please make sure when you're using routers and you know really beefing up your flows getting fancy you need to make sure sure that these strings match your listeners down here okay cool so now that I've hopefully got you hyped up about routers I love this part I'm going to go ahead and
run it so you can see in action and you can hopefully see like oh yeah sometimes we go down this Branch sometimes we go down this Branch let's go ahead and look at it in action so we're going to come down here and we're going to start running in our fourth module this is example number three and we're going to go okay cool let's go ahead and run this so sometimes we would expect this other times we would expect this one so you can see out the gate we went ahead and triggered method number three
which means cool we went down the success path let's try running it again you know it's random so we might have to run it a few times to eventually we get the fourth method so yeah cool so it took a few tries So eventually on the third try you can see oh we randomly are going down false so whenever we hit false oh cool we're going to go down the failed branch and now we're going to trigger the fourth method so hopefully you guys are starting to see the power of this and going back to
the sales example where you're really categorizing based on a condition and then going oh support or sales or for example if you're going to go off another example is lead scoring so if you're going to hire a candidate well ooh this candidate scored a c oh yeah that's not we don't want to hire a c candidate cool let's go ahead and send them down the rejected Branch to where we're going to type them up a letter saying hey we appreciate your interest for this job but not the time and if a candidate scored an a
we'd go okay cool I need to send them down the request a follow-up interview so that we can get them hired Branch so hopefully this is starting to make sense to where you can dynamically respond inside of a single flow to all sorts of different conditions so all right so now that we've got that out of the way let's go ahead and start beefing up our flows even more and we're going to start introducing our crews into our floats so this is where we're going to get all the magic of working with code with all
the magic of working with Cru I'm super excited for you guys to see this in action so let's go ahead and hop over to module number five all right guys welcome to module number five where we're going to start combining the power of flows with cruise I'm super excited for you guys to see this in action and it's actually super super simple to set up and that's exactly what I wanted to show you guys first instead of going off and manually typing everything up what we can do now is use the crew aai CLI so
command line interface to go ahead and generate a new project for us that contains all proper project setup to make this super easy to run so what we can do is we will inside of our project go over to this folder so with cruise and what we can do now is start using the crei CLI so what we can do is type in crei create cuz we want to create something and what do we want to create a flow and let's just call this our demo flow and what we'll do is go ahead and just
press enter and it will go off and create a new flow for us and all the proper yeah like you see it just went ahead and create everything for us and it's a little hard to see but you can see inside of our example number five module number five it created our new project structure for us okay so before we dive into the code I do want to go ahead and point out like what the heck's going on because I know there's been a bunch of changes in crew aai so I want to just make
this like super straightforward and understand what's going on so inside of CI when you're working with flows we have actually structured out a project for you so that it's super straightforward to actually combine everything together so inside of your Source folder you'll notice the name of your project so in our case we called it demo flow over here well inside of here what you'll notice is you have access and it gets spun up with two different folders and a main file so our main file is where we're actually going to store our flow just like
we've been doing all throughout the rest of our project this main.py file is where everything's actually going to run now outside of that we have a few other folders the cruise folder is where you can go ahead and actually add in your cruise because as you'll see later on um and some of the other examples I'm going to point out at the end of the video you might actually want to have multiple Crews happening inside of one flow so one crew might synthesize some data while another crew goes off and generates some data and all
of this needs to happen in one flow so this is exactly how you can get multiple Crews to work together for each different crew you'll just create a new one right in here in your Crews folder cool outside of that we want to make it so that you can have tools that can get shared across multiple Crews so that's why we have a highlevel tools folder so you know if you're building a flow that works primarily on YouTube data you might have one set of YouTube Tool tools that get used throughout both of your different
Crews okay cool hopefully that's making sense now let's actually talk about something else if you have begun to work with crei you notice that we now have a pi project. tomile this is how we manage all dependencies for your crew projects there has been a big change recently to where we have migrated over from poetry over to UV quick background what's happening is a lot of these AI packages are super beefy and they took forever to install by switching over to UV it happened we install everything lightning fast now so I'm going to show you
that in here just a second but I did just want to point out this is a big difference so no longer can you do poetry install going forward it's UV but we don't even have to do that because good news everything's wrapped up inside of the crew aai CLI so let me show you what I'm talking about what you'll notice is now what we can do let's CD into our new project demo flows and oh real fast I do want to go ahead and update our envs so you can see by default our environment variables
are your API key what we need to do is just go ahead and copy our initial one paste it in here all right cool that was a close one we almost messed up now what we can go ahead and do once we're in our project what we want to do is first install our dependencies and then we want to run our flow so what we can do first is we can do crei install and what this will do is it will go off and install all the the dependencies we need to run this project so
in our case you can see hey I just went crei and CI tools so let's go ahead and run this and the first install usually takes the longest but like I say that was lightning fast so I hope you guys are seeing how awesome UV is when it comes to installing now some of these packages were already cached that's why it was super fast your first time might take a little bit longer but after that it's boom Lightning Fast cool so now we are set up to go off and run our flow but before I
do that let's actually probably take a step back and say like hey what the heck are we trying to run here before we just go off in and kick off our flow so here's what we're doing we are going to write a poem using flows but in order to do this we are going to combine the power of code with the power of crew so what are we going to do well to start off our poem flow which at this point hopefully some of the keywords are clicking this is going to be a structured State
flow where we're going to say okay my structured state I'm going to have a sentence count to determine how many sentences I want there to be in my poem and then I'm going to have a string where I can store my actual poem okay cool makes sense so let's go ahead and run this so whenever we first kick off this flow we're going to generate a sentence count so it's going to be between one and five then what we're going to do is go okay cool I now know I need to generate a sentence I
generate a poem but now I need to go ahead and figure out you know am I going to do one to five sentences well that is where we can start using passing inputs into our Cruise so you can see in Step number two we're going to focus on generating that poem and because this is an AI task where we're actually going to be leaning on crew AI to go off and perform this action for us we can go okay in my generate poem step what I want to do is go ahead and use my poem
crew to go ahead and kick off a new crew that's going to create a poem using this sentence count so you can see we're kind of using state to go off and actually go okay cool I need to generate a poem with three sentences okay so a lot is happening between this line and this line and I don't just want to hand way over it I want to dive into how the heck this is actually working because if you're coming from like old school crew AI this will probably be like what the heck's going on
and so let's go ahead and dive into what's happening well what we're doing is we're going to go okay cool I created a poem crew that is over here in my poem crew folder that that gets created by default what you'll notice in here there's actually a poem crew file and this poem crew contains all the specific agents and task we need to write a poem so you can see for Simplicity we only have two things in here we have an a single agent poem writer and we have a specific an individual task whose whole
job is to go off and write the poem and in order to create a crew you notice that we need to pass in our agents and our task now a lot has changed in this and we're going to keep diving deeper just because I want to like give a deep dive on how the heck this is all working so one of the things that you'll notice is man this is kind of weird like we have I'm defining my agent here but down here I'm just calling out self. agent how am I able like I don't
see agents anywhere I just see an agent decorator well what's happening under the hood to make this even simpler for you guys is as you're going off and creating these Crews using our new format what's actually happening is under the hood you're actually slowly adding these agents to self. agent so if you added 10 more agents right here they would just keep getting added to self. agents that way you can go ahead and just access your agents right here just like the note says all of these are created by adding the agent decorator very similarly
when it comes to working with task you know you can add all the task you want right here and as you get done adding them they'll be added right here and it is important to note as you go off and are creating your Crews the order is important and this example just with regular crew it goes task one gets done then task two then task three so this would get kicked off first cuz it's at the top of the file then this will get kicked off cuz it's second so ordered is important when defining your
task it is not important when defining your agents okay so now this makes sense how I'm adding in agents into my crew but now what doesn't make sense it's like what the heck is this config file like what like I see it says config where is this coming from great question well inside of our new approach with working with crew AI everything is defined in yaml files so what you can do is you'll now see we have an agents. yo file and a task. yo. file so what's happening in here is and let's look at
our agent first we're going to say all right you're a professional poem writer Your Role is a crei poem writer your job is to write funny light-hearted poems about CI in this sentence count and what's interesting is you'll notice huh sentence count is in curly brackets what's also weird is that this is being a variable and if I remember back to a while ago I saw that variable before it just so happens that this was an input that we were passing into our crew so it's super cool is like this is where the worlds start
merging we are now passing in our sentence count from State into our crews so hopefully this is making sense so sentence count the way we're able to access it is by inside of our yaml files we just use curly brackets to go okay cool this was a variable that I want to pass in so all we have to do is just say name of the variable and put curly bracket around it and once we actually go off and compile it and CI compiles it under the hood it'll go oh you need to do a sentence
count of four you know so that's what's going to happen eventually once all this gets processed and interpolated okay but outside of that just know CI poem writer is going to write a poem similarly this task. EML file is going to do the exact same thing it's going to go okay I'm here to write a poem I want to write about a poem how crew AI is awesome which it is and then what we're going to do is go okay cool once again I want to make sure it has a count of insert variable here
and then I want to make sure my expected output is a beautiful poem with exactly this many sentences and if you wanted to just to like show you guys what is all possible you can go off let me save this if you wanted to you can also pass in more variables here I just want to like show you guys realm of possible I know this is taking a bit longer but we could also say style and then we could say I want this to be in the style of funny but funny but serious and then
okay well cool over here now I could say and in the style of style so hopefully you're kind of seeing like oh that is pretty cool it's pretty straightforward to pass in these inputs into my actual EML files okay so let's clear everything up and go back to our initial State great so this is all looking awesome so hopefully this is making sense we Dove deep into our poem crew we talked about it this all makes sense looks good I understand this is very similar to old style crei the only difference big difference so far
is when we're defining our agents and task we're just going to pull it all from our config file which we defined up here and over here in this config folder okay cool the only other thing that is different is this crew method just know whenever you're defining a crew in this new format it's getting wrapped in a crew base and crew base in order to actually get the crew from this let's see def crew it's not going to show it right now I I was thinking about the wrong part but just just know in order
to actually like get this crew you have to call the crew method so this is just like the structure of a crew this is like the thing of the recipe and to get the actual crew from it you have to call do crew so just something that's new just keep it on your radar as you're going off and just know this is like the instructions and this is okay no this is the real deal this is the blueprint and this is the house cool so once we have the full crew in action now we can
go cool it's time to call kickoff like we've done time and time before and now we can pass in our appropriate variables that we've grabbed from State awesome now once we are done what we can do is go okay cool I would like to go ahead and print that generated poem so you can go okay cool here's the poem I grabbed the result cuz kickoff takes a little bit to run once we're done we're going to get the result and in our case what we're going to do is we just want to grab the raw
string so the raw poem that gets generated reason why it's called raw is just because in crew kickoff land you have the option to generate types I know I'm kind of going the weeds but I just want like there's a lot that has changed so I just want to like throw a lot out there at you please if you have comments hop over into the comments down below ask away or that free school Community I mentioned please ask also crei has some support docs as well so we have a bunch of different resources we just
want to make sure you guys are fully supported as you're going off and building your awesome Crews but just know whenever you get done generating something from a crew you have to be able to access it and the raw value will get the string version of it if you kind of set up your crew to spit back a Json object you could call you know we have a few different options you could call dict to get the Json representation if you did a pedantic object you can call model dump so we just wanted to give
you guys as many different options to get back whatever type of data you requested awesome so now generate poem is done we're going to listen for it and the final thing we're going to do is save our poem to a text file okay so let's go ahead and actually look at this in action and eventually I'm going to cover these cool new method for down here called plot so how the heck do we run it now that we've talked about it together so what we can do hop down here and now that we're in our
demo flow what we can do is called crew Ai and then in our case we'll just call it kickoff sorry crew AI flow kickoff and the best way I like to think about is like crei that's the tool I'm using what am I working with a flow now what the heck would I like to do with it well in my case I'd like to kick it off if you ever forget what are the options that you have feel free to come back over to the p pro.top file and you can see oh I can kick
it off or I can plot it so hopefully that makes sense so create ey flow kickoff now this will go off and do everything that we just talked about it's going to start running the flow it's going to go ahead and start first time takes the longest after that it's super Speedy but what it's going to go off and do go ahead and generate that random number once it generates that random number we're going to go kick off that poem create it then yeah so the crew is running you can see Tada now we are
generating yeah how many sentences do we say yeah so we're going to write write a sentence of four so a poem of four sentences all around pretty cool and then what we can see at the very end is it's done we got the result and we are finally saving it and I can actually access this saved poem so you can see down here I have our poem which looks awesome guys super freaking cool so hopefully you can see that we've now combined the power of flows with Cru so now I want to go ahead and
show you something else that you can do when it comes to running your flows so so far you've seen how we have run crew AI flow kickoff to go ahead and kick off our flows just like this well now it's time for us to dive into the other option which is to start running plot which will go ahead and generate a visualization of your plot so how do we do this well we can use once again our crei CLI to call crew aai flow plot this will go off and generate a new plot so you
can see plotting the flow and then once it's done we'll see a new file down here that will actually be an HTML file that we can go off and look at an interactive version of our flow so we're going to go ahead and do this together super fast let me go ahead and get this open for you guys so that you can see that we have this set up and actually can look at our new flows together so let me go ahead and move this down here so you can guys can see what I'm seeing
so let's make this a little bit bigger what you'll notice is now is inside of our five module five where we working on our demo flow you can see that we have crei flow HTML right here so it's the name of our project so you can see I'm going to go ahead and open this up and you can see it's a really cool looking version of our flow so let's go ahead and make this full screen I'm bring it back over here so you guys can see it interactive and I want to walk through everything
that you're seeing so it all makes sense so what are we looking at well right now you can see that we have multiple different steps going on in our flow so first things first we have up top our generate sentence count and this is is denoted as an orange method because it's represented by start so start is an orange block and that's exactly what we had over here inside our start method was generate sentence count that's exactly what's right here next what you can see is we were calling a crew method so in our generate
poem we were calling a crew so that's why you'll notice this second method is orange and white just like it is down here and then finally we're just going to do a plain old regular code method and that's why this save poem is denoted as just a regular old plain method right here so hopefully this makes sense I think it's pretty cool it's a nice way to visualize all what you're doing and as you go off and potentially add in routers to where you're going down from different branches or you're working with you know certain
other types of codes to where you have like one event triggers off five like all those different types of flows that you could create super easy you can just plot them look at them as an HTML and go ahead and take screenshots whatever you'd like to do with them so all around super exciting new feature so if you don't have any other questions about this what we're going to do next is we're going to start diving in deeper into actually going off and working with and creating our more advanced flow where now that we're going
to put everything that we've just learned together and we're going to start putting it together to go off and write a 20,000 word book together so I'm super excited for you guys to see this in action so let's go ahead and hop over to our final module all right guys welcome to module number six where we're going to have a flow write an entire book for us I'm so excited for you guys to see this example in action because it's going to tie everything that we've been working on so far into one awesome example now
just a quick side note if you do like these type of real world examples with flows if you hop over to the flow documentations over on crei I have an example link down below what you'll notice is there's a bunch of other real world examples that you can use and we have code for each one of these so you can see these in action and go off and use them for inspiration to go on and create your own cool now of that let's hop back to our example of creating a book using flows now what's
super important about this flow is that we're going to be using multiple Crews together to go off and write this book so without diving into the code yet let's look at a high level overview of what's about to happen before we dive into the code so it all makes sense so and just to give you a quick side note I do want to even take a step back further to understand why this is so important especially because we are overcoming some of the the limiting parts of crew AI with flows so let's a quick history
lesson with regular crew it would be very very hard if not impossible to write a very large book you're fighting two major things one is you're fighting context windows so remember a context window right now I think we can Max output 8,000 tokens so that's roughly around 2,000 words when it 2 to 3,000 words whenever you're working with like a chat GPT 40 mini that's as Max you can get so obviously 3000w book is not a you know that's not a book that's more like a short paragraph or you know a short story so that's
issue one context window issue number two when it comes to crei it is pretty much like do task one outline book now once you're done with task one do task two which is in our case write book so outline then write well the issue is is like okay well what I really want to happen though is like I want to outline a book and I want to have chapter chapter chapter chapter and I want to basically go off and generate multiple task I want to Dynamic Al generate task and for each task then I want
to kick off the next crew so right now crew AI does not have that Dynamic task capabilities so we can actually substitute flows in here to go off and do this so let me show you what I mean now what we can do is go okay what I would like to do is have a crew go off and outline a book that's one of the crews that you're about to see in flows this book outline crew will go off and say oh well for this book it looks like you probably need you know let's just
say six different chapters to really write a great book so I will generate a chapter outline and I'll do six of them to write your book for you we might work with a different topic and oh wait we need 10 chapters to go off and generate a book for this topic so we know as you can see we're dynamically generating some number of chapter outlines then for each chapter we then want to have a crew that will write that chapter so the key word here is for each item do something so in our case it's
for each chapter call another crew so that's exactly what's going to happen inside of our code we're going to outline a book it's going to generate six chapter outlines 10 chapter outlines and for each chapter outline we're going to pass it over into a crew and we're going to go off and write that chapter in detail and we're going to get up to you know like we said 4,000 2 to 4,000 words per chapter which is very reasonable for a real world chapter then once we're done we're going to pass everything over into a final
method where we're going to join and save everything together in one actual Complete Book CU a book is nothing more than a bunch of chapters tied together and integrated fashion okay cool hopefully this is making sense so now that we understand everything at a high level let's go ahead and hop back over to the code so we can see these core steps and action so that when you're off on your own you'll be able to replicate exactly what we're doing here all right cool so let's walk through the flow top to bottom to understand what's
going on and then let's dive deep into the cruise where a lot of the Magic's actually happening to start what we're doing is we are creating a new flow book flow and this is going to be a structured State flow reason we know it's structured state is cuz right here I'm saying that hey this flow is going to be using book State our book State you can see right here has some general information about the book we are trying to create such as the title of the book the goal for the book the book outline
topic and additional information we're also storing some placeholders for the actual book that we will be generating throughout this flow okay cool makes sense so far now let's go ahead and actually look diving deeper and you don't need this initial State sorry about that you only need this okay so sorry about that I was old example but this is all you need to get everything working now let's go ahead and start going from top to bottom with each of the different steps in this flow so step one we are all about generating the outline of
the book so what we're going to do is say all right cool I would like to use the outline crew that we'll look at later and I want you to go off and generate me a outline for a book and I want the book to be about this topic and this goal now what's super important is to note that we are going to get back a book outline object from this crew we're going to get a list so for every chapter we're going to get a book outline and or a chapter outline and you might
be wondering what the heck is a chapter outline well we're going to start using some types inside of our flows and Crews which are going to be basically allow us to start being a little bit more intentional with the type of returns that we're going to get back so in our case if we wanted to write a book about AI in 2024 like we do right here just current state of AI in September 2024 well what we might expect is chapter 1 to be the introduction of the entire book so Title One introduction to AI
2024 description a brief overview of what's going on in AI in 2024 chapter 1 could be AI in healthcare chapter 2 could be AI in finance so each one of these will be a its own object with a little bit of provided title of the book and a brief description of what we need to talk about the reason why we're storing all this is because we're just trying to get just enough information in this book outline so later when we go over to write a chapter we go oh you want to do Ai and Healthcare
cool here's the description as well awesome I have everything I need to know to go off and write a chapter now so just hopefully this is setting up some context for what's happening so what we're gonna do this outline crew is going to go off generate an outline of an entire book and it's going to spit back to us a bunch of chapter outlines so it might give us six 10 eight doesn't matter it's just going to give us back a bunch and we are going to store all of those outlines to our state so
self. State book outline we're going to store all of those chapters and you can look what the heck is book outline well because we are being structured we're going to say oh well book outline is nothing more than a list of chapter outlines cool totally makes sense now let's keep chugging along well after generate book outline the next function that's going to get kicked off is going to be the right chapters the reason why is because we're listening for whenever book outline is done we call write chapters now this is where we're going to get
a little bit deeper into actually going off and dynamically handling multiple chapter outlines because remember the core problem is some books might need 10 chapters some might need five either way we just need to handle that use case so how do we do it well we are going to in our right chapters step we're going to say cool what I would like to do is for each chapter outline that we were generated in the previous step I would like to go off and write a single chapter just like this write chapter that's all we're doing
so for each one of these outlines write a chapter now what's the Quirk of this is we're going to say all right I don't want to go off and write chap chter one which might take 2 minutes then write chapter 2 which might take another 2 minutes and repeat and repeat and repeat all in all in this case that could take 12 15 minutes what I would like to happen is to kick all of these off in parallel so that in way instead of it taking 2 plus 2 plus 2 plus 2 it's actually just
all in all it's just 2 minutes beginning to end because these are all running in parallel so that's exactly what we're doing right here we're saying all right I want to kick off a crew I want to call the right single chapter function which which is going to do nothing more than call a crew we'll talk about this more but just high level we're just going to spin off each one of these write a chapter crews in parallel so they're all running in parallel each one's writing their own chapter all right hopefully that makes sense
how do we do that though well we went ahead and created an asynchronous task and this asynchronous task all it's doing is it's going off and writing a single chapter write a chapter what does this function do well it takes in a chapter outline and it goes and passes all of that information into a new crew in this case it's going to be write book chapter crew so this write a book chapter crew is going to take in hey what's the goal topic chapter title everything that we've generated so far we're going to be passing
into here in addition to that when it comes to our book outline all we're trying to do is we also want to pass in all of the other chapters to help um this is just like I found a big Improvement doing this we by passing in all the other chapters it made a very cohesive book so you know it's kind of like if you're writing a book but I told you what chapter 2 was but I didn't tell you what chapter 1 or chapter 3 was you would have no idea of like how to tie
things together so by passing in all the chapters we have a good idea of what's all going to be in the book as a whole so found this really improve the the quality of the book all right so hopefully this is making sense so far you know feel free copy this code have cursor explain it to you chbt explain it to you but just know all we've done is we have gone off for every book outline chapter we are going to go off and turn that outline into an actual chapter with text so hopefully that
makes sense and what we're doing is we're going to go off and create a job and we're going to wait for all of chapters to be generated how do we do that right here we're going to await for all of the chapters to finish up being written and once they're done we're going to have a list of all of the different chapters for the book so what do we do once we have all these chapters well we're going to update our book inside of our state to actually include all of those chapters so if we
come back out to the top the book it's nothing more than a list of chapters ex exactly what you would expect it is important to note with the way that this function works is that it'll make sure that chapter one stays chapter one and chapter 2 stays chapter 2 so you'll you'll see that here in just a second but I know sometimes with asynchronous functions it's like oh I was going off and turning outline number three into a chapter and it finished first oh that should be the first item in the list nope that's not
how it works whatever task was created first will stay first in the array so it'll make sure our chapters in our book Stay 1 2 3 4 5 6 all right hopefully that makes sense sorry for the tangent if you weren't sure what I was talking about Okay cool so after we have written our entire book what we're going to hop into next is going off and joining and saving all of our chapters together how do we do this well we're just going to iterate through each one of the chapters in our state and we're
going to say cool all I want want to do is just put together the we're using markdown I just want to put the book content we're basically having one long string which will represent the entire book and we're just iterating through each chapter first things first we add the chapter title second thing we go off and add the content so title content next thing we do title content and we just keep repeating title content title content until we've written the entire book so once it's finally done then what we'll do is we'll title it or
wrap it all together and we'll say cool my book title I'm just going to grab it from State then I'm going to go off and now that I've joined the entire book together I'm going to go ahead and make sure I get the title of the book I want to get it ready and properly formatted so that I can go off and save this book locally to my computer so I can go off and read this markdown version of the book cool so I know that was a lot hopefully this is making sense so far
now what I want to do because we've gone very overview high level of how everything ties together I want to actually dive into the crews so that you can see exactly how we're going to go off and say you know book outline crew here's how you go off and create a book outline and here's how you go ahead and start using some of those types that we talked about earlier such as chapter and chapter outline so let's clear out all this and go ahead and hop over to our cruise so the first thing we're going
to do is hop back over to our file explorer and we're going to go ahead and first look at our outline crew I'm going to walk you through this and then we're going to dive over to our right book chapter crew crew all right so first things first what are we doing well inside of our outline crew we really have two agents two agents and two task what are we trying to do well if we're going to go off and write a book we need to go off and do some research so we're going to
have a research agent who's going to go off and do all the exploring for us in our case it's going to look up the AI Trends then we're going to have an outliner who's going to take all that researcher research information and go off and go okay cool now that I know what's happening in the world I now know I need to go off and write eight book outline chapters so that's exactly what's going on inside of this crew now how do we Define what the actual logic is for and instructions for those agents and
task well over here in our yaml file once again our agents file we're just going to say hey we're going to go off and you need to research this topic we're using variables and I want you to go up and look at this goal you can see exactly how we're passing in our variables awesome now when it comes to our task same thing I want you to be a little bit more specific once again go off and look at this topic and look at this goal and here's my expected outcome so all around go research
the internet once you're done I want you to go off and write a nice book where each chapter is in sequential order you know shoot for around this many words you know llms aren't the best with numbers but hey it's still a pretty good guiding principle all right now what do we want to do next well I want to use CBT 40 when it comes to these agents just because I want to write a really high quality book and I found 40 is a little bit more intelligent than 40 mini which is the default awesome
now what else do I want to do inside of this crew well if you remember what we want to do is we don't just want our crew to go off and just hey here's the outline here's a bunch of uh here's just a long output string output of here's the outline we want our crew to return a structured response how the heck do we do this glad you asked that question in our final task in our crew we're saying Hey I want you when you're generating an outline I want to make sure your output of
this crew is in the form of a book outline so what the heck is a book outline glad you asked again we can go ahead and click and look at this type and you can see oh a book outline is nothing more than an object where we have some chapters and each chapter is nothing more than just a list of chapter outlines and then each chapter outline has a title and description just like we talked about before and this totally makes sense cuz we're generating an outline so all we want to do is just say
hey man this book is going to have 10 chapters and here's the outline for each chapter totally makes sense when it comes to working with this crew okay now how the heck did we actually access that book outline and everything well if you remember back over in our crew what we were doing towards the end is once we generated our all of our chapters you'll notice that we got an output and we said we went ahead and grabbed oh sorry I hopped to the wrong wrong section you can see we went ahead and generated that
outline and then I called output I I grabbed output where how did I know to do this well it's because in the return type of this final crew this task which is the final task in This Crew it's going to return this book outline and this book outline is nothing more than an object with one property in it chapters and this is because I set it up this way feel free to set up your Crews and types however you like differently but I just said hey I want my book outline to have a single property
which is Chapters and I want this chapters to just be a list of all the chapter outlines so now whenever I get my output I can just go ahead and grab the property on there that I want to in my case chapters and this chapters is going to return all of the different chapter outlines in my book hopefully this makes sense I know we're kind of like a lot of dots are happening because this is new a lot of the features are new but uh I wanted to go ahead and like explain how everything connects
when you're off on your own building your own Cruise you'll have a good understanding like okay cool Brandon talked about in order to get Types on my outputs of a crew I need to make sure that my final task in a crew has some sort of output type so in my case a book outline and in order to Define that well I need to go off and make probably the easiest ways to make a type file where I Define all my types that way it's so easy to go ahead and grab those same types that
are shared between my Crews and my my flows okay cool this is all making sense now that we've like done a high level on the generate an outline crew hope it's making sense passing in topics generating a ton of chapter outlines we're now going to hop down into the next crew which is WR a book chapter crew so let's go ahead and look at this super fast let's open up our WR book chapter crew and let's first look at what's going on here at a high level so once again we are going to go off
and we're going to have m multiple agents and task inside of our crew in this one it pretty much going to follow the same structure as the last one first things first we're going to go off and make sure that our agents have or sorry our crew has a researcher and a writer reason why is because if you remember what we're doing is turning a outline into a chapter so maybe let's say our description was 200 words of like hey right now in AI Healthcare this device is coming out this device is coming out and
these new models make you know cancer detection super super easy okay cool those are five bullet points of things I need to go off and turn into a full-on chapter so I'm just expanding upon those identified items in the outline so I need to go off and research more about those topics so that I can convert those topics into a full-on chapter so that's why we have a researcher and a writer exact same as we did in the other model all right cool so hopefully that's making sense and I'll show you the config in just
a second but you can see there's a ton of similarities because say not only were our agents going to go ahead and have we're going to only have two agents but our task are going to follow the same structure to where task one research test two is right but not only do we just want to write we want to write and return a specific output in this case we're writing a chapter that's going to go into our book so what is the chapter half it has nothing more than title and content all right cool hopefully
this makes sense so let's dive in super fast into our config file so you can see exactly the instructions we're giving for our agents and our task so when it comes to our agents what we're doing is we're going to be passing in a lot more variables this time we're going to say all right researcher here's what I'm trying to write about here's the specific chapter title I'm trying to write about here's the overarching goal of the book and here's a specific outline for this chapter that you need to go off and write about all
of these are variables that we are passing in when you look right here goal topic chapter all of these are the exact same so hopefully this is making sense then when it comes to the actual task for our crew you can see we're just being very specific about what we want to do hey when you're researching go off and make sure you include and look up all this information and here's some general background information as well then when you're going off and writing this book or writing this chapter for this book once again here's exactly
what you need to know about and more importantly here's the outline for the entire book just so you have some general context of what we're doing in the grand scheme of things awesome hopefully this makes sense definitely feel free to steal these types of instructions as you go off and create your own Crews all right now that we're finally done talking about this crew I think we're getting pretty close to the point to where we can go off and run it and see in action so let's go ahead and clear this all out let's go
ahead and open up our terminal so that we can start running this crew and before we do that real quick I do just want to mention in order to run this crew successfully you need to update your environment variables for this project so you can see over here in our right a crew you can see down here we have our environment variables you need to pass in an open AI key and a serper key serper is how we're going to go off and search the internet and open AI is how is we're going to turn
all of our Research into an actual book okay hopefully this makes sense now what do we need to do next two things we need to First install everything to run our crew so we can do crew aai install this will go off and install all of our dependencies create our virtual environment it'll do everything that we need to sick now what we can do next is go ahead and kick off this crew in our case flow so crew AI flow kickoff this will go ahead and run it now I'm going to just quickly talk about
what's happening as it's going on it does take a minute to write this whole book so obviously pause and come back but just know the first kickoff always takes the longest because we're organizing all your packages getting you set up for success in order to go off and run your flow the second time you run any flow it's a thousand times faster all right cool so what you can see is it's going off right now and searching exactly what we told it to what's happening in AI in 2024 so it's going to go off and
right now we are in the outline of book section so yeah book outlining agent so we're going off and we're researching everything that's happening in AI in September 2024 and it's going to go off and ultimately create that once again recap it's going to go off and find oh cool now that I've researched the internet it seems like there's like eight chapters that we could really cover in this book and I'm going to return a list of book outline chapters so it's going to go off and say hey chapter 1 is going to be called
chapter 1 intro to AI 2024 and we're briefly going to talk about the 10 topics throughout the rest of the book then from there it's going to go off and generate chapter 2 3 4 5 6 so forth and so forth in the outline then once it's done we're going to hop over to the next stage which is going to be go off and not only are we going to start turning these outlines into actual chapters is pretty much what we're going to do and once we're finally done we're going to merge everything together so
I know this is mouthful there's a lot going on thank you guys so much for sticking with it so far but I'm going to go ahead and pause because it's going to take usually about I've noticed about like 4 minutes to write a whole book which is honestly wild in the grand scheme of things but I will go ahead and resume the second this flow is done running all right guys so literally 20 seconds after I paused it went ahead and finished writing the whole book so I just want to show you how cool this
is so if you open up your file explorer once it's done running you'll notice that you now have a new file so in our case it is the current state of AI in September 2024 so if I open this up it is a full-blown book about everything that is happening as of September 2024 so all this data is scraped from the internet and put in a well formatted book just like this and if you have some specific tools so I have like a markdown preview I can actually see a nice representation of this book so
you can see like hey here's the introduction of like I provided no additional input this is just the AI doing its thing which is so cool but you can see like hey here's what's going on as of September 24 you know here's some of the key areas that it is talking about and then what it's going to do is just start diving through some of the major trends that are happening inside of basically AI so all around this is like yeah so you can kind of see like all right here's what's happening in AI investment
and Regulatory keep scrolling down you can kind of see like all right here's what's happening with machine learning keep scrolling and scrolling and scrolling and we're going to keep talking about more in more different things that are going on inside of inside of the world of AI as of September 202 for so this is a fullon book this is so freaking cool guys so I do want to show I went ahead and threw this over into a word counter and this book happened to be around 13,000 Words which is way above any context window that
you could get if you were just to say you know normal crew ai go WR a book normally you're C at like two 3,000 mags so we went ahead and combined all these together to make a 13,000 word book which is wild and we could do much more depending on the topic that you get that's a little bit more not as new where there's you know more information you could get 20 30 like I've seen way more than that so all around this is awesome so I just want to show you guys this is cool
now before we go I do just want to mention a cool few World applications of this shout out to Richard in my AI developer accelerator Community few things that you could do with this is actually for example Richard is to generate custom AI lead magnets for your viewers so for example if a viewer was to fill out some a form for wanting to work with one of your services or whatsoever you could actually kick off a flow like this to write them a short book so if you were in the let's say Fitness space it
could be a workout if you were in the coding space you could write at a custom coding plan for how they could go from their current state to a full SE developer like you could basically use this exact flow to go off and provide a more detailed case study example guide for your users and a and instantly this took under 2 minutes to generate so imagine getting a custom 12K 20K you know lead magnet to your viewers that is specific to where they're at in their journey and where they want to go and you can
just generate it on the fly with flows just like this so all around this is such a cool tool guys I hope you guys are stoked to use it because I've been blown away with seeing Flows In Action and this is just scratching the surface I cannot express how many more use cases there are with flows and I have a bunch more coming for you guys with more examples real world examples as time goes on so thank you guys and that's a wrap for this video guys I can't can't wait to see what you guys
go off and create now that you're experts at building flows and just as a few quick reminders all the source code in this video is completely for free just click that link down description below and if you'd like some additional support and to meet fellow AI developers I have launched that school Community with over 3,000 different AI developers of all skill levels we'd love to have you a part of the group and then finally I did just launch a full stack AI marketing course so if you're interested in taking what you've learned today and actually
going off and building real world AI applications you're definitely going to love love checking out that course we have over 200 people taking it already and they're absolutely loving it I have a link down description below as well and there's some free courses in there inside as well so you can get a taste of the course before you go off and fully try it out so let me know if you have any questions and I cannot wait to see you guys in the next video see you