How-to-Build-Your-Own-AI-Chatbot-with-ChatGPT-API

How to Build Your Own AI Chatbot with ChatGPT API

The concept of ChatGPT has been a very popular one, with many companies adapting to its technology. The AI chatbot answers questions in a simpler way that is easy to understand and almost human-like, although it is just a robot. How to build your own AI Chatbot with ChatGPT API?

If you are familiar with ChatGPT and want to build your own version of it, here are the steps. Of course, it is not a super simple task, but with some experience, you should be able to do this, especially by following the steps below.

How can you make your own AI chatbot?

In a new movie by the developers of ChatGPT, the ChatGPT API model is now available for the public so that they can use it for themselves. The new “GPT-3.5-turbo” model is perfect for beginners as it is a very cheap model and you can practice it and do a lot of things with it.

Here is a simple way to make your own AI chatbot:

Read also: How to Use ChatGPT with Microsoft Excel

The pre-steps

Here are some basic things you need to install and update before moving on to actually creating a chatbot. Make sure to follow these beforehand.

Step 1:

The first step is to install Python on your computer. Also, make sure to set it up before moving on to the next step.

Step 2:

The next step is to run the setup file; while doing that, make sure to enable the checkbox for “Add Python.exe to PATH.” After that, you just install Python.

Step 3:

Now you have to make sure that Python is running properly on your computer.

Step 4:

Along with Python, Pip is also downloaded on your computer; this allows you to install all Python libraries from the Terminal, such as OpenAI and Gradio libraries. To make sure it is updated, first, open the Windows Terminal.

Step 5:

Now, you run the command, “python -m pip install -U pip,” and you are done.

Step 6:

After that, you have to install the OpenAI library using Pip. It is very simple. Go to the Pip terminal and command “pip install openai” and run the command. If it does not work, replace “pip” with “pip3” which is essential if you have Linux.

Step 7:

After installing OpenAI, you must install one last thing-Gradio on Pip. This will help you develop a friendly web interface and share the chatbot with other people. Do this by going to Pip Terminal and commanding “pip install gradio” and running it.

Step 8:

Before getting started, you also need to download a code editor, and here are three you can choose from:

  1. Notepad++
  2. Sublime Text
  3. Caret app

Step 9:

Now you are almost done with the pre-steps. First, create a free OpenAI account and sign in, or log in if you already have one.

Step 10:

After that, go to the top right corner, where you will find your profile, and click on it. And then, select “view API keys,” which is the third option.

Step 11:

Moving on, now you need to click on “Create new secret key.” It is important to copy this key, as it will disappear after you see it once. It cannot be shared with others as well and is free for up to $5 credit.

The actual steps

Finally, we can get started with building your own chatbot as the earlier steps are all done. Here is how you can do it.

Step 1:

To start off, open Notepad++, Sublime Text/Caret app. Afterward, you need to add the slightly changed code, which is 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 to replace the “Your API key” with the API key that you have.

Step 2:

Now you must save the file. Select “Save as,” set the name of the file as “XYZ.py,” and change “Save as type” to “All types.” You can replace the “XYZ” part but make sure to add “.py.”

Step 3:

Go to the location where the “XYZ.py” is saved and right-click it. Now, choose “Copy as Path” and then go to your Terminal.

Step 4:

At the Terminal, command “python” and then paste the “Copy as Path” that you copied earlier and run it.

Step 5:

After running the program, scroll to the very end and find the “Running on local URL,” and copy the URL.

Step 6:

Copy this URL, not the “Running on public URL” option. After that, paste the URL on your web browser.

Step 7:

You are done now. You can copy the “Running on public URL” URL and send it to family and friends. This link will work for 72 hours, and you must keep your computer on. To restart the chatbot, you will need to repeat “step 4” again.

Conclusion

There are a lot of steps, but making your own chatbot is very simple and easy to do. Even if you do not succeed the first time, you can try again and see where you went wrong. This can be a very fun experience. Hopefully, this article helped you find a way to make your own chatbot! 

About Author

Tahsin Anika