Spaces:
Configuration error
Configuration error
Download src/utils/cache_helper.py from Darshankumarr03/JanSahayak: direct link, hf CLI and curl.
- Browser
- Download file 1.66 kB
-
https://huggingface.co/spaces/Darshankumarr03/JanSahayak/resolve/main/src/utils/cache_helper.py
- Command line
-
hf download hf://spaces/Darshankumarr03/JanSahayak/src/utils/cache_helper.py
-
curl -L -o cache_helper.py https://huggingface.co/spaces/Darshankumarr03/JanSahayak/resolve/main/src/utils/cache_helper.py
1.66 kB
| """ | |
| Caching utilities for expensive operations. | |
| """ | |
| from functools import lru_cache | |
| import hashlib | |
| import json | |
| from typing import Any, Dict | |
| class CacheHelper: | |
| """Helper class for caching expensive operations.""" | |
| _llm_cache: Dict[str, Any] = {} | |
| _rag_cache: Dict[str, Any] = {} | |
| def hash_query(query: str, context: str = "") -> str: | |
| """Generate hash for caching key.""" | |
| combined = f"{query}|{context}" | |
| return hashlib.md5(combined.encode()).hexdigest() | |
| def get_llm_cache(key: str) -> Any: | |
| """Get cached LLM result.""" | |
| return CacheHelper._llm_cache.get(key) | |
| def set_llm_cache(key: str, value: Any): | |
| """Cache LLM result.""" | |
| # Limit cache size to prevent memory issues | |
| if len(CacheHelper._llm_cache) > 100: | |
| # Remove oldest entry (first key) | |
| first_key = next(iter(CacheHelper._llm_cache)) | |
| del CacheHelper._llm_cache[first_key] | |
| CacheHelper._llm_cache[key] = value | |
| def get_rag_cache(key: str) -> Any: | |
| """Get cached RAG result.""" | |
| return CacheHelper._rag_cache.get(key) | |
| def set_rag_cache(key: str, value: Any): | |
| """Cache RAG result.""" | |
| if len(CacheHelper._rag_cache) > 50: | |
| first_key = next(iter(CacheHelper._rag_cache)) | |
| del CacheHelper._rag_cache[first_key] | |
| CacheHelper._rag_cache[key] = value | |
| def clear_all(): | |
| """Clear all caches.""" | |
| CacheHelper._llm_cache.clear() | |
| CacheHelper._rag_cache.clear() | |