In the current digital era, chatbots have gained popularity as a way of automating interactions with customers and users. From responding to frequently asked queries to making personalised recommendations, they are capable of handling a wide range of jobs. Chatbots are now more sophisticated, conversational, and human-like in their interactions because to advances in artificial intelligence (AI) and natural language processing (NLP).
An AI system called ChatGPT, created by OpenAI, can produce responses to text-based input that resemble those of a human. You can incorporate this AI model into your chatbot and give your users a more customized and interesting experience by using ChatGPT API. We’ll look at how to use the ChatGPT API to create an AI chatbot in this article.
Table of Contents
Install OpenAI and Gradio Libraries
Step 1: Install the OpenAI library
- Install Python and pip (the Python package manager) on your system, if necessary. If not, you can get Python from the official website and install it. (https://www.python.org/downloads/) and pip is also included.
- Open a terminal or command prompt.
- Run the following command to install the OpenAI library using pip:
pip install openai
Step 2: Install Gradio
- You can now install Gradio, which enables you to quickly create a user-friendly web interface for your AI chatbot, after installing the OpenAI framework.
- Run the following command to install Gradio using pip in the terminal or command prompt:
pip install gradio
Create API Key
To create an API key, follow the steps given below. Currently, creating an API key in Open AI is not free for users.
Step 1: Visit the OpenAI website at https://openai.com
Step 2: Click on the “Get Started” or “Sign up” button for the plan you selected. You will be redirected to the sign-up page.
Step 3: Include your name, email address, billing information, and any other information that may be required. To complete the sign-up procedure and make the payment for your chosen plan, simply follow the on-screen instructions.
Step 4: An API key will be provided to you by OpenAI once your payment has been confirmed. This API key is a special identification number that enables you to utilize the OpenAI API in your programmes.
Step 5: According to the OpenAI website, you can interact with the OpenAI services and authenticate your API calls using the API key. To protect your account and data, be sure to keep your API key secure and adhere to standard practises for securing API keys.
Build Your Own AI Chatbot With ChatGPT API and Gradio
We will be using the most recent and potent model that powers GPT-3.5, OpenAI’s “gpt-3.5-turbo” model, to deploy the AI chatbot. In comparison to earlier models, this model has been trained through September 2021 and offers greater reactivity and the capacity to remember conversation context. In addition, it is less expensive than the Davinci model.
We will make use of Gradio, a platform that enables the building of straightforward online user interfaces for AI applications, for the user interface. This web interface will be readily available both locally and online, giving consumers a practical way to communicate with the chatbot and get responses from the potent gpt-3.5-turbo model.
- First make sure to import OpenAI and Gradio in Python
import openai import gradio as gr
openai package is used for interacting with OpenAI’s API, and the
gradio package is used for creating the web interface for the AI chatbot.
- Replace your API key
openai.api_key = "Your API key"
- OpenAI Chat
def openai_chat(prompt): completions = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=1024, n=1, temperature=0.5, ) message = completions.choices.text return message.strip()
- Gradio Interface Function
def chatbot(input, history=): output = openai_chat(input) history.append((input, output)) return history, history
- Launch Interface
gr.Interface(fn = chatbot, inputs = ["text",'state'], outputs = ["chatbot",'state']).launch(debug = True)
- Make sure to replace the “Your API key” text with your own API key generated above and save your file name as file.py
- Open the Terminal, navigate to the directory where your file is present and run the below command.
- At the bottom you will get a local and public URL. Now, copy the local URL and paste it into the web browser.
- And that is how you build your own AI chatbot with the ChatGPT API. Your ChatGPT-powered AI chatbot is live.
FAQs for Creating Chatbot using ChatGPT
Can I use external APIs with ChatGPT to provide dynamic responses?
Yes, you may utilise ChatGPT and external APIs together to deliver dynamic replies. To produce pertinent and current responses depending on user queries or contextual information, you may integrate external APIs for weather data, news updates, or other data sources.
How do I handle user interactions in my chatbot?
By integrating user messages in the input messages array, you may manage user interactions. Users can converse with the chatbot by asking questions, giving commands, or both. The context that the user messages offer will determine how the assistant responds.
Can I use system messages to guide the assistant’s behavior?
Yes, the behaviour of the assistant can be controlled by system messages. System messages establish the conversation’s initial context and tone. For instance, you may tell the assistant to speak like a certain character or adhere to certain rules via a system message.
How do I extract the assistant’s response from the API output?
By accessing the choices field in the API response, which contains the created messages from the assistant, the assistant’s response can be extracted from the API result. The response from the assistant is then retrievable from the related message object’s content field.
Can I train ChatGPT to make my chatbot more specific?
As of September 2021, ChatGPT’s underlying GPT-3.5 model does not support fine-tuning. Only the base GPT-3 models can be adjusted. However, you can direct the assistant’s behaviour and tailor it to your use case without fine-tuning by using system messages and user prompts.
This article is to help you build an AI chatbot using the ChatGPT API. We trust that it has been helpful to you. Please feel free to share your thoughts and feedback in the comment section below.