LM wrapper that returns cached results if they exist, and uses the underlying LM if not. :param lm: LM Underlying LM :param cache_db: str Path to cache db
(self, lm, cache_db)
| 174 | |
| 175 | class CachingLM: |
| 176 | def __init__(self, lm, cache_db) -> None: |
| 177 | """LM wrapper that returns cached results if they exist, and uses the underlying LM if not. |
| 178 | |
| 179 | :param lm: LM |
| 180 | Underlying LM |
| 181 | :param cache_db: str |
| 182 | Path to cache db |
| 183 | """ |
| 184 | self.lm = lm |
| 185 | self.cache_db = cache_db |
| 186 | if os.path.dirname(cache_db): |
| 187 | os.makedirs(os.path.dirname(cache_db), exist_ok=True) |
| 188 | self.dbdict = SqliteDict(cache_db, autocommit=True) |
| 189 | |
| 190 | # add hook to lm |
| 191 | lm.set_cache_hook(self.get_cache_hook()) |
| 192 | |
| 193 | def __getattr__(self, attr): |
| 194 | lm_attr = getattr(self.lm, attr) |
nothing calls this directly
no test coverage detected