Skip to content

Commit

Permalink
always update_costs when set calc_usage=True
Browse files Browse the repository at this point in the history
  • Loading branch information
hy.li committed Aug 13, 2023
1 parent bab605a commit 9373634
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions metagpt/provider/openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ async def acompletion_text(self, messages: list[dict], stream=False) -> str:
return self.get_choice_text(rsp)

def _calc_usage(self, messages: list[dict], rsp: str) -> dict:
usage = {}
if CONFIG.calc_usage:
usage = {}
try:
prompt_tokens = count_message_tokens(messages, self.model)
completion_tokens = count_string_tokens(rsp, self.model)
Expand All @@ -234,7 +234,8 @@ def _calc_usage(self, messages: list[dict], rsp: str) -> dict:
return usage
except Exception as e:
logger.error("usage calculation failed!", e)
CONFIG.calc_usage = False
else:
return usage

async def acompletion_batch(self, batch: list[list[dict]]) -> list[dict]:
"""返回完整JSON"""
Expand Down Expand Up @@ -270,7 +271,6 @@ def _update_costs(self, usage: dict):
self._cost_manager.update_cost(prompt_tokens, completion_tokens, self.model)
except Exception as e:
logger.error("updating costs failed!", e)
CONFIG.calc_usage = False

def get_costs(self) -> Costs:
return self._cost_manager.get_costs()
Expand Down

0 comments on commit 9373634

Please sign in to comment.