ChatGPT in Python for Beginners – Build A Chatbot

Today, we’ll be building a custom ChatGPT chatbot that is extremely easy to make. And not just that, we’ll actually make three 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 gonna host this on the web. And all of this is gonna be as beginner-friendly as possible. So if you’ve never touched code before but really wanted to try this out, this is the video for you.

Alright, let’s start by making the simplest ChatGPT powered application you can build. Basics. And to start out, you’ll need three things. First of all, you need an 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 gonna 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 here we’re just gonna 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 used 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 Chat GPT 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 gonna do is drag and drop this folder onto the icon of VS Code. Okay, and right away, it should open up “01_chat_GPT_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 parentheses 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. 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 at adapting to change, as are most of us being early adopters of AI technology. Alright, so let’s talk about this in non-coder terms, okay? There are three parts to this, as you can see. The first one is really simple to understand. Alright, 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 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: “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.api_key =”, 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 a programming tutorial. Just follow what we’re doing here. And now that we’ve activated all the things that OpenAI can do for us here, and we’ve 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 brand new ChatGPT model that is 10 times cheaper, that was 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 an encyclopedia that explains all these things. Because, trust me, if I tell you’re 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. We 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 I 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. 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 open AI’, 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 open AI’. And I have that one tool. 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 Igor, 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 a week, simple answers are gonna cost you a fraction of a cent here. Next Level. And also, now you understand how to go further with this, and that’s all what number two is about, alright? The second app we’ll be looking at here is built by Greg Baugues (I hope I’m pronouncing that correctly, Baugues, 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). 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 gonna 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 gonna 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?” and 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 the 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 our 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 to quit (). 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 Chat GPT 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 inputs 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 – it’s essentially just the answer to the prompt that Chat GPT is giving you. 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 looks back, and we get to enter another input. This way, the conversation keeps going, and it always remembers the history because the list that we feed to the Chat GPT 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 to ask for an input in Python” It would show and explain the exact line that we used in here. Alright, so that’s it.

But now let’s move on to App Number Three, which is the one I’m most excited about. Customize. So, as promised, with this, you’re gonna 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 free install Gradio. Enter. With command K, I clear all this and let’s get right into it. 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. Alright, 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 inputs 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 it happen in seconds, alright? 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’s some tips to help with tiredness: 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=True” with a capital “T”. Another thing you could customize here is “title=Your Title”. So, if we change the text inside the parenthesis to “Digital Psychologist”, we can save this and press play again. Aha, there’s a little issue. The thing with this is, every time I 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 .gradio.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 you will 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 “Your app 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 run 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, alright? 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 10 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.

Privacy Policy | Privacy Policy