AI Blog Zone

"AI for a smarter tomorrow – insights and analysis on the future of artificial intelligence"

ChatGPTAI Tools

How To Build Chatbot Like ChatGPT In Python 2023 | OpenAI API

How to build Chatbot, How to build chatbot using Python, How to build chatbot using ChatGPT, How to build chatbot using OpenAI API, How to build custom Chatbot

Chatbots have become increasingly popular in recent years, with many businesses and individuals using them to provide automated customer service, personal assistance, and more. However, building a chatbot can be a daunting task, requiring a deep understanding of natural language processing and programming languages. Fortunately, with the help of OpenAI and Gradio, creating a custom chatbot has never been easier.

In this article, we will walk you through the process of building a custom chatbot using OpenAI and Gradio. We will cover two different approaches: building a chatbot from scratch using OpenAI’s GPT-3 API, and building a chatbot using Gradio’s pre-built interface.


Watch Video: How to install VS Code and Python

How to setup Python for VSCode in 2023

Part 1: How To Build Chatbot In Python Using OpenAI API

To begin building your chatbot with OpenAI, you will need to have an OpenAI account and an API key. Once you have these, you can follow the steps outlined below:

OpenAI API Keys

Source Code:

import openai

openai.api_key = “####”

messages = []

system_msg = input(“What type of chatbot would you like to create?\n”)

messages.append({“role”: “system”, “content”: system_msg})

print(“Your new assistant is ready!”)

while input != “quit()”:

    message = input()

    messages.append({“role”: “user”, “content”: message})

    response = openai.ChatCompletion.create(



    reply = response[“choices”][0][“message”][“content”]

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

    print(“\n” + reply + “\n”)

Step 1: Install the OpenAI package using pip. Open a terminal and type “pip install openai” to install the package.

Step 2: Import the OpenAI package and link your account by inserting your custom API key.

Step 3: Create a list called “messages”, which is what the OpenAI API wants to receive. You always need to send it a list as specified in the documentation.

Step 4: Set up a system message variable that takes your input, and then take the user’s answer to this and append it to the “messages” variable in exactly the format that OpenAI is asking for.

Step 5: Run a while loop that runs until you tell it to quit. This is where all the magic happens inside the loop.

Step 6: Take a new input for the user’s message, append it to the “messages” list, and start talking to the Chat GPT 3.5 Turbo API. Then, create a new variable that saves the response that you just got back from the OpenAI API.

Step 7: Append the reply that you just got from ChatGPT to the list, so once the conversation continues, it remembers what you just talked about.

Step 8: Print the reply so the user knows what ChatGPT just replied.

How To Build Chatbot In Python Using OpenAI API

Part 2: How To Build Chatbot In Python Using Gradio library

Gradio is a Python library that allows you to create a custom chatbot inside a browser and even share it with a friend. To build a chatbot with Gradio, you will need to follow these steps:

Source Code:

import openai

import gradio

openai.api_key = “####”

messages = [{“role”: “system”, “content”: “You are a financial experts that specializes in real estate investment and negotiation”}]

def CustomChatGPT(user_input):

    messages.append({“role”: “user”, “content”: user_input})

    response = openai.ChatCompletion.create(

        model = “gpt-3.5-turbo”,

        messages = messages


    ChatGPT_reply = response[“choices”][0][“message”][“content”]

    messages.append({“role”: “assistant”, “content”: ChatGPT_reply})

    return ChatGPT_reply

demo = gradio.Interface(fn=CustomChatGPT, inputs = “text”, outputs = “text”, title = “Real Estate Pro”)


Step 1: Install the Gradio package using pip. Open a terminal and type “pip install gradio” to install the package.

Step 2: Copy and paste your OpenAI API key into the appropriate section in the code.

Step 3: Customize your chatbot by editing the “messages” variable. In the example given in the code, the chatbot is set up as a psychologist.

Step 4: Define a function that will consult the ChatGPT API using the user inputs that the function is based upon to generate a prompt reply, which it will feed back into Gradio.

Step 5: Use Gradio’s pre-built interface to set up the simplest implementation of your chatbot.

How To Build Chatbot In Python Using Gradio library

Once you have completed these steps, you will be able to run your chatbot and begin conversing with it.

How to build chatbot in Python using OpenAI API

Copy and open it into any web browser.


Output of How to build chatbot like ChatGPT in Python using OpenAI API


Creating a custom chatbot has never been easier thanks to OpenAI and Gradio. With these tools, anyone can build a chatbot that is tailored to their specific needs, whether that be providing customer service or acting as a personal assistant. By following the steps outlined in this article, you can create a chatbot that is both functional and easy to use. So why wait? Start building your own chatbot.

Also Read:

Ankit Singh

Myself Ankit Singh and I belong to Kolkata West Bengal, India. As a data scientist and AI developer with three years of experience, I have had the opportunity to work on a variety of projects involving the design and development of machine learning models and artificial intelligence systems.I have gained extensive knowledge and skills in data analysis and visualization, as well as the use of various programming languages and frameworks, including Python, R, and TensorFlow. I have also developed expertise in a wide range of machine learning techniques, including supervised and unsupervised learning, deep learning, and natural language processing.Throughout my career, I have had the opportunity to work on projects in various industries, including finance, healthcare, and retail. I have been responsible for end-to-end project development, from data acquisition and preprocessing to model training and deployment.In addition to my technical skills, I have also developed strong communication and problem-solving abilities, which have been crucial in working with cross-functional teams and delivering successful projects. Overall, my experience as a data scientist and AI developer has been extremely rewarding and has allowed me to make a meaningful impact in the field.

Leave a Reply

Your email address will not be published. Required fields are marked *