From 89b4361ef48969c5d98cf27e94d46ccc8bbc5e48 Mon Sep 17 00:00:00 2001 From: "hy.li" Date: Mon, 7 Aug 2023 16:32:04 +0800 Subject: [PATCH 1/3] remove config: update_costs --- config/config.yaml | 5 ++--- metagpt/config.py | 1 - metagpt/provider/openai_api.py | 30 ++++++++++++++++++++---------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index ceab18854..b5d1e60d5 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -63,6 +63,5 @@ SD_T2I_API: "/sdapi/v1/txt2img" #PUPPETEER_CONFIG: "./config/puppeteer-config.json" #MMDC: "./node_modules/.bin/mmdc" -### for update_costs & calc_usage -UPDATE_COSTS: false -CALC_USAGE: false \ No newline at end of file +### for calc_usage +# CALC_USAGE: false \ No newline at end of file diff --git a/metagpt/config.py b/metagpt/config.py index d53571468..3621002eb 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -79,7 +79,6 @@ class Config(metaclass=Singleton): self.total_cost = 0.0 self.puppeteer_config = self._get("PUPPETEER_CONFIG","") self.mmdc = self._get("MMDC","mmdc") - self.update_costs = self._get("UPDATE_COSTS",True) self.calc_usage = self._get("CALC_USAGE",True) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index fe9532d43..5847a4001 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -220,13 +220,18 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return self.get_choice_text(rsp) def _calc_usage(self, messages: list[dict], rsp: str) -> dict: - usage = {} if CONFIG.calc_usage: - prompt_tokens = count_message_tokens(messages, self.model) - completion_tokens = count_string_tokens(rsp, self.model) - usage['prompt_tokens'] = prompt_tokens - usage['completion_tokens'] = completion_tokens - return usage + usage = {} + try: + prompt_tokens = count_message_tokens(messages, self.model) + completion_tokens = count_string_tokens(rsp, self.model) + usage['prompt_tokens'] = prompt_tokens + usage['completion_tokens'] = completion_tokens + return usage + except Exception as e: + logger.error("usage calculation failed!", e) + CONFIG.calc_usage = False + pass async def acompletion_batch(self, batch: list[list[dict]]) -> list[dict]: """返回完整JSON""" @@ -255,10 +260,15 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return results def _update_costs(self, usage: dict): - if CONFIG.update_costs: - prompt_tokens = int(usage['prompt_tokens']) - completion_tokens = int(usage['completion_tokens']) - self._cost_manager.update_cost(prompt_tokens, completion_tokens, self.model) + if CONFIG.calc_usage: + try: + prompt_tokens = int(usage['prompt_tokens']) + completion_tokens = int(usage['completion_tokens']) + 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 + pass def get_costs(self) -> Costs: return self._cost_manager.get_costs() From eff26f5cd79b48ff40b2f10a4ef7ca87c44ff7e7 Mon Sep 17 00:00:00 2001 From: "hy.li" Date: Thu, 10 Aug 2023 21:33:31 +0800 Subject: [PATCH 2/3] rm pass --- metagpt/provider/openai_api.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 5847a4001..25a5c700b 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -231,7 +231,6 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): except Exception as e: logger.error("usage calculation failed!", e) CONFIG.calc_usage = False - pass async def acompletion_batch(self, batch: list[list[dict]]) -> list[dict]: """返回完整JSON""" @@ -268,7 +267,6 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): except Exception as e: logger.error("updating costs failed!", e) CONFIG.calc_usage = False - pass def get_costs(self) -> Costs: return self._cost_manager.get_costs() From 9373634e8605d5c857507c9072af7fd557d4dd0b Mon Sep 17 00:00:00 2001 From: "hy.li" Date: Sun, 13 Aug 2023 16:54:35 +0800 Subject: [PATCH 3/3] always update_costs when set calc_usage=True --- metagpt/provider/openai_api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index dd806ecb3..89c7a55da 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -224,8 +224,8 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): 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) @@ -234,7 +234,8 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): 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""" @@ -270,7 +271,6 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): 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()