00:00Today we'll be building a custom ChatGPT chatbot that is extremely easy to make
00:05Not only that, but we'll also create a free version—one that is very simple and accessible even to people who have never touched code before
00:12 We'll develop a second version that allows you to customize it and interact with it back and forth
00:16Finally, I'll show you how to make a custom ChatGPT-powered chatbot that you can send to your friends, because we're going to host this on the web
00:24 All of this is going to be as beginner-friendly as possible, so if you've never touched code before but really want to try this out, this is the video for you
00:32 Alright, let's start by making the simplest ChatGPT-powered application you can build
00:34To get started, you'll need..
00:36First of all, you need an IDE, which is an app that allows you to code
00:41We'll be using Visual Studio Code here because it's free, one of the most popular ones, and it's what I use
00:46It involves a simple download and installation on both Windows and Mac, with many tutorials available on how to do it
00:51Once you install it, it will look something like this
00:53You can head on over to Extensions
00:55 This is the second thing you need: you'll need to install Python on your machine
00:58Search for Python, and then click Install
01:00This will install the newest version of Python on your machine
01:03We're almost ready to do this
01:07 The last thing you'll need to prepare is the secret key from OpenAI
01:10You can simply get that by creating an OpenAI account, which I showed you how to do in this two-minute video
01:16It's the same account you used to log into ChatGPT
01:17By going to this URL, we can simply click plus, and for the sake of the video, we'll be creating this API key
01:23Super 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
01:34So, let's copy it and head on over to VS Code
01:37Okay, 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
01:42You can find a link to get this in the description below
01:46And 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
01:52But 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 Visual Studio Code
01:59Okay, and right away, it should open up—"ChatGPT Simple"—and this is going to be our first application
02:07As the name implies, it's going to be extremely simple
02:09First things first, inside of the quotation marks right here, you will want to post your API key
02:13Nice
02:17Let me just note here that there are many different ways of doing some of the things I'll be showing you in this video
02:21All right, but as this is a beginner tutorial, the focus here is on simplicity, and that's it
02:23This is the whole..
02:26Application, so let me explain
02:28But 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
02:42Alright, so let's talk about this in non-coder terms
02:45Okay, there are three parts to this, as you can see
02:49The first one is really simple to understand
02:53All right, OpenAI, the company behind ChatGPT, created a so-called package that allows us to really easily implement all this
02:58Okay, as you can see, if I remove these empty lines, it's four lines of code
03:02It couldn't get any simpler, and that's because we imported the OpenAI package
03:05This is what that It looks like in Python, okay, import OpenAI—a package that contains a bunch of complicated tricks that do a lot, compressed into two words: import openai
03:14With this package, we teach Python how to link to our OpenAI account by using the API key, as discussed before
03:21So, that's the second part: by saying openai.api_key equals, and then, if you put something into parentheses in Python, you tell the program this is a string—programming lingo for a word with all kinds of different signs in it
03:35If it was just whole numbers, we could call this an integer, but again, this is not going to be a programming tutorial; just follow what we're doing here
03:41And now that we activated all the things that OpenAI can do for us and linked our OpenAI account to it..
03:47This little script we're dealing with here
03:50Next 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
03:55Just know we're calling the GPT-3.5 Turbo, which is the brand new ChatGPT model that is 10 times cheaper and was released last week
04:04We're sending a message as a user that says, "Write an essay about penguins." Again, you don't need to know these things by heart, nor do you need to understand them to get started
04:13But if you want, OpenAI provides a so-called documentation, which is essentially an encyclopedia that explains all these things, because trust me, you're not a single person who was born with the ability to implement OpenAI
04:22APIs, and even the most advanced coders who 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
04:31This is exactly where you get a better understanding of what's going on, again, link below
04:34But now, let's complete this app and try to run it
04:36 Before I do that, we need to address one more thing, which is this print command
04:40If you start learning coding, the first thing they will always teach you is a print command
04:46Every language has this, and it's essentially just a way to display some text in the terminal, which we will be entering this very second
04:50I could do two things: I could press the play button here to run the Python file, or I could go up to Terminal and open a new terminal
04:56And the terminal opens up for us, and before we run this, there's one last step to completing your first step with ChatGPT, and that is actually installing this package
05:04The way you do this is by saying `pip install openai`, because obviously not every machine comes with the OpenAI plugins pre-installed
05:10You have to do it first, and that's usually the case with all the things we import up here
05:18Luckily, 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
05:24Since I already have this, it says "Requirement already satisfied," and I can move forward
05:28Alternatively, if you run into problems, you can also do `pip install --upgrade openai`, and I have that one tool, and now..
05:34For real, we’ve actually set up everything, and it's ready to go
05:38I'll just delete everything in the terminal by pressing Command + K, just to make this look nicer
05:42Now we can do this and ask ChatGPT to write an essay about penguins by pressing play
05:45As per usual with ChatGPT, it takes a few seconds, and there you go
05:49We did it! Penguins are considered one of the most fascinating creatures on the planet, yada yada yada
05:57It worked
06:05 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 displayed in the terminal
06:14Now, what you could do next is..
06:16Change the prompt to, let's say, give me free ideas for apps I could build with OpenAI APIs
06:20And again, if I press play, this is almost identical to using ChatGPT in the browser
06:24Number one, personalized health assistant
06:27Number two, personal finance assistant
06:29Number three, personalized education platform with some explanations
06:31And now you could keep using this just like ChatGPT, but at this point, you might be asking yourself, "Hagar, 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 are two things you must consider
06:47First of all, this is ten times cheaper than using the Playground, and way cheaper than paying 20 bucks a month
06:52For ChatGPT, if you only use this a few times a week, simple answers are going to cost you a fraction of a cent
06:58Here, now you also understand how to go further with this, and that's what the second part is about
07:01Alright, and the second app we'll be looking at here is built by Greg Borsch—I hope I'm pronouncing that correctly
07:07Bourget, I think he's French; either way, he wrote this extremely user-friendly blog post that shows you how to build a ChatGPT-powered chatbot where you can actually go back and forth, and it remembers your answers, as opposed to what we just built, which is one prompt in and one answer out, in just 16 lines of code—actually 14 if you remove the two spaces
07:26This goes a little deeper, and explaining all this is a little..
07:28Beyond the scope of this tutorial, but if you read through this blog post and carefully consult the documentation from OpenAI, I'm confident that you'll be able to figure all this out if you have some Python basics
07:40And again, all we need to do here is copy over our API key
07:44Now, 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
07:50What type of chatbot would you like to create? We get to type in the terminal here, so we're just going to say "Sassy and tired personal assistant," and hit enter
07:57It 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
08:06Let's just say, "How can I..." Start learning Python today
08:11Instead 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
08:20Here are a few ways to get started
08:22For all the intermediate Python people, a quick explanation of what's going on here would look something like this: we import the OpenAI package, then link our account and get access by inserting our custom API key
08:32We create a list called messages, and this is what the API wants to receive; you always need to send it a list, as specified in the documentation
08:40Then 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
08:44Append it to the messages variable in exactly the format that OpenAI is asking for
08:47A 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
08:56Open, close brackets, and all the magic here happens inside the while loop
08:58We take a new input for the user's message, obviously, and again, we append that to the messages list
09:04Then 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 previous inputs and the new input that just happened
09:14Then we create a brand new variable that saves the response we just received
09:20Back from the OpenAI API, this part is just copy-pasted from the documentation
09:24It's essentially just the answer to the prompt that ChatGPT is giving you, and then finally we append the reply that we just received from ChatGPT to the list
09:33So, once the conversation continues, it remembers what we just talked about
09:35In the very last step, we just print the reply so the user knows what ChatGPT just responded with, and now it looks back, and we get to enter another input
09:44This way, the conversation keeps going, and it always remembers the history because the list will feed to the ChatGPT API and gets longer and longer with every single prompt we type into the terminal
09:51Alright, so I hope that made sense
09:53If not, you can always go into ChatGPT and ask about it
09:56Some of the things that I talked about, one by one, right? And ChatGPT-2 will explain, so for example, you could say how to ask for an input in Python, and it would show and explain the exact line that we used here
10:05Alright, so that's it
10:06But now let's move on to app number three, which is the one I'm most excited about
10:10As promised, with this you're going to be able to set up your own ChatGPT-powered chatbot inside a browser and even share it with a friend, all in this super simple script
10:17 First things first, there's a new package here
10:20Up until now, we used OpenAI, but here we're using RiveScript
10:24So, what you want to do is either `pip install RiveScript` — because I already have it, it says requirement already satisfied — or you can run `pip install RiveScript`
10:32With Command K, I clear all this, and let's get right into it
10:33So, first of all, again, I'll need my API key, so I'll just go ahead and copy and paste that into this part where it says API key, as we did before
10:44All right, and here we have the first messages variable, and this is where you customize your chatbot
10:48Okay, 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
10:58Next up, we're going to be defining a function and explaining what a function is to a beginner
11:03Goes 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
11:11And 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
11:20Down here, we have the simplest Gradio implementation
11:22Now, all of the things I just said don't have to make sense to you; just copy and paste the script, install the OpenAI and Gradio packages
11:32So, let's just hit play here and see what happens
11:33As I warned you, this is extremely simple
11:35All you have to do is copy this link (HTTP) and then this IP address
11:39Now, what we get to do is open up a web browser and paste this
11:44Voilá! That is your custom web-based ChatGPT language model that we just made in seconds
11:49All right, and as we set
11:51This thing up to be a psychologist; we get to ask it
11:53Let's just say, "How do I deal with tiredness," and press submit
11:56Here's some tips to help with tiredness: get enough sleep, practice good sleep hygiene, exercise regularly, etc
12:01And 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
12:11I can help you tackle issues related to mental health and well-being, including dealing with tiredness, etc
12:18Isn'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
12:26And the way you do that is by going..
12:29Into the brackets in between launch and saying share equals true with a capital T
12:32Another thing you could customize here is title equals your title, so if we change the text inside the quotation marks to "Digital Psychologist," we can save this and press play again
12:41Ah, there's a little issue
12:42The 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
12:51The easiest way is just quitting the app every time we update something and opening it back up
12:55Now, if I press play, it provides me with the local URL as before, but also with one that ends with .gradio.live
13:00If I copy this one and head over to my phone, right—which I will disconnect from my local..
13:07I will just paste this, and you will see you will get the brand new Digital Psychologist app right on our phone
13:17Incredible, right? And now it's infinitely customizable because if you go up here and change your app from Psychologist to, let's say, a financial expert that specializes in real estate investment and negotiation, and I’ll change the title to 'Real Estate Pro'
13:30We just built a ChatGPT web app in no time
13:34Okay, 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
13:42All right, so every time you consult on how to implement the API, it's going to be talking to you about the old DaVinci GPT API
13:52Still great, and some people argue that it's better for certain use cases, but it's 10 times more expensive
13:59So, at the end of the day, your most reliable source of truth is always going to be the OpenAI documentation
14:02So, it 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
14:11Alright, so if you enjoyed this, check out this video, which will take you even further along the journey of doing even more with ChatGPT.