OpenAI API Explained: Learn GPT, Codex, Fine-Tuning & Zero-Shot Classification Using Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this open AI tutorial you're going to learn how to use Python with the open AI API to do some really cool stuff here we're going to start at a high level and learn how to do text generation text classification we'll learn how to do language translation maybe translate English to French you're also going to create images and we're going to learn how to create code and once you have the high level stuff down which we'll cover and you know no worries everything I'm going to walk you through everything we're actually going to do a mini project we're going to take some Twitter tweets is that the best way to say it at tweets and actually fine tune a model to make it better at classifying tweet sentiment so maybe you could use that in a trading algorithm who knows but regardless the sky's the limit with this stuff maybe I probably shouldn't say this but I'm actually having this technology read my email and respond to some of them okay and the quite frankly responses are good and it's a limited set of emails that I'm sending to just promotional stuff but anyways the point is the sky's the limit with this stuff so if you don't know who I am my name is Leo Smiggle I'm an algorithmic Trader that likes to use AI to empower my training capabilities and I like to code in general so without me rambling on why don't we go ahead and get started let's go to openai.com click API and then here you'll want to click login and then sign in with whatever authentication provider you want either with Google or Microsoft I'll use my personal Google account and now go to the right hand side click view API Keys create a new secret key and here it should start with SK Dash you want to take this copy and paste it and now that you have an API key we can start making API calls now that we have our open AI API key we're going to want to save it to a file I'm going to use a DOT EMV file and we're also going to want to install this software into a virtual environment so let's do that now open AI API key equals and there's my API key I'm going to write this to a file period EnV which we just you know conventionally call it a DOT EMV file I'm going to save this I'm going to open up another tab so you can see exactly what I did right now we have a directory with just a DOT EMB file in it and again I can print this out and you see the open AI key value pair and now what we want to do now that we have our API key saved is to create that virtual environment let's say type Python 3 module v e n v the EnV we're just using the virtual environment module to create a virtual environment called venv now this will take one second here I'll list this out if you're on Windows or you're running into any issues check the description below low because it will be potentially different for your system and I'll cover those in the documentation so now that we've installed the virtual environment software we can activate this virtual environment by typing Source venv bin activate and you'll see this venv up here here now what we need to do is install the software that we want I'm going to say pip install and open Ai and we're also going to want to install the dot EMV software I believe that's python.env so that way it'll load the dot EMB file for US automatically now that we have our virtual environment loaded and our API key ready to go open up your favorite editor in my case I'm going to use jupyter notebook not because it's my favorite but because I'm going to save this entire tutorial in a jupyter notebook that you're going to be able to download off of GitHub if you want to follow along now authenticating with openai is very easy all we need to do is pass the a API key into open AI after reading it from the system environment so let's go ahead and do that now we will import OS because we're going to use that to read our system environment and our environment variables will import open AI because we're going to play with all of the cool models that they give us and from dot EMV import load.emv this will just help us load the environment variables into our environment and if it works it's true so now our API key is in the system environment now we'll grab that using OS so API key equals OS get EnV open AI API key and then we'll pass this API key into open AI so open open AI it's like a tongue twister API key equals API key and then I hit enter we don't know if that works yet so let's go ahead and ask open AI we'll make a request to the open AI API to see what type of models that we're going to be able to play with today so we'll say models equals open AI model list and then I'm going to print the models and now we have this open AI object and we can see here's a Babbage model there's an ADA model there's a DaVinci model and some other models too so that did indeed work we did authenticate now there's one issue here is that you know whenever you do make a request to open AI it passes us back this you know model open AI model object now what we can do is we can convert this to a python dictionary list or a pandas data frame so let's install pandas at the moment so pip install pandas I'm using a bang and then to pass this command to my terminal if you wanted to if you're just running on your terminal you just type pip install pandas you might already have it installed but now that that's installed I can use pandas to make this easier to see so I'll import pandas as PD and then data equals PD data frame models which we just uh you know got from that request and I'll just save data here and then data dot head and then this should give us a easier to see list of all of the models so now that we're able to unders make a request to open Ai and see the various models let's talk about the open AI model types openai classifies their models into three distinct families the first is the gpt3 or generative pre-train Transformer 3 models this is essentially what chat GPT is based on and you know set the world Ablaze with all the cool stuff it can do even though GPD technology has been around for a while there's the Codex family which is great for code specific tasks such as you know give me a program that does X and then finally there's content filtering that you know essentially allows you to understand the you know whatever it's saying and whether it is malicious or not even though open AI at this point is actually recommending to use the content moderation endpoint primarily where folk going to be focused on the TPT and codex models now with the Codex uh the the GPT models there's four of them and listed in descending order of both pricing capabilities there's DaVinci so if you can Da Vinci is what Chachi PT is based on it's a special realized fine-tuned version of that and it's pretty much excellent at least you know relatively speaking in a year we'll probably say oh these are terrible but it's excellent for doing you know pretty much anything with natural language below that is the Curie model that is pretty good too but at a cheaper price point then you have Babbage and then Ada finally and then we'll use Ada in our experiments because well it's the cheapest for fine tuning and then with the Codex model there's two there's Da Vinci again the most capable and then there is the Cushman which is uh you know less capable but faster and cheaper so that brings begs the question what model to do you choose well here's an easy way to find out let's head back to the open AI website we'll go to openai.com front slash API we'll click login and then once we're logged in we should see a few Links at the top we're going to want to click on playground this playground lets you essentially test all the models out to see whether or not they work for you so we could you know select a model here we'll we'll try out Ada I'll say give me an engaging way to ask a YouTube audience that is currently watching an amazing open AI API Python tutorial to like And subscribe let's see what it says now that took she's open AI to create basic Abel's panel yeah not so hot so we can say okay well this model might not work out so well we'll use DaVinci because you know we know that works for pretty much anything if you found this open AI API Python tutorial helpful please be sure to hit the like and the Subscribe button to stay up to date on all of our amazing tutorials I agree with this 100 but you see the point here right you don't want to start programming using uh this Ada model to find out where intrinsically it just doesn't work very well for what you need so that's the best thing to do is before you start coding come to the playground play with what you're trying to figure out whether it's sentiment code whatever and then start on coding we'll start by performing some basic NLP or natural language processing tasks such as inserting and generating text now all of the stuff that we're going to cover is actually very easy one of the more difficult things is actually determining what you really want the AI model to do that is you know prompt design what we asked the model to do do is the prompt and there's so many so much depth to prompt design there's actually entire YouTube channels dedicated to various prompts right and if you're you know bend down this rabbit hole and you start really digging in you'll see that there's so many cool things that you can do with these models but you know I don't want to digress too much my point is whenever you're creating these prompts take a step back and think about exactly what you want remember you're not having a conversation with a friend you are trying to tell or use natural language to tell a computer model hey hey take this turn it into code and run it for me right so think about it that way you have to be very precise and very clear with what you're telling these models in order to get the results that you desire we'll start with text generation and completing some text as you'll notice it is so easy to do almost embarrassingly so which is amazing because these models are so powerful and accessible to almost anyone so we'll say text equals open AI dot completion dot create and then we'll pass it the model model equals text DaVinci zero zero three the prompt will say rants is famous or it's we'll say Max tokens and tokens are essentially the currency for these models one token is four characters I believe or roughly 0.75 of a word or 75 of a word so you can think of a 750 word article being about two cents I believe that's roughly speaking about correct so then temperature is essentially creativity we won't make it creative at all and let's see what you know this da Vinci model gives us France is renowned for its rich food and varied Cuisine which is and it cut off because we only had so many tokens and if we wanted to we could actually get some some more responses for this right so we'll say four uh you know whatever in range we're just going to throw away the one put this right here the underscore is conventional for whenever you're throwing something away and not using it and then text equals open AI completion dot create and then we'll just take the same exact stuff that we had up here although I will increase the model limit this time so we can get better responses we'll just say you know 100 and then we'll say print make it a little easier to see text choices right and you can see here we have this choices right here which I'm accessing that I'm going to access the first one and then I'm going to access text so choices zero first element text okay so choices zero text and then I will print uh let's do this print and then we should see a list of choices and responses so we'll see if that's does indeed occur okay France is famous for his rich and buried Cuisine which is renowned for its fresh ingredients classic techniques and flavorful combinations so you can see popular dishes yeah so it does vary these right so that's interesting that you can have these you know five responses if you don't want to do it this way which I don't suggest you do you can go back up here copy all of this I'm going to create a new row here let's see here this this this this this and we can just go comma n equals five make it interesting and I'm actually going to make this smaller so it's a little easier to see and then we can print this and now you'll be able to see you have a bunch of various uh outputs right so you don't actually have to Loop through you can just give it you know n times so that's pretty easy to do right but what about text generation and sentiment analysis well I'm glad you asked as I said earlier the API is super easy to use and in fact we do both of these things with the completion create method so let's give it a shot give me a funny tagline for a YouTube channel that discusses grading and programming I actually have two channels once dedicated trading and this one's dedicated to programming although with you know algorithmic trading I'll just do n equals main program trading and not boring yeah that's funny um okay because well some of this stuff to many can be boring but anyways you get the point uh we can also do the same thing for sentiment analysis right so whether it's a tweet or classifying uh and if an email is Spam or not so is the following email ham or Spam ham as a good email spam is a bad email you have won the billion dollar Lottery all you need to do is click the following link and it should give us spam this is most likely spam that's right now let's try it with a different model 801 which is the cheapest least capable model the following email spam so it actually classified that let's see if we can trick it is the okay what is the sentiment of the following tweet I'm really happy that the movie ended early I didn't enjoy it maybe that'll work see I'm trying to throw in happy there uh let's see so the positive or negative the sentiment of the following tweet is in unit okay clearly not what we wanted didn't see see if this will give us negative all right so you get the point there right so again that's why I said you know use the playground to determine whether or not the model really even makes sense uh you know I might not use Ada unless you know I was really concerned about the cost and probably fine tuning which we'll do a little bit later so now that we know how to do all of the you know text generation you know creating inserting and determining the sentiment let's talk about editing text now I do a lot of reading and maybe it's just me or I'm weird actually I know I'm weird but hey we're all unique in our own way right when things are in passive voice it bothers me okay I I guess it just stuck with me you know my English class so let let me copy some text and in passive voice a car was bought by John a car was hit by a truck the website was developed by ABC and what we'll do this time is we'll take this text and we'll ask the model in this case we'll just do DaVinci edit001 to edit the text into active voice so response equals open AI this time we're not going to use create or completion right we're going to use edit and then create so all of that you know we're using the create method but we're using you know the edit class okay so then we'll say model equals this is a little bit different text DaVinci edit zero one input equals input text instruction will equal convert the following well convert the sentences into active voice and temperature equals zero and now what we need to do is we'll just Loop through this to make it easy to read we'll say four choice and response voices print Choice text and when I run this it should convert each one of these into active voice right so John bought the car A truck hit a car ABC developed a website right you see who's performing the action now so that's simple and easy to do and you can do this for pretty much any text it's pretty pretty amazing and what you can also do is you can actually say give me the prior sentence or the old sentence plus the new sentence you can say you know convert this to easier to read use it in a different voice if you want to be salesy or you know whatever you could use a voice of garyvee but you can do all sorts of things for editing and modifying text it's pretty pretty amazing but now that we've sort of covered everything from a natural language perspective again super easy to use you're either going to use the you know completion module or the edit module and then the create methods now let's talk about using codex models to do some code completion code completion is essentially the same as natural language completion the only difference is that we're going to use one of those specialized codex models if you recall there were two of them there's a DaVinci model and the Cushman The Da Vinci is more capable so let's give the DaVinci a spin so what we'll do is we'll ask the model right a pro a python function that accepts the first name second name and birth date as a parameter as parameter values well you know what I'll leave the typos in there and Returns the full name and the number of days from the birthday till today and as I mentioned before it's essentially the same as the completion uh you know completion create only this time we need to change this to completion and the model is not text it's actually code all the codecs start with code DaVinci zero zero two it's not input text because that's for edit it is prompt e is from text the instruction is the prompt so we don't need that temperature is here we don't need to get creative and Max tokens equals 2048 and then we will run this and see what it does and sometimes this can take a minute and let's see what it gives us we've got import date time we have the full name it Returns the full name this function turns the full name and the number of days so full name is first name plus space second name birth date uh today days return full name days so it looks like it does indeed work so that's exactly what we wanted but what happens if we are stuck on a programming problem where we actually want to you know complete or insert code right so let's give this a shot so what happens if we do this and I'm going to let you try to guess at the response here so prompt text this just tells it we're going to use python 3. and then response equals open AI completion create this is we've been typing that a few times right that's basically all you need to do and know the models to use model equals code we'll use DaVinci again DaVinci dot win C 002 prompt equals prompt text remind me next time to just copy and paste again it's easier and Max tokens equal 2048 and suffix equals return X by times x so what do you think this is going to print out right I'm going to let you I'm going to pause here and let you think about that for a second it could actually be very very short we'll see we'll see what it does okay here I go of course there's an error here prompt text equals yeah it is very short so what what it did is Def square and then we return x times x so it understood that this was you know the square function and new to put def Square ahead of that now we can actually use you know the same pattern as before how we went from creating text to editing text so let's edit some code here so what we'll do is we'll type in prompt text equals def process X and then return x times x in fact let's see I'm going to just make it not as nice and see if it figures that out response equals open Ai and again this time we're using that edit module again for uh you know to edit stuff as opposed to create model equals code Vinci edit zero zero one notice the model change input instead of prompt prompt text maybe I should say input text for the jupyter notebook scroll down a little bit uh temperature and instruction equals modify this function to return the cube of the input parameter and we'll go ahead for choice in responses response choices print Choice text and let's see what it gives us here okay def process so it does know how to modify this from Return x times x and it actually did adjust the X to returning the cube so obviously these are pretty basic examples but you get the point you use the Codex model uh you know to whenever you're working with code and you can either use the completion or the edit classes or models whatever you want to call modules whatever you want to call them to edit that code that's essentially what you know GitHub copilot is doing so now that you understand how to edit and create both natural language and code using the open AI API let's see what we can do with some image processing and creation using the dial e models image creation is just as easy as natural language or code creation we're going to follow the same process first we're going to learn how to create images and then we're going to learn how to edit them now if you're interested dolly is just a portmanteau or a combination of salvage or Dolly and the Wall-E Disney film or character I'm not sure which one maybe both I haven't seen it but all we do is we give natural language to open Ai and it returns an image of what we want so let's give it some prompt text of a dog standing on the beach and we'll follow the same process so we take the response and this time it's open AI image not completion or edit create and we don't actually have to pass it a model we just have to pass it a prompt and the size so prompt equals prompt text and we'll say 2 and size and I should mention at the time of this video creation there's three sizes there's 256 by 256 512 by 5 12 and 10 24 by 1024 we'll do 512 by 512. and then for each image we'll print out the URL because the image won't actually just show up on your computer it'll be stored as a URL on Microsoft azure for image in response data print image URL and let's see what we get here I'm going to open these up and hopefully we get a dog standing at a beach let's see perfect so there's a dog looking at the beach looking like he might jump in or lost his favorite ball and then here's another one playing in the sand so that's that's pretty impressive that they just generate these images I mean if you look really closely you can kind of tell these are AI generated images but it's only a matter of time before reality and uh you know these generated images become almost indistinguishable indistinguishable all right so now that we know how to create images let's learn how to edit them image editing is pretty much as easy as image creation the one thing that we do need to do though is pass two minute images one is the original image and one is the mask you can see here I have a dog on a beach and then here we have a mask which is pretty rudimentary I'm sure you can do better if you have more artistic skills and access to canva or photoshop or whatever but the point Remains the Same we just need to pass it original image and a mask of where we want to adjust our image so let's go ahead and create that edited image we'll say response equals open AI image create edit and then we pass it both the original image first and then the math so image equals open the file is located in home Leo Smiggle downloads dog dog.png for me and then we want to read binary because this is a binary file yours might be different your path rather and then the mask for me is dog Dash mask The Prompt will be okay let's see prompt equals a cat standing on a beach so we'll change this from a cat to a or from a dog to a cat we'll make n equals two and size equals five twelve by five twelve so same as the original image and for image in response data print image URL and then it'll give us some URLs on Azure where we can see if this did indeed change this original image from a dog to a cat let's open these up one two and see what we've got there is a cat standing on the same Beach as you can see and here's another cat yeah clearly AI generated but again as this technology matures I think it'll be indistinguishable from reality but maybe what you want is not to edit an existing image with a mask maybe you just want variations of an existing image and that's very easy to do with open ai2 this will be the last thing we do before our mini project and all we need to do is the following so open AI image create variation say image equals open I could spell pass it the original dog image home Leo Smiggle downloads dog.png and we wanted to read the binary we'll say n equals two we'll get two empties and size equals 512 keep it the same then for each of these images we'll print it out so for image and response data print image URL and this should give us two URLs on Azure with the dog on a beach with different poses one two so here we see the dog on the beach with different poses that's the cat so here's that that dog that was modified and you know it looks pretty similar it looks like the same dog although yeah that's not you might if someone looking at this like Ah that's clearly AI generator but I digress so now that you know how to create and edit images and create variations of those images create and edit code and insert code and also do the same thing with natural language you have a full understanding of all of the basic you know open AI API endpoints obviously there's whisper and other things which we're going to cover in different videos but for right now you have a you know complete understanding of all the basics now it's time to learn how to fine tune an existing model and get it better at understanding tweet sentiment so let's go ahead and embark on this fun little mini project before we dig into this mini project where we're going to fine-tune an existing Ada model using tweet sentiment data from kaggle let's take a moment and think about why in the world we want to do this anyways well these gpt3 models are trained on massive amounts of data and this you know this data affects the model weights if you're curious on how this works look up neural networks and bat propagation we're not going to discuss this here but for you know for our intents and purposes you can think of these things as a big number with lots of different weights for various inputs and essentially that's not helpful but the point is what we can do is we can take specialized data for our use case feed it to these already existing models we don't have to create a new model from scratch we can take all of the work done and just you know tweak our stuff for our specific use case and to improve our model and there's really two way two reasons you want to do this one the existing model is in performing the way that you want it to or two you might even be able to use a lower cost model such as Ada to be able to you know analyze the sentiment at a much lower cost so first we're going to get the data from kaggle and then we're going to use the open aicli to format the data appropriately then we're going to fine tune and let's dig into the code we'll first want to go to kaggle.com kagle.com so we can download the Twitter US Airline sentiment so we can fine tune our Ada model if you're not familiar with kaggle kaggle host data science competitions and you know the community rallies around these competitions so you can see some of the latest and greatest techniques and data science if you're interested in data science kaggle is a great website to check out here is a link I will leave this in the description below to the data I've already downloaded I'm going to zoom out a little bit and the first thing you want to do anytime you have a new data set obviously is to fully understand it and explore it so let's import pandas we'll read the CSV file once we have an understanding of the file we'll format it in a way that openai expects so that way we can pass it to the CLI to update upload it to open AI so let's first import pandas put pandas as PD say data set equals pd.read CSV I downloaded my file to home Leo Smiggle downloads tweets dot CSV and let's go ahead and print out the first few rows we can see the Tweet ID Airline sentiment confidence negative reason negative reason confidence Airline all sorts of information but we're primarily concerned about uh in regards to the text of the Tweet itself and the sentiment right so the text in the sentiment so let's create a new data frame using just that a DF equals dataset.filter we'll grab the text oops the text and the airline sentiment and then let's print out the head so now we can see we have the text here and then the airline sentiment now I don't want to train the Ada model on all of this data because it's going to take forever and I'm also cheap right at least in for this case maybe for something in production it can be a little bit different but for our intents and purposes let's I'm going to make this smaller you can choose to make it as small as you want or keep the size whatever but we'll do DF shape and we can see that it is 14 640 rows and two columns I'm going to make it uh let's see 1200 right so we'll say DF we'll slice it 1200 DF equals and then DF shape okay so now it's only 1200 rows now there's one last thing that we need to do before we save it to a CSV file for the CLI to import and that is to change the column names DF dot columns equals and it is prompt and completion and then df.head see if that's correct so prompting completion so now we have 1200 rows formatted in the correct way so let's save this to a CSV without the index and then we'll be able to import it to the CL CLI so df.2 CSV I'm going to say home Leo Smiggle downloads Airline sentiment.csv and the index equals false and I'll save that and now we'll have the file to import into the CLI we'll want to open up our terminal and make sure that our virtual environment is active so that way we have access to the open AI CLI tools I'm going to activate mine now and I see now that it's active because I have this virtual environment prepended here and now I'm just going to pass my file to the repair data command let's do that now open AI tools fine tunes repair data Dash F and my file is at home Leo Smiggle downloads Airline sentiment.csv and then it's going to ask us the number of prompts prepare what I do here pre-part pre I don't know what that is pre e a r e all right so it's going to ask us a few prompts we're going to want to remove the duplicate rows we're going to want to add the suffix separator we do want the white space character at the beginning of the completion we do not want to split it into training and validation sets for this tutorial and then we will save this to a new Json file that will be the same exact format or same name as our Airline sentiment file it'll just have a underscore prepared dot Json L file so with the file in my downloads folder now I can import this and create a training file and then pass that to open AI you'll notice here that model training does take some time for a query model takes right now around 31 minutes it'll take a little less for Ada and Babbage but this process does take take some time let's go ahead and train our model Now by heading back over to jupyter notebook we'll now create the training file from the prepared data I'm going to paste this function in here just to speed up the process so you're not watching me type all we're doing here is creating a file using openai.file.create passing in the file path from here that we pass in as a parameter as uh you know read-only and binary and the purpose is fine tuning and then that what we'll do is we'll take that file training file equals create training file and we'll pass in our existing file so home we go Smiggle downloads that's Airline sentiment underscore prepared dot Json L and then print training file and then scroll down here and we can see that it is indeed creating this file and what we're primarily concerned with is this ID here so I'll just take training file and then ID and this will give us the ID and we're going to use this ID to train our Ada model with our training file ready to go training the model is actually pretty easy all we need to do is pass the training file ID to the open AI fine tune create method let's do that now so fine tuned model equals open AI fine tune create training file and then here's the training file ID not the actual file itself training file ID and we also want to pass it the model I'm going to pass it to Ada because it's the cheapest DaVinci can be pretty expensive for fine tuning and the number of epochs you don't need to pass I'm going to just do it anyways just so you can see epochs equals four that's actually the default and now that we have that print fine fine tuned model and you'll notice that this creates this created the you know this prints out relatively quickly let's take this ID and we can get status updates on it we do this by typing open AI fine tune list events ID equals the ID from this and then data and we can see here that we've just created this fine tune event this is going to take some time and once it goes through the entire process we'll see a final message that says fine tune succeeded so I'll see you when this is done the fine tuning is finally done I say finally because it took over three hours in queue and then once it's the fine tuning started processing it was relatively quick so now that we have the fine tune model let's test out the original Ada model to see if it misclassifies our tweak because if it does we did all this training for no reason so hopefully uh we'll make a fool out of me on YouTube we'll see give me the sentiment of the following tweet the flight landed ahead of time the food was delicious okay so that seems pretty positive to me we'll just use the completion create which we've done a hundred times now to analyze this a response equals open AI completion create we'll take the model that's the text ada001 prompt equals prompt text the max tokens will give five and temperature equals zero and now what we'll do is we'll just print out the response print response we'll take the first choice and print out the text and it definitely shouldn't give us positive negative or neutral and thankfully it doesn't right so it's just printing out the flight landed ahead of time now what we can do is we can scroll up to the output of our fine tuning copy the model name and I'll show you how to get this model name a little bit later if you don't actually have the output ahead in front of you and you paste it in here and we cross our fingers so that it doesn't make me look like an idiot on YouTube positive positive positive so it is positive thankfully okay so you can see that the original model did not work but the fine-tune model did now let's try something else the flight landed ahead of time food was delicious I'm going to delete this and I'm going to say the flight landed on time let's see if it's smart enough to know this so I'll we obviously know the regular atom model doesn't work let's see if this can classify as neutral yeah the flight landed on time I guess that's sort of positive the flight landed behind schedule negative okay so we might have needed to train it with some more data but long story short we can see that our fine-tuned model is definitely giving us better results than the standard Ada model here's a challenge though I mean I guess it's still at a lesser cost but what happens if we just change this to text convincy zero zero three oops what did I do here text there we go frustration wow okay so that's interesting let's see if it can classify the flight landed on time positive okay yeah so we can see that the DaVinci model worked better than even our fine-tuned model although it you know costs more per token the key here is understanding you know do you really need to fine tune a model and why are you doing that is it because you want a reduction in price or do you really need to improve the model you now know how to create or I should say fine tune and open AI model to improve it that's pretty cool right but what happens if you wanted to create your own model use some of this Tech in your own models well enter open ai's text embedding so let me just create a new section here say text embeddings model and this allows you to well actually I should take a step back what are text embeddings well text embeddings we can see here is essentially you know you can't a computer can't be trained on text it doesn't understand text it just understands numbers and what this does is it takes us text and translates it into a vector I think of like 1560 I forget exactly how long but essentially what this text is you know is a converts this text into a vector it shows sort of like the relationship is right it's not just an ASCII conversion it's a relationship of this text here how openai does that is beyond the scope of this but just understand that this text is converted into a vector now and we can use that Vector to train our models right each float can become a feature so let's see exactly how we'll do that now let's go ahead and create some input text input text equals the flight was on time and the food was delicious and then as you know the open AI endpoints are super easy to use so response equals open Ai and here we use the embedding create model equals and this is text embedding ada002 it's their latest you can read about it if you're interested right here you can just search for for this open AI text embedding a to zero zero two model and it'll you know tell you all about it input text so all we're doing is we feed it the model we feed it the input text and it'll respond with the embedding I'm going to print out this embedding print and the length areas various Pawns funny trying to talk and at the same time and having difficulty apparently all right I'm betting perfect so this will print out the length of the embedding now let's actually just print out the embedding itself and then so you should see a length and a large list of floats okay so we see 15 1536 essentially features you can kind of think of it that way it's the 1536 dimensional Vector which is the text embedding you know Ada zero zero two model size so now how do we use this well let's go ahead and train our classifier with the gpt3 embeddings the first thing that we'll do is we'll create a function that takes the text and turns it into an embedding easy enough let's do that now so def get embedding asset the text and the model and text equals text Dot replace I'm going to replace the new lines with spaces and return open AI embedding create and the input equals text and the model equals model and we don't want to return the entire response we just want the actual embedding so I'm going to get the data first element embedding perfect so that'll get us the embedding now what we need to do is read in the Tweet sentiment data that we downloaded earlier and apply this function to some of the fields into the Ada embedding column so that's easy enough too so what we'll do is we'll say data set equals PD read CSV mine is at home we have Smiggle downloads I could spell tweets.csv so that reads in the file now I don't want all of the columns I just want the text and the sentiment column so data set equals data set filter text and Airline sentiment and now I don't want all of the records because I am get well I'll get rate limited right you can only I think there's only 60 API calls a minute so let's just do data set equals create a set dot head do 50 at the moment and now we want to create our Ada embedding column so data set equal well sorry data set Ada embedding equals data set text dot apply and then we're just going to pass in the function X where X is the text get embedding and we'll say X is the text and the model is the text embedding data002 and now let's just print out a sample of this data set head to make sure we got it right and see what happens and now we see that the data frame printed out we see the text and the airline sentiment and this text turned into an ADA betting here now let's create our train and test set so that way we can fit our model first thing that we'll do is we'll create our features so x equals data set data embedding and now this is currently a series so I'll make this a list I can print this out you can see it's just our you know float values essentially the text which is the Ada embeddings Right Here and Now what we want to do is we want to encode the sentiment so we don't want to use neutral positive and negative we want these to be zeros and ones we don't have to do that manually what we can do is we can use SK learns label encoder to do this so let's do that now so from sklearn import pre-processing and then Le which is simple or short for label encoder reprocessing label encoder and then y equals l e fit transform data set and now we want the airline sentiment line sentiment and then I'll print this out and you'll just see a bunch of zeros ones and twos which are really essentially the neutral positive and negative encoded now now that we have this what we can do is split this into train and test sets and again we're going to use SK learn from sklearn model selection import train test split and now what we'll do is we'll split them into you know four four variables X train X test y train and Y test equals train test split X Y we pass the X and Y and then we just give it a percentage of how much that we want as our test size I believe the default is 25 but I'll just put it here anyways and random state or be explicit about it in random state allows us to reproduce our results I'll just put 42 here and then I'll run this so now we have in each one of these variables you know 75 percent of the data to train on and 25 to test on so now let's train we will use a random Force classifier provided by sklearn so from SK learn ensemble Ensemble import random Forest class fire and RFC equals random Forest classifier creating that RFC object and now what we'll do is we'll fit it so RFC fit we fit it on the training data obviously right so what we're doing is we have this x train which is all of this and then we you know fit it to the Y which are the um you know the sentiment so we run this and what I do here slearn sklearn oh boy I spelled this wrong I'll copy and paste this there we go I guess I did all right now we have that perfect okay hypo's aside things were going well all right so now what we do is we have this fit random fourth classifier model now we want to perform some predictions I know that the test isn't going to be that great because we don't have a lot of data I did that purposely Because unless you have a subscription your rate Limited at 60 API calls a second and I didn't want this video to take too much longer so now what we'll do is we'll say RFC pred which are just a random Forest classified predictions equals RFC predict and now we do X test so we're passing in our X test and see how we get how well we predict the Y's right so I'll run that and now what we need to do is print the result so I'll say from SK learn metrics import classification report and confusion Matrix print confusion Matrix y test RFC pred and we'll do the same thing for the classification report why test and RFC red all right so as you can see 38 accuracy still you know better than a coin flip because if you think about it we've got you know three barrels that we're trying to predict I guarantee you if you give it more data it will get a lot better so that's pretty cool you now know how to take this Ada embeddings and use it for your own prediction models we can also perform zero shot classification using the gpt3 embeddings what this means is you know we can predict things on you know Based on data that we haven't seen we can use the open AI embeddings util cosine similarity for this now let me know if you like this method better if you followed along this far I really appreciate it tell me if you'd like me to you know see me type it and talk about it or if I can just copy and paste which method you prefer so basically here we're just importing the embeddings util cosine similarity then we have to get our labels which we're familiar with they're just positive negative and neutral now I'm going to use list comprehension to pass you know everything through that get embedding function that we created earlier the model is still going to be text embedding Ada so for each label in labels run this function and return it so essentially we're going to have you know data for positive negative neutral for each label and this will become more apparent as we see the data now we need to create a function for each label score that we'll call this okay call our get embedding function so I'll copy and paste this here and what you can see here is we're returning the cosine similarity between the text which is the tweet and then the label embeddings okay so let's run this assuming I didn't make any typos which I will not guarantee okay and now what we'll do is we will you know get the label scores for this so label scores equals label score that's the function right here pass in the review text the flight was awful and the food was really bad obviously pretty negative and then what we need to do is pass in our label embeddings a label embeddings and then we'll print out the label scores you'll see here that we have three scores right and now what we can do is we can select the max index you know essentially the Max and then the index to determine which label it was so we can see here labelscores.index Max it'll give us the max value Returns the labels Max index and in this case it should be negative which indeed it is so that's it if you followed along this long congratulations you're one of the few you've learned all of the basics and now you also know how to do fine tuning you can use the embeddings and train something on your own machine and you can even perform zero shot classification with embeddings hopefully you enjoyed this video if there's anything else that you would like to learn let me know in the comments below the next one I'm probably going to do some earnings call analysis using open AI you know for sentiment analysis and also open ai's whisper so hopefully you enjoy this one hope to see you in the next one thanks bye
Info
Channel: Analyzing Alpha
Views: 8,598
Rating: undefined out of 5
Keywords: trading, investing, data science
Id: 8SiRJgiaISQ
Channel Id: undefined
Length: 57min 55sec (3475 seconds)
Published: Wed Feb 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.