[docs]classCachedEngine: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)ifhash_keyinself.cache:returnself.cache[hash_key]else:returnNonedef_save_cache(self,prompt:str,response:str):hash_key=hash_text(prompt)self.cache[hash_key]=responsedef__getstate__(self):# Remove the cache from the state before picklingstate=self.__dict__.copy()delstate["cache"]returnstatedef__setstate__(self,state):# Restore the cache after unpicklingself.__dict__.update(state)self.cache=dc.Cache(self.cache_path)