🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
feat: memory + tags
莘权 马937bd12a6373
diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.pyindex 625d98675..1a8003fba 100644--- a/metagpt/memory/memory.py+++ b/metagpt/memory/memory.py@@ -91,3 +91,11 @@ class Memory:key = class_names[type(action).__name__]rsp += self.index[key]return rsp++ def get_by_tags(self, tags: list) -> list[Message]:+ """Return messages with specified tags"""+ result = []+ for m in self.storage:+ if m.is_contain_tags(tags):+ result.append(m)+ return resultdiff --git a/metagpt/roles/role.py b/metagpt/roles/role.pyindex 00f8ed45f..217272b54 100644--- a/metagpt/roles/role.py+++ b/metagpt/roles/role.py@@ -17,7 +17,7 @@ from metagpt.provider.openai_api import OpenAIGPTAPI as LLMfrom metagpt.actions import Action, ActionOutputfrom metagpt.logs import loggerfrom metagpt.memory import Memory, LongTermMemory-from metagpt.schema import Message+from metagpt.schema import Message, MessageTagPREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """@@ -90,6 +90,11 @@ class RoleContext(BaseModel):def history(self) -> list[Message]:return self.memory.get()+ @property+ def prerequisite(self):+ """Retrieve information with `prerequisite` tag"""+ return self.memory.get_by_tags([MessageTag.Prerequisite.value])+class Role:"""Role/Proxy"""@@ -209,7 +214,7 @@ class Role:# history=self.history)logger.info(f"{self._setting}: ready to {self._rc.todo}")- requirement = self._rc.important_memory+ requirement = self._rc.important_memory or self._rc.prerequisiteresponse = await self._rc.todo.run(requirement, **self._options)# logger.info(response)if isinstance(response, ActionOutput):diff --git a/metagpt/schema.py b/metagpt/schema.pyindex 56e9ad95c..4c577fd7b 100644--- a/metagpt/schema.py+++ b/metagpt/schema.py@@ -60,6 +60,13 @@ class Message:returnself.tags.remove(tag)+ def is_contain_tags(self, tags: list) -> bool:+ """Determine whether the message contains tags."""+ if not tags or not self.tags:+ return False+ intersection = set(tags) & self.tags+ return len(intersection) > 0+@dataclassclass UserMessage(Message):diff --git a/tests/metagpt/roles/test_teacher.py b/tests/metagpt/roles/test_teacher.pyindex 11c268edb..8f673d6e0 100644--- a/tests/metagpt/roles/test_teacher.py+++ b/tests/metagpt/roles/test_teacher.py@@ -60,7 +60,7 @@ def test_init():for i in inputs:seed = Inputs(**i)options = Config().runtime_options- cost_manager = CostManager(options=options)+ cost_manager = CostManager(**options)teacher = Teacher(options=options, cost_manager=cost_manager, name=seed.name, profile=seed.profile,goal=seed.goal, constraints=seed.constraints,desc=seed.desc, **seed.kwargs)