nemoguardrails.embeddings.cache
Module Contents
Classes
Functions
Data
API
Abstract class for cache stores.
Clear the cache.
Get a value from the cache.
Set a value in the cache.
Bases: CacheStore
Filesystem cache store.
This cache store persists the cache between runs by storing it in the filesystem as JSON files.
Example
>>> cache_store = FilesystemCacheStore(cache_dir=’.cache/embeddings’) >>> cache_store.set(‘key’, ‘value’) >>> print(cache_store.get(‘key’)) value
Parameters:
The directory where the cache files will be stored. Defaults to ”./cache”.
Bases: CacheStore
In-memory cache store.
This cache store keeps the cache in memory. It does not persist the cache between runs.
Example: >>> cache_store = InMemoryCacheStore() >>> cache_store.set(‘key’, ‘value’) >>> print(cache_store.get(‘key’)) # Outputs: ‘value’ value
Abstract class for key generators.
Bases: CacheStore
Redis cache store.
This cache store keeps the cache in a Redis database. It can be used to share the cache between multiple machines.
Example
>>> redis_client = redis.Redis(host=‘localhost’, port=6379, db=0) >>> cache_store = RedisCacheStore(redis_client=redis_client) >>> cache_store.set(‘key’, ‘value’) >>> print(cache_store.get(‘key’)) value
Parameters:
The Redis client to use. If not provided, a new client will be created.
Decorator to cache the embeddings.
This decorator caches the embeddings in the cache store.
It uses the cache_config attribute of the class to configure the cache.
If the class does not have a cache_config attribute, it will use the EmbeddingsCacheConfig by default.
This decorator can be applied to the _get_embeddings method of a subclass of EmbeddingsIndex that accepts a list of strings and returns a list of lists of floats.
Example
class MyClass: @property def cache_config(self): return EmbeddingsCacheConfig() @cache_embeddings async def get_embeddings(self, texts: List[str]) -> List[List[float]]:
implementation here
pass
Parameters:
The method to decorate. The first argument should be self.
Returns:
Callable[[Any, List[str]], Awaitable[List[List[float]]]]: The decorated method.