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.9.tar.gz (62.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.9-py3-none-any.whl (54.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: deepcompress-1.4.9.tar.gz
  • Upload date:
  • Size: 62.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.9.tar.gz
Algorithm Hash digest
SHA256 a1e8234b5d53234c69f0d5b2eb13c4465dba68ce3242220518946ef64c7a463e
MD5 a1a0c8e9407fb565996f83e0048f7184
BLAKE2b-256 bee119aa026d10bf93a9eb7adfceaeac05a6c59e1c6bd12719d5ee6e04655958

See more details on using hashes here.

File details

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

File metadata

  • Download URL: deepcompress-1.4.9-py3-none-any.whl
  • Upload date:
  • Size: 54.8 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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0901e29a8a611e56a880a7316f65bd7428affebc066426e9199fa69869450952
MD5 59a25b1ff3de43ebf6f6e5329d70fc4f
BLAKE2b-256 98f7be09ed45cb638989b863d39d7950c25f02c723b949ed783d0b9bd48dd822

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