Pravar Agrawal Technology & Travel

Chatbot With OpenAI API

Chatbots have really popularised the usage of language models in various fields like customer support, customer interaction in recent times. Using OpenAI and Gradio we can quickly create a chatbot model for self tinkering and exploring this modern method of communication assist.

Let’s first start by ensuring if we have the correct python (recommended python 3) in our local:

$ python --version
Python 3.11.4

And checking if the package manager pip is installed:

$ pip --version

Next, we’ll install openai and gradio libraries so that we can start coding our chatbot locally:

pip install openai

pip install gradio

Next is where we’ll add our code to create the chatbot using openai and gradio libraries. Let’s create a file ‘main.py’ with the below code:

import openai
import gradio as gr
from openai.error import RateLimitError
import backoff

openai.api_key = ""

messages = [
    {"role": "chatbot", "content": "Youre helpful assistant."},
]



@backoff.on_exception(backoff.expo, RateLimitError)
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 bot")
outputs = gr.outputs.Textbox(label="Reply")

gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",
             description="Please ask your question",
             theme="compact").launch(share=True)

For the code to access GPT3 based apis, we need to generate an openai based api-key. The chatbot method takes one input in the form of user’s question or input to the chatbot. This intimely uses gradio’s generate response function to obtain user based input’s responses in real-time. We have also added a user interface to make it look sober for the user to address their queries while interacting with the chatbot.

Once we run the python code in terminal, we can access the application in browser following the url: 127.0.0.1:7860 and we’ll come across an interface like below,

chatbot-interface

As we saw one of the most quickest method to create an openai based chatbot, it’s very interesting to see how much data it can process. For now I’ve only tested it with smaller queries, but once I try to find answers for more bigger problems, will definitely share the same in coming posts.