diff --git a/examples/llm_hello_world.py b/examples/llm_hello_world.py index 9340f7d5a..62fc2ed68 100644 --- a/examples/llm_hello_world.py +++ b/examples/llm_hello_world.py @@ -14,9 +14,9 @@ from metagpt.logs import logger async def main(): llm = LLM() # llm type check - id_ques = "what's your name" - logger.info(f"{id_ques}: ") - logger.info(await llm.aask(id_ques)) + question = "what's your name" + logger.info(f"{question}: ") + logger.info(await llm.aask(question)) logger.info("\n\n") logger.info( diff --git a/metagpt/provider/base_llm.py b/metagpt/provider/base_llm.py index 2f57b15aa..7cf3faac0 100644 --- a/metagpt/provider/base_llm.py +++ b/metagpt/provider/base_llm.py @@ -75,7 +75,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 if model else self.model + model = model or self.model usage = usage.model_dump() if isinstance(usage, BaseModel) else usage if calc_usage and self.cost_manager: try: @@ -83,7 +83,7 @@ class BaseLLM(ABC): completion_tokens = int(usage.get("completion_tokens", 0)) self.cost_manager.update_cost(prompt_tokens, completion_tokens, model) except Exception as e: - logger.error(f"{self.__class__.__name__} updats costs failed! exp: {e}") + logger.error(f"{self.__class__.__name__} updates costs failed! exp: {e}") def get_costs(self) -> Costs: if not self.cost_manager: diff --git a/metagpt/provider/dashscope_api.py b/metagpt/provider/dashscope_api.py index 7d7dcb7b7..f2b3a19a1 100644 --- a/metagpt/provider/dashscope_api.py +++ b/metagpt/provider/dashscope_api.py @@ -37,7 +37,7 @@ from metagpt.provider.base_llm import BaseLLM, LLMConfig from metagpt.provider.llm_provider_registry import LLMType, register_provider from metagpt.provider.openai_api import log_and_reraise from metagpt.utils.cost_manager import CostManager -from metagpt.utils.token_counter import DashScope_TOKEN_COSTS +from metagpt.utils.token_counter import DASHSCOPE_TOKEN_COSTS def build_api_arequest( @@ -167,7 +167,7 @@ class DashScopeLLM(BaseLLM): def __init_dashscope(self): self.model = self.config.model self.api_key = self.config.api_key - self.token_costs = DashScope_TOKEN_COSTS + self.token_costs = DASHSCOPE_TOKEN_COSTS self.aclient: AGeneration = AGeneration # check support system_message models diff --git a/metagpt/provider/qianfan_api.py b/metagpt/provider/qianfan_api.py index 6f94b9cea..4cbb76566 100644 --- a/metagpt/provider/qianfan_api.py +++ b/metagpt/provider/qianfan_api.py @@ -22,8 +22,8 @@ from metagpt.provider.llm_provider_registry import register_provider from metagpt.provider.openai_api import log_and_reraise from metagpt.utils.cost_manager import CostManager from metagpt.utils.token_counter import ( - QianFan_EndPoint_TOKEN_COSTS, - QianFan_MODEL_TOKEN_COSTS, + QIANFAN_ENDPOINT_TOKEN_COSTS, + QIANFAN_MODEL_TOKEN_COSTS, ) @@ -74,8 +74,8 @@ class QianFanLLM(BaseLLM): assert not (self.config.model and self.config.endpoint), "Only set `model` or `endpoint` in the config" assert self.config.model or self.config.endpoint, "Should set one of `model` or `endpoint` in the config" - self.token_costs = copy.deepcopy(QianFan_MODEL_TOKEN_COSTS) - self.token_costs.update(QianFan_EndPoint_TOKEN_COSTS) + self.token_costs = copy.deepcopy(QIANFAN_MODEL_TOKEN_COSTS) + self.token_costs.update(QIANFAN_ENDPOINT_TOKEN_COSTS) # self deployed model on the cloud not to calculate usage, it charges resource pool rental fee self.calc_usage = self.config.calc_usage and self.config.endpoint is None @@ -103,8 +103,8 @@ class QianFanLLM(BaseLLM): def _update_costs(self, usage: dict): """update each request's token cost""" - model_or_endpoint = self.config.model if self.config.model else self.config.endpoint - local_calc_usage = True if model_or_endpoint in self.token_costs else False + model_or_endpoint = self.config.model or self.config.endpoint + local_calc_usage = model_or_endpoint in self.token_costs super()._update_costs(usage, model_or_endpoint, local_calc_usage) def get_choice_text(self, resp: JsonBody) -> str: diff --git a/metagpt/utils/token_counter.py b/metagpt/utils/token_counter.py index 220014d8f..167a1d755 100644 --- a/metagpt/utils/token_counter.py +++ b/metagpt/utils/token_counter.py @@ -42,7 +42,7 @@ TOKEN_COSTS = { QianFan Token Price https://cloud.baidu.com/doc/WENXINWORKSHOP/s/hlrk4akp7#tokens%E5%90%8E%E4%BB%98%E8%B4%B9 Due to QianFan has multi price strategies, we unify `Tokens post-payment` as a statistical method. """ -QianFan_MODEL_TOKEN_COSTS = { +QIANFAN_MODEL_TOKEN_COSTS = { "ERNIE-Bot-4": {"prompt": 0.017, "completion": 0.017}, "ERNIE-Bot-8k": {"prompt": 0.0034, "completion": 0.0067}, "ERNIE-Bot": {"prompt": 0.0017, "completion": 0.0017}, @@ -66,35 +66,35 @@ QianFan_MODEL_TOKEN_COSTS = { "Yi-34B-Chat": {"prompt": 0.0, "completion": 0.0}, } -QianFan_EndPoint_TOKEN_COSTS = { - "completions_pro": QianFan_MODEL_TOKEN_COSTS["ERNIE-Bot-4"], - "ernie_bot_8k": QianFan_MODEL_TOKEN_COSTS["ERNIE-Bot-8k"], - "completions": QianFan_MODEL_TOKEN_COSTS["ERNIE-Bot"], - "eb-instant": QianFan_MODEL_TOKEN_COSTS["ERNIE-Bot-turbo"], - "ai_apaas": QianFan_MODEL_TOKEN_COSTS["EB-turbo-AppBuilder"], - "ernie_speed": QianFan_MODEL_TOKEN_COSTS["ERNIE-Speed"], - "bloomz_7b1": QianFan_MODEL_TOKEN_COSTS["BLOOMZ-7B"], - "llama_2_7b": QianFan_MODEL_TOKEN_COSTS["Llama-2-7B-Chat"], - "llama_2_13b": QianFan_MODEL_TOKEN_COSTS["Llama-2-13B-Chat"], - "llama_2_70b": QianFan_MODEL_TOKEN_COSTS["Llama-2-70B-Chat"], - "chatglm2_6b_32k": QianFan_MODEL_TOKEN_COSTS["ChatGLM2-6B-32K"], - "aquilachat_7b": QianFan_MODEL_TOKEN_COSTS["AquilaChat-7B"], - "mixtral_8x7b_instruct": QianFan_MODEL_TOKEN_COSTS["Mixtral-8x7B-Instruct"], - "sqlcoder_7b": QianFan_MODEL_TOKEN_COSTS["SQLCoder-7B"], - "codellama_7b_instruct": QianFan_MODEL_TOKEN_COSTS["CodeLlama-7B-Instruct"], - "xuanyuan_70b_chat": QianFan_MODEL_TOKEN_COSTS["XuanYuan-70B-Chat-4bit"], - "qianfan_bloomz_7b_compressed": QianFan_MODEL_TOKEN_COSTS["Qianfan-BLOOMZ-7B-compressed"], - "qianfan_chinese_llama_2_7b": QianFan_MODEL_TOKEN_COSTS["Qianfan-Chinese-Llama-2-7B"], - "qianfan_chinese_llama_2_13b": QianFan_MODEL_TOKEN_COSTS["Qianfan-Chinese-Llama-2-13B"], - "chatlaw": QianFan_MODEL_TOKEN_COSTS["ChatLaw"], - "yi_34b_chat": QianFan_MODEL_TOKEN_COSTS["Yi-34B-Chat"], +QIANFAN_ENDPOINT_TOKEN_COSTS = { + "completions_pro": QIANFAN_MODEL_TOKEN_COSTS["ERNIE-Bot-4"], + "ernie_bot_8k": QIANFAN_MODEL_TOKEN_COSTS["ERNIE-Bot-8k"], + "completions": QIANFAN_MODEL_TOKEN_COSTS["ERNIE-Bot"], + "eb-instant": QIANFAN_MODEL_TOKEN_COSTS["ERNIE-Bot-turbo"], + "ai_apaas": QIANFAN_MODEL_TOKEN_COSTS["EB-turbo-AppBuilder"], + "ernie_speed": QIANFAN_MODEL_TOKEN_COSTS["ERNIE-Speed"], + "bloomz_7b1": QIANFAN_MODEL_TOKEN_COSTS["BLOOMZ-7B"], + "llama_2_7b": QIANFAN_MODEL_TOKEN_COSTS["Llama-2-7B-Chat"], + "llama_2_13b": QIANFAN_MODEL_TOKEN_COSTS["Llama-2-13B-Chat"], + "llama_2_70b": QIANFAN_MODEL_TOKEN_COSTS["Llama-2-70B-Chat"], + "chatglm2_6b_32k": QIANFAN_MODEL_TOKEN_COSTS["ChatGLM2-6B-32K"], + "aquilachat_7b": QIANFAN_MODEL_TOKEN_COSTS["AquilaChat-7B"], + "mixtral_8x7b_instruct": QIANFAN_MODEL_TOKEN_COSTS["Mixtral-8x7B-Instruct"], + "sqlcoder_7b": QIANFAN_MODEL_TOKEN_COSTS["SQLCoder-7B"], + "codellama_7b_instruct": QIANFAN_MODEL_TOKEN_COSTS["CodeLlama-7B-Instruct"], + "xuanyuan_70b_chat": QIANFAN_MODEL_TOKEN_COSTS["XuanYuan-70B-Chat-4bit"], + "qianfan_bloomz_7b_compressed": QIANFAN_MODEL_TOKEN_COSTS["Qianfan-BLOOMZ-7B-compressed"], + "qianfan_chinese_llama_2_7b": QIANFAN_MODEL_TOKEN_COSTS["Qianfan-Chinese-Llama-2-7B"], + "qianfan_chinese_llama_2_13b": QIANFAN_MODEL_TOKEN_COSTS["Qianfan-Chinese-Llama-2-13B"], + "chatlaw": QIANFAN_MODEL_TOKEN_COSTS["ChatLaw"], + "yi_34b_chat": QIANFAN_MODEL_TOKEN_COSTS["Yi-34B-Chat"], } """ DashScope Token price https://help.aliyun.com/zh/dashscope/developer-reference/tongyi-thousand-questions-metering-and-billing Different model has different detail page. Attention, some model are free for a limited time. """ -DashScope_TOKEN_COSTS = { +DASHSCOPE_TOKEN_COSTS = { "qwen-turbo": {"prompt": 0.0011, "completion": 0.0011}, "qwen-plus": {"prompt": 0.0028, "completion": 0.0028}, "qwen-max": {"prompt": 0.0, "completion": 0.0},