feat(retriever): add token-aware context truncation to cap documents passed to LLM#139
Open
GovindhKishore wants to merge 1 commit intoreactome:mainfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds token-aware context truncation to
HybridRetrieverto cap the number of documents and tokens passed to the LLM. Fixes the unbounded document passing problem described in #138.Motivation
create_stuff_documents_chaininrag_chain.pystuffs ALL retrieved documents into the LLM context with zero token awareness. With two databases installed (Reactome + UniProt), this results in 60-100 documents per query reaching the LLM, causing:Changes
New -
src/util/context_truncator.pytruncate_to_token_limit()- truncates a ranked document list to fit within a token budget and document count limitlangchain-openai, zero new dependenciesModified -
src/retrievers/csv_chroma.pyretrieve_documents()- returnstruncate_to_token_limit(subdirectory_docs)instead of rawsubdirectory_docsaretrieve_documents()- same change applied to async retrieval pathtruncate_to_token_limitfromutil.context_truncatorModified -
config_default.ymlretriever.context_truncationblock withmax_docs: 15andmax_tokens: 12000HybridRetrievercan be added later - current defaults are hardcodedWhy These Default Values
Expected Impact
Limits context passed to the LLM to a maximum of 15 documents and 12,000 tokens per query, down from an unbounded 60-100 documents. This reduces token usage significantly and avoids the "lost in the middle" quality degradation that occurs with excessively long contexts.
Note: Exact token counts per document depend on installed database versions and chunk sizes. A follow-up evaluation with real embeddings will quantify the precise reduction.
Note: GPT-4o supports a 128k context window but the 12,000 token limit is intentional - it reserves space for system prompt, chat history, and model output, while avoiding the "lost in the middle" quality degradation that occurs with excessively long contexts.
Interaction With Other PRs
Truncation runs at the end of
retrieve_documents()andaretrieve_documents()- after WRR ranking and after FlashRank reranking (PR #116). This means truncation always removes the least relevant documents from last, preserving quality ordering established by both ranking stages.Related