From ef1c3d3f7f46fd7323aeecb7000ad99c43564a75 Mon Sep 17 00:00:00 2001 From: afourney Date: Mon, 20 Nov 2023 12:32:55 -0800 Subject: [PATCH] Fixes cache issue from 703 and 679 (#707) * Avoid creating cache database when cache_seed is None (which disables cache) * Add some debugging. * Removed some debugging. * Update autogen/oai/client.py Co-authored-by: Chi Wang * Fixed missing filter function logic from oai/client.py --------- Co-authored-by: Chi Wang --- autogen/oai/client.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/autogen/oai/client.py b/autogen/oai/client.py index 18240596e26f..ee59f06e8095 100644 --- a/autogen/oai/client.py +++ b/autogen/oai/client.py @@ -226,8 +226,10 @@ def yes_or_no_filter(context, response): cache_seed = extra_kwargs.get("cache_seed", 41) filter_func = extra_kwargs.get("filter_func") context = extra_kwargs.get("context") - with diskcache.Cache(f"{self.cache_path_root}/{cache_seed}") as cache: - if cache_seed is not None: + + # Try to load the response from cache + if cache_seed is not None: + with diskcache.Cache(f"{self.cache_path_root}/{cache_seed}") as cache: # Try to get the response from cache key = get_key(params) response = cache.get(key, None) @@ -240,17 +242,28 @@ def yes_or_no_filter(context, response): response.pass_filter = pass_filter # TODO: add response.cost return response - try: - response = self._completions_create(client, params) - except APIError: - logger.debug(f"config {i} failed", exc_info=1) - if i == last: - raise - else: - if cache_seed is not None: - # Cache the response + continue # filter is not passed; try the next config + try: + response = self._completions_create(client, params) + except APIError: + logger.debug(f"config {i} failed", exc_info=1) + if i == last: + raise + else: + if cache_seed is not None: + # Cache the response + with diskcache.Cache(f"{self.cache_path_root}/{cache_seed}") as cache: cache.set(key, response) + + # check the filter + pass_filter = filter_func is None or filter_func(context=context, response=response) + if pass_filter or i == last: + # Return the response if it passes the filter or it is the last client + response.config_id = i + response.pass_filter = pass_filter + # TODO: add response.cost return response + continue # filter is not passed; try the next config def _completions_create(self, client, params): completions = client.chat.completions if "messages" in params else client.completions