From 9f9b7ebe17b09d7bd952173e407dca565e064bb4 Mon Sep 17 00:00:00 2001 From: Stitch-z <284618289@qq.com> Date: Sat, 2 Dec 2023 14:39:51 +0800 Subject: [PATCH] update: optimize the action code for writing tutorials. --- examples/write_tutorial.py | 2 ++ metagpt/roles/tutorial_assistant.py | 30 +++++------------------------ 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/examples/write_tutorial.py b/examples/write_tutorial.py index 71ece5527..8d2b25103 100644 --- a/examples/write_tutorial.py +++ b/examples/write_tutorial.py @@ -1,10 +1,12 @@ #!/usr/bin/env python3 # _*_ coding: utf-8 _*_ + """ @Time : 2023/9/4 21:40:57 @Author : Stitch-z @File : tutorial_assistant.py """ + import asyncio from metagpt.roles.tutorial_assistant import TutorialAssistant diff --git a/metagpt/roles/tutorial_assistant.py b/metagpt/roles/tutorial_assistant.py index 9a7df4f4d..7c9450997 100644 --- a/metagpt/roles/tutorial_assistant.py +++ b/metagpt/roles/tutorial_assistant.py @@ -42,17 +42,7 @@ class TutorialAssistant(Role): self.main_title = "" self.total_content = "" self.language = language - - async def _think(self) -> None: - """Determine the next action to be taken by the role.""" - if self._rc.todo is None: - self._set_state(0) - return - - if self._rc.state + 1 < len(self._states): - self._set_state(self._rc.state + 1) - else: - self._rc.todo = None + self._set_react_mode(react_mode="by_order") async def _handle_directory(self, titles: Dict) -> Message: """Handle the directories for the tutorial document. @@ -75,8 +65,6 @@ class TutorialAssistant(Role): for second_dir in first_dir[key]: directory += f" - {second_dir}\n" self._init_actions(actions) - self._rc.todo = None - return Message(content=directory) async def _act(self) -> Message: """Perform an action as determined by the role. @@ -90,7 +78,8 @@ class TutorialAssistant(Role): self.topic = msg.content resp = await todo.run(topic=self.topic) logger.info(resp) - return await self._handle_directory(resp) + await self._handle_directory(resp) + return await super().react() resp = await todo.run(topic=self.topic) logger.info(resp) if self.total_content != "": @@ -98,17 +87,8 @@ class TutorialAssistant(Role): self.total_content += resp return Message(content=resp, role=self.profile) - async def _react(self) -> Message: - """Execute the assistant's think and actions. - - Returns: - A message containing the final result of the assistant's actions. - """ - while True: - await self._think() - if self._rc.todo is None: - break - msg = await self._act() + async def react(self) -> Message: + msg = await super().react() root_path = TUTORIAL_PATH / datetime.now().strftime("%Y-%m-%d_%H-%M-%S") await File.write(root_path, f"{self.main_title}.md", self.total_content.encode('utf-8')) return msg