🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
refactor: brain memory
莘权 马2be79730a020
diff --git a/metagpt/memory/brain_memory.py b/metagpt/memory/brain_memory.pyindex 45a7c0691..b06bf1036 100644--- a/metagpt/memory/brain_memory.py+++ b/metagpt/memory/brain_memory.py@@ -240,7 +240,8 @@ class BrainMemory(pydantic.BaseModel):logger.debug(f"title rsp: {response}")return response- async def is_related(self, text1, text2, llm):+ @staticmethod+ async def is_related(text1, text2, llm):# command = f"{text1}\n{text2}\n\nIf the two sentences above are related, return [TRUE] brief and clear. Otherwise, return [FALSE]."command = f"{text2}\n\nIs there any sentence above related to the following sentence: {text1}.\nIf is there any relevance, return [TRUE] brief and clear. Otherwise, return [FALSE] brief and clear."rsp = await llm.aask(msg=command, system_msgs=[])@@ -295,8 +296,12 @@ class BrainMemory(pydantic.BaseModel):return None, input_stringdef set_llm_type(self, v):- if v:+ if v and v != self.llm_type:self.llm_type = vself.is_dirty = True+ @property+ def is_history_available(self):+ return self.history or self.historical_summary+DEFAULT_TOKEN_SIZE = 500diff --git a/metagpt/roles/assistant.py b/metagpt/roles/assistant.pyindex 26711486f..54c1e2f43 100644--- a/metagpt/roles/assistant.py+++ b/metagpt/roles/assistant.py@@ -115,16 +115,15 @@ class Assistant(Role):return Trueasync def refine_memory(self) -> str:- history_text = self.memory.history_textlast_talk = self.memory.pop_last_talk()if last_talk is None: # No user feedback, unsure if past conversation is finished.return None- if history_text == "":+ if not self.memory.is_history_available:return last_talkhistory_summary = await self.memory.summerize(max_words=800, keep_language=True, llm=self._llm)- if last_talk and await self.memory.is_related(text1=last_talk, text2=history_summary, llm=self._llm):+ if last_talk and await BrainMemory.is_related(text1=last_talk, text2=history_summary, llm=self._llm):# Merge relevant content.- last_talk = await self.memory.rewrite(sentence=last_talk, context=history_text, llm=self._llm)+ last_talk = await self.memory.rewrite(sentence=last_talk, llm=self._llm)return last_talkreturn last_talk