How I Built a Custom GPT-Based Chatbot in Under 10 Minutes with LlamaIndex

Author:Murphy  |  View: 20751  |  Time: 2025-03-23 18:59:11
Photo by Miguel Á. Padriñán: https://www.pexels.com/photo/two-white-message-balloons-1111368/

Not long ago, I read an article from Jerry Liu that introduced LlamaIndex, an interface that utilizes GPT to synthesize responses to queries using the information provided by the user.

How Unstructured and LlamaIndex can help bring the power of LLM's to your own data

It immediately got my attention, and I knew I had to try it out for myself.

After all, many of the large language models (LLMs) that have taken the world by storm have limited use cases since they aren't trained with the data available to their users.

Thus, there is still a demand for customizable LLMs that are tailored to the needs of their users. Simply put, businesses need LLMs that perform tasks like text summarization, question and answering (Q&A), and text generation with their own information.

To see if LlamaIndex has the potential to meet this demand, I've played around with its features and was genuinely surprised at what I was able to do. I even ended up building a Q&A chatbot in just around 10 minutes!

Here, I walk you through my journey.


What Does LlamaIndex Do?

LlamaIndex generates responses to queries by connecting LLMs to the information provided by users.

As detailed in the documentation, the usage of LlamaIndex entails the following steps:

  1. Load in the documents
  2. Parse the documents into nodes (optional)
  3. Construct the index
  4. Build Indices on top of the constructed indices (optional)
  5. Query the index

Essentially, LlamaIndex loads your data into a document object and then converts it into an index. When the index is provided a query, it passes the query to a GPT prompt to synthesize a response. By default, this is done with OpenAI's text-davinci-003 model.

While this process sounds pretty convoluted, it can be executed with very little code, as you are about to find out.


Setup

To test the versatility of LlamaIndex, I ended up building 3 different chatbots, with each bot being constructed with a different data source. For the sake of brevity, the previously mentioned optional steps (i.e., steps 2 and 4) will be omitted.

First, let's take care of the prerequisites.

LlamaIndex and OpenAI can be installed from pip using the following commands:

pip install llama-index
pip install openai

Users will also need an API key from OpenAI:

Python">import os

os.environ['OPENAI_API_KEY'] = 'API_KEY'

The project will also need the following imports:

<script src="https://gist.github.com/anair123/b2e2f8251ff01e1732210cbc0105bd92.js"></script>

Loading the data

Data can be loaded either manually or through a data loader. For this case, I have loaded 3 types of data:

The first index will be created with the local .txt file, which is located in a folder named data. This data will be loaded manually.

<script src="https://gist.github.com/anair123/5bdddff3f877934a62239393cf1a0bd2.js"></script>

The second index will be created using data from the Wikipedia page on apples. It can be loaded with one of LlamaIndex's data loaders.

<script src="https://gist.github.com/anair123/32b4bbe777f4f6f7107f2db8deaf55f2.js"></script>

The third index will be constructed with the YouTube video showing how to bake a vanilla cake. This data will also be loaded with a data loader.

<script src="https://gist.github.com/anair123/64a213d01061a7175b858fd7c8a12cea.js"></script>

Construct the Indices

With all the data loaded into document objects, we can construct the index for each chatbot.

Each index can be constructed from the document object with a one-liner.

<script src="https://gist.github.com/anair123/66cc356a4c6f997a67377cb24a00274e.js"></script>

Surprised? From loading the data to creating the index, the usage of LlamaIndex requires just a few lines of code!


Query the Index

The constructed indices can now generate responses for any given query. Once again, this step can be completed with a one-liner.

  1. Querying the index build with the .txt file
    <script src="https://gist.github.com/anair123/6d3baa71653e2c86696b5b618aae2456.js"></script>
    Code Output (Created By Author)

In case you were wondering, this is the right answer.

2. Querying the index built with the Wikipedia page (topic: apples)

<script src="https://gist.github.com/anair123/b95a325aafa072b50194356e748b64b0.js"></script>
Code Output (Creating By Author)

3. Querying the index build with the YouTube video (topic: vanilla cake recipe)

<script src="https://gist.github.com/anair123/0c3d8820f412e05b62e3bb04bd68f2b6.js"></script>
Code Output (Created By Author)

Finally, it's also worth noting that the indices will only provide answers to queries when they contain the needed context.

Here's how the same index created with the YouTube video's data would respond to a completely irrelevant query.

<script src="https://gist.github.com/anair123/d2f8ac48be2e1ec53a2f3d95d340e818.js"></script>
Code Output (Created By Author)

Thankfully, it seems that LlamaIndex has taken measures against hallucination (i.e when a model confidently gives an answer not justified by the given data).


Deploying the Chatbots with a Web App

Finally, we can create a web app in order to share the constructed indices with end users.

To do so, we need to first save the indices using the save_to_disk method.

<script src="https://gist.github.com/anair123/32f295cf393e31ee5b311e4b53e2b046.js"></script>

These indices will be used in a Streamlit app. The underlying code for the entire app is as follows:

<script src="https://gist.github.com/anair123/7478d80b63015c319fa6f9a92e9faa1f.js"></script>

In the app, users can select the data source that they wish to base their questions on and write their query in the provided box.

We can see how the indices perform after running the app:

streamlit run app.py

Querying the index built with the .txt file (my favorite fruits):

Streamlit App (Created By Author)

Querying the index built with the Wikipedia page (apples):

Streamlit App (Created By Author)

Querying the index built with the Youtube video (vanilla cake recipe):

Streamlit App (Created By Author)

Pretty cool, right? We've built a functioning web app in just 10 minutes!


Final Remarks

Photo by Prateek Katyal on Unsplash

So far, I have only implemented the basic functionalities of the LlamaIndex interface. There are many areas that haven't been explored in this project, such as customizing LLMs and using non-default settings.

For more information, I invite you to visit the documentation.

If you plan on experimenting with this tool yourself, do be wary of the costs that are incurred from the use of OpenAI's API. This project cost me a mere $1, but that can be attributed to my working with small documents (the pricing is based on the number of tokens you use). If you get too carried away, you may end up picking up a nasty bill.

Finally, all of the source code used to create the Q&A chatbots can be accessed in this Github repository:

GitHub – anair12GitHubting-A-Custom-GPT-Based-Chatbot-With-LlamaIndex

Thank you for reading!

Tags: AI Data Science Python

Comment