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.
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.
The Semantic Prompt Compressor offers a user-friendly CLI, allowing users to compress prompts and analyze results effortlessly. Here are some key commands:
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
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%
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
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%
To save compression results, execute:
prompt-compress compress "I am really interested in learning more about Python programming" -o results.json
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}%")
The library's compression rules are flexible, allowing users to define rules in YAML format. The default rules include:
Example rules configuration:
rule_groups:
remove_fillers:
enabled: true
patterns:
- pattern: "I am really"
replacement: "I'm"
- pattern: "more about"
replacement: "about"
Contributions are encouraged. Users interested in enhancing the library can submit Pull Requests.
No comments yet.
Sign in to be the first to comment.