How To Build a GPT-3 Chatbot with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey yo welcome to learn with jade my little corner of the internet for anyone looking to eat something other than copy pasta i'm jade manley and today we're going to build the gpt3 chat bot that you can text whenever you need life advice the best part is we're going to do it in less than 50 lines of code give or take now this is a video tutorial but if you want to go more in depth you can find my blog tutorial in the description all right our tech stack today we're going to be building this in python using the flask framework we'll be writing our code in vs code editor we use the github desktop app to host our repo we'll be using an api key from open ai to access gpt-3 and we'll be signing up for twilio to handling our messaging service uh finally we'll use render to run our chat bot uh in the cloud so without further ado bon appetit first things first we're going to want to create a new project folder we'll call ours gpt3 jbot we're going to change into the gpt3 jbot directory we just created with the cd and we're going to create a virtual environment you'll see around the internet most people use them as the virtual environment folder but feel free to name it whatever you want we are going to say python 3 because i have some issues on the new mac mini and after the command completes we'll have a subdirectory within our gpg3 jbot folder called vend and what we basically need to do is activate our virtual environment with the following command we'll know our environment is activated whenever you see that vem ahead of our folder great inside of our virtual environment we need to install all of our necessary packages using pip so we're going to pip install and basically what we need is open ai we need twilio we need flask and we need python dot m so hit enter and let's get all these going okay open the github desktop app and we're going to create a new repository all right and before we hit create repo we want to make sure that we add a python git ignore file and let's initialize it using a readme and this is our chat bot app and again adding the get ignore file will prevent us from uploading our virtual environment as well as our dot n file that contains our super secret api access token which we'll get to so let's go ahead and create the repository awesome and then let's open it in visual studio code now first thing we're going to do is create that dot m file and within this we're going to be exporting our open ai api key we're going to set it's equal to our api access key all right so we need an api key from open ai to actually build our app um i just tweeted at greg brockman who was kind enough to hand out beta invites otherwise you can apply on their site i'm not exactly sure how long it takes to get accepted but good luck you can also just use my key psych i'm going to regenerate it by the time i hit publish on this blog post but if i forget then you're in luck so once we have it let's go back to visual studio code and we're going to drop it in here and hit save now that we've got the skeleton for our project it's time to give it a brain i'm not going to spend a lot of time going over exactly what gpt 3 is or how it works partially because i myself don't truly understand it but mostly because there's a ton out there on the internet that you can read i'll link some helpful resources in the description openai gives us a playground where we can really mess around with gpt3 some of the key things to keep in mind are over here on the right hand side the engine api open ai has four engines to choose from this is really the black box of gpt-3 but we're going to use davinci because that's supposedly one of the best ones response length controls how much text is generated think character count here for all you microsoft word or google doc users temperature this setting controls the randomness of the generated text the higher the temperature the crazier it spits out top p this parameter also has some control over the randomness and creativity of the text generated for some reason it's used less than the temperature so let's stick mostly to temp temperature the frequency penalty works by lowering the chances of a word being selected again the more times the word has already been used the frequency penalty does not consider how frequently a word has been used but just if the word exists in the text per twilio ultimate guide the difference between these two is the frequency penalty you can think of as a way to prevent word repetitions and the presence penalty is a way to prevent topic risk uh repetitions the best of don't need it don't worry about it uh the start see the stop uh the stop sequence it helps to prevent gpt three from cutting off mid-sentence uh the start text automatically appends after the user's input uh this will happen before sending a request to gpt-3 which will come in handy when we're building our bot the restart text is the text to append after the model's generation to continue the pattern structure in other words it helps so we don't need to type the prefix all the time now we we pulled from the q a uh examples here and i slightly modified it but let's hit submit and see what our chat bots response is so here we are you know i've kind of prompted the the question and answer of a person and the bot jabe i gave it a little bit of a brain beforehand saying uh he was mentored by elon musk loves twitter love selling merchandise loves memes and youtube we'll see what happens here let's go ahead and submit and we see here gpt3 spits out something totally random i'd love to help you but i'm just a bot if you want to see my personal tweets just search for jamie dobrik also created by the amazing david dobrik sorry david don't sue me no idea who jamie dobrik is but it seems gpt-3 is smart enough to pick up this at symbol great now we're ready to export our presets to python what you're going to see here is export code go ahead and copy what we have and let's head back over to visual studio code all right back in vs code let's create a new file we'll call it jbot dot pi and what we're going to do is import the necessary packages we're going to get our open ai key using the dot m package we installed earlier so we have to do here is load.m and this open.api key and we're going to use os.getm and we're going to be getting our open ai key which again is in our dot m file open ai key and actually over here we have it open ai api key so it's our open ai api key set this completion equal to open a i dot completion and then let's paste what we copied over from our exported playground so we have our start sequence our restart sequence and the last thing we want to do is let's take out our session prompt and let's save it into its own variable and we can get rid of this comma and this unnecessary quotation and we'll keep just the prompt open for now as we're going to add something in and what we're going to do now is we're actually going to take this response part here and we're going to put it within a function the ask function that's going to take in a question so let's say define ask which takes in a question it also is going to take in a chat log which will set equal to none to start great all of these again were pasted from the exported code and we're going to add in a prompt text variable variable at the top here and we'll set the prompt equal to the prompt text all right and within this ask the finishing touches are to access the actual text nested within the request body and we can get that by going into our response which is going to look like this json blurb where we got to go into choices take the first row and then we want to access this text which again is is coming from the open uh ai api call and uh every good python function needs a good return statement so let's return um the story but let's make sure we get that in a string format and lastly where the chat log comes in is is we're basically creating another function append interaction to chat log that will help our bot to remember now it's time to use our flask framework so we're going to create a new file app.py and copy pasta in this code okay so what's happening here to empower our bot to respond in real time via the twilio sms api we need to use a web hook and the web hook is what will tell twilio there is an incoming message we're going to format it using flash this is the basic flask formatting where our endpoint is going to be jbot it's going to be a post request which means we're going to be including some information and really within here we're just going to use our chat log which is kind of getting the chat log we have our answer which is using the ask function to get an incoming message and then we are going to append back to that chat log to update it and then really getting that down into just a message that's going to be passed back and forth via text through the web hook to our bot and back to our phone we'll configure twilio to let it know about our flask endpoint and then it can start receiving incoming text messages uh which we're routing in here again to the gbt3 api and eventually sending it back via twilio now if you named anything different just pay attention to whatever you swapped out this is just the jabe function call but the endpoint is really what's going to be important keep in mind these are all of the necessary modules to import coming from both twilio and we're actually importing from our jbot file the ask and the append interaction to chat log function so from here we're going to sprinkle on some twilio messaging magic and we're really just returning a string that that we'll eventually use uh to to pass this back to our chatbot the final few parts of this tutorial are going to jump around a bit but it will make sense at the end so great job keeping up so far let's save and head back to our github repo now since this was a solo project and a rather small one we've been on the main branch and what we want to do now is upload all of our profiles to the github directory and so we'll name it gbt3 jbot just our chatbot app not a part of an organization we're going to keep it private because render will still be able to access even our private repo go ahead and publish the repository thinking thinking thinking looks like we got a commit first so first commit code to set up our gpt3 chat bot we'll commit it and then let's push it to origin and now if we go back to github say refresh we'll see we have our app script our jbot script and none of the other dot m or vem folders have been uploaded because those were in our git ignore uh this is where a lot of folks will instruct you to use aws or even heroku which i've heard has has made a lot of improvements we're going to use render because i found it super easy it's somewhat costly but easy so first thing you need to do is sign up for a free render account we're going to log into ours at the top of the page click new and select a web service and we're going to select the one that we just created here and render has solid documentation for how to deploy a flask app basically what we're going to do is we're going to give it a name gpt 3 chat bot it's going to be python3 choose kind of the nearest area to you it's gonna be on our main branch and then the build command will be pip install requirements okay so to do this we are gonna head back to our terminal and you'll notice that the build is going to pull from our requirements uh file but we haven't done yet is actually saved our requirements file so let's actually do pip freeze requirements dot txt and we're going to run that and now if we were to go to our vs code you'll see we you'll see we have a requirements txt file that has everything we need for the build to run our chatbot so let's head back over to render and here you see we will install with this pip install r our requirements at txt and what we want to call it here is unicorn app app because we had our flask name was the app and we'll choose the seven dollars a month which is our current and go ahead and create the web service you'll see it's cloning in from our github it's checking out the commit and it's downloading the cache looks like the build failed let's go to our github desktop and i think we're going to have to created requirements added requirements dot txt let's push this to origin lovely let's go back to render and let's try and manually deploy again and this time hopefully it works because we actually have our requirements in our github repo downloading downloading running looks like we've made it farther than we did last time and build was successful great let's head over to twilio okay why don't you head on over to twilio step one sign up for twilio step two buy a phone number step three add a small bounce to your number once you've completed those steps meet me here on the active numbers dashboard and what we're gonna do is we're gonna configure twilio messaging to use a web hook and that web hook really what we're going to do is actually go to render and get this url and we're going to go ahead and paste it here and the one thing to remember if we go back to vs code in our flask app we have this endpoint slash jbot so if we go back to our render url we got to add our jbot because that's what we're going to be posting the message to through flask and again the http post uh great let's go ahead and save this and now what we're ready to do is fire up our phone and let's send a text to our bob [Music] and there you have it a working gpt3 chat bot feel free to share your bot text number twilio text number with friends and have them send funny screen grabs of any conversations they have with your jbot thanks for joining me on this journey tutorial and i look forward to seeing you again in the near future this is jabe signing off
Info
Channel: Learn With Jabe
Views: 12,965
Rating: 4.9139786 out of 5
Keywords:
Id: C-8sF81k7cY
Channel Id: undefined
Length: 17min 18sec (1038 seconds)
Published: Sun Feb 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.