How to Build Your Own AI Chatbot With ChatGPT API
In this article, I will show you how to build an AI chatbot using the ChatGPT API. I have also implemented a Gradio interface so you can easily create a live AI model demo and share it with your colleagues.
As the rate of technological development continues to skyrocket, more and more people and companies are turning to chatbots to boost their customer service, productivity, and user experience. Nevertheless, creating an AI chatbot from scratch may seem impossible if you aren’t a developer or coder.
For this purpose, the ChatGPT API has been developed; with just rudimentary coding skills, anybody may rapidly and simply build their own AI chatbot. This tutorial will show you how to create an AI chatbot with the ChatGPT API so that you can use this cutting-edge technology to improve productivity.
Table of Contents
Reasons to Create a Chatbot Using Artificial Intelligence
Companies increasingly use AI chatbots to enhance customer service and interaction as technology develops. If you’re thinking of creating your own AI chatbot, why? To begin, an AI chatbot can help you save time and money by performing routine tasks that normally need human assistance. The time saved by having a chatbot handle simple inquiries and requests for help allows your team to concentrate on more complex problems.
Your consumers will always have access to help when they need it, thanks to a chatbot powered by AI. This has the potential to boost happiness and loyalty among existing clients.
Also, creating your own AI chatbot can help you save time and money while increasing client happiness and loyalty. Why not give it a shot and see if it works?
What You’ll Need to Create an AI Chatbot
Before we get into the specifics of creating your own AI chatbot with the ChatGPT API, let’s go through everything you’ll need to get started. While being an expert is unnecessary, having a basic understanding of programming languages such as Python or JavaScript will make the process much easier. You’ll also need a development environment like Visual Studio Code or PyCharm.
The next step is to create an account with ChatGPT API and receive an API key. Using this key, you can communicate with the GPT-3 language model and generate responses for your chatbot. You may also consider adopting a platform like Dialogflow or Botpress to aid with the development process.
We recommend you follow the instructions from top to bottom without skipping any part.
Things to Note Before Building AI ChatBot
- A ChatGPT chatbot can be built on any Operating System; Microsoft Windows, macOS, Linux, etc. I use Windows 10 in this tutorial, but the methods are equivalent to other platforms.
- The guide is intended for general users, and the directions are presented clearly with examples. Hence, even if you only have a basic understanding of computers, you can quickly design your own AI chatbot.
- You don’t need a powerful machine to construct an AI chatbot. OpenAI’s cloud API handles the heavy task.
Set Up the Software Environment to Create an AI Chatbot
You need tools to set up the environment before creating an AI chatbot powered by ChatGPT. You will need Python, Pip, OpenAI, Gradio libraries, an OpenAI API key, and a code editor like Notepad++. All these tools may seem intimidating initially, but the steps are easy and can be deployed by anyone.
Install Python
1. To begin, you must install Python on your computer. Download the setup file for your platform by clicking on this link.
2. Next, run the setup file and tick the “Add Python.exe to PATH” checkbox. This is a really essential stage. Then, click “Install Now” and follow the on-screen instructions to install Python.
3. Open Terminal (Command Prompt) on your PC to see if Python is correctly installed. I’m using Windows Command Prompt. Once there, use the following command to get the Python version. You may need to type “python3 –version” instead of “python –version” on Linux or other systems.
To open the command prompt on Windows 10, click start and type cmd. Then select “Command Prompt” from the list.
python --version
Install or Upgrade Pip.
Pip is installed on your machine during Python installation. Pip is Python’s package manager. It essentially allows you to install thousands of Python libraries from the Terminal. We can use Pip to install the OpenAI and Gradio libraries. This is how we install or upgrade Python packages using Pip.
1. Open the Windows Command Prompt or any Terminal you use on your PC. Now, run the command below to update Pip. Again, you may have to add 3 to Python and Pip on other operating systems.
python -m pip install -U pip
Install Gradio and OpenAI Libraries
1. Install the OpenAI library. The library will allow us to interface with ChatGPT via their API. Run the following line in the Terminal to install the OpenAI library with Pip. If the command does not work, try pip3 instead.
pip install openai
2. Now that we have installed the OpenAi library, let’s install Gradio libraries for Python. Gradio enables you to easily create a user-friendly web interface to demonstrate your AI chatbot. It also makes sharing the chatbot on the internet simple via a shareable URL.
pip install gradio
Install a Code Editor
I will provide you with complete Python code, so you will need a code editor to make some changes. I recommend Notepad++ for Windows (Download), but you can use any text (code) editor. Click here to download and install Notepad++.
We’re almost done configuring the software development environment, and it’s time to obtain the OpenAI API key.
Create an Account on OpenAI and Get a Free API Key
To build a ChatGPT-powered AI chatbot, you’ll need an OpenAI API key. The API key will allow you to call ChatGPT from within your own app and interface and display the results. The OpenAI API key is currently available to all free users.
- Go to platform.openai.com/signup and sign up for a free account. You can also log in if you already have an OpenAI account.
- Then, in the top-right corner, click on your profile and select “See API keys” from the drop-down menu.
- Once you access the API dashboard, click “Create new secret key.” The system will ask you to copy the API to somewhere safe because you can’t reaccess the API after closing the dialog box; please copy the key and paste it into Notepad++ or anywhere you can retrieve it later.
Create Your Own AI Chatbot Using the ChatGPT API and Gradio
Now that you have installed all the necessary development software and obtained an open ai API key, it’s time to put the AI chatbot to work. We’re utilizing OpenAI’s current “gpt-3.5-turbo” model, which drives GPT-3.5, for this chatbot.
We are also using Gradio to build a simple web interface that will be accessible both locally and remotely.
1. First, open Notepad++ (If you prefer a different code editor, open it) and paste the code below. Thanks to Amrrs on GitHub, I adapted his code and added the Gradio interface.
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)
2. After pasting the code, replace the “Your API key” text with your own API key generated at OpenAi in the previous steps. That’s the only change you have to make.
3. Then save it by clicking “File” in the top menu and selecting “Save As…” from the drop-down menu.
4. Then, name the file “app.py” and change “Save as type” to “All types” from the drop-down menu. After that save the file to a convenient location. For this tutorial, I saved the “app.py” in a folder in Drive C called “apps“. However, you can choose any location and edit the name to whatever you like, just make sure it ends with .py.
5. After saving the file, navigate to its location (app.py). Then hold the “shift key” on your keyboard, right-click on the “app.py” file, and choose “Copy as path“. If you can’t find the “Copy as path” option, click on the file and use the shortcut keys “Ctrl + Shift + C”.
6. Now, open Command Prompt (Terminal). In the Command Prompt window, enter “python”, add a space, then paste the path you have copied (right-click on the blank page to paste). Remember that the file path will be different for your computer depending on the location you saved the file.
Also, as we have discussed earlier, on Linux systems, you may have to use python3
.
python "C:\apps\app.py"
7. When you hit enter, you may get a few warnings because of deprecated components but don’t worry just disregard them. At the bottom, you will get two addresses; the local and public URLs. The local URL (Address) is used to run the AI chatbot on your PC while the public one is shared with your friends and family for testing your AI chatbot. Now, copy the local URL and paste it into the web browser.
After pasting the URL in your browser, hit the enter key. Now command or ask the AI with the left text box and you will get a prompt answer at the right text box.
Congratulations! as you have successfully built your own AI chatbot with the ChatGPT API. Your ChatGPT-powered AI chatbot is now active and working like a charm. Copy the public URL and share it with your friends and family and enjoy instant gratification.
You should, however, note that the public AI chatbot link shared with your friends will be live for only 72 hours. So if you want them to continue enjoying your chatbot you need to restart the server and share the newly generated link with them again. To restart the AI chatbot server, just copy the path of the file (app.py) again and run the command again as you did in step 5. Remember however that the local URL will be the same, but the public URL will change after every server restart.
Wrapping Up on How to Make Your Own AI Chatbot With ChatGPT
I hope you enjoyed building your own AI chatbot with ChatGPT. In our subsequent articles, I will show you how you can customize the “get-3.5-turbo” AI chatbot with your own roles like instructing it to only answer questions from a given category. with AI, we have endless opportunities.
Anyway, that is all from us at Lizbotech Engineering. If you are facing any issues, let us know in the comment section below. We will absolutely help you out with the limited time available and don’t forget to join us on Telegram, the link is right under this paragraph.
Hello sir, is it possible to create your own bot without chatgpt api? If possible how do we go about it?
Yes, it is possible but not easy as you will have to program, train and enhance everything from scratch which is going to take ages. And on top of that, the computers (server resources) that you will need will be some kind of a killer budget.