babylon.rag.pre_embeddings.cache_manager

Embedding cache management for the RAG system.

This module provides functionality for caching embeddings to reduce duplicate operations and API costs.

Classes

CacheConfig(**data)

Configuration for embedding cache management.

EmbeddingCacheManager([config])

Manages caching of embeddings to reduce duplicate operations.

class babylon.rag.pre_embeddings.cache_manager.CacheConfig(**data)[source]

Bases: BaseModel

Configuration for embedding cache management.

Parameters:
  • max_cache_size (int)

  • persist_cache (bool)

  • cache_file_path (str)

  • hash_algorithm (str)

max_cache_size

Maximum number of embeddings to keep in cache

persist_cache

Whether to persist cache to disk

cache_file_path

Path to cache file for persistence

hash_algorithm

Algorithm to use for content hashing

model_config: ClassVar[ConfigDict] = {'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

max_cache_size: int
persist_cache: bool
cache_file_path: str
hash_algorithm: str
class babylon.rag.pre_embeddings.cache_manager.EmbeddingCacheManager(config=None)[source]

Bases: object

Manages caching of embeddings to reduce duplicate operations.

This class handles caching of embeddings based on content hashes, with support for LRU eviction, persistence, and metrics collection.

Parameters:

config (CacheConfig | None)

__init__(config=None)[source]

Initialize with configuration options.

Parameters:

config (CacheConfig | None) – Configuration for cache behavior

hash_content(content)[source]

Generate a hash for the given content.

Parameters:

content (str) – Content to hash

Return type:

str

Returns:

Hash string for the content

get_from_cache(content_hash)[source]

Retrieve an embedding from cache by content hash.

Parameters:

content_hash (str) – Hash of the content to retrieve embedding for

Return type:

list[float] | None

Returns:

Cached embedding or None if not found

add_to_cache(content_hash, embedding)[source]

Add or update an embedding in the cache.

Parameters:
  • content_hash (str) – Hash of the content

  • embedding (list[float]) – Embedding vector to cache

Return type:

None

clear_cache()[source]

Clear all entries from the cache.

Return type:

None

save_cache()[source]

Save the cache to disk if persistence is enabled.

Return type:

None

load_cache()[source]

Load the cache from disk if persistence is enabled.

Return type:

None

get_cache_stats()[source]

Get statistics about the current cache state.

Return type:

dict[str, Any]

Returns:

Dictionary of cache statistics