Skip to content

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

refine: add terminate in Interpreter instead of Role.

刘棒棒803359a7588f
diff --git a/metagpt/actions/mi/execute_nb_code.py b/metagpt/actions/mi/execute_nb_code.py
index 0e4563a37..3a64a8bec 100644
--- a/metagpt/actions/mi/execute_nb_code.py
+++ b/metagpt/actions/mi/execute_nb_code.py
@@ -58,7 +58,8 @@ class ExecuteNbCode(Action):
async def terminate(self):
"""kill NotebookClient"""
- await self.nb_client._async_cleanup_kernel()
+ if self.nb_client.km is not None:
+ await self.nb_client._async_cleanup_kernel()
async def reset(self):
"""reset NotebookClient"""
diff --git a/metagpt/roles/mi/interpreter.py b/metagpt/roles/mi/interpreter.py
index fa50098e9..2392863a0 100644
--- a/metagpt/roles/mi/interpreter.py
+++ b/metagpt/roles/mi/interpreter.py
@@ -42,6 +42,10 @@ class Interpreter(Role):
def working_memory(self):
return self.rc.working_memory
+ async def _plan_and_act(self) -> Message:
+ await super()._plan_and_act()
+ await self.execute_code.terminate()
+
async def _act_on_task(self, current_task: Task) -> TaskResult:
code, result, is_success = await self._write_and_exec_code()
task_result = TaskResult(code=code, result=result, is_success=is_success)
diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py
index 73edfc52b..3938664ba 100644
--- a/metagpt/roles/role.py
+++ b/metagpt/roles/role.py
@@ -503,9 +503,6 @@ class Role(SerializationMixin, ContextMixin, BaseModel):
self.rc.memory.add(rsp) # add to persistent memory
- if hasattr(self, "execute_code") and hasattr(self.execute_code, "terminate"):
- await self.execute_code.terminate()
-
return rsp
async def _act_on_task(self, current_task: Task) -> TaskResult: