PitchHut logo
Semantic Prompt Compressor
by excellent_apricot_mara
Reduce LLM token count while preserving meaning.
Pitch

This Python library compresses prompts effectively without sacrificing important information. Users can utilize modular, rule-based compression with configurable options. The CLI offers features for compression, analysis, and statistics, making it an essential tool for optimizing large language model interactions.

Description

Semantic Prompt Compressor

The Semantic Prompt Compressor is a powerful Python library designed to optimize and compress prompts for large language models (LLMs) while maintaining their semantic integrity. This tool significantly reduces token usage, making it ideal for developers and researchers working with natural language processing.

Key Features

  • Modular and Rule-Based Compression: Tailor the compression process with customizable rules tailored to specific use cases.
  • Multiple Compression Profiles: Choose from various profiles, such as 'safe' and 'quality', to optimize the approach based on requirements.
  • Configurable Compression Rules: Adapt the compression algorithms using rules defined in YAML format.
  • Token Count Reduction Analysis: Gain insights into the effectiveness of compression through detailed statistics.
  • Command-Line Interface (CLI): Easily utilize the tool through straightforward command-line commands.

Usage

Command Line Interface

The Semantic Prompt Compressor offers a user-friendly CLI, allowing users to compress prompts and analyze results effortlessly. Here are some key commands:

Basic Compression

To compress a prompt, execute:

prompt-compress compress "I am really interested in learning more about Python programming"

Output:

I'm interested in learning about Python programming
Show Compression Statistics

For insights on compression statistics, use:

prompt-compress compress "I am really interested in learning more about Python programming" -s

Output:

=== Compression Statistics ===
Original tokens: 12
Compressed tokens: 9
Tokens saved: 3
Compression ratio: 25.00%
Detailed Analysis

To further analyze compression results, run:

prompt-compress compress "I am really interested in learning more about Python programming" -v

Output:

=== Compression Statistics ===
Original tokens: 12
Compressed tokens: 9
Tokens saved: 3
Compression ratio: 25.00%

=== Detailed Analysis ===
Original text: I am really interested in learning more about Python programming
Compressed text: I'm interested in learning about Python programming
Analyze Compression Opportunities

Evaluate potential compression with:

prompt-compress analyze "I am really interested in learning more about Python programming"

Output:

=== Compression Statistics ===
Original tokens: 12
Compressed tokens: 9
Tokens saved: 3
Compression ratio: 25.00%
Save Results to File

To save compression results, execute:

prompt-compress compress "I am really interested in learning more about Python programming" -o results.json

Python API Usage

In addition to the CLI, the library offers a Python API for integration into applications. Here’s how to use it:

from prompt_compressor import PromptCompressor

# Initialize with default rules
compressor = PromptCompressor()

# Compress a prompt
compressed = compressor.compress("I am really interested in learning more about Python programming")

# Analyze compression
analysis = compressor.analyze_prompt("I am really interested in learning more about Python programming")
print(f"Original tokens: {analysis['original_tokens']}")
print(f"Compressed tokens: {analysis['compressed_tokens']}")
print(f"Compression ratio: {analysis['compression_ratio']:.2f}%")

Compression Rules

The library's compression rules are flexible, allowing users to define rules in YAML format. The default rules include:

  • Removal of fillers and greetings
  • Stripping unnecessary modifiers
  • Collapsing redundant phrases
  • Preserving technical terms and entities
  • Proper handling of contractions

Example rules configuration:

rule_groups:
  remove_fillers:
    enabled: true
    patterns:
      - pattern: "I am really"
        replacement: "I'm"
      - pattern: "more about"
        replacement: "about"

Contributing

Contributions are encouraged. Users interested in enhancing the library can submit Pull Requests.

0 comments

No comments yet.

Sign in to be the first to comment.