Surgically prune retrieved node texts before feeding into LlamaIndex query engines.
Integrate sentence-level prompt context reduction into LlamaIndex index retrievers and synthesized responses.
Install LLMSlim and the official LlamaIndex SDK using your package manager:
pip install llmslim llama-index-core1. LlamaIndex Retriever queries vector store for top-K nodes.
2. LLMSlim processes node text content with query-aware compression.
3. Dense context passed to ResponseSynthesizer.
Complete, runnable implementation wrapper for LlamaIndex:
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llmslim import compress_documents
# Ingest documents and initialize index
documents = SimpleDirectoryReader("./data").load_data()
index = VectorStoreIndex.from_documents(documents)
# Custom retrieval wrapper with LLMSlim pruning
retriever = index.as_retriever(similarity_top_k=5)
nodes = retriever.retrieve("What is total operating expenditure?")
node_texts = [n.get_content() for n in nodes]
compressed_nodes = compress_documents(node_texts, query="operating expenditure", target_ratio=0.35)
print(f"Compressed {len(node_texts)} nodes to high-density context payload.")| Metric Dimension | Uncompressed Payload | LLMSlim Compressed | Recorded Impact |
|---|---|---|---|
| RAG Node Text Volume | 7,500 tokens | 2,625 tokens | 65% Token Reduction |
LLMSlim processes node string content while maintaining your underlying NodeWithScore objects.