A programming framework for agentic AI
Update cache if cost not calculated (#903)
Yiran Wucbd1e19307c9
diff --git a/autogen/oai/client.py b/autogen/oai/client.pyindex 147f412fd..fedcea4d6 100644--- a/autogen/oai/client.py+++ b/autogen/oai/client.py@@ -235,9 +235,15 @@ class OpenAIWrapper:# Try to get the response from cachekey = get_key(params)response = cache.get(key, None)+if response is not None:+ try:+ response.cost+ except AttributeError:+ # update atrribute if cost is not calculated+ response.cost = self.cost(response)+ cache.set(key, response)self._update_usage_summary(response, use_cache=True)- if response is not None:# check the filterpass_filter = filter_func is None or filter_func(context=context, response=response)if pass_filter or i == last:diff --git a/test/oai/test_client.py b/test/oai/test_client.pyindex 5e392855b..450331238 100644--- a/test/oai/test_client.py+++ b/test/oai/test_client.py@@ -80,7 +80,7 @@ def test_usage_summary():# check updateclient._update_usage_summary(response, use_cache=True)assert (- client.actual_usage_summary["total_cost"] == response.cost * 2+ client.total_usage_summary["total_cost"] == response.cost * 2), "total_cost should be equal to response.cost * 2"# check clear