A step-by-step guide to constructing a chatbot based on your personal documents with GPT Exploring different approaches Constructing document Q&A chatbot step-by-step Some final words

-

Chatting with ChatGPT is fun and informative — I’ve been chit-chatting with it for past time and exploring some recent ideas to learn. But these are more casual use cases and the novelty can quickly wean off, especially once you realize that it may well generate hallucinations.

How might we use it in a more productive way? With the recent release of the GPT 3.5 series API by OpenAI, we are able to do way more than simply chit-chatting. One very productive use case for businesses and your personal use is QA (Query Answering) — [1]. You should utilize it for customer support, synthesizing user research, your personal knowledge management, and more!

Ask a bot for document-related questions. Image generated with Stable Diffusion.

In this text, I’ll explore the right way to construct your personal Q&A chatbot based on your personal data, including why some approaches won’t work, and a step-by-step guide for constructing a document Q&A chatbot in an efficient way with llama-index and GPT API.

(Should you only need to know the right way to construct the Q&A chatbot, you’ll be able to jump on to the section “Constructing document Q&A chatbot step-by-step”)

My day job is as a product manager — reading customer feedback and internal documents takes a giant chunk of my life. When ChatGPT got here out, I immediately considered the thought of using it as an assistant to assist me synthesize customer feedback or find related old product documents in regards to the feature I’m working on.

I first considered fine-tuning the GPT model with my very own data to attain the goal. But fine-tuning costs quite some money and requires a giant dataset with examples. It’s also not possible to fine-tune each time when there may be a change to the document. A fair more crucial point is that fine-tuning simply CANNOT let the model “know” all the data within the documents, but fairly it teaches the model a recent skill. Subsequently, for (multi-)document QA, fine-tuning isn’t the method to go.

The second approach that comes into my mind is prompt engineering by providing the context within the prompts. For instance, as an alternative of asking the query directly, I can append the unique document content before the actual query. However the GPT model has a limited attention span — it may well only soak up just a few thousand words within the prompt (about 4000 tokens or 3000 words). It’s not possible to provide it all of the context within the prompt, provided that now we have hundreds of customer feedback emails and lots of of product documents. It’s also costly in the event you pass in an extended context to the API since the pricing is predicated on the variety of tokens you utilize.

I'll ask you questions based on the next context:
— Start of Context —

YOUR DOCUMENT CONTENT

— End of Context—
My query is: “What features do users wish to see within the app?”

(If you desire to learn more about fine-tuning and prompt engineering for GPT, you’ll be able to read the article: https://medium.com/design-bootcamp/3-ways-to-tailor-foundation-language-models-like-gpt-for-your-business-e68530a763bd)

Since the prompt has limitations on the variety of input tokens, I got here up with the thought of first using an algorithm to go looking the documents and select the relevant excerpts after which passing only these relevant contexts to the GPT model with my questions. While I used to be researching this concept, I got here across a library called gpt-index (now renamed to LlamaIndex), which does exactly what I desired to do and it is simple to make use of [2].

Extract relevant parts from the documents after which feed them to the prompt. Icons from https://www.flaticon.com/

In the subsequent section, I’ll give a step-by-step tutorial on using LlamaIndex and GPT to construct a Q&A chatbot on your personal data.

On this section, we are going to construct a Q&A chatbot based on existing documents with LlamaIndex and GPT (text-davinci-003), so which you could ask questions on your document and get a solution from the chatbot, all in natural language.

Prerequisites

Before we start the tutorial, we’d like to arrange just a few things:

  • Your OpenAI API Key, which may be found at https://platform.openai.com/account/api-keys.
  • A database of your documents. LlamaIndex supports many various data sources like Notion, Google Docs, Asana, etc [3]. For this tutorial, we’ll just use a straightforward text file for demonstration.
  • A neighborhood Python environment or an internet Google Colab notebook.

Workflow

The workflow is simple and takes only just a few steps:

  1. Construct an index of your document data with LlamaIndex
  2. Query the index with natural language
  3. LlamaIndex will retrieve the relevant parts and pass them to the GPT prompt
  4. Ask GPT with the relevant context and construct a response

What LlamaIndex does is convert your original document data right into a vectorized index, which could be very efficient to question. It is going to use this index to seek out essentially the most relevant parts based on the similarity of the query and the info. Then, it would plug in what’s retrieved into the prompt it would send to GPT in order that GPT has the context for answering your query.

We’ll must install the libraries first. Simply run the next command in your terminal or on Google Colab notebook. These commands will install each LlamaIndex and OpenAI.

!pip install llama-index
!pip install openai

Next, we’ll import the libraries in python and arrange your OpenAI API key in a recent .py file.

# Import mandatory packages
from llama_index import GPTSimpleVectorIndex, Document, SimpleDirectoryReader
import os

os.environ['OPENAI_API_KEY'] = 'sk-YOUR-API-KEY'

After we installed the required libraries and import them, we are going to must construct an index of your document.

To load your document, you should utilize the SimpleDirectoryReader method provided by LllamaIndex or you’ll be able to load it from strings.

# Loading from a directory
documents = SimpleDirectoryReader('your_directory').load_data()

# Loading from strings, assuming you saved your data to strings text1, text2, ...
text_list = [text1, text2, ...]
documents = [Document(t) for t in text_list]

LlamaIndex also provides a wide range of data connectors, including Notion, Asana, Google Drive, Obsidian, etc. You’ll find the available data connectors at https://llamahub.ai/.

After loading the documents, we are able to then construct the index simply with

# Construct a straightforward vector index
index = GPTSimpleVectorIndex(documents)

If you desire to save the index and cargo it for future use, you should utilize the next methods

# Save your index to a index.json file
index.save_to_disk('index.json')
# Load the index out of your saved index.json file
index = GPTSimpleVectorIndex.load_from_disk('index.json')

Querying the index is easy

# Querying the index
response = index.query("What features do users wish to see within the app?")
print(response)
An example response.

And voilà! You’re going to get your answer printed. Under the hood, LlamaIndex will take your prompt, seek for relevant chunks within the index, and pass your prompt and the relevant chunks to GPT.

The steps above show only a quite simple starter usage for query answering with LlamaIndex and GPT. But you’ll be able to do way more than that. In reality, you’ll be able to configure LlamaIndex to make use of a unique large language model (LLM), use a unique style of index for various tasks, update existing indices with a recent index, etc. Should you’re interested, you’ll be able to read their doc at https://gpt-index.readthedocs.io/en/latest/index.html.

On this post, we’ve seen the right way to use GPT together with LlamaIndex to construct a document question-answering chatbot. While GPT (and other LLM) is powerful in itself, its powers may be much amplified if we mix it with other tools, data, or processes.

What would you utilize a document question-answering chatbot for?

:

[1] What Is Query Answering? — Hugging Face. 5 Dec. 2022, https://huggingface.co/tasks/question-answering.

[2] Liu, Jerry. LlamaIndex. Nov. 2022. GitHub, https://github.com/jerryjliu/gpt_index.

admin

What are your thoughts on this topic?
Let us know in the comments below.

2 COMMENTS

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sign Up
Sign Up
8 months ago

The point of view of your article has taught me a lot, and I already know how to improve the paper on gate.oi, thank you. https://www.gate.io/signup/XwNAU

positive jazz
positive jazz
4 months ago

positive jazz

Share this article

Recent posts

Grey Wolf Optimizer — How It Can Be Used with Computer Vision

As a bonus, get the code to use feature extraction anywhereImage created by DALL·E 3 based on the prompt “Draw a pack of futuristic...

Artificial intelligence corporations flock to ‘AI representative city Gwangju’

Artificial intelligence (AI) specialized corporations are flocking to Gwangju, the representative city of artificial intelligence in Korea. Gwangju City (Mayor Kang Ki-jeong) held a gathering...

The Pillars of Responsible AI: Navigating Ethical Frameworks and Accountability in an AI-Driven World

Within the rapidly evolving realm of recent technology, the concept of ‘Responsible AI’ has surfaced to handle and mitigate the problems arising from AI...

Ministry of Culture-GIST, MOU to ascertain AI overseas news evaluation platform

The Ministry of Culture, Sports and Tourism (Minister Yoo In-chon) announced on the fifteenth that it could sign a business agreement with the Gwangju...

“Samsung significantly strengthens headset secret development team to reply to Apple’s ‘Vision Pro’”

A report has emerged that Samsung Electronics is significantly increasing the dimensions of its internal XR (mixed reality) headset development team following the launch...

Recent comments

бнанс рестраця для США on Model Evaluation in Time Series Forecasting
Bonus Pendaftaran Binance on Meet Our Fleet
Créer un compte gratuit on About Me — How I give AI artists a hand
To tài khon binance on China completely blocks ‘Chat GPT’
Regístrese para obtener 100 USDT on Reducing bias and improving safety in DALL·E 2
crystal teeth whitening on What babies can teach AI
binance referral bonus on DALL·E API now available in public beta
www.binance.com prihlásení on Neural Networks and Life
Büyü Yapılmışsa Nasıl Bozulur on Introduction to PyTorch: from training loop to prediction
yıldızname on OpenAI Function Calling
Kısmet Bağlılığını Çözmek İçin Dua on Examining Flights within the U.S. with AWS and Power BI
Kısmet Bağlılığını Çözmek İçin Dua on How Meta’s AI Generates Music Based on a Reference Melody
Kısmet Bağlılığını Çözmek İçin Dua on ‘이루다’의 스캐터랩, 기업용 AI 시장에 도전장
uçak oyunu bahis on Thanks!
para kazandıran uçak oyunu on Make Machine Learning Work for You
medyum on Teaching with AI
aviator oyunu oyna on Machine Learning for Beginners !
yıldızname on Final DXA-nation
adet kanı büyüsü on ‘Fake ChatGPT’ app on the App Store
Eşini Eve Bağlamak İçin Dua on LLMs and the Emerging ML Tech Stack
aviator oyunu oyna on AI as Artist’s Augmentation
Büyü Yapılmışsa Nasıl Bozulur on Some Guy Is Trying To Turn $100 Into $100,000 With ChatGPT
Eşini Eve Bağlamak İçin Dua on Latest embedding models and API updates
Kısmet Bağlılığını Çözmek İçin Dua on Jorge Torres, Co-founder & CEO of MindsDB – Interview Series
gideni geri getiren büyü on Joining the battle against health care bias
uçak oyunu bahis on A faster method to teach a robot
uçak oyunu bahis on Introducing the GPT Store
para kazandıran uçak oyunu on Upgrading AI-powered travel products to first-class
para kazandıran uçak oyunu on 10 Best AI Scheduling Assistants (September 2023)
aviator oyunu oyna on 🤗Hugging Face Transformers Agent
Kısmet Bağlılığını Çözmek İçin Dua on Time Series Prediction with Transformers
para kazandıran uçak oyunu on How China is regulating robotaxis
bağlanma büyüsü on MLflow on Cloud
para kazandıran uçak oyunu on Can The 2024 US Elections Leverage Generative AI?
Canbar Büyüsü on The reverse imitation game
bağlanma büyüsü on The NYU AI School Returns Summer 2023
para kazandıran uçak oyunu on Beyond ChatGPT; AI Agent: A Recent World of Staff
Büyü Yapılmışsa Nasıl Bozulur on The Murky World of AI and Copyright
gideni geri getiren büyü on ‘Midjourney 5.2’ creates magical images
Büyü Yapılmışsa Nasıl Bozulur on Microsoft launches the brand new Bing, with ChatGPT inbuilt
gideni geri getiren büyü on MemCon 2023: We’ll Be There — Will You?
adet kanı büyüsü on Meet the Fellow: Umang Bhatt
aviator oyunu oyna on Meet the Fellow: Umang Bhatt
abrir uma conta na binance on The reverse imitation game
código de indicac~ao binance on Neural Networks and Life
Larry Devin Vaughn Wall on How China is regulating robotaxis
Jon Aron Devon Bond on How China is regulating robotaxis
otvorenie úctu na binance on Evolution of Blockchain by DLC
puravive reviews consumer reports on AI-Driven Platform Could Streamline Drug Development
puravive reviews consumer reports on How OpenAI is approaching 2024 worldwide elections
www.binance.com Registrácia on DALL·E now available in beta