The RIGHT WAY To Build AI Agents with CrewAI (BONUS: 100% Local)
The RIGHT WAY To Build AI Agents with CrewAI (BONUS: 100% Local)
The RIGHT Way To Build AI Agents with CrewAI: Harnessing the Power of Lightning AI (100% Local)
In today’s rapidly evolving AI landscape, the power to efficiently create and manage AI agents defines the cutting edge of technology utilization in various sectors. This article dives into the most effective strategies for building AI agents using CrewAI, augmented by the capabilities of the cloud-based code editor and environment, Lightning AI. This combo not only optimizes collaboration but also significantly enhances the implementation and functionality of open-source AI models.
Introduction to CrewAI and Lightning AI
CrewAI represents a revolutionary approach in defining and managing AI agents tailored to specific tasks. It leverages modular structures allowing for flexibility and scalability in AI development. On the other hand, Lightning AI provides a robust platform that simplifies coding by offering a cloud-based environment similar to VS Code, which eliminates common setup obstacles like Python environment management.
The integration of these technologies facilitates a more structured, streamlined process in AI agent development. The flexibility of Lightning AI’s cloud-based tools, combined with the structure provided by CrewAI, paves the way for building sophisticated AI systems efficiently.
Setting Up Your AI Development Studio on Lightning AI
First and foremost, setting up your codebase environment is crucial. With Lightning AI, you start by creating a new studio which is straightforward—just sign up, receive some free credits, and click ‘New Studio’. This studio serves as your primary workspace where you can collaborate, manage, and deploy your AI agents.
Every action in the studio setup is guided by prompts and does not require detailed manual environment configurations, which helps in avoiding the "dependency hell" commonly associated with Python projects.
Structuring Your CrewAI Framework: Modular and Manageable
The essence of a CrewAI framework lies in its modular architecture. Begin by creating a ‘source’ folder to establish the structure, then further segment it by specific functions like a ‘financial analyst crew’. Within these designated folders, configurations and definitions are stored in YAML files, streamlining the management and scalability of multiple agents and tasks.
This method not only organizes tasks and agents efficiently but is also foreseen to support automatic API generation for managing these crews in future updates, straight from the CrewAI’s planned features.
Defining Agents and Tasks: Clarity and Precision
The configuration files are where you define the detailed roles and expected outputs of each AI agent. For instance, creating agents with specific functions like ‘company researcher’ or ‘financial analyst’ involves detailing their tasks down to expected outcomes and processes involved. This clarity helps in setting precise expectations and functionality from each AI agent developed under Lightning AI’s robust infrastructure.
Implementing Agents and Tasks: The Operational Backbone
Once your agents and tasks are defined, integrate them through a main Python management file. This integration involves importing necessary libraries and linking tasks with specific agents. A typical setup might include triggers like researching a specific company which then passes information to a financial analysis agent to compute and deliver detailed financial reports.
Powering Your AI with Open-Source Models and Lightning AI’s GPUs
A pivotal aspect of modern AI agent development is the integration of powerful AI models. Switching between models like GPT-4 or Cloud-3 becomes seamless with Lightning AI’s environment, thereby enhancing the AI’s analytical capabilities. The environment also allows extending these capabilities by connecting to open-source models, significantly reducing operational costs and increasing the accessibility of high-level AI tools.
Deploying and Scaling with Lightning AI
Finally, deploying your AI agents is facilitated by the pre-configured environment of Lightning AI, which supports various machine learning models and tools to suit different tasks. The scalability is ensured by easy management tools and the flexibility to upgrade or change computational resources as per task demands.
Conclusion: The Future of AI Development
By utilizing the structured approach provided by CrewAI and the robust, flexible environment of Lightning AI, developers can build efficient, scalable, and highly functional AI agents. This methodology not only streamlines the development process but also opens up new avenues for innovation and application in AI across industries.
BONUS: Keeping It 100% Local
In an age where data privacy and local operation are paramount, Lightning AI ensures that all developments can be conducted within a controlled, secure cloud environment, emphasizing the commitment to privacy and data security.
Developing AI with CrewAI and Lightning AI places powerful tools in the hands of developers, empowering them to create advanced AI solutions that are ready to meet the challenges of both today and tomorrow. Try cloning the provided environment as described to experiment and expand on what’s possible in your AI ventures.
[h3]Watch this video for the full details:[/h3]
This is the “golden path” for building AI Agents using CrewAI. If you follow this template, you’ll be set up to use all upcoming features with CrewAI, such as an automatically created API for your agents.
Enjoy 🙂
CrewAI Lightning Studio: https://lightning.ai/mberman84/studios/sour-sapphire-39pg~01hnzq5jpxqckky30rmwwz0r3q/
Cloud IDE, Fine-tuning, Inference, Data Management and more!
Try Lightning AI Studio FREE Today: https://bit.ly/3uBiWO9
Join My Newsletter for Regular AI Updates ????????
https://www.matthewberman.com/
Need AI Consulting? ✅
https://forwardfuture.ai/
My Links ????
???????? Subscribe: https://www.youtube.com/@matthew_berman
???????? Twitter: https://twitter.com/matthewberman
???????? Discord: https://discord.gg/xxysSXBxFW
???????? Patreon: https://patreon.com/MatthewBerman
Media/Sponsorship Inquiries ????
https://bit.ly/44TC45V
Disclosures
I’m an investor in CrewAI
[h3]Transcript[/h3]
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. I’ll call it grock llm just to save some time but we’ll call it olama mixol just like that and it should all work now I believe there might be one more thing we have to do but let’s give it a try and so poetry run financial analyst all right and there it goes so it’s probably going to be pretty slow because it is a very big model Mixr and we’re not using a super beefy GPU but I just want to show you getting it working and if we switch back to the oama studio we can actually see the GPU going bur right there so you can see it’s it’s working right now and there it goes look at that all right so if I stop it has a bunch of good information right there go back look at the GPU and now it goes down to zero again so we’ve confirmed it actually works so now you know how to structure a crew and do so in the most modern way you know how to spin up an open- Source model with lightning Ai and power it with their gpus and you know how to expose the endpoint and plug it into your crew if you liked this video please consider giving a like And subscribe and I’ll see you in the next one