Tech

Create your own chat bot with GPT-Index & LangChain

Step by step documentation to create chat bot with Gpt-Index using Python

Muhammad Amin

Flutter Developer

Chatbots have become increasingly popular in recent years, with businesses of all sizes leveraging them to improve customer engagement, reduce response times, and enhance user experience. However, building a chatbot from scratch can be a daunting task, requiring significant expertise in natural language processing (NLP) and machine learning.

That's where GPT-Index comes in. GPT-Index is a powerful tool that allows you to create a chatbot based on the data feed by you. With GPT-Index, you don't need to be an expert in NLP or machine learning. You simply need to provide the data you want the chatbot to use, and GPT-Index will take care of the rest.


Let's begin by installing the necessary Python packages

We need to install two dependencies called gpt index and langchain, which can be done with the following lines:


pip install gpt_index
pip install langchain

Import all packages that will be used


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


Create the function

This is known as embedding, and it must be repeated whenever fresh data is received. The code below describes the functions required to build the index and query it:


def construct_index(directory_path):
    # set maximum input size
    max_input_size = 4096
    # set 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


Configure the OpenAI API Key

If you don't already have one, sign up for one. Next click your account icon in the top right of the screen and select "See API Keys". Create an API key.


Screen Shot 2023-03-22 at 00.51.15.png



os.environ["OPENAI_API_KEY"] = "YOUR_KEY"


Gathering data

You can get data from specific URL or you can download data file and give the path of that file. In our above case we are getting data from the text file and giving the path of that file. GPT-Index supports a wide range of data formats, including CSV, JSON, and Excel. You can also connect GPT-Index to your existing database or API to automatically pull in new data as it becomes available.



Write the Ask Function

Finally we will write the function which will ask AI. The function created index file from where it retrieves all file information which we provided in above step



def ask_ai():
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    while True: 
        query = input("What do you want to ask? ")
        response = index.query(query, response_mode="compact")
        display(Markdown(f"Response: <b>{response.response}</b>"))



The full script should look like this :


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 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_ai():
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    while True: 
        query = input("What do you want to ask? ")
        response = index.query(query, response_mode="compact")
        display(Markdown(f"Response: <b>{response.response}</b>"))



Ask Question

Once your chatbot has been setup, it's time to test it. GPT-Index provides a user-friendly interface that allows you to test your chatbot in real-time. You can also integrate your chatbot with your website or messaging platform to provide a seamless user experience.


Deploy your chatbot

After you've tested your chatbot and made any necessary tweaks, it's time to deploy it. GPT-Index provides a range of deployment options, including hosting the chatbot on your own server or integrating it with popular messaging platforms like Facebook Messenger or Slack.

In conclusion, creating a chatbot with GPT-Index is a straightforward and efficient process that allows you to leverage the power of machine learning without the need for advanced technical expertise. With GPT-Index, you can quickly and easily create a chatbot that can improve customer engagement, reduce response times, and enhance user experience.

We are the partners you’ve been searching for.

Tell us about your project.