The ChatGPT API is now available! Find out how to use it to create your own customized AI chatbot based on OpenAI’s gpt-3.5-turbo model.
Following the phenomenal success of ChatGPT since its launch in late 2022, OpenAI announces the launch of the ChatGPT API for developers March 1, 2023.
The new “gpt-3.5-turbo” model on which ChatGPT Plus is based is available for a price 10 times lower and is extremely responsive.
This API offers a myriad of possibilitiesand even non-coders can implement it to create their own AI chatbots and other applications.
Through this folder, you will discover how to build your own AI chatbot with the ChatGPT API. It’s possible to create a chatbot on any platform, including Windows, macOS, Linux or ChromeOS.
Preparing the software environment
You don’t need a powerful PC with a state-of-the-art CPU and GPU, since computing power is provided by the API via the cloud.
First, you need to set up the software environment to create an AI chatbot. You’ll need several tools such as Python, Pip, OpenAI, Gradioan API key and a code editor such as Notepad++.
For install Python, go to the official website at this address and download the installation file for your platform. Run the file and check the “Add Python.exe to PATH” box.
Then click on “Install now and follow the steps. Check that Python is correctly installed by opening the terminal or command prompt on your computer. Enter the command “Python – -version”.
The Pip package manager is installed simultaneously with Python. To update it, open the terminal on your computer and run the command “python -m pip install -U pip”.
Now it’s time to install the OpenAI librarylibrary, enabling interaction with ChatGPT via the API. In the terminal, run the “pip install openai” command.
Then install Gradio library to quickly develop a web interface to demo your AI chatbot. This also makes it easy to share your chatbot on the Internet via a shareable link. Enter the command “pip install gradio”.
Finally, you’ll need to download a code editor such as Notepad++ for Windows, Sublime Text for macOS and Linux, Caret for ChromeOS or VS Code. Download and install the program.
In this folder, we will use Windows 11 for the examples. On Linux and macOS, it may be necessary to replace “Python” with “Python3” in the commands.
Get the OpenAI API key for free
To create an AI chatbot based on ChatGPT, you’ll need an OpenAI API key. It will allow you to call ChatGPT on your own interface and display the results.
Currently, OpenAI offers free API keys with $5 in free credit for the first three months. If you’ve created your OpenAI account before, you can get the equivalent of $18 in free credit.
Once you’ve used up your free credits, you’ll need to pay for API access. For now, it’s free for all users.
First, you’ll need to create an account on the official OpenAI website at this address. If you already have one, simply log in.
On your profile, click on the “View API Keys” button (button in the drop-down menu at the top right of the page. Then click on the “Create new secret key” button to generate a new API key.
As soon as the key is generated, copy it to a Notepad file. Do not share or display this key in public, as it is a private key reserved for you. You can create up to five keys and delete them at will.
How to create an AI chatbot with the ChatGPT API?
It’s time to deploy your AI chatbot. To do this, you’ll use the latest “gpt-3.5-turbo” model on which GPT-3.5 is based.
It is more powerful than Davinciand its training stopped in September 2021. It is also a more economical and responsive model than its predecessors, and able to memorize context of a conversation.
As a user interface, we will use Gradio to create a simple web interface available locally and on the web.
First open Notepad++ or the code editor of your choice, and copy/paste the code below :
“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) “
Make sure you replace the text “Your API Key” with your own API key generated in the previous step. This is the only change you need to make.
Then click on “File” in theand select “save as” from the drop-down menu.
Next, define file name as “app.py and change “save as type” to “all types” in the drop-down menu.
Save the file on an easily accessible location like your Desktop. You can change the name as you like, but make sure you keep the “.py” extension.
Go to the location where you saved the app.py file, do a right-click and choose “copy as path (copy as path).
Open the terminal and enter “python” followed by a space, then paste the path. For example, “python” C:\Users\lebigdata\Desktop\app.py ” “.
Ignore any warnings. At the bottom of the screen, you’ll find a local and a public URL. Copy the local URL and paste it into your web browser.
Alternatively, you can copy the public URL and share it with your friends and family. Visit link will be active for 72 hoursprovided you keep your computer on since the server instance is running on it.
To stop the server, open Terminal and execute the command “Ctrl + C”. To restart it, copy the file path again and run the command again. The local URL will be the same, but the public URL changes with each reboot.
This is it, you’ve created your AI chatbot with the ChatGPT API! Your robot is ready to use. You can ask it questions to get answers, and use it as a chatbot on your website.
How to customize your ChatGPT chabot?
One of the highlights of the gpt-3.5 turbo model is the ability to assign a role to your AI. You can make him funny, angry, or even specialize in a theme such as food, technology, health or any other field.
All you need to do is make a small change in the code to personalize the robot. For example, to create an AI specialized in foodHere’s how it works.
Go to right-click on the “app.py” file and select “edit with Notepad++”. Then make some changes to this specific code.
Feed the AI with information relevant to the role. Then save the file by pressing “Ctrl + S”.
Open the terminal and run the “app.py” file in the same way as before. You’ll get a public URL and a local URL. Copy the local URL.
If a server is already running, press “Ctrl + C” to stop it. Restart the server again. You’ll need to restart it each time you make changes to the “app.py” file.
Open the local URL in your web browser, and you’ll get a personalized AI chatbot that answers only food-related questions. That’s it!
You now know how to create an AI chatbot with ChatGPT 3.5 and customize it. The possibilities are endless, so it’s up to you to exploit this powerful tool to your heart’s content!
Also check out our feature on the best ways to make money with ChatGPT, or our complete guide to writing a book with ChatGPT and MidJourney!