Build Chatbot With Custom Knowledge Base Using ChatGPT API

Build Chatbot With Custom Knowledge Base Using ChatGPT API

In this article, we’ll see how to Build Chatbot With Custom Knowledge Base Using ChatGPT API.

What is Chatbot

A chatbot is an artificial intelligence (AI) program designed to simulate human conversation, typically through text or voice interactions. Chatbots are often used in customer service, as they can provide automated assistance to users by answering frequently asked questions or helping users complete simple tasks.

Chatbots use natural language processing (NLP) to understand and interpret user input, and they can respond with pre-programmed answers or generate responses on the fly based on machine learning algorithms. Some chatbots are designed to mimic human personalities, while others are more straightforward and functional.

More About Building Chatbot With Custom Knowledge Base Using ChatGPT API

A chatbot with a custom knowledge base using ChatGPT API is a type of chatbot that is powered by the ChatGPT API and uses a custom knowledge base to generate responses to user queries. This type of chatbot can be designed to answer specific questions related to a particular domain or industry, such as customer service, healthcare, finance, or e-commerce.

The custom knowledge base is created by collecting and organizing information relevant to the chatbot’s scope and domain. This information can be in the form of FAQs, product descriptions, manuals, and other types of content. The knowledge base can then be integrated with the ChatGPT API, which uses natural language processing and machine learning techniques to understand user queries and generate responses based on the information in the knowledge base.

The advantages of using a chatbot with a custom knowledge base using ChatGPT API include improved accuracy and efficiency in answering user queries, increased user engagement and satisfaction, and reduced workload for human operators. This type of chatbot can also be customized and trained over time to improve its performance and adapt to changing user needs and preferences.

Installation Guide

Before you proceed to build Custom Knowledge Base Using ChatGPT API, make sure that you have already installed “python” and “pip” on your device. For Python, run the “python –version” command to check whether it is installed or not. For pip, run the ” pip –version” command to check whether it is installed or not.  If it isn’t installed then you can get it from https://www.python.org/downloads/

1) Now, install “openai” by running the “pip install openai” command in your terminal. We will use it as the LLM (Large language model) to train and create an AI chatbot. And we will also import the LangChain framework from OpenAI. Note that, Linux and macOS users may have to use pip3 instead of pip.

2). Let’s install GPT Index, which is also called LlamaIndex. It allows the LLM to connect to the external data that is our knowledge base. Here, we are installing an older version of gpt_index . This will ensure you don’t get any errors while running the code. If you have already installed gpt_index, run the “pip install gpt_index==0.4.24” command again and it will override the latest one.

3). After that, install PyPDF2 to parse PDF files. If you want to feed your data in PDF format, this library will help the program read the data effortlessly. Apart from that, install PyCryptodome by running the below command. This is again done to avoid any errors while parsing PDF files.
pip install PyPDF2
pip install PyCryptodome

4). Finally, install the Gradio library by running the “pip install gradio” command in your terminal. This is meant for creating a simple UI to interact with the trained AI chatbot. We are now done installing all the required libraries to train an AI chatbot.

5). Go to https://platform.openai.com/account/api-keys . Next, click on your profile in the top-right corner and select “View API keys” from the drop-down menu. Here, click on “Create new secret key” and copy the API key. Do note that you can’t copy or view the entire API key later on. So it’s strongly recommended to copy and paste the API key to a Notepad file immediately.

6). Next, go to platform.openai.com/account/usage and check if you have enough credit left. If you have exhausted all your free credit, you can buy the OpenAI API from https://openai.com/pricing.

7). Lastly, do not share or display the API key in public. It’s a private key meant only for access to your account. You can also delete API keys and create multiple private keys (up to five).

8). Now that we have set up the software environment and got the API key from OpenAI, let’s train the AI chatbot. Here, we will use the “gpt-3.5-turbo” model because it’s cheaper and faster than the other models. If you want to use the latest “gpt-4” model, you must have access to the GPT 4 API which you get by joining the waitlist https://openai.com/waitlist/gpt-4-api. With that out of the way, let’s jump to the instructions.

9) First, create a new folder called docs in an accessible location like the Desktop. You can choose another location as well according to your preference. However, keep the folder name docs

10) Next, move the documents you wish to use for training the AI inside the “docs” folder. You can add multiple text or PDF files (even scanned ones). If you have a large table in Excel, you can import it as a CSV or PDF file and then add it to the “docs” folder. Here lets we create a “sample.txt” file inside the “docs” directory and add the following conversion to the file:

you : what is your name
bot : my name is Umang
you : what is your location
bot : my location is Ahmedabad
you : what do you like?
bot : i like playing computer games
you : which is your favourite computer games?
bot : my favourite game is need for speed
you : what is your occupation
bot : i am software engineer

11). Now, we create a Python program. Let’s create a file called “app.py” with the following code and save the file to the location where you created the “docs” folder.

from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os

os.environ["OPENAI_API_KEY"] = 'Your API Key'

def construct_index(directory_path):
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 20
chunk_size_limit = 600

prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)

llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))

documents = SimpleDirectoryReader(directory_path).load_data()

index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)

index.save_to_disk('index.json')

return index

def chatbot(input_text):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
response = index.query(input_text, response_mode="compact")
return response.response

iface = gr.Interface(fn=chatbot,
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
outputs="text",
title="Custom-trained AI Chatbot")

index = construct_index("docs")
iface.launch(share=True)

12) On the above code, find OPENAI_API_KEY code and add your API key.

13) Now, open the terminal and run the “python app.py” command. After a few moments, open “http://127.0.0.1:7860/” in the browser to see the output. you can ask a question based on the “sample.txt” file and you will get an answer nicely from a chatbot.

You can check out the below video for the whole installation process

Building a chatbot with a custom knowledge base using ChatGPT can be a powerful tool for improving your customer service and engagement. By integrating a comprehensive knowledge base with ChatGPT, you can create a chatbot that is able to provide accurate and relevant information to your customers. With the right training and testing, your chatbot can.

I hope this article helps you to Build Chatbot With Custom Knowledge Base Using ChatGPT API!

Write a Reply or Comment

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