ChatGPT in Python for Beginners - Build A Chatbot

Build three ChatGPT chatbots in Python, from a simple one-shot script to a conversational terminal app that keeps message history, to a web interface with Gradio. GPT-3.5 Turbo costs much less than older models, and Gradio lets you share your bot via a live URL.
Chapters
Today we'll be building a custom ChatGPT chatbot that is extremely easy to make. And not just that, we'll actually make free versions. One extremely simple one accessible even to people that have never touched code before. A second one which allows you to customize it and then go back and forth and talk to it. And then finally, I'll show you how to make a custom ChatGPT powered chatbot that you can send to your friend. Because we're going to host this on the web and all of this is going to be as beginner friendly as possible. So if you never touched code before, but really wanted to try this out, this is the video for you. All right, let's start by making the simplest ChatGPT powered application you can build. All right, and to start out, you'll need three things. First of all, you need the ide, which is an app that allows you to code. We'll be using Visual Studio code here because it's free, one of the most popular ones, and it's what I use. It's just a simple download and installation on both Windows and Mac, with many tutorials out there on how to do it. Once you install it, it's going to look something like this and you can head on over to Extensions. And this is the second thing you need. You're going to need to install Python on your machine. So we're going to look for Python and then we're just going to click Install that is going to install the newest version of Python on your machine. And we're almost ready to do this. And the last thing you'll need to prepare is the secret key from OpenAI. You can simply get that by creating an OpenAI account, which I showed you how to do in this two minute video. It's the same account you use to log into ChatGPT. And by going to this URL we can simply click plus. And for the sake of the video, we'll be creating this API key super briefly. An API is just a simple way for two applications to interact. Essentially, it's going to allow us to plug the ChatGPT language model into our app with this key. So let's copy it and head on over to VS Code. Okay. Actually, there's one more step, and that is that you'll need to download the folder that I provide for you to follow along. For this video. You can find a link to get this in the description below. And for anybody who's a little more familiar with all this, you can go to my brand new GitHub account that we'll be using for this channel and get it from there. But for beginners, the easiest way is just to download this folder and then all you're going to do is drag and drop this folder onto the icon of VS code. Okay? And right away it should open up 01chatgptc simple. And this is going to be our first application. And as the name implies, it's going to be extremely simple. First things first, inside of the quotation marks right here, you will want to post your API key. Nice. And let me just note here, there's a lot of different ways of doing some of the things I'll be showing you in this video, all right? But as this is a beginner tutorial, the focus here is on simplicity. And that's it. This is the whole application. So let me explain. But essentially this is going to allow you to go in and prompt ChatGPT to write an essay about penguins, one of our favorite prompts on this channel. Because those little guys are really good to adapting to change, as are most of us being early adopters of AI technology. All right, so let's talk about this in non coder terms. Okay? There's three parts to this, as you can see. The first one is really simple to understand. All right? OpenAI, the company behind ChatGPT, created a so called package that allows us to really easily implement all this. Okay? As you can see, if I remove these empty lines, it's fine. Four lines of code couldn't get any simpler. And that's because we imported the OpenAI package. This is what that looks like in python. Okay? Import OpenAI a package is a bunch of complicated tricks that do a lot compressed into two words. Import OpenAI and with this package we teach Python how to link to our OpenAI account by using the API key as discussed before. So that's the second part. By saying OpenAI apikey equals. And then if you put something into parentheses in Python you tell the program this is a string. That's programming lingo for a word with all kinds of different signs in it. If it was just whole numbers, we could call this integer. But again, this is not going to be programming tutorial. Just follow what we're doing here. And now that we activated all the things that OpenAI can do for us here, and we linked our OpenAI account to this little script we're dealing with here. Next up, we're going to actually ask ChatGPT something. And if you're a beginner, you don't really have to understand what's going on here? Just know we're calling the GPT 3.5 Turbo, which is the new ChatGPT model that is 10 times cheaper, that has been released last week, and we're sending a message as a user that says write an essay about penguins. And again, you don't need to know these things by heart, neither do you need to understand them to get started. But if you want to, OpenAI provides a so called documentation, which is essentially encyclopedia that explains all these things, because trust me, if I tell you, not a single person has been born with the ability to implement OpenAI APIs. And even the most advanced coders that have never dealt with this before will go into here and read up on how it's done, and I would recommend you do the same. This is exactly where you get a better understanding of what's going on. Again, link below. But now let's complete this app and let's try and run this. But before we do that, we need to address one more thing in here, which is this print command. And if you start learning coding, the first thing they will always teach you is a print command. Every language has this, and it's essentially just a way to display some text in the terminal, which we'll be entering this very second. Now, I could do two things. I could press the play button here, run Python file, or I could go up to terminal, new terminal, and the terminal opens up for us. And before we run this, there's one last step to completing your first app in ChatGPT, and that is actually installing this package. And the way you do this is by saying pip install OpenAI, because obviously not every machine comes with OpenAI plugins pre installed. You have to do it first. And that's usually the case with all the things that we import up here. Luckily, you only have to do this once. And every time you run a new app that uses the OpenAI package, it will be able to install the app. So because I already have this, it says requirement already satisfied and I can move forward. Alternatively, if you run into problems, simply put, you can also do PIP free install OpenAI. And I have that one too. And now for real, we're actually set up, everything is ready to go. I'll just delete everything in the terminal by pressing Command K just to make this look nicer. And now we can do this and ask ChatGPT to write an essay about penguins by pressing play. As per usual with ChatGPT, it takes a few seconds and there you go. We did it. Penguins are considered as one of the most fascinating creatures on the planet, yada yada yada. It worked. So what happened here is we connected the script to our OpenAI account. Then we asked the GPT 3.5 Turbo model to give us a reply to the prompt, Write an essay about penguins. And by including this line that prints the response from the GPT 3.5 Turbo model, it was actually shown in the terminal. Now, what you could do next is change the prompt to, let's say, give me free ideas for apps I could build with OpenAI APIs. And again, if I press play, this is almost identical to using ChatGPT in the browser. Number one, personalized health assistant. Number two, personal finance assistant. Number three, personalized education platform with some explanations. And now you could keep using this just like ChatGPT. But at this point you might be asking yourself, hey Gor, what's the point of this? Why use Python inside of VS code and bother with the API key and change it in here instead of the browser? And that's a fair question, but there's two things you must consider. First of all, this is 10 times cheaper than using the playground and way cheaper than paying 20 bucks a month for ChatGPT. If you only use this a few times times a week, simple answers are going to cost you a fraction of a cent here. And also now you understand how to go further with this. And that's all what number two is about. All right. And the second app we'll be looking at here is built by Greg Bosch. I hope I'm pronouncing that correctly. Bourge. I think it's French. Either way, he wrote this extremely user friendly blog post that shows you how to build that ChatGPT powered chatbot where you can actually go back and forth and it remembers your answer, as opposed to what we just built, which is one prompt in one answer out in just 16 lines of code. Actually 14 if you remove the two spaces and this goes a little deeper. And explaining all this is a little beyond the scope of this tutorial, but if you read through this blog post and you carefully consult the documentation from OpenAI here, I'm confident that you'll be able to figure all this out if you have some Python basics. And again, all we need to do here is copy over our API key. Now all we have to do is press play and it's going to ask us a question, because that's what this is set up to do. What type of chatbot would you like to create? And we get to type in the Terminal here. So we're just going to say a sassy and tired personal assistant and hit Enter. It replies with your new assistant is ready. Alright, now we can prompt this custom sassy and tired personal assistant, ChatGPT, to do anything that ChatGPT could do. Let's just say, how can I start learning Python today? Enter. And instead of giving us a classic, helpful and neutral step by step tutorial, we'll get. Well, darling, it's great to hear that you're interested in learning Python. Here are a few ways to get started for all you Python intermediate people. A quick explanation on what's going on here would look something like this. We import the OpenAI package and then link our account and get access by inserting our custom API key. We create a list that is called messages and this is what the API wants to receive. You always need to send it a list as specified in the documentation. Then we're going to set up a system message variable that takes an input, and then we're going to take the user's answer to this and append it to the messages variable in exactly the format that OpenAI is asking for. A simple print statement tells the user that the new assistant is ready. And to round everything out, we're going to run one while loop that runs until we tell it quit. Open close brackets. And all the magic here happens inside the while loop. We take a new input for the user's message, obviously, and again we append that to the messages list. Then we start Talking to the ChatGPT 3.5 Turbo API and as mentioned before, we feed it the messages variable with the list that has been expanded with both the first input that happened previously and the new input that happened just now. Then we create a brand new variable that saves the response that we just got back from the OpenAI API. This part is just copy pasted from the documentation, essentially just the answer to the prompt that ChatGPT is giving you. And then finally we append the reply that we just got from ChatGPT to the list. So once the conversation continues, it remembers what we just talked about in the very last step. We just print the reply so the user knows what ChatGPT just replied. And now it loops back and we get to enter another input. This way the conversation keeps going and it always remembers the history because the list we feed to the ChatGPT API gets longer and longer with every single time we type a prompt into the terminal. Alright, so I hope that made sense. If not, you can always go into ChatGPT and ask about some of the things that I talked about one by one, right? And ChatGPT will explain. So for example, you could say, how do I ask for an input in Python? And it would show and explain the exact line that we used in here. All right, so that's it. But now let's move on to app number three, which is the one I'm most excited about. So, as promised, with this, you're going to be able to set up your own ChatGPT powered chatbot inside of a browser and even share it with a friend, all in this super simple script. So first things first, there's a new package here. Okay, up until now we used OpenAI, but here we're using Gradio. So what you want to do is either pip install Gradio because I already have it, it says requirement already satisfied, or you pip it free install radio enter with command K. I clear all this and let's get right into it. So first of all, again, I'll need my API key, so I'll just go ahead and copy paste that into this part where it says API key as we did before. All right, and here we have the first messages variable, and this is where you customize your Chatbot. Okay, so here it says you are a psychologist. So let's just go with this to begin with. And that means this is going to be a custom ChatGPT language model that is a psychologist. Next up, we're going to be defining a function. And explaining what a function is to a beginner goes beyond the scope of this tutorial. But again, you can always just go into ChatGPT and ask what is a function in Python? As with any other part of this code, and it will help you out. And inside of the function we're again going to be consulting the ChatGPT API and it's going to use the user input that this function is based upon to generate a prompt reply, which it will feed back into Gradio. And down here we have the simplest Gradio implementation. Now, all of the things I just said don't have to make sense to you. Just copy, paste the script, install the OpenAI and Gradio package. So let's just hit play here and see what happens. As I warned you, this is extremely simple. All you have to do is copy this link HTTP and then this IP address. And now what we get to do is open up a web browser and paste this. Voila. That is your custom web based ChatGPT language model that we just made happen in seconds. All right, and as we set this thing up to be a psychologist, we get to ask it. Let's just say how do I deal with tiredness? And press submit. Here are some tips to help with get enough sleep, practice good sleep, hygiene, exercise regularly, etc. And the cool thing about this is if I go ahead and say now write me a tweet about your qualifications and my problems. You'll immediately see that it remembers our entire conversation. As a qualified psychologist, I can help you tackle issues related to mental health and well being, including dealing with tiredness, etc. Isn't that just great? So I think this is really exciting because this is extremely convenient and you could immediately show this off to anyone you know. And the way you do that is by going into the brackets in between launch and saying share equals true with a capital T. Another thing you could customize here is title equals your titles. So if we change the text inside the quotation marks to Digital Psychologist, we can save this and press play again. Aha. There's a little issue. The thing with this is every time we want to update it, we would need to include another part of the code which makes it a little more complicated. The easiest way is just quitting the app every time we update something and opening it back up. And now if I press play, it provides me with the local URL as before, but also with one that ends with Radio live. And if I copy this one and I head on over to my phone, right, Which I will disconnect from my local network and I'll just paste this. You'll see, we'll get the brand new Digital Psychologist app right on our phone. Incredible. Right? And now it's infinitely customizable because if you go up here and change URI Psychologist to let's say you are a financial expert that specializes in real estate investment and negotiation. And I'll change the title to Real Estate Pro. We just built a ChatGPT web app in no time. Okay, but what if you ran into problems somewhere along the way? Well, as I said, you can always consult ChatGPT. Just be aware it has not yet been updated to the new ChatGPT 3.5 Turbo API. All right, so every time you consult it on how to implement the API, it's going to be talking to you about the old DaVinci GPT API, which is still great. And some people argue that it's better for certain use cases, but it's ten times more expensive. So at the end of the day, your most reliable source of truth is always going to be the OpenAI documentation. So we did it. And now I really want to hear from you guys. Do you want more coding tutorials like this? If so, leave a comment below and I'll start making more guides and live streams like this one. Alright, so if you enjoyed this, check out this video which will take you even further along on the journey of doing even more with ChatGPT.










