PDFStract

repository·main·Indexed 41 days ago

https://github.com/aksarav/pdfstract

A unified data preparation layer for RAG (Retrieval-Augmented Generation) pipelines. PDFStract provides a single API, CLI, and WebUI to extract, chunk, and embed PDF documents, supporting over 25 implementations across converters (e.g., Marker, Docling, PyMuPDF4LLM), chunkers (e.g., Semantic, Recursive, Neural), and embedding providers (e.g., OpenAI, Google, Ollama). Version 1.1.1.

Tokens
27.8K
Snippets
104
Records
127
Agent score
96%

What's inside PDFStract

  1. Overview of PDFStract Architecture

    main

    PDFStract is a unified platform for PDF extraction, conversion, text chunking, and embedding. It is designed as a production-grade data preparation layer for RAG (Retrieval-Augmented Generation) workflows.

    Users can interact with the system through three primary entry points:

    1. CLI: For command-line operations.
    2. Web API: For service-based integration (FastAPI).
    3. Python Library: For direct programmatic use in Python applications.

    The architecture is built on a factory-based pattern that supports over 25 implementations across three core layers: Converters, Chunkers, and Embeddings.

  2. Configure PDF Conversion and Text Chunking in Web UI

    main

    The Web UI allows you to select specific tools and parameters for the extraction pipeline.

    PDF Conversion Libraries

    • Docling: Balanced performance and quality (default).
    • Marker: Best for complex layouts and figures.
    • PyMuPDF4LLM: Fastest for simple documents.
    • Unstructured: Great for document structure.
    • PaddleOCR: Best for scanned/image PDFs.

    Text Chunking Methods

    • Token: Simple token-based splitting (default).
    • Semantic: AI-powered semantic chunking.
    • Recursive: Smart recursive text splitting.
    • Sentence: Sentence-boundary aware.
    • Code: Code-aware chunking.

    Chunking Settings

    • Chunk Size: Target size in tokens (default: 512).
    • Overlap: Overlap between chunks (default: 50).
    • Min Size: Minimum chunk size threshold.
    • Max Size: Maximum chunk size limit.
  3. How the Factory Pattern works in PDFStract

    main

    PDFStract uses a Factory Pattern to manage its various implementations (Converters, Chunkers, and Embeddings). This pattern ensures the system is extensible and efficient.

    Key mechanisms include:

    • Registry: A static mapping that links provider names to their specific module paths and class names.
    • Lazy Loading: Modules are only imported via importlib.import_module() when they are first requested, ensuring fast startup times and low memory usage.
    • Singleton Cache: Once an implementation is instantiated, it is cached (thread-safely) for subsequent calls.
    • Dependency Injection: API keys, device selections, and model paths are injected during instantiation.
    • Credential Validation: The system performs runtime checks for API keys and configuration completeness.
    • Graceful Fallback: If an optional dependency is missing, the provider is marked as unavailable rather than causing a hard system failure.
  4. How PDFStract works: The RAG data pipeline

    main

    PDFStract follows a modular linear architecture designed to transform unstructured PDF data into structured, vector-ready content for RAG (Retrieval-Augmented Generation) applications:

    PDF Input $\rightarrow$ Converter Library $\rightarrow$ Raw Text $\rightarrow$ Chunker $\rightarrow$ Structured Chunks $\rightarrow$ Your RAG Pipeline

    Because each stage is modular, you can swap out specific components (like changing the extraction library or the chunking strategy) by simply changing a parameter, without altering the rest of your pipeline logic.

  5. Chunk Size Guidelines for RAG

    main

    Selecting the right chunk size and overlap is critical for retrieval performance. Use these recommended settings as a starting point:

    Use CaseRecommended SizeOverlap
    General RAG512 tokens50
    Detailed Q&A256 tokens25
    Summarization1024 tokens100
    Code search512 tokens50
    Long documents1024+ tokens100
  6. Compare PDF extraction libraries

    main

    Choose a library based on your document type and performance needs:

    LibrarySpeedQualityOCRBest For
    pymupdf4llm⚡ FastGood❌Simple text PDFs, quick extraction
    markitdown⚡ FastGood❌Markdown output, Microsoft docs
    marker🔄 MediumExcellent✅Complex layouts, figures, tables
    docling🔄 MediumExcellent✅Document intelligence, structure
    unstructured🔄 MediumVery Good✅Structured documents, forms
    pytesseract🐢 SlowGood✅Scanned PDFs, legacy documents
    paddleocr🔄 MediumVery Good✅Multi-language OCR
    deepseek⚡ FastVery Good✅GPU-accelerated OCR
    mineru🔄 MediumExcellent✅Scientific papers, LaTeX
  7. Understanding the PDFStract Data Flow

    main

    A typical request follows these phases:

    1. Entry: Triggered via CLI, Web endpoint, or Library method.
    2. Validation: Verifies the PDF path and configuration parameters.
    3. Conversion: The ConverterFactory loads a converter to read the PDF and extract text.
    4. Chunking: The ChunkerFactory loads a chunker to segment the text into Chunk objects.
    5. Embedding: The EmbeddingsFactory loads a wrapper to call an LLM or local model to generate vectors.
    6. Support:
      • Queue Manager: Parallelizes work.
      • Results Manager: Saves results to ~/.pdfstract/results/<task_id>/.
      • Logger: Records activity via Loguru to ~/.pdfstract/logs/.
      • Database: Persists metadata.
  8. Start the PDFStract Web UI

    main

    You can launch the PDFStract web interface using the CLI. By default, it runs on http://localhost:8000. You can customize the host and port using flags.

    CLI Commands

    • Basic start: pdfstract web
    • Custom port: pdfstract web --port <port>
    • Custom host and port: pdfstract web --host <host> --port <port>
    # Start PDFStract web interface
    pdfstract web
    
    # Or specify custom port
    pdfstract web --port 3000
    
    # With custom host
    pdfstract web --host 0.0.0.0 --port 8080
  9. Run the PDFStract Web UI

    main

    You can run the PDFStract frontend locally using make.

    1. Clone the repository.
    2. Run make up.
    3. Access the Web UI at http://localhost:3000 and the API at http://localhost:8000.
    git clone https://github.com/aksarav/pdfstract.git
    cd pdfstract
    make up
  10. Quick Start with PDFStract

    main

    You can run the complete RAG pipeline (Extract → Chunk → Embed) in a single line of code using convert_chunk_embed, or execute the steps individually for more control.

    Complete Pipeline

    from pdfstract import PDFStract
    
    pdfstract = PDFStract()
    result = pdfstract.convert_chunk_embed('document.pdf')

    Step-by-Step Pipeline

    from pdfstract import PDFStract
    
    pdfstract = PDFStract()
    
    # 1. Extract
    text = pdfstract.convert('document.pdf', library='auto')
    
    # 2. Chunk
    chunks = pdfstract.chunk(text, chunker='semantic', chunk_size=512)
    
    # 3. Embed
    vectors = pdfstract.embed_texts([c['text'] for c in chunks['chunks']])
    from pdfstract import PDFStract
    
    pdfstract = PDFStract()
    
    # Complete pipeline: Extract → Chunk → Embed
    result = pdfstract.convert_chunk_embed('document.pdf')