OpenAI has announced the ChatGPT API to the general public and developer community in a ground-breaking announcement. In particular, the new “gpt-3.5-turbo” model, which runs ChatGPT Plus, has been made available at a 10x lower cost and is also incredibly responsive. In essence, OpenAI has created many opportunities, and anybody, coder or not, may use the new ChatGPT API to develop their own AI chatbot. Hence, in this post, we’ll show you how to use the ChatGPT API to create your own AI chatbot.
Build Your Own Chatbot With ChatGPT API (2023)
We have included step-by-step instructions for creating your own AI chatbot using the ChatGPT API in this article. We have covered all the little aspects for common users here, from installing libraries to setting up tools to building the AI chatbot from start. We advise that you read the instructions cover-to-cover, beginning to end.
Things to Remember Before You Build an AI Chatbot
Regardless of the operating system you use—Windows, macOS, Linux, or ChromeOS—you can create a ChatGPT chatbot. Although I am using Windows 11 in this essay, the procedures are essentially the same on other systems.
The instructions are thoroughly presented with examples and the manual is intended for ordinary users. Thus, you may simply construct your own AI chatbot even if you just have a basic understanding of computers.
You don’t need a robust machine with a strong CPU or GPU to build an AI chatbot. The cloud-based OpenAI API does the strenuous work.
Set Up the Software Environment to Create an AI Chatbot
Before you can construct an AI chatbot using ChatGPT, you need to set up the environment using a few tools. A code editor like Notepad++, Python, Pip, OpenAI, and Gradio libraries, as well as an OpenAI API key, are also required. Although using all of these tools can initially appear overwhelming, I assure you that it is simple and anyone can do it. Go ahead and do the following.
Install Python
1. First, you need to install Python on your computer. Open this link and download the setup file for your platform.
2. Next, run the setup file and make sure to enable the checkbox for “Add Python.exe to PATH.” This is an extremely important step. After that, click on “Install Now” and follow the usual steps to install Python.
3. To check if Python is properly installed, open Terminal on your computer. I am using Windows Terminal on Windows, but you can also use Command Prompt. Once here, run the below command below, and it will output the Python version. On Linux or other platforms, you may have to use python3 --version
instead of python --version
.
python --version
Upgrade Pip
Pip is concurrently installed on your machine together with Python. We will discover how to update it to the most recent version in this area. In case you’re unaware, Pip is Python’s package manager. In essence, it allows you to use the Terminal to install hundreds of Python modules. Installing OpenAI and Gradio libraries is possible via Pip. Here’s how you approach it.
1. Open the Terminal of your choice on your PC. As for me, I’m using the Window Terminal Now, run the below command to update Pip. Again, you may have to use python3
and pip3
on Linux or other platforms.
python -m pip install -U pip
Install OpenAI and Gradio Libraries
1. Now, it’s time to install the OpenAI library, which will allow us to interact with ChatGPT through their API. In the Terminal, run the below command to install the OpenAI library using Pip. If the command does not work, try running it with pip3
.
pip install openai
2. After the installation is done, let’s install Gradio. Gradio allows you to quickly develop a friendly web interface so that you can demo your AI chatbot. It also lets you easily share the chatbot on the internet through a shareable link.
pip install gradio Download a Code Editor Finally, we need a code editor to edit some of the code. On Windows, I would recommend Notepad++ (Download). Simply download and install the program via the attached link. You can also use VS Code on any platform if you are comfortable with powerful IDEs. Other than VS Code, you can install Sublime Text (Download) on macOS and Linux. For ChromeOS, you can use the excellent Caret app (Download) to edit the code. We are almost done setting up the software environment, and it’s time to get the OpenAI API key.
Get the OpenAI API Key For Free
Now, to create a ChatGPT-powered AI chatbot, you need an API key from OpenAI. The API key will allow you to call ChatGPT in your own interface and display the results right there. Currently, OpenAI is offering free API keys with $5 worth of free credit for the first three months. If you created your OpenAI account earlier, you may have free credit worth $18. After the free credit is exhausted, you will have to pay for the API access. But for now, it’s available to all free users.
1. Head to platform.openai.com/signup and create a free account. If you already have an OpenAI account, simply log in.
2. Next, click on your profile in the top-right corner and select “View API keys” from the drop-down menu.
3. Here, click on “Create new secret key” and copy the API key. Do note that you can’t copy or view the entire API key later on. So it’s strongly recommended to copy and paste the API key to a Notepad file immediately.
4. Also, do not share or display the API key in public. It’s a private key meant only for access to your account. You can also delete API keys and create multiple private keys (up to five).
Build Your Own AI Chatbot With ChatGPT API and Gradio
It’s now time to put the AI chatbot into use. We are doing this by utilising OpenAI’s most recent “gpt-3.5-turbo” model, which runs GPT-3.5. It has been prepared through September 2021 and is considerably more potent than Davinci. Also, it is far more affordable, responsive, and retains the context of the discussion than prior models. In terms of the user experience, Gradio will be used to provide a straightforward web interface that will be accessible both locally and online.
1. First, open Notepad++ (or your choice of code editor) and paste the below code. Thanks to armrrs on GitHub, I have repurposed his code and implemented the Gradio interface as well.
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. This is how it looks in the code editor. Make sure to replace the “Your API key
” text with your own API key generated above. That’s the only change you have to make.
3. Next, click on “File” in the top menu and select “Save As…” from the drop-down menu.
4. After that, set the file name as “app.py
” and change “Save as type” to “All types” from the drop-down menu. Then, save the file to an easily-accessible location like the Desktop. You can change the name to your preference, but make sure .py
is appended.
5. Now, move to the location where you saved the file (app.py). Right-click on it and choose “Copy as path“.
6. Open the Terminal and run the below command. Simply enter python
, add a space, paste the path (right-click to quickly paste), and hit Enter. Keep in mind, the file path will be different for your computer. Also, on Linux systems, you may have to use python3
.
python "C:\Users\mearj\Desktop\app.py"
7. You may get a few warnings, but ignore them. At the bottom, you will get a local and public URL. Now, copy the local URL and paste it into the web browser.
And that is how you build your own AI chatbot with the ChatGPT API. Your ChatGPT-powered AI chatbot is live. Now, you can ask any question you want and get answers in a jiffy. In addition to ChatGPT alternatives, you can use your own chatbot instead of the official website.
9. You can also copy the public URL and share it with your friends and family. The link will be live for 72 hours, but you also need to keep your computer turned on since the server instance is running on your computer.
10. To stop the server, move to the Terminal and press “Ctrl + C“. If it does not work, press “Ctrl + C” again.
11. To restart the AI chatbot server, simply copy the path of the file again and run the below command again (similar to step #6). Keep in mind, the local URL will be the same, but the public URL will change after every server restart.
python "C:\Users\mearj\Desktop\app.py"
Create Your Personalized ChatGPT API-Powered Chatbot
The best part about the “gpt-3.5-turbo” model is that you can assign a role to your AI. You can make it funny, angry, or a specialist in food, tech, health, or whatever you want. You just need to make one small change in the code and it will be personalized. For example – I have created a Food AI, and here’s how:
1. Right-click on the “app.py” file and choose “Edit with Notepad++“.
2. Here, make changes to this particular code only. Simply feed the information to the AI to assume that role. Now, 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."}, ]
3. Open Terminal and run the “app.py” file in a similar fashion as you did above. You will get a local and public URL. Copy the local URL. If a server is already running, press “Ctrl + C” to stop it. And then restart the server again. You will have to restart the server after every change you make to the “app.py” file.
python "C:\Users\mearj\Desktop\app.py"
4. Open the local URL in the web browser and you will get a personalized AI chatbot that only answers food-related queries. That’s it. You can create a Doctor AI, an AI that replies like Shakespeare, which talks in morse code, anything you wish.
Make Your Own AI Chatbot With ChatGPT 3.5 Model
So using ChatGPT 3.5, here is how you may create your own AI chatbot. The “gpt-3.5-turbo” model can also be customised with your own responsibilities. With AI, the options are unlimited and you can do anything. That’s all we have to say, though. Visit our linked page to find out how to utilise ChatGPT on Android and iOS. Follow our curated post to find out about all the fun things you can do with ChatGPT. Finally, let us know in the comment box below if you are having any problems. We will certainly make an effort to assist you.