in this video I'm going to show you the optimal way to set up your crew AI team this comes straight from the crew AI founder and he has shown me the future of what a crew AI codebase looks like and we're going to be building all of this using lightning AI which is not only a cloud-based code editor which allows you to collaborate with anybody on your code in the cloud but you can also power your open- Source models with it which I'll also be showing you how to do so we're going to build a Crea I team and then we're going to swap out gp4 and we're going to power it using mixol or mistol we'll see and I want to thank lightning. AI for sponsoring this video I'm super excited to build this team on lightning and I'm going to publish this lightning studio in the description below so you can get access to it you can clone it and you can play around with it yourself the first thing we're going to be doing is building our crew AI code framework and so the first thing we need to do is create a new studio so if you don't already have a lightning account go ahead head and sign up they'll give you some free credits to get started click new studio right there and we're going to create a code Studio then we click Start and this entire environment is in the cloud so it really saves you a lot of headaches with python environment management because it's essentially a fresh environment every single time and if you've been watching my videos you know how frustrating python environment management is all right so the high level of how we're going to structure our crew is now very modular we're going to have a separate area area for tools we're going to be using yaml to Define our agents and tasks and everything is going to pipe into a very short main. py file so I just spun up a new lightning studio and that's really all we have to do we can already get started so the first thing we're going to do is create a source folder so go ahead rightclick click new folder and then type source and everything should feel very familiar and lightning because it is essentially vs code in the class and the cool thing is you don't actually need to set up your environment you don't need to struggle with your python environments with dealing with all of these dependencies it just works so we created our source folder and then within the source folder we're going to name our new crew so we're going to actually create another folder within that and I want to make a financial analyst crew and so just like that I name it financial analyst crew hit enter then that creates another folder all right then within that folder we're going to create a another folder config and all of this is just to structure what we need to put everything together so from here we're going to write click we're going to create a new folder we're going to call it config and this is where we're going to put our definitions of our tasks and our agents and as I mentioned they're both going to be Emo files so let's do that now right click we're going to say new file and we're going to call it agents.
for the first one and then we're going to right click new file and we're going to call it task. for the second one and now this is where we're going to Define our agents and tasks and the nice thing about doing it this way so if you start getting into the habit of structuring your cruise just like this I believe pretty soon crew AI is going to give you the ability to actually expose an endpoint to control your cruise based on this structure so it'll automatically be able to create you an API so that's why it's good to follow the golden path here so let's start writing our tasks the first task is going to be researching a specific company that I want analyze so I'm going to call it research company task and then we put a colon after it and then we need two parameters within that description and expected output So within the description this is where we're going to describe the task at hand so what we're going to say is use a Search tool and we'll come back to this we'll actually build the Search tool to look up this Company's stock information and then I put company name and the fact that I have curly braces around the company name now makes it a variable which we're going to pass in later and this makes it so that we can basically pass in any company name the goal is to prepare enough information to make an informed analysis of the company's stock performance the next thing we need is the expected output and for the expected output I say all of the relevant Financial wh I spelled Financial wrong financial information about the company's stock performance okay so that's the first task it's as simple as that now I'm going to make a second task so I'm just going to copy that first task paste it again and the second task is going to be analyze company task so I'll delete the descriptions from before and I'll delete the expected output from before as well and we'll put new ones in place so I switched over to Claude 3 which by the way is my new go-to model I no longer prefer GPT 4 Cloud 3 has just been so good and I asked what metrics should someone include in a financial analysis and it gave me a bunch of different metrics and so I'm simply going to copy paste these into the description of the task I want so I say take company names and again I pass in the variable financial information analyze it and provide a financial analysis including profitability ratio liquidity ratio solvency ratios and so on and for the expected output I am going to say a nicely formatted analysis including all of the financial metrics necessary for a thorough financial analysis of a company and so let's stop there obviously I can get much more sophisticated than this but that's not the point I just want to get something up and working and then I want to show you powering it using an open- Source model and powering that by lightning AI gpus okay so we have two tasks so research and then analyze and then we need agents now so let's click over to the agents. yl file so here is the structure of an agent definition and the name is going to be company researcher the role I'm going to name it Financial researcher the goal very similar to the task now one quick thing that I just added is I explicitly said using search tools I don't know if I actually have to do that but I like explicitly saying it CU it doesn't hurt and it makes me feel like okay it's actually going to know to use those tools okay backstory an expert Financial researcher who spends all day and night thinking about financial performance of different companies now we're going to set allow delegation to false because we don't want this agent delegating this task to anybody else and we're going to set verose to true because I want to actually see everything that this agent is thinking so I'm going to copy that and I'm going to paste it down here to create our second agent now typically what I do is I match every agent to an individual task so if there are two tasks there'll be two agents so let's start changing this now so instead of company researcher we're going to call this the company analyst and instead of financial researcher we'll call it financial analyst and then we're going to replace the goal so for the goal I'm going to say take provided company financial information and create a thorough financial report about a given company the backstory an expert financial analyst who prides themselves on creating clear and easily readable Financial reports of different companies again allow delegation false there's no need for that and verbose true so now we are done with agents now uh one quick thing I want to mention is you don't actually have to save any of the files while you're working with lightning AI it just saves it automatically as you're going which is really nice I've obviously gotten into the habit of hitting command save all the time so it doesn't hurt but you don't really have to do that and you can shut this off you can shut down the environment and come back to it later and it's all just going to be there working ready to go just like you left it okay next we're going to create our main file that puts all the agents in task together so rightclick on financial analyst crew and we're going to create a new file and let's call that crew.
Pi so the first thing we need to do is import all the relevant libraries so we're going to say from crew AI import agent crew process and task and those are the four main pillars of a crew AI project then we're going to import crew base agent crew and task from the crew ai. project and also because grock is awesome we're going to be using grock to power all of this and we could do so through Lang chain so make sure you have all of these installed and we can pip install them if we need to later but right now we just need Lang chain grock import check Rock now let's actually create the crew base so enter enter and we'll say at crew base and then we're going to create a class financial analyst crew open PRS close pars colon we're going to describe what it is and then we're going to load up the agents and tasks that we just created then we need to set the grock information so we're going to do def init self none and then self. grock llm and this is where we pull in the grock chat information we're going to say temperature zero model name and we're going to be using mixol and we'll need to set the token but we'll do that in a minute now let's pull our agents and our task into this file so at agent okay so I wrote out the first agent definition so we have at agent def company researcher self and then we point it at agent and we're going to be returning a new agent object config and then we pull in the config from the agents.
yl file and we pass in the grock L M as it's llm and remember as I've said a lot of times you can actually pass in a different model from different sources into each individual agent so if you want to use gp4 for one clad for another Mixel for another and then an open source model powered by lightning as another you could totally do that next let's copy this and we're going to create our second agent and this second agent is company analyst okay so it's going to do the same thing we're going to pass it as an agent return the agent op object and instead of company researcher we're going to call it company analyst and that's it th those are our definitions for the two agents now we need to create definitions for our tasks so just like agent we say at task at the top we'll say def company research task same as before just like agents except instead of agent we say task here new line then we'll return a task object with definitions config is equal to self. task config and then we'll pull in the relevant task and rather than passing in an llm which wouldn't make sense for a task we're going to pass in our agent that we want to do this task so we say self. compan researcher and that should be good now let's copy it and create the second task down here here we'll say def analyze company task we'll create the task return the task but we'll say analyze company task here and we actually want the company analyst as the agent and I think we're done I think this looks good ah no we need one more thing so we actually have to Define our crew which puts it all together all the agents and all the tasks so we say at crew def crew and we pass it itself make a new crew object so here we're going to return a crew and now we need to pass it all the information to actually create that crew so we'll do agents is equal to self.
agents and the self. Agents is automatically created by these decorators right here just a heads up on that so that's all we got to do then for task we do the same thing self. task then we need to set a process so we're going to do process.
sequential because we just want it happening one after another and then verbose is equal to two because we want it to Output everything now I think we're done all right now we need to actually create our main. py so we already have one right here so let's go ahead and move that into the financial analyst crew okay main. py and all it does is print hello lightning world so so we're going to import OS and we're going to import the rest of what we need to basically use a m file so Fromm import load.
MV load. M then we're going to import our financial analyst crew so it's as simple as from financialist analy financialanalyst crew. crew import and then import the financial analyst crew so now we have everything we need all right and this is the entire main file so we're going to Define run we're going to pass in the inputs and the only input we have is company name Tesla and the nice thing is crew automatically interpolates all of that for us then we're going to say financial analyst crew.
crew kickoff we'll pass in the inputs and then we put this kind of main portion down at the bottom now let's create our n file and we're almost done so on the very outside of this entire folder we're going to rightclick new file and we're going to call ITV and we're going to pass a grock API key right there so I grabbed a new grock API key and yes I am going to revoke this key before publishing the video so that's it grock API key and then pass it in just like that okay so I am going to create some tools now and we're not actually going to create them from scratch we're just going to use tools that are currently in the crew aai examples Library already for us so we have an SEC tools which is kind of exactly what we need so let's go ahead and we're just going to copy the whole file we'll switch back and then in the financial analyst crew we're going to right click and we're going to create a new folder and we're going to call it tools then we're going to create a new file in there and we're going to call it SEC tools. py and we're going to paste in all that code so all of this hopefully will just work out of the box all right now we're going to use poetry to put it all together and to actually run the project so to do that we're going to first do pip install poetry then we're going to create a new file and we're going to call it Pi project. TL and you don't really have to know how much of this works and to be honest I don't really know how much of this works uh I'm just not super familiar with poetry but this is the way crew is going to be done going forward so we're going to paste in all this stuff and it basically just sets up poetry for us and the only thing that we're going to need to customize is the financial analyst crew name right there next we're going to lock the dependencies so poetry lock all right should be done and there it is the poetry.
loock file perfect then we're going to do poetry install Okay so this is installing all the dependencies now all right there it's done now we should be able to run it from here so poetry run financial analyst crew hit enter uh I missed one thing so I forgot to actually rename it to what we need so financial analyst crew right there now that it's there we should be able to run it properly so I'm going to do poetry install one more time just in case all right now we got no warning so now it should work poetry run financial analyst crew all right so I got another module not found issu so let's do pip install linkchain grock and I'm not sure why we have to do do that again but let's try it hopefully this works okay so let's run it again all right it looks like we got it working wonderful wow look how lightning fast that was okay so let's see what it did so here we go entering the chain Final Answer Tesla's ticker symbol current stock price market cap getting all this great information about Tesla and recent news as well then it passes it on to the financial analyst and it's doing a bunch of different calculations okay all perfect and then we have the ratios and the different metrics that we asked for and then the final output perfect look at this beautiful so it all worked and it was lightning fast so now we have our crew set up but it's using Gro which is fantastic but we want to try running it with an open- Source model powered by lightning AI so you can plug in any model that you want so leave this tab open all right then we're going to go to the studio templates page within lightning. so it's lightning. a/ Studios and this basically comes with a bunch of preconfigured Studios that are ready to go and they have a bunch of them a bunch of really cool ones from running olama fine-tuning you can build diffusion models they just have a bunch of them all right and it already has this run mistal mixture of experts and all we need to do is Click open template right there so we select select what team space then click open and this takes a little while to load up because it's doing a lot in the background but it sets up everything for us and then I'm going to show you how to then expose the API from oama and then we're going to take that API endpoint and then power our model with it all right so it's done now if we look up in the top right and we hover over this button we can actually see that we're using a GPU an a1g and if we click on it then click into the gpus we can see we have a bunch available all the way through A1 100s h100s and yeah they do cost a lot of money to run but you don't need these beefy gpus for all the models but we will likely need something very powerful for mixol and now they have L4 support so you can just get another beastly GPU running your models but I'll just show you that we get it working I'm not actually going to let it do all the inference because that's not the point let's continue so once we have that we're going to come down here and we're going to click this plus button for the plugins we're going to go down to serving and then we're going to do API Builder and click install once that's done we exit out of here on the right side we now have this tab API Builder go ahead and click that now this is one that's just by default and you can actually just go ahead and right click on it and delete this like that and we're going to do a new API okay so we'll keep the name the same and for the port it's 11434 and that is just o llama's Port we don't need any authentication although if we're going to make this into a production environment yes you definitely want authentication then let's enable it then we're going to click on the name and we're going to grab this base URL right there so now this is the endpoint that is exposed with our open AI compatible API so now I switch back to our code so now to do it I'll leave the old code in there but what we need to do is right here from Lang chain.
llms we're going to import oama and then we're going to on a new line create a new llm olama mixol then we're going to call olama we're going to pass in the model mixol and then for this base URL we're going to enter the URL that we copied earlier so just like that so then we'll copy this we're going to comment out this line and we'll do self.