Build a chatbot with custom knowledge base using Python and GPT-3

·

5 min read

Building a chatbot with a custom knowledge base might seem like a daunting task, but with the help of GPT-3, ChatGPT, and Python, it can be a lot easier than you might think. In this tutorial, we will go over the steps to build a chatbot that can answer questions from a custom knowledge base.

What is ChatGPT and GPT-3?

You have probably used GPT-3 and ChatGPT interchangeably, or maybe you’re not entirely sure what the difference is. GPT-3 and ChatGPT are both Large Language Models (LLMs) built and managed by OpenAI. These are machine-learning models that can be used to generate natural-sounding text, code, etc. They are trained using large data sets and this helps them improve at doing lots of natural-language tasks, like answering questions and holding conversations.

GPT-3 is a general-purpose language model. It can hold natural conversations, write code, do translation tasks, and more. You can think of it as a flexible know-it-all that can expound on any topic you want.

ChatGPT on the other hand, is a version of GPT-3 that’s been turned into a friendly interface. It has been specifically trained to be good at holding conversations.

Let’s build a chatbot:

Note: This is a Google Colab application

Step 1: Set up the environment

The first step is to create an OpenAI account and obtain an API key. This is necessary to use GPT-3 and ChatGPT. Click Here to get started:

  • Sign up to create an account or log in if you already have an account.

  • Click your account icon on the top right of the screen and select "View API Keys".

  • Click on “Create new secret key”.

  • Your OpenAI API key will be automatically generated

Step 2: Install dependencies

The dependencies we will use are LlamaIndex and LangChain:

  • LlamaIndex is a simple, flexible interface between your external data and Large Language Models. LlamaIndex offers data connectors to your existing data sources and data formats (APIs, PDFs, docs, SQL, etc.) and provides indices over your unstructured and structured data for use with LLMs.

  • LangChain is a framework for developing applications powered by language models. We can use it for chatbots, Generative Question-Answering (GQA), summarization, and much more. The core idea of the library is that we can “chain” together different components to create more advanced use cases around LLMs.

Once you have your API key, you can install the llama-index and langchain Python packages by running the following commands:

pip install llama-index
pip install langchain

Step 3: Collect the data

Next, collect the data you want to use as your knowledge base - A knowledge base is a repository of information about a product, service, department, or topic. The data in your knowledge base can come from anywhere. This could be anything from a list of frequently asked questions to a more extensive dataset of structured information. You can use Python to preprocess the data and prepare it for use with GPT-3.

For example, you can generate lists of questions and answers using ChatGPT and store them in a folder. Then you can read the content of these files for further operations.

Step 4: Preprocess the data and query the index

The following code defines the functions we need to construct the index and query it.

from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import sys
import os
from IPython.display import Markdown, display

def construct_index(directory_path):
   # set maximum input size
   max_input_size = 4096
   # set the number of output tokens
   num_outputs = 2000
   # set maximum chunk overlap
   max_chunk_overlap = 20
   # set chunk size limit
   chunk_size_limit = 600

   # define LLM
   llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="text-davinci-003", max_tokens=num_outputs))
   prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
   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 ask_chatbot():
   index = GPTSimpleVectorIndex.load_from_disk('index.json')
   keep_prompting=True
   while keep_prompting:
       query = input("How can I help you today?  -Enter 'exit' if done!")
       if query=='exit':
         keep_prompting=False
       else:
         response = index.query(query, response_mode="compact")
         display(Markdown(f"Response: <b>{response.response}</b>"))

The construct_index function in the above snippet takes the folder directory path as an argument. This function extracts the content of each file in the folder, then splits it into chunks, and embeds it with OpenAI's embeddings API.

Step 4b: Enter the OpenAI API key

You will need the OpenAI API key we set up in step 1 above to query the index. If you don't have one yet, get it by signing up and following the process explained in step 1 above to create an API key.

You will get a prompt to paste your API key into the text input.

os.environ["OPENAI_API_KEY"] = input("Paste your OpenAI key here and hit enter:")

Step 5: Ask your chatbot questions

It's time to have fun and test our AI-powered chatbot. If you've used the provided example data for your custom knowledge base, you can ask the chatbot questions based on the knowledge base provided. Run the function below to start your chatbot.

ask_chatbot()

Conclusion:

From the steps above we know that building a chatbot with a custom knowledge base using GPT-3, ChatGPT, and Python can be a simple task. Whether you’re building a chatbot to answer customer support queries or just want it as a great addition to your portfolio, this tutorial article provides a step-by-step guide and code examples to help readers build their chatbots. With the right tools and techniques, anyone can create a chatbot that can answer questions from a custom knowledge base.