-
Notifications
You must be signed in to change notification settings - Fork 0
Assignees: @leonvanbokhorst #14
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,90 @@ | ||
"""Module for caching embeddings.""" | ||
|
||
import hashlib | ||
from typing import Dict, List, Optional | ||
from typing import Dict, List, Optional, Union | ||
|
||
|
||
class EmbeddingCache: | ||
"""Hash-based cache for storing embeddings.""" | ||
""" | ||
Hash-based cache for storing embeddings. | ||
|
||
This class provides a simple key-value store for embeddings, using SHA-256 hashes | ||
of the input text as keys. This ensures consistent lookup regardless of minor | ||
text variations. | ||
|
||
Attributes: | ||
_cache (Dict[str, List[float]]): Internal storage for hashed key-value pairs. | ||
""" | ||
|
||
def __init__(self): | ||
self._cache: Dict[str, List[float]] = {} | ||
|
||
@staticmethod | ||
def get_stable_hash(text: str) -> str: | ||
"""Generate a stable hash for the given text.""" | ||
""" | ||
Generate a stable hash for the given text. | ||
|
||
Args: | ||
text (str): The input text to hash. | ||
|
||
Returns: | ||
str: A SHA-256 hash of the input text. | ||
""" | ||
return hashlib.sha256(text.encode()).hexdigest() | ||
|
||
def get(self, key: str) -> Optional[List[float]]: | ||
"""Retrieve an embedding from the cache using a hashed key.""" | ||
""" | ||
Retrieve an embedding from the cache using a hashed key. | ||
|
||
Args: | ||
key (str): The text key to look up. | ||
|
||
Returns: | ||
Optional[List[float]]: The stored embedding if found, None otherwise. | ||
""" | ||
return self._cache.get(self.get_stable_hash(key)) | ||
|
||
def set(self, key: str, value: List[float]) -> None: | ||
"""Store an embedding in the cache using a hashed key.""" | ||
""" | ||
Store an embedding in the cache using a hashed key. | ||
|
||
Args: | ||
key (str): The text key to associate with the embedding. | ||
value (List[float]): The embedding to store. | ||
""" | ||
self._cache[self.get_stable_hash(key)] = value | ||
|
||
def clear(self) -> None: | ||
"""Clear all entries from the cache.""" | ||
self._cache.clear() | ||
|
||
def __len__(self) -> int: | ||
""" | ||
Get the number of entries in the cache. | ||
|
||
Returns: | ||
int: The number of cached embeddings. | ||
""" | ||
return len(self._cache) | ||
|
||
def __contains__(self, key: str) -> bool: | ||
""" | ||
Check if a key exists in the cache. | ||
|
||
Args: | ||
key (str): The text key to check. | ||
|
||
Returns: | ||
bool: True if the key exists in the cache, False otherwise. | ||
""" | ||
return self.get_stable_hash(key) in self._cache | ||
|
||
def update(self, items: Dict[str, List[float]]) -> None: | ||
""" | ||
Update the cache with multiple key-value pairs. | ||
|
||
Args: | ||
items (Dict[str, List[float]]): A dictionary of text keys and their embeddings. | ||
""" | ||
for key, value in items.items(): | ||
self.set(key, value) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.