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