LLMSlim + LlamaIndex
A BaseNodePostprocessor for query engines. Use it where you would use LongLLMLinguaPostprocessor.
LLMSlimPostprocessor is a LlamaIndex BaseNodePostprocessor backed by llmslim.compress_documents. It keeps the nodes that answer the query inside a token budget, runs locally with no model and no API call, and keeps each node's score, id and metadata. Python 3.10+.
1. Package installation
The extra installs llama-index-core 0.12 or newer and needs Python 3.10 or newer: llama-index-core dropped Python 3.9 in 0.14.17, and its last 3.9 release does not import on 3.9 (its banks dependency uses 3.10 syntax).
pip install "llmslim[llamaindex]" # llama-index-core; Python 3.10+; needs llmslim 0.8.1 or newer2. Architecture & execution flow
3. Runnable example and its output
from llama_index.core import Document, VectorStoreIndex
from llama_index.core.embeddings import MockEmbedding
from llama_index.core.llms import MockLLM
from llmslim.integrations.llama_index import LLMSlimPostprocessor
documents = [
Document(
text="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.",
metadata={"source": "shipping.md"},
),
Document(
text="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.",
metadata={"source": "refunds.md"},
),
Document(
text="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.",
metadata={"source": "about.md"},
),
]
# MockEmbedding and MockLLM keep the example offline; use your own models.
index = VectorStoreIndex.from_documents(documents, embed_model=MockEmbedding(embed_dim=8))
query_engine = index.as_query_engine(
llm=MockLLM(),
similarity_top_k=3,
node_postprocessors=[LLMSlimPostprocessor(target_ratio=0.25)],
)
response = query_engine.query("How many days do I have to return an item for a refund?")
for item in response.source_nodes:
print(round(item.score, 3), item.node.metadata)
print(item.node.get_content())Output (Python 3.12, llama-index-core 0.14.25, token counts from tiktoken):
1.0 {'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 nodes share one budget of 25% of their 158 tokens (40 tokens). The shipping and company nodes do not answer the question and are dropped; the refund node keeps the answer and loses its sentence about gift cards. The score is the retriever's (the mock embedding scores every node 1.0).
4. Options
Keyword arguments, shown as name = default.