Clicky

Home » Latest News » How to Build Your Own AI Chatbot With ChatGPT API

How to Build Your Own AI Chatbot With ChatGPT API

By

Mark

| Updated on:

We bring you a tutorial on how to build your own AI chatbot using the ChatGPT API. 

Open AI’s ChatGPT can performs various tasks and gives developers endless opportunities to develop tools using this platform. One such opportunity is to create chatbots using ChatGPT. You can develop your own AI chatbot using ChatGPT API with minimum, or zero coding experience is possible.

Several people are already experimenting with the ChatGPT API. If you plan to join the race, this guide uncovers the detailed process of creating an AI chatbot using ChatGPT API. It contains everything you need to develop a chatbot from scratch. So, without wasting time, let’s dive in!

Build AI Chatbot With ChatGPT API

Build Your Own Chatbot With ChatGPT API (2023)

One of the best parts of ChatGPT is that its functionalities are not limited to its website only. Developers can integrate ChatGPT into other websites and use it as a virtual assistant, chatbot, etc. The API also allows customized chatbots to be created, making it ideal for different businesses.

Besides, you don’t need coding knowledge or experience to create a ChatGPT-powered chatbot. You can create your chatbot using the straightforward process defined below.

Also Read – What is ChatGPT Api and How to Use It?

Things to Remember Before You Build an AI Chatbot

Here are a few points to keep in mind before beginning the chatbot creation process:

  • You can create an AI chatbot with ChatGPT using any platform, including Windows, Linux, ChromeOS, MacOS, etc. Creating a chatbot using any of these platforms is almost similar.
  • You don’t need computer programming or technical knowledge to follow the steps in this tutorial. You can easily perform the steps in the guide to build a powerful chatbot.
  • You can create a chatbot using any computer. It doesn’t require a high-powered CPU or GPU.
  • The chatbot will use the GPT-3 language model for interaction.

Set Up the Software Environment to Create an AI Chatbot

Now that you know the basics, it’s time to begin developing the chatbot. First, you need to gather a few things to streamline the AI chatbot development:

  • Python programming app
  • Open AI
  • Open AI API
  • Pip
  • Gradio libraries
  • Code editors like Notepad++

Once you are ready with the tools, it’s time to dive into the development process.

Install Python

Installing Python on your computer is the first step for building a ChatGPT-based chatbot. Python is the programming language that we will use to create the chatbot. To install Python on your PC, follow the steps below:

Step 1: Visit https://www.python.org/downloads/ and click the Download Python button.

Note: If you use a different OS, click the appropriate option from the ones below and install Python on your device. In this guide, we are working on Windows 11. So, I’ve clicked the Download Python button for Windows.

Step 2: Click the Install Now button after downloading and running the file. Remember to check the box “Add Python.exe to PATH.”

Step 3: Then check if the installation is successful. Open the command prompt on your Windows PC.

Step 4: Type python –version in the command prompt. It will return the python version as the output. This indicates the installation is successful. (For Linux platforms, try the python3 –version code.)

Upgrade Pip

When you install Python, Pip is automatically installed on your system. Pip is the package manager for Python, but you need to upgrade it to the latest python version. The package installs thousands of python libraries to your device. You can also add the Gradio and Open AI libraries while upgrading Pip. Below are the steps you need to follow:

Step 1: Open the command prompt on your Windows device.

Step 2: Type the following code: python -m pip install -U pip. For Linux devices, use the python3 -m pip install -U pip code.

Step 3: Run the code by pressing enter.

Install OpenAI and Gradio Libraries

Now we need the Open AI and Gradio libraries. The Open AI library allows users to interact with the Open AI API. Alternatively, the Gradio library creates a user-friendly web interface. You can also share the chatbot using a shareable link via Gradio. Below are the steps to install Open AI and Gradio libraries.

Step 1: Install the Open AI library by typing the following code in the command prompt “pip install openai”. If this code doesn’t work, type pip3 instead of pip.

Step 2: Type the following command in the command prompt to install the Gradio library “pip install gradio.”

After successful installation, move to the next step.

Download a Code Editor

Building a chatbot without programming is impossible. So, the next step is to download a code editor on your PC.

Several code editors are available online. However, for this tutorial, we will use the widely used code editor, Notepad++. To install Notepad ++, visit https://notepad-plus-plus.org/downloads/. Then download the latest version and run the setup to complete the installation process.

Alternatively, try other platforms like Sublime Text editor, Caret, or VS code.

Get the OpenAI API Key For Free

It is mandatory to have an OpenAI API Key to create a chatbot using ChatGPT. Don’t worry. You can get this key for free using your Open AI account. Open AI gives free credits worth $5 for the first three months. Users who created their accounts earlier get free credits worth $18. However, if you’ve exhausted your free API Keys, you must pay for a new one.

Below are the steps to get your OpenAI API key:

Step 1: Visit the https://platform.openai.com/signup page and create an account. You can also sign into your existing account by entering your login credentials.

Step 2: Click the profile icon on the top-right corner.

Step 3: Navigate to the View API keys option.

Step 4: Click the Create New Key button.

Step 5: Copy this key and write it safely. You won’t be able to view it again. Also, keep it private. Do not share it publicly.

You can delete the keys and create upto five new API keys.

Build Your Own AI Chatbot With ChatGPT API and Gradio

So, you’ve successfully gathered everything you need to create the AI chatbot. Let’s now program it. This model uses the GPT-3.5 turbo model and Gradio library to create an interactive web interface. Here are the steps you need to follow to code the chatbot:

Step 1: Open the code editor on your device. In our case, we will use Notepad++.

Step 2: We will use the github code given by amrrs. Visit https://github.com/amrrs/chatgpt-api-python and open the ChatGPT_API_in_Python.ipynb file.

Step 3: Copy the code:

import openai

Import gradio as gra

openai.api_key = 'sk-'

messages = [

    {"role": "system", "content": "You are a kind, helpful assistant."},

]     

while True:

    message = input("User : ")

    if message:

        messages.append(

            {"role": "user", "content": message},

        )

        chat = openai.ChatCompletion.create(

            model="gpt-3.5-turbo", messages=messages

        )

    reply = chat.choices[0].message.content

    print(f"ChatGPT: {reply}")

    messages.append({"role": "assistant", "content": reply})

return reply;

inputs = gra.inputs.Textbox(lines=7, label="Chat with AI")

outputs = gra.outputs.Textbox(label="Reply")

gra.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",

             description="Ask anything you want",

             theme="compact").launch(share=True)

Step 4: Replace the API Key with your API key in the above code.

Step 5: Save the file as chatbot.py and change the file type to All types. Keep the file location easily accessible.

Step 6: Go to the location where you saved the file. Right-click on the file and click the copy path option.

Step 7: Now open the command prompt and type the following command:

python {file path}.

Step 8: Click Enter. (For Linux systems, type python3).

Step 9: Ignore the warning messages on your screen. Copy the public URL at the end of the prompt and paste it into any web browser.

Step 10: Your chatbot is ready! You can now interact with this chatbot and share its URL with friends or family.

Note: This link will be live for 72 hours, and you will have to keep your device on because it will act as a server for your chatbot.

  • To stop the chatbot, use Ctrl + C.
  • Copy and paste the path in the command prompt to restart the chatbot. Then follow steps 8 and 9.

Create Your Personalized ChatGPT API-Powered Chatbot

The above bot will function without any errors. However, you can modify this chatbot to personalize it according to your requirements. You can make it act as an expert in a particular field, like food, health, real estate, finance, etc., by modifying the above code. Below is an example of modifying the code to make the AI assume itself to be a tech enthusiast.

Step 1: Open the python file created above and click the edit with Notepad++ option.

Step 2: Change the following section in the above code.

messages = [

    {"role": "system", "content": "You are an AI specialized in Technology. Do not answer anything other than technology-related queries."},

]

This won’t answer any other questions. Again, do not modify other sections in the code.

Step 3: Save the file by pressing ctrl + S.

Step 4: run the “app.py” file & Copy the file path and enter it on any website. Your personalized bot is ready for use!

Make Your Own AI Chatbot With ChatGPT 3.5 Model

That’s all about developing your ChatGPT-powered chatbot! GPT-3.5 is a powerful language model capable of generating helpful chatbots. You can create a generic or personalized chatbot using ChatGPT-3.5. The steps described in this guide will help you develop and run your chatbot successfully. And you don’t need coding experience to get the job done. So what are you waiting for? Type the code and get your chatbot ready!

Leave a Comment