Skip to content

Repository files navigation

AI PDF Analyzer with MongoDB, RAG, and Ollama

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.

Prerequisites

  • 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.

Clone the Repository

Run the following command to clone the repository to your machine:

git clone https://github.com/activus-d/pdf-doc-analyzer.git

Set Up Your Environment

Navigate into the project folder:

cd pdf-doc-analyzer

Start the Ollama server. This command holds its terminal, so leave it running and open a second terminal for the remaining steps:

ollama serve

Open a new terminal window and pull the language model by running the following command:

cd pdf-doc-analyzer
ollama pull llama3.2:3b

Create and activate a virtual environment:

python -m venv venv
source venv/bin/activate

On Windows, activate the virtual environment with:

venv\Scripts\activate

Install the required packages:

python -m pip install -r requirements.txt

Create 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.

Run the Tool

Make sure Ollama is running before you use either command.

Build the outline and summary for a PDF:

python cli.py analyze sample.pdf

Start an interactive question-and-answer session about the PDF:

python cli.py ask sample.pdf

The 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.

About

Complete sample code to build an AI PDF Analyzer with MongoDB, RAG, and Ollama

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages