Skip to content

Commit

Permalink
Update cache if cost not calculated (microsoft#903)
Browse files Browse the repository at this point in the history
* update cost for cache

* fix test
  • Loading branch information
yiranwu0 authored and rlam3 committed Dec 19, 2023
1 parent f89aa98 commit 8eba4e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion autogen/oai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,15 @@ def yes_or_no_filter(context, response):
# Try to get the response from cache
key = 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 filter
pass_filter = filter_func is None or filter_func(context=context, response=response)
if pass_filter or i == last:
Expand Down
2 changes: 1 addition & 1 deletion test/oai/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_usage_summary():
# check update
client._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
Expand Down

0 comments on commit 8eba4e8

Please sign in to comment.