Guide to Setting Up a PDF-Reading Chatbot with ChatGPT.

Saurabh Pandey
𝐀𝐈 𝐦𝐨𝐧𝐤𝐬.𝐢𝐨
5 min readOct 5, 2023

Last year in 2022 on Nov 30 ChatGPT, a super-smart digital assistant created by OpenAI. Think of it as a chatbot that can answer questions, help with writing, translate languages, and even assist in tasks like coding. It’s designed to chat like a human, making it easy and natural to use. Many businesses integrate ChatGPT into their platforms to enhance user experience. It’s a versatile tool that can fit into many roles!

ChatGPT is like a digital brain trained on lots of books, articles, and websites. When you ask it something, it thinks back to what it learned and gives you an answer, just like a friend recalling something they read! It’s made to chat naturally, so talking to it feels easy and friendly.

Right now, ChatGPT knows stuff only up to November 2021. In this guide, we’ll make a chatbot like ChatGPT that can read PDFs and answer your questions. We’ll be using Pycharm for this, so make sure you have it. You can get the free version of Pycharm [from this link].

So, what’s the plan? We’ll use the Langchain Framework in Python to build our chatbot. This bot will have a special tool called LLM (chatgpt-3.5), a prompt, and a PDF reader, all linked in a RetrievalQA system.

The PDF reader will read the document and will convert it to VectorDB that can be used as context inside our LLM with a prompt to help LLM understand about the context.

Lets start by setting up the project environment and install dependencies for the project.

  1. Open PyCharm and create a new project as pdfchatbot, you can use different name, Make sure to select python 3.7+
  2. Inside the project create a text file named as requirements.txt
langchain
openai
chromadb
tiktoken
unstructured
pdf2image
pdfminer.six

these are the packages that we will need in our project.

  1. Install requirements, Use the below command to install all the requirements
pip install -r requirements.txt
  1. Create a python package folder src/, we will keep our code related to PDF reader and prompts inside src folder.
  2. Create a python file name pdfloader.py inside `src` python package add below code to it.
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain.document_loaders import UnstructuredPDFLoader

def load_pdf(filepath, openai_api_key):
loader = UnstructuredPDFLoader(filepath)
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
return Chroma.from_documents(texts, embeddings, collection_name=filepath.split("/")[-1].split(".")[0])

The function simply loads a PDF and Create a Chroma vectorstore from a raw documents. PDF file path and OpenAI API key is taken as input. OpenAI is free to use and you can obtained API key from here.

  1. Create a python file prompt.py inside src and add below code to it.
from langchain.prompts import PromptTemplate

def pdfreaderprompt():
prompt_template = """Blockchain, the foundation of Bitcoin, has received extensive attentions recently. Blockchain serves as an immutable ledger which allows transactions take place in a decentralized manner. Blockchain-based applications are springing up, covering numerous fields including financial services, reputation system and Internet of Things (IoT), and so on. However, there are still many challenges of blockchain technology such as scalability and security problems waiting to be overcome. The below contexr presents a comprehensive overview on blockchain technology.
{context}

Make sure you reply question only from the context.

{question}
"""
return PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)

We will be using PDF document on “An Overview of Blockchain Technology” for the tutorial, You can download the PDF from the. The VectorDB context we created before will be used inside the prompt where {context} is declared. The below code convert the textual prompt to PromptTemplate which we can use inside our RetrievalQA system.

  1. Create a python file chatbot.py outside src folder
from langchain.llms import OpenAI
from src.pdfloader import load_pdf
from src.prompt import pdfreaderprompt
from langchain.chains import RetrievalQA

def CreateChatbot():
openai_api_key = input("Input your open AI Key >>")
vectorDB = load_pdf("C:/Users/Saurabh/Downloads/1996a557.pdf", openai_api_key)
llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
prompt = pdfreaderprompt()
chain_type_kwargs = {
"prompt": prompt
}
chatbot = RetrievalQA.from_chain_type(
llm=llm, chain_type="stuff", retriever=vectorDB.as_retriever(),
chain_type_kwargs=chain_type_kwargs
)
return chatbot

if __name__ == '__main__':
chatbot = CreateChatbot()
while (True):
query = input("Ask Anything >> ")
response = chatbot.run(query)
print (response)

Now we have created a chatbot using OpenAI LLM and RetrievalQA Chain. Make sure you replace “path/to/pdf” with filepath of your downloaded PDF.

  1. Run chatbot on terminal using python chatbot.py, make sure you run the command from PyCharm terminal or if you are running the command outside from PyCharm, make sure to activate virtual environment created by the project before running
(venv) PS C:\Users\Saurabh\Desktop\pdfchatbot> python .\chatbot.py
Input your open AI Key >>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
Created a chunk of size 1487, which is longer than the specified 1000
Created a chunk of size 1129, which is longer than the specified 1000
Created a chunk of size 1512, which is longer than the specified 1000
Created a chunk of size 1134, which is longer than the specified 1000
Created a chunk of size 1315, which is longer than the specified 1000
Created a chunk of size 1011, which is longer than the specified 1000
Ask Anything >> explain about BLOCKCHAIN ARCHITECTURE

Blockchain architecture is a decentralized system that allows transactions to take place in a secure and immutable manner. It consists of a distributed ledger, which is a shared database that records all transactions, and a consensus algorithm, which is used to validate and verify transactions. The ledger is maintained by a network of computers, called nodes, which are connected to each other and communicate with each other to ensure the accuracy and security of the ledger. The consensus algorithm is used to ensure that all nodes agree on the validity of the transactions and that the ledger is updated correctly. The consensus algorithm also ensures that the ledger is immutable, meaning that once a transaction is recorded, it cannot be changed or reversed.
Ask Anything >>

Our chatbot is ready to use, Use can use streamlit to create a UI platform for your python code.

Checkout project on github

The specific flow could have many application like
1. Creating an AI Assitant for teaching ( Passing Books as PDF )
2. Creating an AI Assitant for Software ( Passing Software Documents as PDF )
3. Creating an AI Assitant for teaching ( Passing Books as PDF )

--

--