Download utils/cache.py from a2zsolun/AlHaq_QA: direct link, hf CLI and curl.
- Browser
- Download file 1.24 kB
-
https://huggingface.co/spaces/a2zsolun/AlHaq_QA/resolve/main/utils/cache.py
- Command line
-
hf download hf://spaces/a2zsolun/AlHaq_QA/utils/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/a2zsolun/AlHaq_QA/resolve/main/utils/cache.py
1.24 kB
| """ | |
| Cache Module | |
| Responsibilities: | |
| - Store previous responses | |
| - Return cached answers | |
| - Reduce repeated LLM requests | |
| """ | |
| from functools import wraps | |
| # ============================================================ | |
| # Memory Cache Storage | |
| # ============================================================ | |
| response_cache = {} | |
| # ============================================================ | |
| # Cache Decorator | |
| # ============================================================ | |
| def cached_response(function): | |
| """ | |
| Decorator for caching function responses. | |
| """ | |
| def wrapper(*args, **kwargs): | |
| # Create unique cache key | |
| cache_key = str( | |
| args | |
| ) + str( | |
| kwargs | |
| ) | |
| # Check existing response | |
| if cache_key in response_cache: | |
| return response_cache[ | |
| cache_key | |
| ] | |
| # Generate new response | |
| result = function( | |
| *args, | |
| **kwargs | |
| ) | |
| # Store response | |
| response_cache[ | |
| cache_key | |
| ] = result | |
| return result | |
| return wrapper |