Skip to content

Commit

Permalink
updated caching to be a globally set parameter, for both the logs and…
Browse files Browse the repository at this point in the history
… llm calls.
  • Loading branch information
djl11 committed Nov 7, 2024
1 parent 1a8e90f commit d5ed783
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
4 changes: 2 additions & 2 deletions tests/test_evals/test_utils/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ def test_log_caching():
cache_fname = ".test_log_caching.cache.json"
if os.path.exists(cache_fname):
os.remove(cache_fname)
unify.set_log_caching(True)
unify.set_log_caching_fname(cache_fname)
unify.set_caching(True)
unify.set_caching_fname(cache_fname)

# log
unify.log(project=project, a=0, b=1)
Expand Down
1 change: 1 addition & 0 deletions unify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def register_local_model(model_name: str, fn: Callable):
from .evals.utils.projects import *

from .utils import helpers, map, _caching
from .utils._caching import set_caching, set_caching_fname

from .universal_api import chatbot, clients, logging
from .universal_api.clients import multi_llm
Expand Down
35 changes: 13 additions & 22 deletions unify/evals/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

import unify
from unify import BASE_URL
from ...utils._caching import _get_cache, _write_to_cache
from ...utils._caching import (
_get_cache,
_write_to_cache,
_get_caching,
_get_caching_fname,
)
from ...utils.helpers import _validate_api_key, _get_and_maybe_create_project

# log
Expand Down Expand Up @@ -38,24 +43,6 @@
RUNNING_TIME = ContextVar("running_time", default=0.0)


# cache
LOG_CACHING = False
LOG_CACHE_FNAME = ".cache.json"


def set_log_caching(value: bool) -> None:
global LOG_CACHING, LOG_CACHE_FNAME
LOG_CACHING = value


def set_log_caching_fname(value: Optional[str] = None) -> None:
global LOG_CACHE_FNAME
if value is not None:
LOG_CACHE_FNAME = value
else:
LOG_CACHE_FNAME = ".cache.json"


def _removes_unique_trace_values(kw: Dict[str, Any]) -> Dict[str, Any]:
del kw["id"]
del kw["exec_time"]
Expand All @@ -70,21 +57,25 @@ def _removes_unique_trace_values(kw: Dict[str, Any]) -> Dict[str, Any]:

def _handle_cache(fn: Callable) -> Callable:
def wrapped(*args, **kwargs):
if not LOG_CACHING:
if not _get_caching():
return fn(*args, **kwargs)
kw_for_key = copy.deepcopy(kwargs)
if fn.__name__ == "add_log_entries" and "trace" in kwargs:
kw_for_key["trace"] = _removes_unique_trace_values(kw_for_key["trace"])
combined_kw = {**{f"arg{i}": a for i, a in enumerate(args)}, **kw_for_key}
ret = _get_cache(fn_name=fn.__name__, kw=combined_kw, filename=LOG_CACHE_FNAME)
ret = _get_cache(
fn_name=fn.__name__,
kw=combined_kw,
filename=_get_caching_fname(),
)
if ret is not None:
return ret
ret = fn(*args, **kwargs)
_write_to_cache(
fn_name=fn.__name__,
kw=combined_kw,
response=ret,
filename=LOG_CACHE_FNAME,
filename=_get_caching_fname(),
)
return ret

Expand Down
24 changes: 24 additions & 0 deletions unify/utils/_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@

CACHE_LOCK = threading.Lock()

CACHING = False
CACHE_FNAME = ".cache.json"


def set_caching(value: bool) -> None:
global CACHING, CACHE_FNAME
CACHING = value


def set_caching_fname(value: Optional[str] = None) -> None:
global CACHE_FNAME
if value is not None:
CACHE_FNAME = value
else:
CACHE_FNAME = ".cache.json"


def _get_caching():
return CACHING


def _get_caching_fname():
return CACHE_FNAME


def _create_cache_if_none(filename: str = None):
global _cache, _cache_fpath, _cache_dir
Expand Down

0 comments on commit d5ed783

Please sign in to comment.