forked from SylphAI-Inc/AdalFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
47 lines (34 loc) · 1.2 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import hashlib
import diskcache as dc
from pathlib import Path
from typing import Union
def hash_text(text: str):
return hashlib.sha256(f"{text}".encode()).hexdigest()
def hash_text_sha1(text: str): # 160 bits
return hashlib.sha1(text.encode()).hexdigest()
def direct(text: str):
return text
class CachedEngine:
def __init__(self, cache_path: Union[str, Path]):
super().__init__()
self.cache_path = Path(cache_path)
self.cache_path.parent.mkdir(parents=True, exist_ok=True)
self.cache = dc.Cache(cache_path)
def _check_cache(self, prompt: str):
hash_key = hash_text(prompt)
if hash_key in self.cache:
return self.cache[hash_key]
else:
return None
def _save_cache(self, prompt: str, response: str):
hash_key = hash_text(prompt)
self.cache[hash_key] = response
def __getstate__(self):
# Remove the cache from the state before pickling
state = self.__dict__.copy()
del state["cache"]
return state
def __setstate__(self, state):
# Restore the cache after unpickling
self.__dict__.update(state)
self.cache = dc.Cache(self.cache_path)