-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Add LRU caching to AI recommendation engine #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LVT-ENG
wants to merge
1
commit into
main
Choose a base branch
from
bolt/add-ai-cache-6026025191079107177
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ## 2025-05-15 - [Add LRU caching to AI recommendation engine] | ||
| **Learning:** LLM API calls are a significant performance bottleneck in the recommendation pipeline. Since fashion advice for specific garment/event combinations is often static, caching these results provides a massive performance boost (from ~1s to <0.1ms). Using primitive, hashable types for cache keys is essential when working with complex objects like Pydantic models. | ||
|
|
||
| **Action:** Always wrap expensive, repeatable AI logic with `functools.lru_cache` using primitive keys to maximize hits and minimize latency. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import time | ||
| from unittest.mock import MagicMock | ||
| import jules_engine | ||
|
|
||
| def benchmark(): | ||
| print("--- ⚡ BOLT CACHE BENCHMARK (MOCKED LLM) ---") | ||
|
|
||
| # Mock the LLM call to simulate a real, slow API response | ||
| original_model = jules_engine.model | ||
| jules_engine.model = MagicMock() | ||
|
|
||
| def slow_generate_content(prompt): | ||
| time.sleep(1.0) # Simulate 1s network latency | ||
| mock_response = MagicMock() | ||
| mock_response.text = f"Mocked advice for: {prompt[:50]}..." | ||
| return mock_response | ||
|
|
||
| jules_engine.model.generate_content.side_effect = slow_generate_content | ||
|
|
||
| # Clear cache for a clean run | ||
| jules_engine._get_cached_advice.cache_clear() | ||
|
|
||
| test_args = ("Gala", "Balmain Slim-Fit Jeans", "Architectural and structured", "Minimal with memory retention") | ||
|
|
||
| print(f"Executing first call (uncached)...") | ||
| start_time = time.time() | ||
| advice1 = jules_engine._get_cached_advice(*test_args) | ||
| end_time = time.time() | ||
| uncached_duration = end_time - start_time | ||
| print(f"Uncached duration: {uncached_duration:.4f} seconds") | ||
|
|
||
| print(f"\nExecuting second call (cached)...") | ||
| start_time = time.time() | ||
| advice2 = jules_engine._get_cached_advice(*test_args) | ||
| end_time = time.time() | ||
| cached_duration = end_time - start_time | ||
| print(f"Cached duration: {cached_duration:.4f} seconds") | ||
|
|
||
| # Restore original model | ||
| jules_engine.model = original_model | ||
|
|
||
| if advice1 == advice2: | ||
| print("\n[SUCCESS] Cache returned identical result.") | ||
| speedup = uncached_duration / cached_duration if cached_duration > 0 else float('inf') | ||
| print(f"Performance Gain: {speedup:.1f}x faster") | ||
| else: | ||
| print("\n[ERROR] Cache mismatch!") | ||
|
|
||
| if __name__ == "__main__": | ||
| benchmark() |
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduces a dependency on
SECRET_KEY. Upon inspection of the full file (backend/main.py, line 22), the secret key is hardcoded:SECRET_KEY = "LVT_SECRET_PROD_091228222". Hardcoding secrets is a critical security vulnerability as it exposes sensitive credentials directly in the source code. This key should be managed securely by loading it from an environment variable or a secret management service, similar to howGEMINI_API_KEYis handled in the project.