Skip to main content

Free open-source RAG compression middleware. Drop-in API for LLM pipelines.

Project description

Winnow ๐ŸŒพ

Open-source RAG prompt compression middleware. Keep the signal. Drop the noise.

Built with FastAPI, LLMLingua-2, and tiktoken. MIT licensed, self-hostable, pip installable.

๐ŸŒ trywinnow.vercel.app ยท ๐Ÿ“ฆ PyPI ยท ๐Ÿค— HuggingFace Space ยท โญ GitHub


๐ŸŽฏ What is Winnow?

Winnow sits between your vector database and your LLM. It takes raw retrieved document chunks, compresses them using LLMLingua-2 token-level scoring guided by your query, and returns a shorter context that preserves answer-relevant content โ€” cutting token costs by ~50% with less than 3% accuracy loss.

โœจ Key Features

  • ๐Ÿ—œ๏ธ Token Compression: Cuts retrieved context by ~50% using LLMLingua-2
  • ๐ŸŽฏ Query-Guided: Compression is steered by your question โ€” relevant tokens survive
  • ๐Ÿ”’ Protected Words: Mark phrases that must never be removed
  • โš–๏ธ Ratio Control: Tune aggressiveness from 0.1 (light) to 0.9 (heavy)
  • ๐Ÿ”Œ OpenAI-Compatible Proxy: Drop-in /v1/chat/completions โ€” zero code changes
  • ๐Ÿฆœ LangChain Integration: Native WinnowCompressor drop-in wrapper
  • ๐Ÿณ Self-Hostable: Single Docker command, no API key required
  • ๐Ÿ“ฆ Pip Installable: pip install winnow-rag

๐Ÿ“Š Benchmarks

Tested on SQuAD with LLMLingua-2. Baseline F1: 78.4. Avg latency: ~85ms.

Preset Ratio Tokens In Tokens Out Reduction F1 Score F1 Drop
Light 0.7 420 294 ~30% 77.6 <1 pt
Balanced 0.5 420 210 ~50% 76.1 2.3 pt
Aggressive 0.3 420 147 ~65% 73.4 5.0 pt

๐Ÿš€ Quick Start

# Self-host in one command
docker run -p 8000:8000 itsaryanchauhan/winnow

API live at http://localhost:8000 ยท Docs at http://localhost:8000/docs

๐Ÿ“– Full Integration Examples (Docker, pip, LangChain, REST, OpenAI Proxy)

Option 1 โ€” Self-host with Docker

docker run -p 8000:8000 itsaryanchauhan/winnow

Option 2 โ€” pip install

pip install winnow-rag
from winnow import compress

result = compress(
    context=chunks,
    question=query,
    ratio=0.5
)

print(result["compressed_context"])
print(result["original_tokens"])     # e.g. 420
print(result["compressed_tokens"])   # e.g. 210
print(result["compression_ratio"])   # e.g. 0.5
print(result["savings_estimate"])    # e.g. "$0.000525"

Option 3 โ€” LangChain Drop-in

from winnow.langchain import WinnowCompressor

compressor = WinnowCompressor(ratio=0.5)
compressed_docs = compressor.compress_documents(docs, query)

Option 4 โ€” REST API (curl)

curl -X POST http://localhost:8000/v1/compress \
  -H "Content-Type: application/json" \
  -d '{
    "context": "your retrieved chunks here",
    "question": "what is the capital of France?",
    "ratio": 0.5,
    "protect": ["Paris", "France"]
  }'

Option 5 โ€” OpenAI-Compatible Proxy

Zero code changes if you already use the OpenAI SDK โ€” just swap the base URL:

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="not-needed"
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": your_prompt}]
)

Request Parameters

Field Type Required Description
context string โœ… Retrieved RAG chunks to compress
question string โœ… User query โ€” guides which tokens to keep
ratio float โŒ Compression aggressiveness 0.1โ€“0.9. Default: 0.5
protect string[] โŒ Words/phrases that must not be removed

Response Fields

Field Type Description
compressed_context string The compressed output
original_tokens int Token count before compression
compressed_tokens int Token count after compression
compression_ratio float Actual ratio achieved
savings_estimate string Estimated USD saved (gpt-4o pricing)

Batch Compression

curl -X POST http://localhost:8000/v1/compress/batch \
  -H "Content-Type: application/json" \
  -d '{
    "contexts": ["chunk one...", "chunk two..."],
    "question": "your query",
    "ratio": 0.5
  }'

๐Ÿ“ Project Structure

Winnow/
โ”œโ”€โ”€ app/               # FastAPI application
โ”‚   โ””โ”€โ”€ main.py        # API routes and server
โ”œโ”€โ”€ winnow/            # pip package
โ”‚   โ”œโ”€โ”€ __init__.py    # compress() function
โ”‚   โ””โ”€โ”€ langchain.py   # WinnowCompressor
โ”œโ”€โ”€ benchmarks/        # SQuAD benchmark scripts and results
โ”œโ”€โ”€ tests/             # Test suite
โ”œโ”€โ”€ website/           # Next.js website (trywinnow.vercel.app)
โ”œโ”€โ”€ Dockerfile
โ””โ”€โ”€ pyproject.toml

๐Ÿ”Œ API Endpoints

Method Endpoint Description
POST /v1/compress Compress a single context
POST /v1/compress/batch Compress multiple contexts
POST /v1/chat/completions OpenAI-compatible proxy with auto-compression
GET /health Health check

๐Ÿ› ๏ธ Built With

  • API: FastAPI + Python
  • Compression: microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank
  • Tokenizer: tiktoken (cl100k_base)
  • Deploy: Docker + HuggingFace Spaces

๐Ÿ‘ค Author

Created by Aryan Chauhan (@itsaryanchauhan)

๐Ÿ“ž Get in Touch

Have questions or suggestions? Open an issue on the GitHub repository.


MIT License ยท Live Demo ยท HuggingFace Space

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

winnow_compress-0.1.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

winnow_compress-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file winnow_compress-0.1.1.tar.gz.

File metadata

  • Download URL: winnow_compress-0.1.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for winnow_compress-0.1.1.tar.gz
Algorithm Hash digest
SHA256 803d977a1364dd2d841f46a0498b1d0b53fa0db32bc83624bfccd146adef887f
MD5 9b7c0222a9dcc6dbd3b6851112d1be09
BLAKE2b-256 8587abd8b9a67893167cd21f12009daa409123029405617a6880d037f7c4bdb5

See more details on using hashes here.

File details

Details for the file winnow_compress-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for winnow_compress-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d7093d88bccb0cda6f10c78294c431e33dce6bcdf5480ce1be1b90b6d4fdff5
MD5 ec4a7ce385c7138863d6b93a6bbdd78e
BLAKE2b-256 d41a1b01ff213da6aa18fd1202c70e24b8527ff05ee098795e947ec00ad711fc

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