Add score visibility, cache-or-compute pattern, and performance improvements#8
Open
4RSIM3R wants to merge 2 commits into
Open
Add score visibility, cache-or-compute pattern, and performance improvements#84RSIM3R wants to merge 2 commits into
4RSIM3R wants to merge 2 commits into
Conversation
…d batch upsert optimization
…d batch upsert optimization
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
This PR introduces three major enhancements to the semantic cache library that improve developer experience, performance, and observability:
minProximitywith validation - Makes the API more user-friendlygetWithScore()method - Provides visibility into similarity scores for debugging and tuninggetOrSet()method - Implements cache-or-compute pattern for expensive operationsMotivation
Problem 1: No default
minProximityCurrently,
minProximityis required in the config, but the README claims it defaults to 0.9. This creates confusion and forces users to always specify a value even when the default would work.Problem 2: Poor bulk upsert performance
The current implementation loops through items and makes sequential HTTP calls:
Problem 3: No visibility into similarity scores
Users can't see why something matched or didn't match, making it difficult to:
minProximitythresholdProblem 4: Manual cache-miss handling
Users must manually implement the cache-or-compute pattern for expensive operations like LLM calls, leading to boilerplate code.
Changes
1. Default
minProximitywith validation ✅Before:
After:
minProximityis now optional and defaults to0.9[0, 1]are automatically clamped (e.g.,-0.5→0,1.5→1)minProximity?: number2. Batch upsert optimization 🚀
Before: N sequential HTTP requests
After: Single batch HTTP request
Performance impact:
3.
getWithScore()method 🔍Returns cached values with similarity scores for observability:
Use cases:
minProximitySupports both single and bulk operations:
4.
getOrSet()method 💡Implements cache-or-compute pattern to reduce boilerplate:
Features:
minProximity)Bulk example:
Testing
Added comprehensive test coverage:
Validation tests (4 tests):
minProximityis 0.9getWithScore()tests (4 tests):undefinedon missgetOrSet()tests (5 tests):Test results:
All existing tests pass - no breaking changes.
Documentation
Updated
README.mdwith:minProximitydocumentation (optional, defaults to 0.9, auto-clamping)getWithScore()examples (single + bulk)getOrSet()examples (single + bulk, async functions)CacheWithScoreResulttype for TypeScript usersBreaking Changes
None. This is a backwards-compatible enhancement:
minProximitycan still be explicitly providedAdditional Changes
.husky/pre-commithook (was runningbun test runinstead ofbun test)CacheWithScoreResulttype export for TypeScript consumersafterEachMigration Guide
No migration needed! But you can take advantage of new features:
Optional: Simplify initialization
Optional: Add score visibility
Optional: Use cache-or-compute
Checklist
Related Issues
Addresses common feature requests:
Thank you for reviewing! I'm happy to make any changes or answer questions.