-
Notifications
You must be signed in to change notification settings - Fork 0
[codex] Add index-versioned query cache #35
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
Merged
Merged
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
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
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 |
|---|---|---|
| @@ -1,37 +1,74 @@ | ||
| """ | ||
| cache.py - LRU Embedding Cache for RecallForge. | ||
| cache.py - LRU query cache for RecallForge. | ||
|
|
||
| Avoids redundant embed_text / embed_image calls for repeated queries. | ||
| The cache is deterministic: same input → same vector, so caching is safe. | ||
| Avoids redundant query embeddings and generated expansion calls for repeated | ||
| queries. Keys can include model identity and storage index version so cached | ||
| retrieval inputs never cross model or index boundaries. | ||
| """ | ||
|
|
||
| import json | ||
| from hashlib import sha256 | ||
| import numpy as np | ||
| from typing import Any | ||
|
|
||
|
|
||
| class EmbeddingCache: | ||
| """Simple LRU cache backed by a dict + insertion-order list.""" | ||
|
|
||
| def __init__(self, maxsize: int = 256): | ||
| self._maxsize = maxsize | ||
| self._cache: dict[str, np.ndarray] = {} | ||
| self._cache: dict[str, Any] = {} | ||
| self._order: list[str] = [] | ||
| self._hits = 0 | ||
| self._misses = 0 | ||
|
|
||
| def get(self, key: str) -> "np.ndarray | None": | ||
| return self._cache.get(key) | ||
| def get(self, key: str) -> Any: | ||
| if key not in self._cache: | ||
| self._misses += 1 | ||
| return None | ||
| self._hits += 1 | ||
| self._order.remove(key) | ||
| self._order.append(key) | ||
| return self._cache[key] | ||
|
|
||
| def put(self, key: str, vector: np.ndarray) -> None: | ||
| def put(self, key: str, value: Any) -> None: | ||
| if key in self._cache: | ||
| self._order.remove(key) | ||
| elif len(self._cache) >= self._maxsize: | ||
| evict = self._order.pop(0) | ||
| del self._cache[evict] | ||
| self._cache[key] = vector | ||
| self._cache[key] = value | ||
| self._order.append(key) | ||
|
|
||
| def make_key(self, input_type: str, input_data: str) -> str: | ||
| return sha256(f"{input_type}:{input_data}".encode()).hexdigest() | ||
| def make_key( | ||
| self, | ||
| input_type: str, | ||
| input_data: str, | ||
| *, | ||
| model: str | None = None, | ||
| index_version: str | int | None = None, | ||
| namespace: str | None = None, | ||
| ) -> str: | ||
| if model is None and index_version is None and namespace is None: | ||
| return sha256(f"{input_type}:{input_data}".encode()).hexdigest() | ||
|
|
||
| payload = { | ||
| "type": input_type, | ||
| "data": input_data, | ||
| "model": model or "", | ||
| "index_version": "" if index_version is None else str(index_version), | ||
| "namespace": namespace or "", | ||
| } | ||
| return sha256(json.dumps(payload, sort_keys=True, separators=(",", ":")).encode()).hexdigest() | ||
|
|
||
| @property | ||
| def stats(self) -> dict: | ||
| return {"size": len(self._cache), "maxsize": self._maxsize} | ||
|
|
||
| @property | ||
| def metrics(self) -> dict: | ||
| return { | ||
| "size": len(self._cache), | ||
| "maxsize": self._maxsize, | ||
| "hits": self._hits, | ||
| "misses": self._misses, | ||
| } |
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
Oops, something went wrong.
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.
_cache_model_id()only inspectsmodel_name/model_id/model/_model_name, then falls back to the backend class name, but the shipped backends track the active embedding model in fields likeEMBEDDER_MODEL(and MLX can change it at runtime viaset_model_ids). In that case the cache key does not change when the embedder model changes, so repeated queries can reuse vectors generated by the previous model, producing retrieval in the wrong embedding space after a model switch.Useful? React with 👍 / 👎.