Skip to main content

Fast statistical compression for LLM prompts - 50% token reduction with 91% quality retention

Project description

Compression Prompt - Python Implementation

Fast, intelligent prompt compression for LLMs - Save 50% tokens while maintaining 91% quality

Python port of the Rust implementation. Achieves 50% token reduction with 91% quality retention using pure statistical filtering.

Quick Start

Installation

cd python
pip install -e .

Or install from source:

pip install -e ".[dev]"  # With development dependencies

Basic Usage

from compression_prompt import Compressor, CompressorConfig

# Use default configuration (50% compression)
compressor = Compressor()

text = """
Your long text here...
This will be compressed using statistical filtering
to save 50% tokens while maintaining quality.
"""

result = compressor.compress(text)

print(f"Original: {result.original_tokens} tokens")
print(f"Compressed: {result.compressed_tokens} tokens")
print(f"Saved: {result.tokens_removed} tokens ({(1-result.compression_ratio)*100:.1f}%)")
print(f"\nCompressed text:\n{result.compressed}")

Advanced Configuration

from compression_prompt import (
    Compressor, CompressorConfig, 
    StatisticalFilterConfig
)

# Custom compression ratio
config = CompressorConfig(target_ratio=0.7)  # Keep 70% of tokens
filter_config = StatisticalFilterConfig(
    compression_ratio=0.7,
    idf_weight=0.3,
    position_weight=0.2,
    pos_weight=0.2,
    entity_weight=0.2,
    entropy_weight=0.1,
)

compressor = Compressor(config, filter_config)
result = compressor.compress(text)

Quality Metrics

from compression_prompt import QualityMetrics

original = "Your original text..."
compressed = "Your compressed text..."

metrics = QualityMetrics.calculate(original, compressed)
print(metrics.format())

Output:

Quality Metrics:
- Keyword Retention: 92.0%
- Entity Retention: 89.5%
- Vocabulary Ratio: 78.3%
- Info Density: 0.845
- Overall Score: 89.2%

Command Line Usage

# Compress file to stdout
compress input.txt

# Conservative compression (70%)
compress -r 0.7 input.txt

# Aggressive compression (30%)
compress -r 0.3 input.txt

# Show statistics
compress -s input.txt

# Save to file
compress -o output.txt input.txt

# Read from stdin
cat input.txt | compress

Features

  • Zero Dependencies: Pure Python implementation, no external libraries required
  • Fast: Optimized statistical filtering
  • Multilingual: Supports 10+ languages (EN, ES, PT, FR, DE, IT, RU, ZH, JA, AR, HI)
  • Smart Filtering: Preserves code blocks, JSON, paths, identifiers
  • Contextual: Intelligent stopword handling based on context
  • Customizable: Fine-tune weights and parameters for your use case

Configuration Options

CompressorConfig

CompressorConfig(
    target_ratio=0.5,        # Target compression ratio (0.0-1.0)
    min_input_tokens=100,    # Minimum tokens to attempt compression
    min_input_bytes=1024     # Minimum bytes to attempt compression
)

StatisticalFilterConfig

StatisticalFilterConfig(
    compression_ratio=0.5,              # Keep 50% of tokens
    
    # Feature weights (sum should be ~1.0)
    idf_weight=0.3,                     # Inverse document frequency
    position_weight=0.2,                # Position in text (start/end important)
    pos_weight=0.2,                     # Part-of-speech heuristics
    entity_weight=0.2,                  # Named entity detection
    entropy_weight=0.1,                 # Local vocabulary diversity
    
    # Protection features
    enable_protection_masks=True,       # Protect code/JSON/paths
    enable_contextual_stopwords=True,   # Smart stopword filtering
    preserve_negations=True,            # Keep "not", "never", etc.
    preserve_comparators=True,          # Keep ">=", "!=", etc.
    
    # Domain-specific
    domain_terms=["YourTerm"],          # Always preserve these terms
    min_gap_between_critical=3          # Fill gaps between important tokens
)

Examples

Example 1: RAG System Context Compression

from compression_prompt import Compressor

# Compress retrieved context before sending to LLM
retrieved_docs = get_documents(query)
context = "\n\n".join(doc.text for doc in retrieved_docs)

compressor = Compressor()
result = compressor.compress(context)

# Save 50% tokens while maintaining quality
prompt = f"Context: {result.compressed}\n\nQuestion: {user_question}"
response = llm.generate(prompt)

Example 2: Custom Domain Terms

from compression_prompt import StatisticalFilterConfig, Compressor

# Preserve domain-specific terms
filter_config = StatisticalFilterConfig(
    domain_terms=["TensorFlow", "PyTorch", "CUDA", "GPU"]
)

compressor = Compressor(filter_config=filter_config)
result = compressor.compress(technical_text)

Example 3: Aggressive Compression

from compression_prompt import CompressorConfig, StatisticalFilterConfig, Compressor

# 70% compression (keep only 30% of tokens)
config = CompressorConfig(target_ratio=0.3, min_input_tokens=50)
filter_config = StatisticalFilterConfig(compression_ratio=0.3)

compressor = Compressor(config, filter_config)
result = compressor.compress(text)

print(f"Compressed to {result.compressed_tokens} tokens (from {result.original_tokens})")

Performance Characteristics

Compression Token Savings Keyword Retention Entity Retention Use Case
50% (default) 50% 92.0% 89.5% Best balance
70% (conservative) 30% 99.2% 98.4% High precision
30% (aggressive) 70% 72.4% 71.5% Maximum savings

Development

Running Tests

pytest tests/

Code Formatting

black compression_prompt/

Type Checking

mypy compression_prompt/

Differences from Rust Version

The Python implementation maintains feature parity with the Rust version:

  • ✅ Same statistical filtering algorithm
  • ✅ Same configuration options
  • ✅ Same quality metrics
  • ✅ CLI tool with identical interface
  • ⏳ Image output (optional, requires Pillow)

Performance differences:

  • Rust: ~0.16ms average, 10.58 MB/s throughput
  • Python: ~1-5ms average (still very fast for most use cases)

License

MIT

See Also

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

compression-prompt-0.1.0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

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

compression_prompt-0.1.0-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file compression-prompt-0.1.0.tar.gz.

File metadata

  • Download URL: compression-prompt-0.1.0.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for compression-prompt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0b52fbe27b1e713195a9ff83b1d6c503bccf0e0e44dcd6b36d17ef491cba9893
MD5 8fbd4c7845ee99c8c2bf16b3140dc980
BLAKE2b-256 f6a1693c77c98151ed8397a267416a74d7f1aa9a52954cc7ca333da1d3fd44ab

See more details on using hashes here.

File details

Details for the file compression_prompt-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for compression_prompt-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed3fbbdd70ce89039955f56e2ba61f5b914e210ae13477b32cd8a218e6b21ae1
MD5 a71e1e1077762c1725248230c8218038
BLAKE2b-256 68ea036df7b6722d07e93508b1dfe377ef20e85d4567dcc4b05d55a1a7fed6d1

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