From 7dd02ae4b11a5494a470125f785e4acbf7406b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 23 Aug 2023 15:53:33 +0800 Subject: [PATCH] feat: A definition has been provided for the return value of _think: returning false indicates that further reasoning cannot continue. --- metagpt/roles/fork_meta_role.py | 5 ++++- metagpt/roles/researcher.py | 9 +++++++-- metagpt/roles/role.py | 8 +++++--- metagpt/roles/teacher.py | 7 +++++-- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/metagpt/roles/fork_meta_role.py b/metagpt/roles/fork_meta_role.py index 5311bc4f0..57d467080 100644 --- a/metagpt/roles/fork_meta_role.py +++ b/metagpt/roles/fork_meta_role.py @@ -10,6 +10,8 @@ For more about `fork` node in activity diagrams, see: `https://www.uml-diagrams.org/activity-diagrams.html` This file defines a `fork` style meta role capable of generating arbitrary roles at runtime based on a configuration file. +@Modified By: mashenquan, 2023/8/22. A definition has been provided for the return value of _think: returning false indicates that further reasoning cannot continue. + """ import re @@ -82,12 +84,13 @@ class ForkMetaRole(Role): """Everything will be done part by part.""" if self._rc.todo is None: self._set_state(0) - return + return True if self._rc.state + 1 < len(self._states): self._set_state(self._rc.state + 1) else: self._rc.todo = None + return False async def _react(self) -> Message: ret = Message(content="") diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index 30545c5c0..f3ff7f8e5 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -1,4 +1,8 @@ #!/usr/bin/env python +""" +@Modified By: mashenquan, 2023/8/22. A definition has been provided for the return value of _think: returning false indicates that further reasoning cannot continue. + +""" import asyncio @@ -40,15 +44,16 @@ class Researcher(Role): if language not in ("en-us", "zh-cn"): logger.warning(f"The language `{language}` has not been tested, it may not work.") - async def _think(self) -> None: + async def _think(self) -> bool: if self._rc.todo is None: self._set_state(0) - return + return True if self._rc.state + 1 < len(self._states): self._set_state(self._rc.state + 1) else: self._rc.todo = None + return False async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 217272b54..493c172ae 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -7,6 +7,7 @@ @Modified By: mashenquan, 2023-8-7, :class:`Role` + properties. @Modified By: mashenquan, 2023/8/20. Remove global configuration `CONFIG`, enable configuration support for business isolation; Change cost control from global to company level. +@Modified By: mashenquan, 2023/8/22. A definition has been provided for the return value of _think: returning false indicates that further reasoning cannot continue. """ from __future__ import annotations @@ -192,12 +193,12 @@ class Role: return self._setting.desc return PREFIX_TEMPLATE.format(**self._setting.dict()) - async def _think(self) -> None: - """思考要做什么,决定下一步的action""" + async def _think(self) -> bool: + """Consider what to do and decide on the next course of action. Return false if nothing can be done.""" if len(self._actions) == 1: # 如果只有一个动作,那就只能做这个 self._set_state(0) - return + return True prompt = self._get_prefix() prompt += STATE_TEMPLATE.format(history=self._rc.history, states="\n".join(self._states), n_states=len(self._states) - 1) @@ -207,6 +208,7 @@ class Role: logger.warning(f'Invalid answer of state, {next_state=}') next_state = "0" self._set_state(int(next_state)) + return True async def _act(self) -> Message: # prompt = self.get_prefix() diff --git a/metagpt/roles/teacher.py b/metagpt/roles/teacher.py index f29f384db..9a68fa9e0 100644 --- a/metagpt/roles/teacher.py +++ b/metagpt/roles/teacher.py @@ -4,6 +4,8 @@ @Time : 2023/7/27 @Author : mashenquan @File : teacher.py +@Modified By: mashenquan, 2023/8/22. A definition has been provided for the return value of _think: returning false indicates that further reasoning cannot continue. + """ @@ -31,16 +33,17 @@ class Teacher(Role): self._init_actions(actions) self._watch({TeachingPlanRequirement}) - async def _think(self) -> None: + async def _think(self) -> bool: """Everything will be done part by part.""" if self._rc.todo is None: self._set_state(0) - return + return True if self._rc.state + 1 < len(self._states): self._set_state(self._rc.state + 1) else: self._rc.todo = None + return False async def _react(self) -> Message: ret = Message(content="")