ChatGPT in Python for Beginners – Build Custom Chatbots

ChatGPT in Python for Beginners - Build Custom Chatbots

Looking to get more out of ChatGPT? Get my free E-Book:

In this video, I’ll teach you how to create your very own ChatGPT chatbot from scratch, even if you’re a beginner. I’ll demonstrate three different versions of the chatbot, starting with the simplest one. All you need to follow along are Visual Studio Code, Python, and an OpenAI account with a secret key. Then, I’ll show you how to build a ChatGPT-powered chatbot that can remember previous conversations and even how to test it out. In the final part of the video, I’ll introduce a new tool that allows you to set up your chatbot in a browser and share it with others. If you would like to see more videos like this, leave me a comment below!

VS Code IDE:
API Keys:
My Code:


0:00 Step By Step
0:38 Basics
07:01 Next Level
10:11 Customize

#chatgptapi #chatgpt #ai


🤯 E-Book with 400+ ChatGPT Use Cases:
🌱 YouTube Membership:
💬 Discord:
🐦 Twitter:
📸 Instagram:
💃 TikTok:


The Website, E-Mail, and E-Commerce solution I use:

My Video Equipment:
📷 Camera:
🔍 Lens:
🎤 Mic:
🔊 Audio Mixer:
💡 Light:

Today we'll be building a custom chat GPT chatbot that is extremely easy to Make and not just that will 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 chat GPT 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 never touch code Before but really wanted to try this out This is the video for you alright let's Start by making the simplest chat GPT Powered application you can build Alright and to start out you'll need Three things first of all you need 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 open AI you can simply get that by creating An open AI 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 zero one 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 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 alright 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 alright 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 open AI 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 open AI package this is what that looks like In Python okay import open AI a package Is a bunch of complicated tricks that do A lot compressed into two words import Open Ai and with this package we teach Python how to link to our open air Account by using the API key as

Discussed before so that's the second Part by saying openai dot API key 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 gonna Actually ask chat GPT 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 GP T 3.5 turbo Which is the brand new chat GPT 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 open AI provides a so-called documentation Which is essentially 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 open Ai 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 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 will 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 step in chat GPT And that is actually installing this Package and the way you do this by Saying pip install open AI because Obviously not every machine comes with Open AI 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 chat GPT 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 Open AI 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 Open Ai 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 chat gbt 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's two things you must Consider first of all this is 10 times Cheaper than using the playground and Way cheaper and paying 20 bucks a month For chat GPT if you only use this a few Times a week simple answers are gonna 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 alright and the Second app we'll be looking at here is Built by Greg Bosch I hope I'm Pronouncing that correctly Bourget I Think it's French either way he wrote This extremely user-friendly blog post That shows you how to build that chat GPT powered chat bot 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 Open AI 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 of chatbot would you like to Create and we get to type in the Terminal here so we're just gonna say Assassy 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 chat GPT 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 the python Intermediate people a quick explanation On what's going on here would look Something like this we import the open AI 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 gonna 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 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 Open AI 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 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 chat GPT 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 will 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 chat GPT and ask About some of the things that I talked About one by one right and chat gp2 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 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 so as promised with this you're Gonna be able to set up your own chat GPT powered chat bot 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 open AI but here we're Using radio so what you want to do is Either pip install radio because I Already have it it says requirement Already satisfied or you pip 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 Chat about 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 chat GPT

Language model that is up 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 chat GPT 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 chat GPT 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 radio implementation Now all of the things I just said don't Have to make sense to you just copy Paste the script install the open Ai and Radio 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 Chat GPT language model that we just Made it 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'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 one 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 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 Dot 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 chat GPT 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 chat GPT Just be aware it has not yet been Updated to the new chat GPT 3.5 turbo API alright so every time you consulted 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 open AI documentation so 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 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 chat GPT