This repository contains the code samples for the "How to Build an AI PDF Analyzer with MongoDB, RAG, and Ollama" tutorial. The sample project is a command-line tool that reads a PDF, builds an outline and summary, and answers questions about the document with page and section sources. It covers:
- PDF extraction with text, metadata, and bookmarks
- outline and summary generation through a local language model
- document chunking with page and section metadata
- vector embedding storage and Atlas Vector Search index creation
- retrieval and answer generation grounded in retrieved chunks
The tool is implemented in Python using Ollama with the llama3.2:3b model, the free nomic-embed-text-v1 embedding model via sentence-transformers, and a MongoDB Atlas free tier cluster.
- Python 3.10 or later
- Ollama installed on your machine
- A MongoDB Atlas account with an M0 (free tier) cluster
- Your MongoDB Atlas connection string, available under Database > Connect > Drivers in the Atlas UI
- Your current IP address added to the Atlas access list under Security > Network Access
- A sample PDF with an embedded table of contents and metadata (a title and an author); a copy is included in this repository
Add your current IP address to the MongoDB Atlas access list before you run any script. Go to Security > Network Access in the MongoDB Atlas UI and add it. Your scripts won't connect to your MongoDB cluster without this configuration.
Run the following command to clone the repository to your machine:
git clone https://github.com/activus-d/pdf-doc-analyzer.gitNavigate into the project folder:
cd pdf-doc-analyzerStart the Ollama server. This command holds its terminal, so leave it running and open a second terminal for the remaining steps:
ollama serveOpen a new terminal window and pull the language model by running the following command:
cd pdf-doc-analyzer
ollama pull llama3.2:3bCreate and activate a virtual environment:
python -m venv venv
source venv/bin/activateOn Windows, activate the virtual environment with:
venv\Scripts\activateInstall the required packages:
python -m pip install -r requirements.txtCreate a file named .env in the project folder and add your MongoDB Atlas connection string:
MONGODB_URI="mongodb+srv://<user>:<pass>@<host>/"
Replace <user>, <pass>, and <host> with the values from your Atlas connection string.
Make sure Ollama is running before you use either command.
Build the outline and summary for a PDF:
python cli.py analyze sample.pdfStart an interactive question-and-answer session about the PDF:
python cli.py ask sample.pdfThe ask command creates the vector search index automatically on its first run. Type a question at the prompt and press Enter. Press Enter on an empty line to quit.
Refer to the tutorial for a full explanation of each module, the RAG pipeline, and how the retrieval and answer-generation stages work.