Parameters
Parameter | Type | Default | Description |
---|---|---|---|
session_id | Optional[str] | f"session_{int(time.time())}" | Optional session identifier for cache isolation |
Functions
get_cached_response
Get cached response for the given input text.
Parameters:
input_text
(str): The input text to search for in cachecache_method
(CacheMethod): The cache method to use (“vector_search” or “llm_call”)cache_threshold
(float): Similarity threshold for vector searchduration_minutes
(int): Cache duration in minutesembedding_provider
(Optional[Any]): Embedding provider for vector searchllm_provider
(Optional[Union[Model, str]]): LLM provider for semantic comparison
Optional[Any]
: Cached response if found, None otherwise
store_cache_entry
Store a new cache entry.
Parameters:
input_text
(str): The input textoutput
(Any): The corresponding outputcache_method
(CacheMethod): The cache method usedembedding_provider
(Optional[Any]): Embedding provider for vector search
get_cache_stats
Get cache statistics.
Returns:
Dict[str, Any]
: Cache statistics including:- session_id: Session identifier
- total_entries: Total number of cache entries
- cache_hits: Number of cache hits
- cache_misses: Number of cache misses
- hit_rate: Cache hit rate (0.0 to 1.0)
clear_cache
Clear all cache entries.
get_cache_size
Get the number of cache entries.
Returns:
int
: Number of cache entries
get_session_id
Get the session ID.
Returns:
str
: The session identifier
Features
- Session-Level Caching: Manages cache storage and retrieval for tasks within a session
- Dual Cache Methods: Supports both vector search and exact match capabilities
- Vector Search: Uses embedding providers for semantic similarity matching
- LLM-Based Matching: Uses LLM providers for intelligent semantic comparison
- Cache Expiration: Automatic cleanup of expired cache entries based on duration
- Similarity Thresholding: Configurable similarity thresholds for vector search
- Batch Processing: Efficient batch comparison of cached queries using LLM
- Performance Metrics: Comprehensive cache statistics and hit rate tracking
- Session Isolation: Cache isolation between different sessions
- Error Handling: Robust error handling for embedding and LLM operations
- Memory Management: Efficient memory usage with automatic cleanup
- Debug Support: Detailed logging and error reporting for cache operations
Cache Methods
Vector Search ("vector_search"
)
- Uses embedding providers to create vector representations
- Calculates cosine similarity between input and cached vectors
- Finds most similar cached entry above threshold
- Supports configurable similarity thresholds
LLM Call ("llm_call"
)
- Uses LLM providers for intelligent semantic comparison
- Batch processes multiple cached entries for efficiency
- Leverages LLM reasoning for complex semantic matching
- Falls back to exact matching when LLM is not available