-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a `Cache` to the `LM` object. The cache implements LRU evection. Added tests to verify / demonstrate the behavior. --------- Co-authored-by: liana313 <[email protected]>
- Loading branch information
Showing
6 changed files
with
208 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pandas as pd | ||
|
||
import lotus | ||
from lotus.models import LM | ||
|
||
lm = LM(model="gpt-4o-mini") | ||
|
||
lotus.settings.configure(lm=lm, enable_cache=True) # default caching is False | ||
data = { | ||
"Course Name": [ | ||
"Probability and Random Processes", | ||
"Optimization Methods in Engineering", | ||
"Digital Design and Integrated Circuits", | ||
"Computer Security", | ||
] | ||
} | ||
df = pd.DataFrame(data) | ||
user_instruction = "{Course Name} requires a lot of math" | ||
df = df.sem_filter(user_instruction) | ||
print("====== intial run ======") | ||
print(df) | ||
|
||
# run a second time | ||
df = df.sem_filter(user_instruction) | ||
print("====== second run ======") | ||
print(df) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from collections import OrderedDict | ||
from functools import wraps | ||
from typing import Any, Callable | ||
|
||
import lotus | ||
|
||
|
||
def require_cache_enabled(func: Callable) -> Callable: | ||
"""Decorator to check if caching is enabled before calling the function.""" | ||
|
||
@wraps(func) | ||
def wrapper(self, *args, **kwargs): | ||
if not lotus.settings.enable_cache: | ||
return None | ||
return func(self, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
class Cache: | ||
def __init__(self, max_size: int): | ||
self.max_size = max_size | ||
self.cache: OrderedDict[str, Any] = OrderedDict() | ||
|
||
@require_cache_enabled | ||
def get(self, key: str) -> Any | None: | ||
if key in self.cache: | ||
lotus.logger.debug(f"Cache hit for {key}") | ||
|
||
return self.cache.get(key) | ||
|
||
@require_cache_enabled | ||
def insert(self, key: str, value: Any): | ||
self.cache[key] = value | ||
|
||
# LRU eviction | ||
if len(self.cache) > self.max_size: | ||
self.cache.popitem(last=False) | ||
|
||
def reset(self, max_size: int | None = None): | ||
self.cache.clear() | ||
if max_size is not None: | ||
self.max_size = max_size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters