The END for developers? First look at OpenAI Codex + Python Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
dude that is actually sick i'll never forget june 11th 2020. this was the day the open ai released their gpt-3 model now this shook up the entire tech world because it was so so powerful it was a huge nlp model that was able to handle a whole heap of different tasks from summarization to translation to writing copy people were using this for a whole bunch of different types of applications and they were having amazing success namely because the model was so big and had such a ridiculous number of parameters it was able to generalize to a whole heap of different use cases now the sucky thing is i never got access so when the openai team went and demoed open ai codex a couple of weeks ago i decided we needed to go and check this out now open ai codex is a refined version of the big gpt3 model called davinci but specifically fine-tuned to generate code this means that you could pass through a string to be able to do something in terms of code and the da vinci codex model would be able to generate that code whether it be python or javascript or java the codex model is able to handle it now this is the same model that powers github copilot and it was actually proven or estimated to be able to convert a doc string think of this as a natural bit of text to functional code 28.8 percent of the time now this could be a complete game changer particularly when it comes to the no code revolution everybody's all about these no code tools that allow you to drag and drop different components to be able to do certain things and build applications this could be a complete revolution ideally it would allow people without coding skills to be able to enter in a string specifically for what they want and have the code generated for them not exactly no code but ideally getting further towards that state now this begs the question for all the coders out there what about us could codex replace the role of developers software engineers and data scientists well that's what i wanted to take a look at in this video so i applied for the beta and yesterday i got this email back so we're gonna go and check it out ready to do it let's get to it and so it begins this is the page which announced open ai codex so through here you can actually see that codecs is in fact the model that powers github copilot so again they actually built it in collaboration with github now if you wanted to try to get access to this you can join the waitlist now i don't know what the wait times are like so i think i applied about a week ago and i was able to get access kind of relatively quickly so i i also applied for gpt3 access a while ago and that took a little bit of time but in this case codex was actually reasonably quick now the cool thing as well is that you can also go and read the paper so if you wanted to go and take a look at the fundamentals and the mathematics behind the code you can actually head over to axrv or a a so in this particular case you can actually get the entire code and again as i was saying so the cool thing is that it is or it does mention that gpt3 is specifically fine-tuned for synthesizing code from doc strings and it looks like in this particular case it's able to solve or build functional code around about 28.8 percent of the time which is what we're going to dig into a little bit now and ideally the aim is to take a look at how this could work collaboratively with developers but just as easily we want to take a look to see whether or not this is going to replace us completely so let's first up take a look at what gpt3 in general can do now again if you haven't seen much on jpt3 basically it's a massive model with a ton of parameters which allows the model to actually do a lot of stuff when it comes to natural language processing so say for example you wanted to do translation or pass unstructured data or convert movies to emojis this is the type of stuff that gpd3 really does excel at now in this particular case we're going to focus on open ai codecs which is the fine-tuned version which specifically is for generating code now once you do get access to the beta you're able to actually go to the playground so in this case the link for this is beta.openai.com forward slash playground and you're basically able to log in to be able to trial this out now to use the specific codecs version or the codex model you actually need to go over to here and specify that you want to use davinci codecs so you can see that this is the most capable model in the codex series which can understand and generate code including translating natural language to code now this is the core thing it is converting natural language to code so we can pass through a natural language prompt so do this or loop five times over this and it's going to try to generate the associated code for that particular prompt this is what it's referring to when it actually talks about converting doc strings to functional code or functional models so what are we going to do the nice thing about this is you're also able to choose your language so down here we can see that we can either convert to bash c sharp go java javascript python ruby rust sql and typescript we're going to choose python in this particular case so say for example we wanted to let's say we just wanted to print out something so print at let's make it a comment print out hello world so what we can do is we can pass to our prompt which you can see over there and then right down the bottom we can hit submit and this will ideally try to do something and there you go so this is actually gone and generated the code to print hello world again super simple example but you can see there that i've written i've put in a comment so comment or pound sign print at hello world and it's actually gone and written print hello world so if i took this and threw it into a jupiter notebook you can see that it is in fact printing out hello odd so really easily or really quickly we're able to see that it is generating functional code now let's go and try a different example so say for example we wanted to um bring in pandas so say so pandas is a really popular tabular data set or tabulated library which allows you to work with tabular data sets so specifically csvs or um anything that think of it as being in a table so say you had a sql extract or an excel sheet pandas is the library for that so if i hit submit oh so this has actually gone way further ahead so this is actually gone let's take a look at what it's written there so i've given it the prompt bring in pandas and oh it's actually gone and finished it and written and numpy libraries so it's now gone and written import pandas as pd which is i believe is correct syntax import numpy is mp it's then also gone and created a data frame so a fundamental concept when working with pandas is a data frame so think of this as just a big tabular data set it's uh if you've ever worked with pivot tables it's like pivot table and steroids so we could actually take this code and try to execute it so if i grab this and throw it into my notebook let's see if this works again no errors i don't know how many times i've written code without errors this is obviously a simple example but this is ai literally writing our python code which is pretty sick now if we went and took a look at this data frame let's actually see if it actually understands how to view this data frame so the correct function that it would output if we were to try to view the first five rows of this data frame would be the appending the head method to this df object over here so if i wrote show first five rows of data frame hit submit that is crazy so you can see there that it is actually outputting df.head so that's the method that i was referring to now it's actually going further down and writing out a whole bunch of stuff so it's showing the last five rows it's creating summary statistics it's creating a new data frame or the beginning of a new data frame so if i actually went and copied this this is almost doing like our exploratory data analysis for us which if you're a or not a data scientist or not a software engineer this makes your life a ton easier so describe is going to be the summary statistics so if we wanted to just view the first five rows of our data frame pretty cool right so if we did the tail function that it generated that's working and if we also went and ran described so this is also generating our summary statistics so already we can see that this is generating code now what happens if we gave it a bit of a broader example so say we cleared all of this and let's say we just gave it the beginnings of a machine learning model so say for example we wanted to build a machine learning model machine learning model for classification make this a little bit bigger right so build a machine learning model for classification and let's give it a first couple of lines of code so import pandas as pd so one of the most popular libraries when it comes to python for classification is going to be the library called scikit-learn so if we went and hit submit let's see what it generates so it's actually bringing in a data set so raw github user con where's that data coming from doesn't look like that's a valid link by the looks of it not found okay so that's our first fall down but again it's oh wait it actually went and finished the line that is pretty cool so it's actually gone and ridden let's actually take a look at this link there in this particular case it's looking like that one's not found you win some you lose on now the key thing is that right now we're limiting how much information or how many lines of code it can write so if we actually drop let's go back to basic so import pandas is pd and let's tune our response length over here so if we actually made this a little bit longer so let's give it some more degrees of freedom so we can tune our response length our temperature and think of temperature as controlling the creativity of the model so you've got the ability to tune that up and down top p allows you to choose how many options you choose from the next sequence so basically it's like limiting how much limiting or opening up how many options it can generate for the next sequence within our string of text or block or text we've also got a frequency penalty and a presence penalty we'll come back to that in a second let's actually try this out now so what we've gone and done is we've left it as the same engine we've just gone and changed the response length so if we went and expanded this out so it looks like it's importing a bunch of different libraries this is nuts guys it is actually writing the code to go on ahead and build a machine learning model now i don't know where it's getting this data set from but this looks like it's bringing it in from a person's c drive so you might need to sub that out and i don't know where you'd actually go and get that data but this is actually looking reasonable so it's bringing in a number of libraries which is here it's going and bringing in a data set this is exactly what or pretty much the same style of code that i'd actually gone right if i were to go and try to generate a model myself it's showing the first five rows it's dropping a column it's checking whether or not we've got null values it's performing some value counts so again this is almost like an automated data scientist now i don't know if this is going to be perfect but you can start to see that it's actually able to generate code pretty successfully without too many errors so if i went and brought in this section so this is probably going to fail because i'm not going to have this particular data set so if i brought in this let's get rid of all these lines that we went and generated so i'd need to go and install that library so pip install matplotlib let's make this a bit bigger so that's insult so if we went and ran this now no module named c bonds we can source a bond so these are visualization libraries and and scikit-learn uh what is it sk learn how do we install circuit learn again scikit learn install i can't remember if it's scikit-learn or sk learn scikit-learn cool so if we go and run this that is successfully running that code so it's at least doing a successful import of a bunch of machine learning libraries pretty cool right so that sort of gives you an idea of what's possible uh with open ai codex now this is really focused on python so how might we do this if you wanted to do something slightly different so same you're more of a software engineer you're building websites and things of that like well again the nice thing about the open ai sandbox or playground that you get is that you've also got a javascript sandbox so you can see that up there codex javascript sandbox and this allows you to write javascript code and actually have it rendered inside of this screen here now the cool thing about this is that you can actually bring in links so say for example i wanted to show a picture of rick and morty right so i had this picture here it looks like it's downloading how do we view it so let's actually open it up so this is the picture so say for example i wanted a picture of rick inside of my website now typically you'd have to use like source tags and bring it into your html and you'd have to write that from scratch unless you're using some sort of framework or you're using dreamweaver i don't know how old dreamweaver is now but you'd have to do that type of thing what we can do with codecs is actually get it to try to write that code for us so um say for example right down here in this prompt down the bottom i can write let's make this a bit bigger see if we can see it that's way better okay so i can write show me uh or display actually display a picture of this well actually yeah whatever let's try that you can see that it is actually showing so this is like our template website right it's actually creating a variable it's creating a element to hold our image and then it's actually applying our image to the page so it's actually passed through that url and that was nothing but all i wrote was this so display a picture of this and it's actually starting to generate the code for that website so pretty pretty cool right now let's say that we wanted to make it smaller um we could type make it smaller and you can see that it's gone and made it way smaller i don't know what's another one uh make it twice as big and it's gone i made a big i don't know i'm like a kid in a playground so this is actually allowing you to pass through natural language commands and create a website so from a software engineering perspective again you're probably not going to write repeatable code like this or this these are repeating themselves you can start to see that this could be potentially very very powerful when it comes to allowing you to quickly make changes to your code and fine-tune your code say for example we wanted to change the background um set background color to black um move picture to center so it kind of centered it there centering is always a nightmare add a title called rick and morty's page so you can see that over here it is generating all of this code uh let's let's create a h1 element um set h set title to white so we can see it that is pretty cool guys so you can see that all i'm doing is i'm passing through some natural language commands and it is actually going on ahead and actually generating the code to be able to do that so you could probably take away this code you probably need to clean it up a little bit but it is actually generating the javascript that we can use within our website so we could also export this to jsfiddle if we wanted to try it out but that gives you an idea of how powerful the codex model is so we are just passing through natural language commands and it is generating the code for us now say for example you wanted to use it more in a pipeline style of approach well you can also write it you or write work with it using the python api so i'm going to delete all this stuff that we started doing so we can actually access it using the open ai library so there's an open ai library that's available through openai that allows you to tap in to the codex model so in order to do that we first need to install openai so i'm just going to write exclamation mark pip install open ai and i've already got it installed and then what we can do is import this into our notebook so import open ai and then so what i've written is exclamation mark pip install open ai and then i've gone and imported it into our notebook so import openai and all this code will be available in the description below if you want to try it out but you do need an api key that is a key thing to call out then what we can do is actually set that api key so i've gone and stored this outside of this notebook so i don't sort of share these apis and hit my bandwidth limits too quick so we can bring this in so from constants import api key and then what we need to do in order to actually work with open ai through our notebook is we need to set the api key so we can do this using open ai dot api underscore key and we set it equal to our api key so that basically means that we've now effectively authenticated against the open ai api or python sdk so we can actually start using the codex model through our own python code so it's almost like writing python to write python we're kind of getting a little bit meta here so let's actually go and see if we can actually write some or get our codex model to do some stuff so i'm going to create a variable called prompts and the cool thing about this is that you can not only get it to generate code but also fix your code which i think is going to be super super useful for software developers around the world because more often than not you're going to probably write some code and they'll maybe be a bit of a bug in it so having the ability to sort of like side check your code using something like this could potentially be very very useful at the moment based what i'm seeing it's probably not going to completely replace software developers which is why i think github was pretty intelligent in calling it copilot and not like new pilot or revised pilot or ai pilot it really is going to sort of act like a co-pilot now if you see let's actually try to get it to fix our code so remember that we can import pandas into our notebook uh using import pandas as pd right so let's say we went and screwed up our code so what we can do is say fix this code it's this code and i'm actually going to write the incorrect code so let's actually have a line break and then i'm going to write import well let's actually write imm port pandas ask pd so we know that this is actually incorrect so if we go and print out that prompt let's call it prompt not prompts so this is definitely not correct or not grammatically or syntactically correct from a python perspective so it should be 1m and it should be as pd not ask pd so what we can do is what we can try to do is see if codex is able to fix this code so not just generate new code but actually fix our code so let's try this so what we can do is we can use the completion api through codec so we're going to store the response inside of variable code response and then i'm going to set it equal to openai dot completion dot create and then there's a couple of parameters that we need to pass through to this so if i write openai.completion dot create and hit question my question mark so there are a bunch of arguments that we can pass through to this it doesn't look like it actually specifies them that that's fine let's actually go and take a look at them ourselves so the first parameter that we need to set is the engine that we want to use and in this case we want to use the codex model right so the engine is going to be davinci dash codecs and then the second keyword argument that we want to set is let's just tab this in a bit second keyword argument that we want to go ahead and set is add the prompt to prompt equals uh we're setting it equal to this prompt over here so think of the prompt as the natural language text that we're going to be passing through to codec so no different to how we played it around or played around with it inside of our playground we can pass through our prompt using the python sdk so this basically means that we're going to be passing through this so fix this code and then a line break and then port pandas ask pd so we're going to be passing through that to the codex model then i can add a comma and then we've got a bunch of keyword arguments that sort of allow us to fine-tune what our responses are going to look like so these are temperature and we're going to set this to zero for now and think of zero and think of this as influencing the model's ability to take risks so this is our risk taking ability and it sort of influences the creativity of the model so influences creativity right so a higher number is going to give us more creativity i believe a higher number is going to give us more creativity hit me up in the comments below if that is not right and we've also got the ability to pass through another keyboard parameter which is top p and this gives us a range of so the way these models work is by generating a single line or generating a token and think of a token as a word and what it actually does is it uses a technique called sampling to work out which next token to pass through so if it writes import first then we need to determine what the next token is going to be so top p influences how we actually go about sampling so this is influencing influencing sampling and then we've also got a bunch of penalties that we can apply as well so we've got a frequency penalty and we're going to set that to 0.0 and this what does this influence this provides penalties t's for repeated tokens so basically means that if it starts to or it stops the model from repeating itself over and over so a key thing about these huge nlp models is that sometimes when you go and generate stuff they're going to start repeating themselves because those next set of tokens have a really high probability of being the most appropriate token we don't want it to just continuously repeat itself this is one of the key advantages of this particular model is that we can apply this frequency penalty to tell it to don't just repeat yourself actually try to generate new stuff now we can also pass through a presence penalty and this is also going to influence it to uh have new words right so we wanted to go and generate new stuff so the presence penalty is looking to see whether or not it is applying new tokens so new tokens uh penalties or new words and then we've also got the ability to stop it so this is almost like early stopping so stop equals and then we're going to apply a pound symbol so the stop keyword argument is basically telling our model when to stop when to stop generating stuff now because we're coding in python we basically want it to stop at the next comment so by passing through this pound symbol we're basically saying hey once you reach the next comment within your generated text or generated sequence stop generating stuff as of then now what we can actually do is we can actually go and run this so let's quickly take a look at what we wrote so we're going to be storing everything inside of a variable called response and in order to do this we're writing openai dot completion in capitals dot create so this is using the create method as part of our completion api and then we're passing through a bunch of different keyword parameters so we're setting the engine and this allows us to use the codex model if you wanted to use the baseline davinci model or the baseline gpt3 model it's going to be just davinci and then we are passing through our prompt which is going to be this over here we're setting a number of keyword parameters as well so these are almost like tuning so if you wanted to drop all of these you could you don't actually need to have these in fact let's actually comment these out and run it first so if we just pass through our prompt and i'm just going to delete this little comma here and run this we can actually take a look at our generated code let's actually go to step three so if i go and print out response let's actually type take a look at our response you can see that what it's going and generating is backwards and data url something something weird so that's not exactly the best response so if i type in dot choices and grab the first value dot text you can see that that isn't exactly completing our code all that well so these fine tuning parameters are super important right so that's what it's gone and written as this second line of code now we can actually go and try to tune this a little bit so rather than doing that let's go and unlock our tuning parameters and we've got we've just got to add a comma there and you can see this is generating way better results so it's actually gone and fine-tuned or fixed up our code so we've written fixed this code and then we've written import pandas ask pd and it's returned import pandas as pd so if i went and tried to run this line of code first up you can see that's giving us invalid syntax because this isn't right but if we went and ran this or these two lines of code you can see that these are valid so this is where i think that there is a huge opportunity so in terms of fine-tuning your code or almost using it like a pair programmer i think that this is super super cool now let's say for example we wanted to generate some code so rather than asking it to fix our code let's say we just wanted to create a loop that uh i don't know prints out my name five times so we could write um right a loop that prints out nick five times so let's try this so this is almost like a slightly different approach to using the codex model so rather than passing through fix our code we're now asking it to generate something so if we run this prompt so this is just what our prompt looks like we can actually delete this if we went and run this response now or tried this response it doesn't look like we've got anything back write a loop let's try something slightly different so write a function that prints out nick five times and we need to set the number of tokens as well so right now it's probably written the commencement of this so def print underscore nick underscore five times and it looks kind of promising but this isn't giving us enough scope so what we can actually do is we can set the max number of tokens as well so if i write max underscore tokens and set this to 1024 so this is going to limit how long our response or output can be so if i run this and take a look so that is our function there now so like that is our function right so it's gone we've created a function so def print underscore nick underscore five underscore times and if i actually go and run this function so print underscore nick underscore five underscore times it's printing out neat five times how cool is that so it is actually doing what we're asking it to do so we could also say print it 10 times let's change that now so in this particular case it's actually gone and written def print underscore nick underscore 10 times print nick times 10 and then let's actually see how this works so in this particular case it has not gone and separated it out onto a new line but it's definitely printed at nick 10 times so let's say on different lines take a look at this so it is actually fine-tuning this code so this is the first code right i'm getting so excited this is just so cool so this first code is just printing out nick 10 times on a single line and we went and fine-tuned our statement to say write a function that prints out nick 10 times on different lines and it's gone and fine-tuned this function so if we went and copy this now and run this down here give us ourselves some more room how cool is that so this is actually so it's actually gone and written the functions for print underscore nick underscore 10 underscore times and it's actually written a for loop to print out nick 10 times so you can see it's gone and written it all down there so 1 2 3 4 5 6 7 8 9 10. pretty cool right like i think that that is absolutely ridiculously awesome now what we could also do is let's say if we wanted to give it something a little bit more general so um what's a good example so say we wanted to uh bring in some data right so i've got this data set inside of my folder called uh i think it was titanic.csv let's just take a look yep so titanic so let's say for example we wanted to import this into our notebook so i could write import titanic dot csv into a pandas data frame let's see if this works so what the prompt that we're going to be passing through this time is import titanic.csv into a pandas data frame so if we run and run this try our response take a look at that so it is actually doing it so written titanic underscore df equals pd dot read underscore csv so i know for a fact this is correct syntax so if we went and copied this now assuming we have pandas right so this is probably going to fail if we don't have pandas imported on this particular oh we already imported it remember when we were generating that code so we can actually go let's see if we can view the first five rows view first five rows of titanic dot csv data frame no way so it obviously doesn't let's uh write titanic so in this particular case what it's going to generate is just titanic.head because we haven't really given it the name of the data frame so it doesn't have context right now so if we wrote titanic underscore df data frame take a look at that so it's actually writing the code for us so if we went and copy this that is showing us the first five rows so if you didn't necessarily know how to write this code to do certain things for this data frame this would actually help you so say for example we wanted to drop this column right let's actually go the the appropriate function would be titanic underscore df.drop you pass through the column and then you set access equal to one so it would look something like this let's see if it would actually write the code so it will be titanic underscore df.drop and then it will be passenger id comma access equals one so that would drop that column right so that's what i've written so let's say nyx let's see what it actually goes and generates so we're going to say drop passenger let's just make sure we name it right passenger id column from data frame if this works this would be so cool it's all right so the syntax is slightly different but this is still going to work so this is really really cool so if we go and it's actually gone and reset the data frame so if we go and copy this let's take compare the our code so this is the code that i wrote and keep in mind that this my code is actually just doing it in place so we actually are not applying it and saving it we're just doing it and it's going to return the result what this is doing is it's actually going and reshaping the data frame and it's resetting it without that dropped column so if i went but you'll actually see that if i go and line them up these are almost identical the only difference is that it has gone and wrapped the columns inside of square brackets which is just an extension of the syntax so it's exactly it's perfectly correct so if we go and run this code now and take a look at our data frame titanic you can see that it's gone and dropped the passenger id column pretty cool right now we're you're probably thinking well nick so is this just going to take my job or is this going to completely replace our data scientists well let's put ourselves in like the real hot seat so say for example um your customer came to you and said hey i want a machine learning model that does classification for this data frame so let's take a look so again this is not going to have any context so build a classification model on titanic underscore df data frame and what we're going to do is we're going to extend out the maximum number of tokens rather than having 1024 let's give it 2048. so if we went and passed through this prompt ran this response and took a look at what it's generated so you can see that it hasn't actually gone and generated anything there now we could just give it a slightly different prompt using scikit-learn and you can see it's only generated a dot so i think a key thing or a key limitation with this is the breadth of code that you're passing it through so if you're not giving it a whole heap of prompts it's probably going to struggle a little bit now if we said um slightly you know reshape this slightly differently so let's say build a regression model right so we're not giving it any context and we go and pass through this prompt you can see it's it's going finishing this so with the best parameters it's going and instantiating a linear regression so this is correct right so this is actually appropriately starting up a linear regression model it's then going and specifying or passing through the fit model so again you're able i think it's really really good if you've got like really defined code blocks for what you want to do and if you want to iteratively build stuff but a key thing is that so think of this like a great ship you're still going to want a captain to ideally help steer it that is i think the key takeaway from this it's a great sidekick that you can have above your shoulder to help check your code and it's great for helping you build smaller prompts of code but in terms of the bigger grand scheme of things you're going to at least want to provide it a little bit of direction that being said it is freaking cool guys like you've seen what it's been able to generate from this so it's able to generate a bunch of code we've taken a look at python we've also taken a look at javascript as well and that about wraps it up thanks again for tuning in peace thanks so much for tuning in guys hopefully you enjoyed this video if you did be sure to give it a big thumbs up hit subscribe and click that bell thanks again for tuning in peace
Info
Channel: Nicholas Renotte
Views: 9,381
Rating: 4.8461537 out of 5
Keywords: openai codex, openai, openai codex python, openai codex machine learning, openai codex paper, openai codex tutorial
Id: 3uhP6xDfx3o
Channel Id: undefined
Length: 38min 30sec (2310 seconds)
Published: Wed Aug 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.