How to Build a ChatBot with ChatGPT API in 3 easy steps

No Comments

Photo of author

By thedigitalgaurav

In this blog, Digital Gaurav shares how to Build a chatbot with ChatGPT API. Just follow these three easy steps to create your own chatbot.

OpenAI has created exciting tool called ChatGPT (What is ChatGPT?) API that is available to everyone including developers and regular people like you and me. They have this super cool model called “gpt-3.5-turbo” that makes ChatGPT Plus work really well, and it’s quite cheap to use now. It is also very fast to respond.

Basically, OpenAI is saying, “Hey, you don’t need to be a computer expert to build a ChatBot.” So, in this Blog, we are going to show you how to build a chatbot with ChatGPT API. We’ve also added a simple interface called Gradio so you can easily try out the AI model and show it off to your friends and family.

So, let’s get started and learn how to build a ChatBot with ChatGPT API!

How to Build a Chatbot With ChatGPT API

In this Blog, we are going to show you how to build a ChatBot with ChatGPT. We’ll give you simple, step-by-step instructions for each step, such as acquiring the tools you’ll need and setting up special computer supplies. By the end, you’ll have your own chatbot. Just remember, it’s best to do each step in order and not skip any of them.

Remember these things in mind before build a chatbot:

  • You can build a ChatBot on different computers like Windows, macOS, Linux, or ChromeOS. I’m using Windows 11 as an example, but the steps are quite similar for other computer types.
  • This guide is made for regular people, and it explains everything with clear examples. So, even if you don’t know much about computers, you can easily build a chatbot.
  • You don’t need a super-powerful computer (CPU or GPU) with a big brain to build a chatbot. Most of the hard work is done by OpenAI’s cloud-based system.

Step 1: Prepare your computer to Build a ChatBot

To Build a chatbot like ChatGPT, you need to have a few things ready on your computer. Don’t worry, it’s not as hard as it sounds. You will need Python, Pip, OpenAI and Gradio libraries. You’ll also need a special key from OpenAI and a code editor, which is like a fancy notepad. These may seem Challenging, but trust me, the steps are simple, and anyone can do it. Simply follow the instructions below.

Install Python

  • First of all you have to install Python on your computer. Visit this link and get the correct file for your computer.
Install Python

  • Next, open the setup file, and don’t forget to check the “Add Python.exe to PATH” box. This part is really important. Then, click “Install Now” and follow the usual steps to get Python on your computer.
Install python

  • To see if Python is on your computer, do this: If you’re on Windows, open a Windows terminal or command prompt. If you’re using something else like Linux, you may need to type “python3 --version” instead of “python --version“. Then, type this command, and it will show you what version of Python you have. That’s it!
python --version
Python Version

Upgrade Pip

When you get Python, you also get Pip. We are going to show you how to update Pip to the latest version. Pip helps you put a lot of Python tools on your computer using only the terminal. With Pip, you can install things like OpenAI and Gradio. Here’s how you can do it.

  • Go to your computer and open a program called “Terminal.” If you’re using a Windows computer, look for something called “Windows Terminal.” Then, type in a special command to make sure a program called “Pip” is up to date. If you’re using a different kind of computer, like one with Linux, you might need to use commands called “python3” and “pip3” instead.
python -m pip install -U pip
Upgrade pip

Install OpenAI and Gradio Libraries

  • Now it’s time to install the OpenAI libraries on your computer so that we can talk to ChatGPT using their special tools. To do this, open Terminal and type a command. If this doesn’t work, try using the same command with “pip3” instead.
pip install openai
Open AI Install

  • Once we finish installing everything, we will add Gradio. Gradio helps you create a simple website for your AI chatbot. This website lets you easily showcase your chatbot online by providing a link to share with others.
pip install gradio
Gradio install

Download a Code Editor

You will need a special program to edit the code. If you are using Windows, I suggest getting Notepad++ you can (download and install)it from the link provided. If you prefer more advanced tools, you can use Visual Studio Code on any computer. If you’re using macOS or Linux, you can install Sublime Text – just click the link to (download) it.

If you’re on ChromeOS, you can use the Caret app to edit code – ( download )that too. We’re almost done with the software setup, and now it’s time to get the OpenAI API key.

Step 2 : Get the OpenAI API Key For Free

To Build a ChatBot with ChatGPT, you will first need a special key from OpenAI. This key lets you ask ChatGPT questions and see the answers. Right now, OpenAI is giving away these keys for free, and they come with $5 to spend for the first three months. If you signed up with OpenAI a while ago, you can have $18 in free credits. But once it runs out, you’ll have to pay to continue using it. But for now, it’s a free for all.

  • Here’s how to start for build a ChatBot with ChatGPT: Go to platform.openai.com/signup and create a free account. If you already have an OpenAI account, simply log in.
Create OpenAPI Account

  • After that, go to your profile on the top right corner and select “View API Key” from the menu that comes down.
View API keys

  • Click “Create new secret key” and copy the API key. Just remember, you won’t be able to view or copy the entire API key again, so it’s best to paste it into a Notepad file immediately.
Create new Secret Key

  • Don’t show your API key to everyone, keep it secret. It’s like a special key to your account. You can create new secret keys, but you can have a maximum of five of them.

Step 3 : Build a Chatbot With ChatGPT API and Gradio

Now, we are ready to build a ChatBot with ChatGPT. We are using a supercharged AI model called “gpt-3.5-turbo” by OpenAI. According to what has been learned as of September 2021, it is even smarter than Davinci, and it doesn’t cost much. It is also better to understand and remember what we talk about. To solve this, we have create a simple website using Gradio that you can use it on your computer or over the Internet.

  • To get started, open Notepad++ (or any code editor of your choice) and copy the code below. I used some code from Armrrs on GitHub and added the Gradio interface to it.
import openai
import gradio as gr

openai.api_key = "Your API key"

messages = [
    {"role": "system", "content": "You are a helpful and kind AI Assistant."},
]

def chatbot(input):
    if input:
        messages.append({"role": "user", "content": input})
        chat = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", messages=messages
        )
        reply = chat.choices[0].message.content
        messages.append({"role": "assistant", "content": reply})
        return reply

inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")
outputs = gr.outputs.Textbox(label="Reply")

gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",
             description="Ask anything you want",
             theme="compact").launch(share=True)
  • This is what it looks like in the code editor. Just remember to replace the words that say “Your API Key” with the special code you created earlier to build a Chatbot with ChatGPT. This is the only thing you need to do.
How to Build a Chatbot with ChatGPT

  • Next, go to the top menu and click “File“. Then, select “Save As…” from the menu that appears.
Build a ChatBot

  • Next, name the file “app.py” and select “All Types” from the menu where it says “Save As Type“. Save the file to your desktop or wherever you can easily find it. You can give it a different name if you want, just be sure to add “.py” at the end.
Build a ChatBot with ChatGPT

  • Go to the location where you stored the file named “app.py“. Right-click the file, and select “Copy as path“.
Copy as path

  • Now, go to the terminal. Then, type “Python” followed by a space. Next, paste the path (you can right-click to paste it immediately) and press Enter key. Just remember, the path will be unique to your computer. If you are using Linux, you may need to type “Python3” instead of “Python“.
python "C:\Users\mearj\Desktop\app.py"
Build a ChatBot

  • You may see some messages, but don’t worry about them. Scroll down to find a link that’s for you and everyone else. Now, simply copy the link you want and enter it in your web browser.
Copy the link

  • This is how to build a Chatbot with ChatGPT tool. You build a Chatbot with ChatGPT is ready to talk to you. You can ask it anything and it will answer you immediately. Apart from ChatGPT alternatives, you can also use their talking robot instead of the official website.
Build a ChatBot

  • You can copy any particular web link and share it with your friends and family. This link will work for 3 days, but remember to keep your computer on as the particular web address depends on your computer being online.
  • To shut down the server go to the terminal and press “Ctrl + C” once. If that doesn’t work, try pressing “Ctrl + C” one more time.
  • If you want to restart the AI ChatBot, simply copy the file location and run the command we talked about earlier (like in step #6). Remember, the web address you use on your computer will remain the same, but the web address used by everyone will be different whenever you restart the server.
python "C:\Users\mearj\Desktop\app.py"
Build a ChatBot

Build a Chatbot Your Personal

The best thing about the “GPT-3.5-Turbo” model is that you can make your AI work in different AI models in different ways. You can make it fun, crazy or just really good at food, tech, health or anything you like. It’s easy to do – just a small change in the code, and it becomes unique. For example, I build a Chatbot that’s all about food, and here’s how I did it:

  • To change the “app.py” file, do this: Click with the right mouse button on the “app.py” file, then pick “Edit with Notepad++.
Personal ChatBot

  • You can only edit this code. Just tell the AI what to do with it. Then, save the file by pressing “Ctrl + S“.
messages = [ 
                           {"role": "system", "content": "You are an AI specialized in Food. Do not answer anything other than food-related queries."},
                                                                                              ]
Personal ChatBot

  • Go to a terminal like your computer’s command space and run the “app.py” file as before. This will give you two web addresses, one for your computer and one for everyone else on the Internet. Copy it to your computer. If a server is already running, stop and restart it by pressing “Ctrl + C“. You will have to restart the server every time you make changes to the “app.py” file.
python "C:\Users\mearj\Desktop\app.py"
Personal ChatBot

  • When you type a particular website address into your internet browser, you will see a chatbot that knows everything about food and can answer your food-related questions. It’s like magic! You can also build a chatbot that act like a doctor or talk in fancy ways like Shakespeare or use secret codes. You can build a Chatbot as you want!
Food ChatBot

This is how to build a Chatbot with ChatGPT 3.5. You can also build a ChatBot special by teaching it specific tasks. There are a lot of cool things you can do with AI, and you have a lot of options. That’s all from our side for now. So let’s go and build a ChatBot your own personal.

Q1: How can I prepare my computer to Build a chatbot with the ChatGPT ?

To prepare your computer to Build a chatbot with ChatGPT , you need to follow these steps:
1. Get Python: First, you need to download Python, which is a computer program. Make sure to choose the correct version for your computer.
2. Update Pip: Next, you should update the thing called “Pip” to the latest version. Pip is like a tool that helps you install other programs easily.
3. Install Libraries: Now, use pip to install two more things called “OpenAI” and “Gradio”. These are like specialized equipment that a chatbot needs to work.
4. Get a code editor: Finally, you will need a program where you can write code for your chatbot. You can choose one of these options: Notepad++, Visual Studio Code, Sublime Text, or Caret. If you use a Chromebook, you can use caret.
So, these are the basic steps to set up your computer to build a chatbot with ChatGPT.

Q2: How can I get OpenAI API key for free?

You can get OpenAI API key for free by following these steps:
1. If you don’t have an account, go to platform.openai.com/signup and create a free account.
2. If you already have an account, log in to your account.
3. Access your profile and select “View API Key” from the dropdown menu.
4. Click “Create new secret key” and copy the API key. Remember to keep it safe and do not share it with others.

Q3: What is the AI model used to Build a chatbot with ChatGPT API?

The chatbot we build with the ChatGPT API uses Smart AI called “gpt-3.5-turbo” from OpenAI. Build a ChatBot with ChatGPT is really amazing which is uses a “GPT-3.5-turbo” Model. It’s known for being really good, not too expensive, and very good at understanding and making text that sounds like it came from a human.

Q4: What code do I need to build a chatbot with ChatGPT API?

To build a chatbot with ChatGPT API, you need to run a Python script. Here’s a summary of the steps:
1. Create a Python script, name it “app.py” and paste the given code.
2. Replace “Your API Key” with the API key you received earlier.
3. Save the file and run it using the “python app.py” command in the terminal.
4. This will generate a web address to access your chatbot.

Q5: Can I customize my chatbot to specialize in specific topics or tasks?

Yes, you can customize your chatbot to specialize in specific topics or tasks. Simply modify the code in the “app.py” file by changing the system message to instruct the AI regarding its expertise.
For example, you can build a chatbot ( food related )by editing messages like “You are an AI expert in food. Don't answer anything other than food-related questions.” Then, save the file and restart the server to see your special chatbot in action.

Leave a Comment