I Built an AI Trading Bot with Llama 2!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello world it's siraj and I built an AI investment advisor that generates advice for me that I can then use to build training strategies with on my favorite trading platform composer and in this video I want to show you exactly how it works let's start off with the demo the first thing we're going to do is ask it to just predict the price of Tesla stock next month so I'll ask it to predict the price of Tesla stock next month and then it's going to build a model in Python to make that prediction it's building a time series model to make that prediction now that's very simple that's not all we want to do we also want to generate a thesis based on that so I'll say based on that and the electric vehicle Market in general generate me an investment thesis and after I give it that prompt it's going to generate me an investment thesis now we can use this thesis to generate a trading strategy by going to to composer.trade and then typing in the create with AI feature that exact output and it says in this industry the electrical vehicle industry it's going to grow in the coming years there's going to be some risks on risks based on government subsidies there's going to be some future developments based on regulations and adoption and decreasing cost of production and what composer will do is it's going to generate a trading strategy based on that thesis now you can see that trading strategy right here that it has already generated for us and it's saying when the 50-day moving average price of a company stock is higher than its 200-day moving average the strategy invests in that company otherwise it stays in cash and it does this for Tesla for General Motors and nio so it's Diversified uh across different electrical vehicle makers and when we back test this we can see that this strategy has outperformed the Spy by a lot by around 700 percent and that's just back testing but let's see how it works in a month from now but you know how did I build this and why did I build this why didn't I just use Chad GPT or Bard or Claude or any of the other AIS well first of all I tried to but they're all paid and they're all restricted in some way so if we go to you know chat GPT or finchat this was a nice one or Bard and we asked it to make investment advice or predictions it's going to either say I'm sorry I can't because of red teaming AKA safety training or it's just not going to have that capability because it doesn't have what's called a code interpreter and what a code interpreter is is it's the ability for an AI to not just write code but then execute that code and that's what we want to do now what base model do we want to use for this we want to use an open base model so we can have this code interpreter run ourselves and the most popular one right now is the Llama 2 which was released by Facebook last month and what we can do is we can take the base model of llama2 fine-tuned on code data it's called codelama and then turn that into a code interpreter and run that on our own server or locally on our own machine and I want to show you exactly how to do that and using that code interpreter that's going to be our investment advisor we'll take the outputs of that and make trading strategies on composer with that all right so let's get to the first step here the first step is for us to run this code and I've put this all into a single Google collab notebook for you that you can go from zero to running this in a web interface in five commands that's it it that's all it takes so the first command is to clone this llama2 code interpreter repository then we CD into that repository we install all the requirements and we set demo.launch share equals true so we could see this web interface in the web browser and then we just run it that's it and we need to be connected to a GPU because lama2 is quite a big model it's a 7 billion parameter model to be running on a CPU we want to run it on a GPU and I've got an a100 instance right here on Google collab and it's running right here so when it's running we could start you know running inference asking it different questions seeing it how it performs and then you know copy and paste whatever advice it is into composer to generate a symphony for us and this symphony is going to have a whole range of different um you know variables that it can look at conditional statements you know the moving average the max drawdown the cumulative return and we're going to let composer decide all of those conditionals for us and the waiting Matrix of all these different stocks we just want our AI to generate an investment thesis based on the real-time web and that's why a code interpreter is so great because python code can use apis and apis pull data from the web and in a virtual machine that executes an interpreter that executes python code can actually use real-time data from the web whether it's the Google News API or Twitter or what have you to then create an analysis and an investment thesis off of so that's the idea behind the code interpreter and if we look at the code and we run it we can see that in under chatbot.pi it's very simple code all it's saying is hey this is a llama 2 completion model we get that but what we want you to do is to only output code as a response that's all we're asking it to do so that's going to be a first step just to Output only code and then when we go into the Llama codeinterpreter dot Pi file we'll see that it's going to Output code for us and once it outputs code we're going to pre-process that code so we remove all the text we make sure there's only code there we make sure that code is properly formatted and when it's properly formatted then and only then do we execute that code using this jupyter notebook execute function so it's that easy we can just generate a code block execute it take the output of that code block and if there are any errors keep recursively running that you know installing any dependencies what have you and it's essentially it's our own little data scientist step runs locally on our machine it's that simple and it's running radio which is an interface to run machine learning models now we don't have to use this 7 billion parameter model created by Sun Yoon I mean it's a great model it's using llama2 that was fine-tuned on code and it's you know fine-tuned on instruction data so it's better at questions and answers from humans but it's not the best model I mean there's plenty of other models we could be using right I mean replit has a great three billion parameter model that we could be using called replica code V1 3B and this was trained on 20 different languages and it's a lot smaller so it's going to be cheaper for us to run inference and there's so many options for us to run inference you know collab I think is the cheapest option if we want to do it at scale I think it's like 50 a month but there's also paper space there's you know hugging space itself there's a lot of different GPU providers out there and you know you can also have your own GPU but inference you know it's not cheap it does take money to run off of gpus but it's much cheaper than paying open AI that's for sure and uh you know it's also capable of doing that because openai has been red team that's been safety trained not to give investment advice so it's really powerful to take any kind of uh code based model off of hugging face and then in a python class file saying take this code instruct model take the output of that model and then remove all the text and then execute the code of that model only respond as code so it's almost like we prepend the user's prompt with the statement only output code as a response and then execute that code as an output and then on whatever virtual machine we're on whether it's Google's you know Google Cloud whether it's you know paper space or replica whatever have you and then that is our investment advisor it's kind of simple actually it's it's kind of beautiful this idea of open source llms for running our own unrestricted investment advice and people have extended this to the idea of Agents not only do they make advice but then they actually go out and make investments now I don't like doing that in particular I like having composer do that for me because I get this really pretty dashboard I get to see a community of different Symphonies that other people have created I can back test the possibilities are pretty endless so if you're interested go check out composer today there's going to be a link in the video description and happy investing foreign
Info
Channel: Siraj Raval
Views: 21,865
Rating: undefined out of 5
Keywords:
Id: 6FQz7MDTogs
Channel Id: undefined
Length: 9min 2sec (542 seconds)
Published: Sat Sep 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.