Prompt Compressor
Shrink long prompts before they reach your LLM. A multilingual token classifier scores every word and the least informative ones are deleted, so you pay for fewer tokens and get answers faster.
- Multilingual
- Apache-2.0
- Drop-in for LLMLingua-2
- Runs in your browser
Model card· Training recipe & evals· Cis Labs on Hugging Face
Target fraction of tokens to keep (llmlingua rate).
Use it in code
Already using LLMLingua-2? Only model_name changes. In JavaScript, the same algorithm
this page runs is two small files.
Model card: Cis-Labs/prompt-compressor-bert-base-multilingual-cased · code: GitHub
Get compressor.js and
deps.js from this Space. In a bundler project,
npm i @huggingface/transformers js-tiktoken and swap the CDN URLs in deps.js for the
package names (see the comment at its top).
How it works and limitations
- The text is split into chunks of up to 512 word pieces, preferably at sentence ends.
- A token classifier (mBERT, 178M parameters) gives every word piece a keep probability; a word's score is the mean of its pieces.
- Within each chunk, the highest-scoring words that fit the requested rate (measured in GPT-4 tokens) are
kept, in their original order. With Keep line breaks and questions ticked,
\nand?are always kept; with Keep numbers ticked, so is every word with a digit.
Everything runs on your device with ONNX Runtime Web, using the int8-quantized ONNX weights on
multi-threaded WebAssembly. The model is downloaded once, about 180 MB, and cached by your browser. Because
the weights are quantized, a few borderline words can differ from the full-precision model you run in Python. The chunking,
word merging and threshold are the same as llmlingua's LLMLingua-2 path, and the original
spacing between kept words is restored (llmlingua joins them with single spaces).
The model does not know what you will ask about the text, so it compresses for general informativeness. Check outputs where a dropped negation, unit or qualifier would matter, and do not use it on source code, tables or JSON. Characters outside mBERT's vocabulary (most emoji, some rare symbols) are always dropped.