Spaces:
Running
on
T4
Running
on
T4
File size: 1,195 Bytes
d2beadd |
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 |
import hashlib
import diskcache as dc
from abc import ABC, abstractmethod
class EngineLM(ABC):
system_prompt: str = "You are a helpful, creative, and smart assistant."
model_string: str
@abstractmethod
def generate(self, prompt, system_prompt=None, **kwargs):
pass
def __call__(self, *args, **kwargs):
pass
class CachedEngine:
def __init__(self, cache_path):
super().__init__()
self.cache_path = cache_path
self.cache = dc.Cache(cache_path)
def _hash_prompt(self, prompt: str):
return hashlib.sha256(f"{prompt}".encode()).hexdigest()
def _check_cache(self, prompt: str):
if prompt in self.cache:
return self.cache[prompt]
else:
return None
def _save_cache(self, prompt: str, response: str):
self.cache[prompt] = 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) |