🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
remove config: update_costs
hy.li89b4361ef489
diff --git a/config/config.yaml b/config/config.yamlindex 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 filediff --git a/metagpt/config.py b/metagpt/config.pyindex d53571468..3621002eb 100644--- a/metagpt/config.py+++ b/metagpt/config.py@@ -79,7 +79,6 @@ class Config(metaclass=Singleton):self.total_cost = 0.0self.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.pyindex 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+ passasync def acompletion_batch(self, batch: list[list[dict]]) -> list[dict]:"""返回完整JSON"""@@ -255,10 +260,15 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter):return resultsdef _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+ passdef get_costs(self) -> Costs:return self._cost_manager.get_costs()