Skip to main content

Enterprise document compression for lower-cost LLM and RAG pipelines

Project description

DeepCompress

📄 Compress OCR-heavy documents into smaller, page-cited context for LLM and RAG workflows.

DeepCompress helps you reduce token usage while keeping important document facts traceable. It is useful for contracts, invoices, financial packets, claims, and other long documents where exact values matter.

Why Use It?

  • Reduce context size before sending document text to an LLM.
  • 🔎 Keep page-cited output for retrieval and answer evidence.
  • 🧾 Protect exact values like IDs, dates, amounts, emails, phone numbers, clauses, and totals.
  • 📊 Measure token savings with provider-aware token counters.
  • 🔐 Scrub common PII patterns before LLM analysis when needed.
  • 🧪 Run offline demos and benchmarks without API keys or external services.

Installation

pip install deepcompress

Optional extras:

pip install "deepcompress[gpu]"       # OCR model support
pip install "deepcompress[llm]"       # OpenAI / Anthropic integrations
pip install "deepcompress[vector-db]" # Pinecone / Weaviate integrations
pip install "deepcompress[all]"       # Everything for local development

Quick Start

Compress a document and inspect the measured savings:

import asyncio

from deepcompress import DeepCompressConfig, DocumentCompressor


async def main():
    config = DeepCompressConfig(
        dtoon_mode="raw",
        protect_facts=True,
        vector_db_provider="none",
        cache_enabled=False,
    )

    compressor = DocumentCompressor(config)
    result = await compressor.compress("contract.pdf")

    print(result.optimized_text)
    print(f"Original tokens: {result.original_tokens_measured:,}")
    print(f"Compressed tokens: {result.compressed_tokens_measured:,}")
    print(f"Compression ratio: {result.compression_ratio_measured:.2f}x")
    print(f"Protected facts: {result.protected_facts}")


asyncio.run(main())

Ask A Question

Use the high-level API when you want compression plus an LLM answer:

import asyncio

from deepcompress import compress_and_analyze


async def main():
    result = await compress_and_analyze(
        file="loan_application.pdf",
        query="What is the applicant's total monthly income?",
        llm="openai",
        scrub_pii=True,
    )

    print(result.answer)
    print(f"Tokens saved: {result.tokens_saved:,}")


asyncio.run(main())

For LLM-backed usage, set your provider key in the environment:

# macOS / Linux
export LLM_API_KEY=your-api-key

# Windows PowerShell
$env:LLM_API_KEY="your-api-key"

Common Configuration

from deepcompress import DeepCompressConfig

config = DeepCompressConfig(
    dtoon_mode="raw",              # raw, structured, or rag
    ocr_device="cpu",              # cpu, cuda:0, cuda:1
    token_counter_provider="openai",
    token_counter_model="gpt-4o",
    protect_facts=True,
    cache_enabled=False,
    vector_db_provider="none",
)

Useful modes:

Mode Best For
raw Fast compression without an LLM provider
structured Cleaner structured summaries using an API-backed LLM
rag Retrieval-friendly chunks for RAG pipelines

Offline Demo

Run the enterprise RAG demo without API keys, Redis, Pinecone, CUDA, or network access:

python examples/enterprise_rag_demo/demo.py

The demo compresses a synthetic loan document, builds a local in-memory index, answers a question with evidence, and prints token and cost savings.

Benchmarks

Run the deterministic benchmark:

python benchmarks/run_benchmark.py

The benchmark compares full-text RAG with DeepCompress RAG and reports token usage, cost, answer accuracy, retrieval hit rate, citation correctness, exact-value preservation, and latency.

What You Get Back

DocumentCompressor.compress() returns a compressed document result with:

  • optimized_text - the compressed D-TOON document text
  • original_tokens_measured - measured input token count
  • compressed_tokens_measured - measured compressed token count
  • compression_ratio_measured - measured compression ratio
  • tokens_saved - token reduction
  • cost_saved_usd - estimated GPT-4o-style input savings
  • protected_facts - exact values preserved during compression
  • processing_time_ms - total processing time

Notes And Limits

  • Compression quality depends on document type, OCR quality, and selected mode.
  • structured and rag modes require an API-backed LLM provider.
  • PII scrubbing is pattern-based and should be reviewed for your data domain.
  • Built-in benchmarks use synthetic fixtures; validate results on your own documents before publishing savings claims.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deepcompress-1.4.7.tar.gz (60.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deepcompress-1.4.7-py3-none-any.whl (53.6 kB view details)

Uploaded Python 3

File details

Details for the file deepcompress-1.4.7.tar.gz.

File metadata

  • Download URL: deepcompress-1.4.7.tar.gz
  • Upload date:
  • Size: 60.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for deepcompress-1.4.7.tar.gz
Algorithm Hash digest
SHA256 76e584d93f1eb9202e6fc67910ea7f3831c6e122a598bd20ed3bdfbdd8020165
MD5 5866ebd525bbe61fd85d627bca1ddd70
BLAKE2b-256 913a6e83cdd08e580b8a8ba326eb803adc73a1fd55df895feb5773082865a369

See more details on using hashes here.

File details

Details for the file deepcompress-1.4.7-py3-none-any.whl.

File metadata

  • Download URL: deepcompress-1.4.7-py3-none-any.whl
  • Upload date:
  • Size: 53.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for deepcompress-1.4.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b18dcd47029090c68df89aca70241e1806fb25d683f6911c431b52266f36fb00
MD5 1b5f0c5650084ca96b538c07087917c2
BLAKE2b-256 fe9750c1c7e4dbd4cec03e5f15773333fbac94857dc6e201547e7f4bc4adad70

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page