CrewAI Agents for Stock Analysis (Works with Local Ollama LLMs!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to another video and this one I want to look at a very exciting agent framework called crew Ai and in my opinion this one's been a little bit more straightforward and easier to use than some of the other ones and actually more effective because of that it's just simpler and uh you can often accomplish what you're trying to a lot quicker now that's not to say that everything always goes perfect this is still under very active development but it's a very cool framework in my opinion so there's three Core Concepts there's the agents the tasks and the tools and basically you have the agents powered by llms that will use a tool you create or a lot of already pre-built tools as we'll see in a little to accomplish tasks that you give it now without going into too much detail let's go and uh take a look at some of the examples so they've got a very sort of simple example here called stock analysis now the stock analysis example and I'm not sure if you guys have run it basically allows you to create a couple agents that will go and search financial data about a company they'll look up reports they'll look at their SEC filings sort of compile all that and spit it back to you if we look here um we can see that agents are being instantiated and don't worry we're going to go over a little project that I did to take this sort of to a different step that would make it more useful for me but um they've created agents here let me zoom in a little a financial analyst a uh research analyst and an investment adviser so basically the point of this crew is is to generate some sort of analysis on a company right now here we can see that tasks are being created so there's a research a financial analysis look up the filings and make a recommendation now this is all very cool you can uh go ahead and check this out on GitHub there's all these tools that are created there's for example this browse tools which is very useful to go and uh get stuff from a website look up stuff on the internet um a calculator to do basic computation and um for example this one is a Search tool that looks up company filings on the Edgar website for the SEC right now I'm not going to go over all of this because there's quite a few things but I just want to look at a very simple example that I did to maybe make this a little more useful to me if I were to uh invest in a company right I really don't want an llm to make any sort of final decisions at least not yet because a lot of hallucinations can occur I really want to get hard data now it can be argued that this is maybe an overkill for an agent right you can just make some API calls and get hard data but it's fun and eventually you can combine the two all of this stuff um to get all the stuff from the API from the SEC excuse me um search internet for Relevant articles to find the sentiment of the company things of that sort however what I want to do and what I've done in this project is um like I said something that is could be very simply done by calling just an API and getting the data but eventually I think this could turn into something that gets that and creates a whole report so what I've done in this one is I've created a crew that gets financial data from an API and creates charts and basically adds them to a report and by charts I mean sort of visualizations of important Financial metrics so where do we get started here let's go into the main file because this is basically where everything starts up and just um so everybody's on the same page if you decide to run this I'll put it on GitHub but I am running uh this release 143 of crew and um when you install that you know make sure your Lang chain is compatible etc etc okay so we're in our main.py file which is basically the entry point to our program and you can see we're importing some things creating a financial crew name that whatever you want obviously depending on your use case the llm will take in those tokens so make it relevant to what you're doing then there's a run function some things are getting instantiated some tasks agents but I think the best way to go over this is just look down here and see uh where it all starts off so there is an input that's going to be required by the end user and in our case in a CLI this could be in a guy or a UI or whatever but we're asking the user to enter a company symbol and the metrics that they want to create charts for that'll be added to the report we instantiate the crew U we pass in whatever inputs the user gives and then we do crew. run right so let's go back up here and we've got our agents you can see here I'm bringing in financial research agents let's open up the agents file in this file you can see there's quite a few Imports there's some from crew AI then a lot from Lang chain Lang Chain's an amazing library and they've made it really easy to use other models with crew so run an AMA you can use Gemini and a bunch from hugging face as well I of course had the most success with GPT 4 cuz it's just a very powerful model but um you can also use open Hermes I had some success with that now I'm going to get to this uh in more in depth in a second but I just want to bring attention to right here there's an argument when you're creating an agent that lets you specify which llm you want to use you don't have to use the same llm for every agent right so you can see here I've got a couple agents I've got a markdown report creat I got a chart Creator and a markdown writer which basically is going to create the report at the end so these could very easily have different agents so I could take uh AMA and put that here instead of gp4 now gp4 is great so we're going to use that it does add up so if you want to save money and you've got a powerful computer use a local model or use 3.5 let's now go into actually what some of the attributes of the agent are so let's look at our markdown report Creator and this is kind of like the main agent in my program at least which is going to use a couple different tools and uh pass the outputs to subsequent uh tasks so the agent has a role and here you basically want to tell the computer the LM what its title is I guess you could call it and then the goal is basically you give it sort of a concrete thing it wants to achieve and you can take a look at this in the examples there's some starter templates here where you basically uh you know Define them you can find all of these templates in here and the last thing is the backstory which is basically a resume of that agent what it has done in the past what it's capable of doing and of course these are language models and there's a lot of sort of messing around with what you say to them in order to get it to work and because we want sort of hard data and not any things being imagined I did have to mess around with it a bit I will go back to my point that if you get enough like small and really cheap agents you can have them just do small little things and that may potentially be more accurate crew AI is in active development so there's often sort of little issues for now that maybe make that not as realistic but eventually I can see armies of Agents completing tasks with less powerful models so let's keep moving so I've got the markdown report Creator and it retrieves accurate data of metrics requested for a particular symbol it is an expert in creating markdown reports and it's the best at using tools to gather data from an API so you kind of got to you know massage and complement your agent a little bit just so that it's um happy to do the job if you will and be nice to it because when they take over you don't want to be uh you don't want to be in their crosshairs so we can see there's another argument here called tools and we're going to look at them in a second but basically I'll go over this one real quick par string a user is going to write a sentence or something like I want the free cash flow of a company and they'll put the company symbol right they're not inputting Apple free cash flow so I created a tool that's basically going to extract the important data from that string the metric and the symbol and uh basically pass that down the line right so this agent can use these two tools the par string to get the things required out of the input and another tool that's going to get data from an external API I'm going to look at that in a second let's look at another agent here I've got the chart Creator which creates charts for the data provided and it uses the tool to do that it's very good at creating charts Etc so here you can see the tools I've got the create chart tool and the third agent is the markdown writer which at the end takes the images of the charts that are created and adds them to a markdown file right in markdown format so you can basically have this report at the end and if we go back to the example of stock analysis you can imagine that I know you guys maybe not have used this one or have not seen it but this gets a whole bunch of data from the SEC from the internet and creates these like reports it tells you the top stories and you can basically add on to this by adding data you may want in my case it's chart so you create this huge Report with things that may or may not be hallucinated depending on how well it does in general GPT 4 should do pretty well especially if you are doing stuff like this where you're just getting hard data from an API so you can combine all this and create something pretty powerful I've seen it working for a lot of research stuff a lot of sort of even like YouTubers are probably using it to gather the most recent news reports and create some sort of script for a video etc etc so I think most importantly here let's go look at the tools now because those are pretty important and we're then going to take a look at the the tasks that do things by using the agents and the tools together so this is a big file just because I wanted to keep it in one file but I have seen examples where they separate all the tools into a folder here it is and they have a file for each folder however I didn't do that just to keep it simple although this may be a bit clunky now but um import a couple things now one thing I do want to mention is I'm using quick FS which is a great service it's um somewhere in here quick f.net and they have an API where you can retrieve financial data and I found it to be really great because they also give you computed data you know maybe it's like Revenue growth percentage or stuff like that that you may have to calculate you can get it right from them and it's pretty straightforward so some of the tools okay the extraction tools remember a lot of this stuff you name whatever you want it matters I gave it names that may or not may not be great but they seem to work for my use case so I'm going to look at the doc and see what it has to say about tools real quick so we're going to go to Core Concepts tools what's a tool a tool this is pretty small I'll just read a tool in crew AI is a skill something agents can use to perform tasks so I want to show you all this because there's a lot of already existing tools that uh you don't you don't need to create your own so Lang chain has so many Alpha Vantage which has a lot of financial data is one you can use I saw one for polygon which is very cool you can use it to get the news and uh quotes So as you can see you can build quite an extensive research report just with examples that are already out there I would like to take all of that together with something I've done today because it's more in line with how I would do research but let's go back to what we're doing here so we're I have a parse string function it's a tool that basically as I mentioned before it gets a string of text give me the revenue of Tesla and it'll get revenue and Tesla and create this basically dictionary that we'll then use to look stuff up the next tool is to get the data from Quick FS which is our API you basically say what this function is useful for maybe specify the symbols the more accurate you can be the better just because that'll help the language model figure it out and gp4 is very intelligent intelligent but capable let's say so you don't need to be as detailed but it does add up so if you're using 3 five you want to save some money maybe be more descriptive um explain what you're trying to do and crew AI has certain things that make inputs and outputs more clear or at least definable so that your agents know what to use and this is another trick you can use pantic to basically create schemas or definitions of what you want pieces of data to look like so the next tool is a charting tool that's basically going to create the chart it's going to use map plot lib here you can see I've defined some examples um what sort of inputs it takes etc etc and what it returns right so we're going to let's just say net debt we're going to get net debt for a certain company retrieve that from the API and create a chart for it okay so that's what this does and you can Define what the outputs and what all the structure looks like we're just creating simple bar charts and finally I've got a tool that writes text to markdown now there's better ways to do this I would say you don't need to use an llm to do really any of these things right this is really just fun for me right now so that's why I'm doing it but you can simply create functions that um that will be used uh via a callback and we'll look at that in cre so let's go look here crew um tasks let's see call back right here so we haven't looked at tasks yet but you can see after a a task is completed you can basically run things right whatever and it could be just a function it doesn't have to be some sort of special language model thing so this tab excuse me this tool will write the text to the markdown and you can see it's a pretty straightforward thing creates a report. markdown file and it writes all the correct syntax now the main thing is the tasks I think right and it's probably the the thing that really sort of encompasses what you're trying to accomplish right so tasks are basically things uh goals that need to be accomplished right by your agent so the first one is to get the inputs and extract those uh the metric and the symbol pretty straightforward and here's one tip that I got from the docs the crew a do is if you give it a tip section do your best work in return exactly what I ask I'll give you a commission gives it a little bit of an incentive right I'm not a re an AI researcher I'm not really sure what the return on something like that is or how effective it is but it does work a little bit I don't know to what degree but it does work so a couple of things here you give it a description an agent and the expected output now this is a new thing I believe that I saw in their docs and it's very powerful because you can basically Define what you want this task to output and that's useful because down the line another task will use that as input and here you can see I've got the context argument so get data from API takes a context which is the output from the previous one I believe and we'll use that again I've got the tip section I pass in the context here and expected output from here is what this will spit out for the next task to use finally create the charts again I take a context return a task create Graphics of the data representing the financial metrics of the company okay great and you give it the expected output again I want an a list of all the images that have been created that'll then be added to a markdown file and finally I've got the right markdown uh task that's going to add those images really it's a special markdown syntax to the file so that then you can look at it and that's pretty much it I know I went over it pretty quickly but I don't want to sort of harp all over this too much because there's a a lot of examples and tutorials of what the structure of the tasks agents and tools need to be to make this work like you want it to really there's a lot of room for you to customize here though I will say so the first thing I'll do before we run this is just go and make sure that our main do PI is all good we've got our markdown report Creator which is one of our agents let's put that here this is sort of the main one I guess you could call them okay so here are the tools the chart Creator uses the create chart obviously the markdown report Creator uses a couple different tools so I really only got two agents and one of them can use these tools and the other can create the charts I'm sorry I got three agents and the third one is the markdown writer right you could probably condense this to be less than three but whatever it's cool let's close this again I create the agents markdown report Creator chart Creator and markdown writer like I said this one and this one could maybe be the same one if you told this guy to use more than two tools and then we create the task so we want to parse the inputs extract the symbol in the metrics we want to get the data from an API and we want to then create the chart create the markdown file now here you'll notice this argument here is in excuse me brackets and this is basically that context AR argument so if we'll go to tasks here you can see there's a context and we'll um pass it in here right so we're passing in the parse inputs as the context to this the that to this and then the retrieves to the third one right so it just goes down the line and does it sequentially now I didn't mention this before but if you go back to crew there's a sort of a another component besides the tasks tools and agents called process processes which orchestrate how stuff is executed by the agent by the agents and I did a little bit with a hierarchical sort of method and I'm not sure I really got that down or I'm really confident enough in agent sort of autonomy to really let that ride at least for this use case for writing stuff it might be more useful but we are just going to use the sequential where one task executes after the other in linear and orderly progression okay so we create all these tasks then we just sort of instantiate our crew add the agents add the tasks some stuff for output that we can see you know we can see more output and then we do crew. kickoff okay so before we run this what's going to happen we're going to say I want cogs cost of goods sold for Tesla okay and the thing is going to figure out I want cogs and Tesla from the sentence I write get that from an API get those metrics return sort of an array of data points and then create the chart write it to to a file okay so let's go I'm going to bring in the terminal here and clear this cuz I had gone over my limits we are going to run Python main.py and I'm going to get the uh free cash flows for Tesla so let's make sure we got GPT 4 just one more time check the agents GPT 4 good okay I'm here so I want I want free cash flow for symbol Tesla okay so we wrote a little sentence there there and the agent is going to extract FCF and Tesla and we can see we've got this little array here it did that the parse inputs task worked out pretty good here it's having some issues with uh doing this right off the bat it looks like it's trying a couple times using tool retriev metric data from Quick FS there's the data okay we'll go check if this is accurate in a second so for the last 10 years Tesla's had some negative free cash flows and then positive good here we can see some of the output from the expected output that we had defined and it gets passed in as to the next thing which is the sort of create the chart which expects a metric name and the data cool awesome and it created this chart we can see it over here on the on the left excuse me and it also created the report file now let's go ahead and see okay Moment of Truth here this could all go wrong but I it looks like it worked we're going to check the report. MD file and see if a chart was created and written as markdown syntax to that file okay so here's the chart where's the report um okay that looks good let's open it up okay good so those are Tesla free cash flows okay is that accurate let's go ahead and check so we'll head on over to Quick FS which is also the API we use they've got a website we'll go to Tesla look up Tesla here probably going to be in uh how about key ratios free cash flow cool okay free cash flow here we go starts at negative 1027 ends at 4357 there's one that's higher so the last year is lower let's look at our chart we'll look at the report here make that markdown viewable okay there it is so negative great and then seven yep and four so this looks right okay looks like it worked out now this is a markdown file you could add pretty much anything in here right so um you can have your agents do all this you can create agents for this the ones that we saw in the example crew aai I'm not sure if this actually writes to a report I think it just outputs a bunch of stuff but you can output anything you want 2A ready to go markdown file or PDF whatever so if I do something like hello I believe this should work see there it is so you create basically this large report of all sort of analyses of a company right I'm going to um delete this why don't we try another one so something you want to see is that a company is using the money that they have wisely right so there's different names for that return on Capital employed quick FS has an acronym return on invested Capital so I'm going to do that so what are they doing with the money that they come in how are they using it is it generating new money right are they making good use of the funds that they have so I want uh return invested Capital symbol let's not look at Tesla let's look at what's a really great company I'm trying to think you know there's a lot of really Chipotle is a great company you know and not particularly fine of Chipotle because a lot of the things that they do but uh that's for another day so let's look up how Chipotle does with the return on invested Capital we can see here symbol CMG metric return on invested Capital it's going to go do all the same things I may have to pause it here because this may take a second but it's basically looking it up I think I'll do a third example after this where I'll look up like two metrics but I'm going to stop it because I don't want to keep paying for gp4 so I'll stop it Midway but I want to see if it's able to parse out two symbols okay here we go got the got it pretty good so you can see these are expressed as a percentage so they're returning pretty well I mean 20% on in on the capital that they use right which is a pretty good return and it seems to be pretty consistent and the team this year wasn't that great here but overall pretty good let's see what the chart looks like it should look pretty good above 20% right so here's the chart should generate the report file there it is okay it created that cool we're going to open the markdown file view it as a markdown okay good now it looks like this doesn't look great but return invested capital of 20 I think this is pretty good right you generally you know maybe you want to see it more straight like that but that's really good I think another one we can do is this will be the last one let's try a couple things so let's do Chipotle they're when you know let's get their revenue how much revenue they're making and I also want to know if they're doing share by back so I want let's make it really explicit I want metrics revenue and shares basic let's see how many shares outstanding from symbol CMG okay this should create a couple charts I'm going to stop it before it gets there but I want to see if it's able to parse out the two and create the dictionary that where we then need to look it up so we've got CMG Revenue okay it's talking to itself it's saying I need to do a good job okay here it is okay I'm going to stop this right here CMG revenue and shares basic okay this is good this means that it was able to get the things that I wanted and extract them out right here and then can go and create charts for these things okay so the goal of this was to basically just have uh some agents go out and get metrics for financial data right this was definitely a contrived example I'd say because it's something you could just do with a python script you don't need a language model or any AI for that matter to do this you could just have some python or any programming language that goes out and gets some data and creates charts saves them to a file right I think it'd really shine though when you take something like these examples on the crew GitHub that gets stuff from the SEC website they get a whole bunch of text Data they compile it and they create and you can create this report now I want to be careful with that because it Hall it could hallucinate numbers that may lead you astray so you got to be careful with that really get your tasks down right to make sure it doesn't do that change the temperature on the llms ETC if you combine this sort of report Creation with what I just did I think it could be a pretty useful tool you can imagine something like like you know if you go here to the markdown file if it was something like you know you have the llm go and figure out what it says in their in a company's 10 Q or 10K in their financial reports it could create sections like risks and disclosures and summarize that for you and add it to the report and then add all the charts Etc it could be pretty cool you know it'd look like this it just look like markdown you basically have this really nice report that you can read and figure out if you want to invest in this company again really just sort of a toy at this point and there's many times I will say that the thing sort of just runs away with itself if you're not using gp4 especially and doesn't end up completing the tasks so breaking down the tasks I'm not an expert in really how to do this I'm going to look more into it but I think if you break down the tasks into more concrete goals more simple things it can do it a lot better and if you can get it to work with sort of Cheaper agents or cheaper llms that's even better right so I think I'll leave it there I will um keep messing with this let me know what you guys are doing with it of I think that this is really great for research right or when you're writing as well doing sort of creative things where you need ideas where the where the language model can sort of take a bunch of information or gather a whole bunch of stuff and create bullet points things of that nature but I'd also like to see it work for I'd think it'd be really cool if agents could could have a whole crew of Agents sort of doing research for you and whatever the topic may be and you know generating visuals etc etc so just messing around here I thought it was really fun let me know what you think if you guys are using crew and um I guess if you'd like to see more crew stuff so we'll see you guys in the next one thanks for watching uh take care [Music] bye my kitty here so we can fig we can see it figured out share Zoop Nvidia come here kitty here's my feline agent her favorite tasks are I don't know what do you like to do scratch scratch people and big poops in the litter box okay okay sorry
Info
Channel: tylerwhatsgood
Views: 10,527
Rating: undefined out of 5
Keywords: crewai, ollama, openai
Id: U_Sg3Odf1vk
Channel Id: undefined
Length: 28min 16sec (1696 seconds)
Published: Mon Feb 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.