# Easiest Guide to build a chatGPT for your PDF documents using GPT-3/3.5

In this guide, we will see how to build a [**chatGPT**](https://harishgarg.com/writing/chatgpt-the-complete-guide/) for your PDF documents i.e. an AI that will answer your questions based on a particular PDF document.

You could use this to ask questions about your textbooks, ebooks, or anything else as long as it’s in a PDF file format

We will be using

Let’s go.

Table of Contents

*   [The process to build a chatGPT for your PDF documents](https://harishgarg.com/#The_process_to_build_a_chatGPT_for_your_PDF_documents)
*   [Requirements to build a chatGPT for your PDF documents](https://harishgarg.com/#Requirements_to_build_a_chatGPT_for_your_PDF_documents)
*   [Install Python packages](https://harishgarg.com/#Install_Python_packages)
*   [Setup your working directory/folder](https://harishgarg.com/#Setup_your_working_directoryfolder)
*   [Import the required Python packages](https://harishgarg.com/#Import_the_required_Python_packages)
*   [Process the PDF](https://harishgarg.com/#Process_the_PDF)
*   [Create embeddings](https://harishgarg.com/#Create_embeddings)
*   [Query the PDF document using the embeddings](https://harishgarg.com/#Query_the_PDF_document_using_the_embeddings)
*   [Conclusion](https://harishgarg.com/#Conclusion)

The process to build a chatGPT for your PDF documents
-----------------------------------------------------

There is the main steps we are going to follow to build a chatGPT for your PDF documents

1.  First, we will extract the text from a pdf document and process it and make it ready for the next step.
2.  Next, we will use an embedding AI model to create embeddings from this text.
3.  Next, we will build the query part that will take the user’s question and uses the embeddings created from the pdf document, and uses the GPT3/3.5 API to answer that question.

Requirements to build a chatGPT for your PDF documents
------------------------------------------------------

1.  We will be using [OpenAI GPT-3/3.5](https://harishgarg.com/writing/how-do-you-get-access-to-openai-gpt-3/) API for this. Grab your API key from your [OpenAI Account](https://platform.openai.com/account/api-keys).
2.  Python 3.x or higher installed on your computer.

Install Python packages
-----------------------

First, install the necessary python packages. Depending on your python installation, you could use _pip install <package>_ or _python -m pip install <package>._ Run these from your command line program.

The python packages you need to install are:

*   PyPDF2
*   langchain
*   openai
*   faiss-cpu

Setup your working directory/folder
-----------------------------------

create a new directory or folder and create a .env file inside the folder and write below text into it

OPENAI\_API\_KEY=your-openai-api-key

make sure to replace the text _your-openai-api-key_ with your actual OpenAI API key.

Import the required Python packages
-----------------------------------

You can do this in a Jupyter Notebook / Google Colab Notebook or a python .py on your computer

Make sure it’s in the same folder as the .env file you created above.

\# import the modules
from PyPDF2 import PdfReader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text\_splitter import CharacterTextSplitter
from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
import os
\# load .env file
from dotenv import load\_dotenv
load\_dotenv()

Process the PDF
---------------

We start with reading in the pdf document.

reader \= PdfReader('my\_pdf\_doc.pdf')

raw\_text \= ''
for i, page in enumerate(reader.pages):
    text \= page.extract\_text()
    if text:
        raw\_text += text

Next we split the pdf contents into chunks.

text\_splitter \= CharacterTextSplitter(        
    separator \= "\\n",
    chunk\_size \= 1000,
    chunk\_overlap  \= 200,
    length\_function \= len,
)
texts \= text\_splitter.split\_text(raw\_text)

Create embeddings
-----------------

Now, it’s time to create embeddings from the text chunks we created above from the pdf document.

embeddings \= OpenAIEmbeddings()

We then save the embeddings so that we do need not to create them again and again. The below code saves them to the disk. However, you could also save them to various [vector databases covered here](https://harishgarg.com/writing/best-vector-databases-for-ai-apps/).

import pickle
with open("foo.pkl", 'wb') as f:
    pickle.dump(embeddings, f)

Query the PDF document using the embeddings
-------------------------------------------

First we load the saved embeddings

with open("foo.pkl", 'rb') as f: 
   new\_docsearch \= pickle.load(f)

There are two ways to query the PDF document using mebeddings

Below method will list the most similar chunks that might contain the answer to the query
docsearch \= FAISS.from\_texts(texts, new\_docsearch)

query \= "Your query here"
docs \= docsearch.similarity\_search(query)
print(docs\[0\].page\_content)

Another way to query is to use embeddings to build a prompt and then use LLM model like GPT-3 to answer the question directly.

from langchain.chains.question\_answering import load\_qa\_chain
from langchain.llms import OpenAI

chain \= load\_qa\_chain(OpenAI(temperature\=0), chain\_type\="stuff")
chain.run(input\_documents\=docs, question\=query)

Conclusion
----------

You can use this technique for all kinds of text data beyond just PDFs. You can also the techniques explained here to turn this into a web-based knowledge retrieval system.

Also, here is the [_**complete code**_](https://github.com/harish-garg/Doc-to-chatbot/blob/main/notebook.ipynb) used in this guide.
