Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

Merge branch 'main' into feat_qianfan

better62982557dfe81d6
diff --cc metagpt/provider/zhipuai_api.py
index b7c160a41,9e8e5fb53..4cbee4038
--- a/metagpt/provider/zhipuai_api.py
+++ b/metagpt/provider/zhipuai_api.py
@@@ -57,8 -53,18 +53,8 @@@ class ZhiPuAILLM(BaseLLM)
kwargs = {"model": self.model, "messages": messages, "stream": stream, "temperature": 0.3}
return kwargs
- def _update_costs(self, usage: dict):
- """update each request's token cost"""
- if self.config.calc_usage:
- try:
- prompt_tokens = int(usage.get("prompt_tokens", 0))
- completion_tokens = int(usage.get("completion_tokens", 0))
- self.cost_manager.update_cost(prompt_tokens, completion_tokens, self.model)
- except Exception as e:
- logger.error(f"zhipuai updats costs failed! exp: {e}")
-
def completion(self, messages: list[dict], timeout=3) -> dict:
- resp = self.llm.chat.completions.create(**self._const_kwargs(messages))
+ resp: Completion = self.llm.chat.completions.create(**self._const_kwargs(messages))
usage = resp.usage.model_dump()
self._update_costs(usage)
return resp.model_dump()
diff --cc metagpt/utils/cost_manager.py
index e1c0f415b,c4c93f91f..4e6b65b2c
--- a/metagpt/utils/cost_manager.py
+++ b/metagpt/utils/cost_manager.py
@@@ -42,9 -41,12 +42,13 @@@ class CostManager(BaseModel)
"""
self.total_prompt_tokens += prompt_tokens
self.total_completion_tokens += completion_tokens
+ if model not in TOKEN_COSTS:
+ logger.warning(f"Model {model} not found in TOKEN_COSTS.")
+ return
+
cost = (
- prompt_tokens * TOKEN_COSTS[model]["prompt"] + completion_tokens * TOKEN_COSTS[model]["completion"]
+ prompt_tokens * self.token_costs[model]["prompt"]
+ + completion_tokens * self.token_costs[model]["completion"]
) / 1000
self.total_cost += cost
logger.info(
diff --cc tests/metagpt/provider/test_zhipuai_api.py
index 8ec9ab4f9,ad2ececa2..c51010122
--- a/tests/metagpt/provider/test_zhipuai_api.py
+++ b/tests/metagpt/provider/test_zhipuai_api.py
@@@ -6,20 -6,18 +6,20 @@@ import pytes
from metagpt.provider.zhipuai_api import ZhiPuAILLM
from tests.metagpt.provider.mock_llm_config import mock_llm_config_zhipu
+from tests.metagpt.provider.req_resp_const import (
+ get_part_chat_completion,
+ llm_general_chat_funcs_test,
+ messages,
+ prompt,
+ resp_cont_tmpl,
+)
-prompt_msg = "who are you"
-messages = [{"role": "user", "content": prompt_msg}]
-
-resp_content = "I'm chatglm-turbo"
-default_resp = {
- "choices": [{"finish_reason": "stop", "index": 0, "message": {"content": resp_content, "role": "assistant"}}],
- "usage": {"completion_tokens": 22, "prompt_tokens": 19, "total_tokens": 41},
-}
+name = "ChatGLM-4"
+resp_cont = resp_cont_tmpl.format(name=name)
+default_resp = get_part_chat_completion(name)
- async def mock_zhipuai_acreate_stream(**kwargs):
+ async def mock_zhipuai_acreate_stream(self, **kwargs):
class MockResponse(object):
async def _aread(self):
class Iterator(object):