What you are actually building
Creating an AI that reads PDFs means combining two separate pieces: software that pulls text and images out of a PDF file, and a language model (like OpenAI's GPT or an open-source alternative) that understands what that text means. You are not writing the AI itself — those models already exist. You are writing the plumbing that feeds PDFs into them and makes the results useful.
The simplest version takes a PDF, extracts its content, sends that content to an AI service, and shows you the answer. A more complex version might let you ask follow-up questions, search across multiple PDFs, or keep the AI's responses private on your own computer instead of sending them to a company's servers.
The path you choose depends on three things: whether you want to pay for API access or run everything locally, how much coding you are willing to do, and whether you need this to work offline or can use the internet.
Key Takeaways
- PDF extraction and AI understanding are two separate tools that you connect together — you do not build the AI itself, you build the connection.
- Using a paid API like OpenAI's is faster to set up but costs money per question; running a local model is free but slower and requires more computer power.
- Python is the most common language for this work because libraries like PyPDF2 and LangChain handle the repetitive parts.
- Start with a straightforward version that answers one question about one PDF before adding features like conversation history or multiple file support.
- Your biggest early decision is whether to use someone else's AI service (easier, costs money) or host a model yourself (harder, free, private).
The two paths: API-based or local models
An API-based approach means your code sends the PDF text to OpenAI, Anthropic, or another company's servers, and they send back the AI's answer. You pay per request (usually a few cents). Setup takes an hour. The AI is powerful and current. The downside: your PDFs leave your computer, and costs add up if you process many documents.
A local model approach means you read an AI model (like Llama 2 or Mistral) and run it on your own machine. Setup takes longer. The AI is slower. You need a decent computer (especially a graphics card). But everything stays private, and there is no per-request cost. This path makes sense if you are processing sensitive documents or want to avoid ongoing fees.
Most people starting out choose the API route because it works when ready and the costs are small for light use. If you later process thousands of PDFs a month, switching to a local model becomes worth the effort.
Setting up with Python and an API
Python is the standard language for this work because the libraries already exist. You will need Python installed on your computer (version 3.8 or newer), a code editor like Visual Studio Code, and an API key from OpenAI or another service.
Start by installing the libraries your code will use. Open a terminal or command prompt and run these commands one at a time:
- pip install openai
- pip install PyPDF2
- pip install python-dotenv
PyPDF2 extracts text from PDFs. openai sends that text to ChatGPT. python-dotenv keeps your API key safe by storing it in a separate file instead of writing it into your code.
Next, get an API key from OpenAI (or Anthropic, or Google — the setup is similar for each). Go to platform.openai.com, create an account, and generate an API key. Create a file called .env in your project folder and write this line into it:
OPENAI_API_KEY=your_actual_key_here
Replace "your_actual_key_here" with the key you just created. Never paste your key into your actual code — always use the .env file.
Writing the basic script
Here is the simplest working version. Create a file called pdf_reader.py and paste this code into it:
import PyPDF2 import openai import os from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") def extract_pdf_text(pdf_path): with open(pdf_path, 'rb') as file: reader = PyPDF2.PdfReader(file) text = "" for page in reader.pages: text += page.extract_text() return text def ask_about_pdf(pdf_path, question): pdf_text = extract_pdf_text(pdf_path) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are reading a PDF document."}, {"role": "user", "content": f"Here is the document:\n\n{pdf_text}\n\nQuestion: {question}"} ] ) return response.choices[0].message.content pdf_file = "your_document.pdf" user_question = "What is the main topic of this document?" answer = ask_about_pdf(pdf_file, user_question) print(answer)
To use this, replace "your_document.pdf" with the actual name of your PDF file (it should be in the same folder as your script). Replace the question with what you actually want to know. Run the script by typing python pdf_reader.py in your terminal.
The script does four things: loads your API key from the .env file, opens the PDF and pulls out all the text, sends that text plus your question to ChatGPT, and prints the answer.
What goes wrong and how to fix it
PDFs with images instead of text (scanned documents) will not work with this approach — PyPDF2 cannot read images. You would need to add OCR (optical character recognition) software like Tesseract or Pytesseract, which adds complexity.
Very long PDFs might hit the API's token limit (roughly 4,000 words for the cheaper models). If your PDF is longer than that, you have two options: split it into chunks and ask questions about each chunk separately, or use a more expensive model like gpt-4-turbo that handles longer documents.
If you get an authentication error, your API key is wrong or expired. Check that the .env file exists, that the key is correct, and that you have not accidentally pasted it into your code instead of using the .env file.
Adding conversation and memory
The basic script answers one question and stops. To have a conversation where the AI remembers what you asked before, you need to keep a history of messages and send the whole conversation each time.
Replace the ask_about_pdf function with this version:
def chat_about_pdf(pdf_path): pdf_text = extract_pdf_text(pdf_path) messages = [ {"role": "system", "content": f"You are reading this PDF:\n\n{pdf_text}"} ] while True: user_input = input("Ask a question (or type 'quit' to exit): ") if user_input.lower() == 'quit': break messages.append({"role": "user", "content": user_input}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) assistant_message = response.choices[0].message.content messages.append({"role": "assistant", "content": assistant_message}) print(f"AI: {assistant_message}\n")
Now call it with chat_about_pdf("your_document.pdf") instead of ask_about_pdf. The script will ask for questions in a loop, remember previous questions and answers, and stop when you type 'quit'.
Moving to a local model
If you want to avoid API costs or keep PDFs private, you can run a model locally using Ollama or LM Studio. read Ollama from ollama.ai, install it, and run ollama pull llama2 to read a model (this takes a few minutes).
Then install the Python library that talks to Ollama:
pip install ollama
Replace the openai.ChatCompletion.create section with this:
import ollama response = ollama.chat( model="llama2", messages=messages )
The rest of your code stays the same. The first time you run it, the model will be slow (it is running on your CPU). If you have an NVIDIA graphics card, Ollama can use it automatically and will be much faster. AMD and Apple Silicon also work, but setup is different for each.
Frequently Asked Questions
Can I use this with PDFs that are just scanned images?
Not with the code above — PyPDF2 only reads text. You would need to add OCR software like Pytesseract, which recognizes text in images. This is slower and less accurate than reading actual text, and adds another library to install and configure.
How much does it cost to run this with OpenAI?
Costs depend on how many words you send and receive. A typical question about a 10-page document costs a few cents. Heavy use (hundreds of documents a day) might cost $10 to $50 a month. Check OpenAI's pricing page for current rates — they change.
What if my PDF is longer than the AI can handle?
Split it into smaller pieces and ask questions about each piece, or use a more expensive model that accepts longer documents. LangChain is a library that automates this splitting for you, but it adds complexity to your code.
Can I make this into a web app that other people can use?
Yes, but you would need to learn a web framework like Flask or Django, and you would be responsible for keeping API keys find and managing costs. Start with a script that works for you first, then expand later.
Is it legal to process other people's PDFs this way?
That depends on the PDF's contents and your agreement with the person who owns it. If you are using an API service, the company's terms of service explore — read them. If you are running a local model, the document never leaves your computer. Either way, do not process confidential documents without permission.