ChatGPT in Python for Beginners - Build A Chatbot
Summary
TLDRThis tutorial guides beginners through creating three versions of a ChatGPT-powered chatbot using Python and Visual Studio Code. It starts with a simple script that prompts ChatGPT to write an essay, progresses to a customizable chatbot with memory, and culminates in a web-hosted chatbot accessible to others. The video emphasizes ease of use, cost-effectiveness, and the potential for further customization, encouraging viewers to explore coding and AI technology.
Takeaways
- ๐ The tutorial aims to guide beginners through building three versions of a ChatGPT-powered chatbot, varying in complexity and features.
- ๐ ๏ธ To start, you need an IDE like Visual Studio Code, Python installed on your machine, and an API key from OpenAI.
- ๐ The API key from OpenAI is essential for integrating the ChatGPT language model into your application.
- ๐ A starter folder is provided for users to follow along, which can be downloaded from a link in the video description or from the presenter's GitHub account.
- ๐ก The first app is a simple script that prompts ChatGPT to write an essay about penguins, demonstrating the basic use of the OpenAI package in Python.
- ๐ The script includes importing the OpenAI package, setting the API key, and making a request to the GPT-3.5 Turbo model with a user prompt.
- ๐ The second app, based on Greg Baugues' blog post, allows for a back-and-forth conversation with ChatGPT that remembers previous inputs.
- ๐ The conversation history is maintained by appending each new user input and ChatGPT's response to a list that is continually fed into the API.
- ๐ The third app uses Gradio to create a web-based chatbot that can be shared and accessed through a URL, showcasing the potential for customization.
- ๐ง Customization of the chatbot involves editing the script to change the role and responses of the ChatGPT model, such as setting it up as a psychologist.
- ๐ฑ The web-based chatbot can be accessed on different devices, including mobile phones, by sharing the .gradio.live URL.
- โ For troubleshooting and further learning, the presenter suggests consulting the OpenAI documentation and using ChatGPT for specific coding queries.
Q & A
What are the three versions of ChatGPT-powered chatbots discussed in the script?
-The script discusses three versions: 1) An extremely simple version accessible to beginners, 2) A customizable version that allows back-and-forth conversation, and 3) A web-hosted version that can be shared with friends.
What is an IDE and why is Visual Studio Code recommended in the script?
-An IDE is an Integrated Development Environment, an application for coding. Visual Studio Code is recommended because it's free, popular, and the script's author uses it, with many tutorials available for setup.
Why is Python installed on the machine as part of the setup process?
-Python is installed because it's the programming language used to interact with the OpenAI API and build the chatbot applications discussed in the script.
What is the purpose of obtaining a secret key from OpenAI?
-The secret key from OpenAI is used to authenticate and allow the script to access the OpenAI API, enabling the integration of the ChatGPT language model into the applications.
How does the script suggest obtaining the OpenAI secret key?
-The script suggests creating an OpenAI account and generating an API key through a provided URL, which is the same account used to log into ChatGPT.
What is the role of the 'OpenAI' package in the script?
-The 'OpenAI' package is a Python package that simplifies the implementation of the OpenAI API, allowing the script to easily send requests to the ChatGPT model.
What is the significance of the 'print' command in the script?
-The 'print' command is used to display text in the terminal, showing the responses from the ChatGPT model after processing the prompts.
Why is the Gradio package used in the third app?
-The Gradio package is used to create a web-based interface for the chatbot, allowing users to interact with the ChatGPT model through a browser.
How can the chatbot be customized in the third app?
-The chatbot can be customized by modifying the script, particularly the 'messages' variable and the function that interacts with the ChatGPT API, to change the chatbot's role and responses.
What is the advantage of using the script's method over using ChatGPT directly in the browser?
-The script's method is more cost-effective, as it uses the new GPT 3.5 Turbo model which is 10 times cheaper than the playground, and provides a deeper understanding of how to implement and customize the ChatGPT API.
How can the user get help if they encounter problems while following the script?
-Users can consult ChatGPT for help with specific issues, but should refer to the OpenAI documentation for the most accurate and updated information on implementing the API.
Outlines
๐ค Building a Custom ChatGPT Chatbot
The video script introduces a tutorial on creating a custom ChatGPT chatbot in three different versions, catering to beginners and those with coding experience. The first version is extremely simple and requires no coding knowledge. The second version allows customization and conversational interaction. The third version demonstrates how to host a chatbot online. The tutorial emphasizes beginner-friendliness and guides viewers through setting up an IDE, installing Python, obtaining an OpenAI API key, and downloading a starter folder. It also touches on the simplicity of the code involved and encourages viewers to explore OpenAI's documentation for a deeper understanding.
๐ Coding the Simplest Chat GPT Application
This section of the script provides a step-by-step guide to coding the simplest Chat GPT application. It begins with installing necessary packages like OpenAI using pip and setting up the Python environment. The script explains the process of importing the OpenAI package, linking the account with an API key, and using the GPT-3.5 Turbo model to generate text based on a prompt. The tutorial simplifies the coding process by highlighting the use of a print statement to display the AI's response in the terminal. It also suggests changing the prompt to explore different functionalities of the ChatGPT model and emphasizes the cost-effectiveness of this approach compared to using the ChatGPT playground.
๐ Creating an Interactive Chatbot with Gradio
The final part of the script focuses on creating an interactive ChatGPT-powered chatbot using Gradio, which can be accessed through a web browser. The process involves installing the Gradio package, customizing the chatbot's role and responses, and defining a function to interact with the ChatGPT API. The script demonstrates how to launch the chatbot locally and share it online, allowing others to engage with the custom AI model. It also shows how to customize the chatbot's behavior and appearance, and how to update and share the web app with others. The tutorial concludes with troubleshooting advice, directing users to the OpenAI documentation for the most accurate and up-to-date information.
Mindmap
Keywords
๐กChatbot
๐กIDE (Integrated Development Environment)
๐กPython
๐กOpenAI
๐กAPI Key
๐กGradio
๐กCustomization
๐กAPI (Application Programming Interface)
๐กDocumentation
๐กFunction (in programming)
๐กWeb Application
Highlights
Building three versions of a custom ChatGPT chatbot for different skill levels.
The first version is extremely simple and accessible to coding beginners.
The second version allows customization for a chatbot that remembers past interactions.
The third version demonstrates hosting a chatbot on the web for sharing.
Using Visual Studio Code as the IDE for coding the chatbots.
Installing Python and the OpenAI extension in Visual Studio Code.
Obtaining an API key from OpenAI for application integration.
Importing the OpenAI package to link the script with the OpenAI account.
Creating a simple application to prompt ChatGPT to write an essay about penguins.
The importance of understanding the basics of Python for beginners.
Using 'pip install openai' to install the necessary package for the application.
Customizing the chatbot by changing the prompt and observing the responses.
The cost-effectiveness of using the custom chatbot over the playground or subscription.
Exploring a user-friendly blog post for building an interactive ChatGPT chatbot.
Demonstrating a chatbot that takes on a 'sassy and tired personal assistant' persona.
Using Gradio for creating a web-based ChatGPT application.
Customizing the web app to act as a 'Digital Psychologist' and sharing it online.
The ease of re-customizing the web app for different personas, like a 'Real Estate Pro'.
The importance of consulting the OpenAI documentation for reliable API implementation guidance.
Invitation for feedback on whether the audience wants more coding tutorials.
Transcripts
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 Chat GPT powered application you can build.
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.
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 Chat GPT 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.
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.
Browse More Related Video
How to Build a No-Code Text-to-Image Mobile/Web App Using Replit Code Agent
2024ๅนดใใๅงใใChatGPTใฎไฝฟใๆน่ถ ๅ ฅ้
Build AI Apps in 5 Minutes: Dify AI + Docker Setup
How to Learn Cyber Security FAST using ChatGPT as a Beginner: Using ChatGPT To Learn Cybersecurity
I built 10 AI Tools, and am giving it for free!
How to Build Custom AI Chatbots ๐ฅ(No Code)
5.0 / 5 (0 votes)