[geekan/MetaGPT]在 common.py 的 def role_raise_decorator(func): 方法的异常捕获代码,除了 Exception 应该打印完整的 ex

2024-05-16 752 views
8

Bug description 在 common.py 的 def role_raise_decorator(func): 方法的异常捕获代码没有打印完整的ex 如果调用LLM出现错误时,比如404错误,或者模型不支持时,控制台只会显示warning信息.

There is a exception in role's execution, in order to resume, we delete the newest role communication message in the role's memory.

但丢失了原始Exception的信息.

Bug solved method 应该增加ex捕获,并打印

def role_raise_decorator(func):
    async def wrapper(self, *args, **kwargs):
        try:
            return await func(self, *args, **kwargs)
        except KeyboardInterrupt as kbi:
            logger.error(f"KeyboardInterrupt: {kbi} occurs, start to serialize the project")
            if self.latest_observed_msg:
                self.rc.memory.delete(self.latest_observed_msg)
            # raise again to make it captured outside
            raise Exception(format_trackback_info(limit=None))
        except Exception as ex:                                                      # 捕获ex
            logger.error(f"Exception: {ex} occurs, start to serialize the project")  # 打印 ex的具体错误.
            if self.latest_observed_msg:
                logger.warning(
                    "There is a exception in role's execution, in order to resume, "
                    "we delete the newest role communication message in the role's memory."
                )
                # remove role newest observed msg to make it observed again
                self.rc.memory.delete(self.latest_observed_msg)
            # raise again to make it captured outside
            raise Exception(format_trackback_info(limit=None))

    return wrapper

Environment information

  • LLM type and model name: Auzre + gpt-3.5

  • System version:

  • Python version:

  • packages version:
  • installation method:

Screenshots or logs 如果不加的话,就只有下面的提示

2024-01-12 17:22:52.030 | ERROR    | metagpt.utils.common:wrapper:509 - Exception: Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}} occurs, start to serialize the project
2024-01-12 17:22:52.032 | WARNING  | metagpt.utils.common:wrapper:511 - There is a exception in role's execution, in order to resume, we delete the newest role communication message in the role's memory.

回答

4

好建议,添加这行代码后日志更清晰,提交个PR?😁

7

@better629 你能看一下这个优化吗?

4

这里有一个无效模型的日志,或者如下所示

2024-03-21 21:26:40.770 | INFO     | metagpt.actions.write_prd:run:86 - New requirement detected: write a 2048 game
2024-03-21 21:26:41.361 | ERROR    | metagpt.utils.common:log_it:553 - Finished call to 'metagpt.actions.action_node.ActionNode._aask_v1' after 0.590(s), this was the 1st time calling it. exp: Error code: 404 - {'error': {'message': 'The model `gpt-4-turbo-preview1` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}

所以,不需要添加更多。