feat: caché-aware single file RAG#421
Open
mBerasategui-ehu wants to merge 1 commit into
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.
Implementation Log: Cache-Aware RAG & Grep RAG
Tracking all changes made per
IMPLEMENTATION_PLAN_CACHED_GREP_RAG.md.Cache-Aware Single File RAG ✅
Implementation choice: Option B — Extended
simple_augment.pywith automatic cache-aware mode detection. When an assistant hasrag_processor: "single_file_rag"in its metadata,simple_augmentautomatically splits the RAG context into a separate user message (before the question), enabling LLM provider prompt caching. No new prompt processor file needed. No auto-selection inmain.pyneeded. Fully backward compatible.Files Changed
backend/lamb/completions/pps/simple_augment.py_is_single_file_rag()helper + cache-aware logic. When assistant metadata hasrag_processor: "single_file_rag", context is emitted as a separate user message before conversation history. When not, existing template-based behavior is unchanged.backend/tests/test_cache_aware_augment.pybackend/tests/test_integration_cache.pycached_tokenswhile standard mode gets zero. Run with--realflag.docker-compose-example.yamlGLOBAL_LOG_LEVEL=WARNINGto${GLOBAL_LOG_LEVEL:-WARNING}so debug logs can be enabled from the project.envVerified with real API call (OpenAI gpt-4o-mini)
Integration test passed. Ran
python tests/test_integration_cache.py --real --size 12000against the live OpenAI API. Results:Real cost savings: 46% with gpt-4o-mini. With gpt-4o the savings would be ~88%.
How it works
simple_augmentchecksassistant.metadataforrag_processor == "single_file_rag"[system] → [user: file context] → [prev msgs] → [user: question]Example: messages sent to the LLM
With prompt template:
"Responde la pregunta del usuario: --- {user_input} ---\n\nEste es el contexto:\n--- {context} ---"Placeholder handling in cache mode
In cache mode,
{context}is replaced with empty string (the actual file content is already in Message 2). The--- ---structure remains as a template artifact. This avoids duplicating the file in the prompt while keeping the template structure intact.{user_input}is replaced with the actual question as usual.The replacement is language-agnostic — works regardless of what language the template is written in.
Verified in production (Docker + backend logs)
Confirmed working via
docker compose -f docker-compose-example.yaml up -d --buildwithGLOBAL_LOG_LEVEL=DEBUG. Backend log output:Message [0] and [1] are identical across requests → cached. Only message [2] changes.