Skip to content

Mullassery/PyStreamPDF

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyStreamPDF

The Intelligence Engine for PDFs

License: MIT Version: v2.0.0 Status: Production Ready


The Problem You're Facing

You're using AI agents with RAG systems to work with PDFs. It works, but it's wasteful and expensive:

The Painful Truth

  • 📄 A 100-page technical manual takes 10-30 seconds to convert (if using multi-GPU)
  • 💰 Your token costs are 10-50x higher than necessary
  • 🔍 You generate embeddings for content your agent will never use
  • ⏱️ API calls are slow because you're passing entire document context
  • 💾 Storage costs balloon as you keep full markdown versions

Example: A 500-page user manual for a support chatbot:

  • Traditional: Convert 500 pages → 2-3 million tokens → $30-50 per conversation
  • PyStreamPDF: Find 2-3 relevant pages → 50-150k tokens → $0.30-1.50 per conversation

Why This Happens

Current tools force you into a bad workflow:

Traditional RAG Workflow (Wasteful):
1. Convert entire PDF to markdown (100% of pages)
2. Generate embeddings for everything (100% indexed)
3. Store complete representation (100% stored)
4. Retrieve small portions on demand (use ~1%)

Result: You're processing and paying for 100x more than you use

PyStreamPDF: A Better Way

Instead of converting everything, PyStreamPDF finds and converts only what matters:

PyStreamPDF Workflow (Efficient):
1. Analyze PDF structure (no conversion needed)
2. Find relevant pages intelligently (5-10% identified)
3. Convert only selected sections to markdown (5-10% processed)
4. Optimize context for your AI system

Result: 10-50x cost reduction with same or better accuracy

Concrete Benefits

Problem Traditional PyStreamPDF
Processing Time 30 seconds 0.5 seconds
Token Usage 2M tokens 50-150k tokens
Cost per Query $30-50 $0.30-1.50
Storage Full document Indexed metadata only
API Latency Slow (full context) Fast (minimal context)
Accuracy Hits irrelevant content Finds only relevant sections

Is PyStreamPDF Right for You?

You Need PyStreamPDF If You:

  • ✅ Use LLMs/AI agents to process PDFs (RAG, document Q&A, summarization)
  • ✅ Have large PDFs (100+ pages) and your token costs are growing
  • ✅ Want faster, cheaper AI-PDF interactions without sacrificing accuracy
  • ✅ Need to handle multiple PDF formats (technical docs, manuals, reports)
  • ✅ Work with encryption or permissions (secure PDFs)
  • ✅ Want production-ready, tested code (not experiments)

Use Cases

  • 📚 Document Q&A: Support chatbots, knowledge base search
  • 📊 Data Extraction: Pull specific information from reports
  • 📖 Summarization: Quick summaries without processing entire documents
  • 🔍 Research: Find citations and relevant sections across large archives
  • 🔒 Compliance & Audit: Contract analysis, compliance workflows

Quick Install & Run (2 minutes)

Ready to see it in action? Get started immediately:

Install

# Using pip
pip install pystreampdf

# Or using uv
uv add pystreampdf

# Or from source
git clone https://github.com/Mullassery/PyStreamPDF.git && cd PyStreamPDF && pip install -e .

30-Second Example

import pystreampdf

# Open and search
doc = pystreampdf.open("research_paper.pdf")
index = doc.build_index(":memory:")

# Find what you need (not the whole document!)
results = index.search("neural networks", top_k=3)
print(f"Found in {len(results)} pages, used only ~15K tokens")

That's it. No complex config, no wrapper scripts, no bloat.


How It Works (Under the Hood)

  1. Parse Structure — Analyze PDF hierarchy (headings, pages, metadata) without converting
  2. Intelligent Retrieval — Find relevant pages using semantic + structural + keyword search
  3. Selective Conversion — Convert only found pages to markdown (not the whole document)
  4. Token-Aware Assembly — Build context respecting your token budget
  5. Breadcrumb Navigation — Include heading paths so your AI understands context

The key insight: Most questions only need 1-5% of a PDF. Stop converting the other 95%.


More Examples

Open and Parse a PDF

import pystreampdf

# Open a PDF
doc = pystreampdf.open("example.pdf")
print(f"Pages: {doc.page_count}")

# Get a single page
page = doc.page(1)
print(f"Page 1 text: {page.text[:200]}")

# Get document structure
structure = doc.structure
for heading in structure.headings[:5]:
    print(f"{'  ' * heading.level}{heading.text}")

Build an Index and Search

# Build index for fast searching
index = doc.build_index("doc_index.db")

# Search for content
results = index.search("machine learning", top_k=5)
for result in results:
    print(f"Page {result.page_number}: {result.snippet}")

# Persist and reload
index2 = pystreampdf.load_index("doc_index.db")

Navigate with Agent Context

# Create a navigator for hierarchical browsing
nav = doc.navigator_with_index(index)

# Get top-level chapters
chapters = nav.chapters()
for chapter in chapters:
    print(f"Chapter: {chapter.heading.text} (pages {chapter.start_page}-{chapter.end_page})")

# Retrieve context for a query with token budget
context = nav.retrieve("attention mechanisms", max_tokens=2000)
print(f"Query: {context.query}")
print(f"Total tokens: {context.total_tokens}")
for section in context.sections:
    print(f"  {section.heading_path}: {len(section.content)} chars")

Security & Compliance Features

# Check if PDF is encrypted
is_encrypted = pystreampdf.PdfDocument.is_encrypted("document.pdf")

# Open encrypted PDF with password
doc = pystreampdf.PdfDocument.open_with_password("document.pdf", "password")

# Get document permissions
perms = pystreampdf.PdfDocument.permissions("document.pdf")
print(f"Can copy: {perms.can_copy}, Can print: {perms.can_print}")

# Fingerprint for integrity checking
fingerprint = doc.fingerprint()
print(f"SHA-256: {fingerprint}")

# Audit logging
audit = pystreampdf.PyAuditLog.new("audit.jsonl")
audit.record_open(doc.path)
audit.record_search(doc.path, "query", results_count=5)
events = audit.events()

Real Cost Savings Example

Processing a 300-page technical manual with GPT-4 for support queries:

Traditional RAG System

  • Manual → Markdown: ~20 seconds
  • Embeddings generated: 300 pages × 400 tokens = 120,000 tokens
  • Per query tokens: 120,000 (full doc) + 500 (query) = 120,500 tokens
  • Cost per query: ~$1.80 (at $15/1M tokens)
  • Monthly cost (1,000 queries): ~$1,800

PyStreamPDF

  • Manual → Analyzed: ~0.5 seconds (structure only)
  • Pages indexed: Metadata only (no embeddings)
  • Per query tokens: 2,000 (relevant pages) + 500 (query) = 2,500 tokens
  • Cost per query: ~$0.04 (at $15/1M tokens)
  • Monthly cost (1,000 queries): ~$40

Savings: 95% cost reduction ($1,760/month) while improving accuracy


Feature Comparison

Feature Traditional PyStreamPDF
PDF Parsing ⏱️ Slow ✅ Fast
Token Efficiency ❌ Uses all tokens ✅ Uses 5-10%
Retrieval Speed ❌ Slow (full context) ✅ <50ms
Cost per Query ❌ $1-10 ✅ $0.01-1
Large Documents ❌ Memory issues >100 pages ✅ Handles 1000+ pages
Structured Navigation ❌ Manual parsing ✅ Automatic hierarchy
Security Support ❌ Basic ✅ Encryption, permissions, audit
Semantic Understanding ❌ None ✅ Entities, relationships, knowledge graphs
Fact Verification ❌ None ✅ Grounding, hallucination detection
Intelligent Assembly ❌ Fixed order ✅ 4 adaptive strategies
Production Ready ❌ Experimental ✅ 94/94 tests passing

Current Status: v2.0.0 (Semantic Intelligence)

What's Complete

Phase 1a: Foundation (v0.1)

  • Project scaffolding with Cargo workspace
  • Core data types (document, page, structure)
  • Python bindings via PyO3

Phase 1b: Intelligent Indexing (v0.5)

  • Real PDF parsing with pdfium-render
  • SQLite knowledge index with FTS5
  • Keyword search, page retrieval, index persistence

Phase 2: Agent Integration (v1.0)

  • Hierarchical heading extraction with page ranges
  • Dynamic markdown generation with token budgets
  • Token-efficient context assembly
  • PdfNavigator for structured browsing

Phase 3: Advanced Features (v1.5)

  • Full-text FTS5 indexing (not just preview)
  • Thread-safe index sharing with Arc
  • Real heading level detection (H1-H4)
  • Breadcrumb paths in context sections
  • Security module (encryption detection, password handling, permissions)
  • Audit logging with JSON-lines format
  • Form field detection framework
  • Scanned PDF detection
  • SHA-256 fingerprinting

Phase 4: Semantic Intelligence (v2.0) — CURRENT (94 tests passing)

Phase 4.1: Entity Extraction (19 tests)

  • Concept extraction: persons, organizations, locations, concepts, methods, metrics, dates
  • Pattern matching + domain-specific keyword detection (14 categories)
  • Confidence scoring per entity
  • Batch processing and deduplication

Phase 4.2: Relationships & Knowledge Graphs (30 tests)

  • 14 relationship types with bidirectional reversal (CITES, EXTENDS, REFUTES, USES, ENABLES, REFINES, AUTHOR_OF, RELATED_TO)
  • Pattern-based extraction with evidence tracking
  • In-memory knowledge graph with adjacency lists
  • BFS neighbor queries at varying depths, shortest path finding
  • Similarity detection via Jaccard coefficient
  • Influence calculation and node statistics

Phase 4.3: Fact Verification & Context Assembly (22 tests)

  • Grounding as confidence spectrum (0-1), not binary
  • 5 verification status levels: GROUNDED, PARTIALLY_GROUNDED, NOT_GROUNDED, REFUTED, UNCERTAIN
  • Evidence-based fact verification with support/refute analysis
  • Hallucination detection with confidence thresholds
  • 4 assembly strategies (scholarly, technical, survey, tutorial)
  • Token-aware context optimization respecting budget constraints
  • Coverage and coherence scoring

Why PyStreamPDF

Performance First

  • Parse PDFs 10x faster than traditional approaches
  • Retrieve relevant pages in <50ms
  • Convert selected pages to markdown in <1s

Cost Reduction

  • 10-50x reduction in token consumption
  • Eliminate unnecessary processing
  • Orders of magnitude savings for large document collections

AI-Native Design

  • APIs built for how agents actually work
  • Agent-native navigation
  • Token-aware context generation

Production-Ready

  • Security-aware (encrypted PDFs, permissions)
  • Large document optimization (1000+ pages)
  • Production observability

Open Source

  • MIT License
  • No vendor lock-in
  • Community-driven

The Insight

Most questions require less than 1% of a PDF.

Most AI systems currently process 100% anyway.

PyStreamPDF changes that fundamental inefficiency.


License

MIT License — See LICENSE for details


Vision

Transform how the world works with PDF data in AI systems.

From:

"A faster PDF-to-Markdown converter"

To:

"The retrieval engine for PDFs"

Only convert what's needed. Retrieve what matters. Optimize everything else.

About

Intelligence engine for PDFs. Selective retrieval, structure analysis, token-efficient RAG. 10-50x cost reduction for document processing.

Topics

Resources

License

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages