LLMSlim + Haystack
A Haystack 2.x and 3 component that compresses retrieved documents between the retriever and the prompt builder.
LLMSlimDocumentCompressor is a Haystack component backed by llmslim.compress_documents. It takes documents and an optional query, returns the kept documents inside a token budget, runs locally with no model and no API call, and keeps each document's id, score and meta.
1. Package installation
The extra installs haystack-ai 2.0 or newer. The component works with Haystack 2.x and Haystack 3.
pip install "llmslim[haystack]" # haystack-ai; needs llmslim 0.8.1 or newer2. Architecture & execution flow
3. Runnable example and its output
from haystack import Document, Pipeline
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.document_stores.in_memory import InMemoryDocumentStore
from llmslim.integrations.haystack import LLMSlimDocumentCompressor
store = InMemoryDocumentStore()
store.write_documents(
[
Document(
content="Standard shipping takes three to seven business days within the country. "
"Express shipping arrives in one or two business days and costs fifteen dollars. "
"We ship to forty countries, and international orders clear customs in about a week. "
"Tracking numbers are emailed as soon as the parcel leaves the warehouse.",
meta={"source": "shipping.md"},
),
Document(
content="You can return any item within 30 days of delivery for a full refund. "
"Refunds go back to the original payment method within five business days. "
"Items must be unused and in their original packaging. "
"Gift cards and final-sale items cannot be returned.",
meta={"source": "refunds.md"},
),
Document(
content="The company was founded in 2015 by two engineers in Pune. "
"Our team of eighty people works across design, engineering and support. "
"The office has a rooftop garden and a small library of design books. "
"Every Friday afternoon the team hosts an open demo for customers.",
meta={"source": "about.md"},
),
]
)
pipeline = Pipeline()
pipeline.add_component("retriever", InMemoryBM25Retriever(document_store=store, top_k=3))
pipeline.add_component("compressor", LLMSlimDocumentCompressor(target_ratio=0.25))
pipeline.connect("retriever.documents", "compressor.documents")
query = "How many days do I have to return an item for a refund?"
result = pipeline.run({"retriever": {"query": query}, "compressor": {"query": query}})
for doc in result["compressor"]["documents"]:
print(round(doc.score, 3), doc.meta)
print(doc.content)Output (Python 3.12, haystack-ai 3.3.0, token counts from tiktoken):
5.838 {'source': 'refunds.md', 'llmslim_original_tokens': 50, 'llmslim_compressed_tokens': 40}
You can return any item within 30 days of delivery for a full refund. Refunds go back to the original payment method within five business days. Items must be unused and in their original packaging.The three retrieved documents share one budget of 25% of their 158 tokens (40 tokens). The shipping and company pages do not answer the question and are dropped; the refund page keeps the answer and loses its sentence about gift cards. The score is the BM25 retriever's.
4. Options
Keyword arguments, shown as name = default.