🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
fix anthropic_api _update_costs problem
better6298a8e29902f21
diff --git a/metagpt/provider/base_llm.py b/metagpt/provider/base_llm.pyindex fa14496aa..70de4e10d 100644--- a/metagpt/provider/base_llm.py+++ b/metagpt/provider/base_llm.py@@ -10,10 +10,9 @@ from __future__ import annotationsimport jsonfrom abc import ABC, abstractmethod-from typing import Dict, Optional, Union+from typing import Optional, Unionfrom openai import AsyncOpenAI-from openai.types import CompletionUsagefrom pydantic import BaseModelfrom tenacity import (after_log,@@ -28,7 +27,6 @@ from metagpt.logs import loggerfrom metagpt.schema import Messagefrom metagpt.utils.common import log_and_reraisefrom metagpt.utils.cost_manager import CostManager, Costs-from metagpt.utils.exceptions import handle_exceptionclass BaseLLM(ABC):@@ -88,6 +86,7 @@ class BaseLLM(ABC):local_calc_usage (bool): some models don't calculate usage, it will overwrite LLMConfig.calc_usage"""calc_usage = self.config.calc_usage and local_calc_usage+ model = model or self.pricing_planmodel = model or self.modelusage = usage.model_dump() if isinstance(usage, BaseModel) else usageif calc_usage and self.cost_manager:@@ -225,20 +224,6 @@ class BaseLLM(ABC):"""return json.loads(self.get_choice_function(rsp)["arguments"], strict=False)- @handle_exception- def _update_costs(self, usage: CompletionUsage | Dict):- """- Updates the costs based on the provided usage information.- """- if self.config.calc_usage and usage and self.cost_manager:- if isinstance(usage, Dict):- prompt_tokens = int(usage.get("prompt_tokens", 0))- completion_tokens = int(usage.get("completion_tokens", 0))- else:- prompt_tokens = usage.prompt_tokens- completion_tokens = usage.completion_tokens- self.cost_manager.update_cost(prompt_tokens, completion_tokens, self.pricing_plan)-def messages_to_prompt(self, messages: list[dict]):"""[{"role": "user", "content": msg}] to user: <msg> etc."""return "\n".join([f"{i['role']}: {i['content']}" for i in messages])