From b04d84f6b31cc504780d160fb61955417fb219b3 Mon Sep 17 00:00:00 2001 From: ImmortalZ Date: Tue, 9 May 2023 06:13:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20solve=20the=20infinite=20loop=20caused?= =?UTF-8?q?=20by=20'add=5Fmemory'=20function=20when=20run=E2=80=A6=20(#431?= =?UTF-8?q?8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: solve the infinite loop caused by 'add_memory' function when run 'pause_to_reflect' function run steps: 'add_memory' -> 'pause_to_reflect' -> 'add_memory': infinite loop --- langchain/experimental/generative_agents/memory.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/langchain/experimental/generative_agents/memory.py b/langchain/experimental/generative_agents/memory.py index 5f1d65f423..8cfdacb7f9 100644 --- a/langchain/experimental/generative_agents/memory.py +++ b/langchain/experimental/generative_agents/memory.py @@ -44,6 +44,7 @@ class GenerativeAgentMemory(BaseMemory): relevant_memories_key: str = "relevant_memories" relevant_memories_simple_key: str = "relevant_memories_simple" most_recent_memories_key: str = "most_recent_memories" + reflecting: bool = False def chain(self, prompt: PromptTemplate) -> LLMChain: return LLMChain(llm=self.llm, prompt=prompt, verbose=self.verbose) @@ -136,10 +137,13 @@ class GenerativeAgentMemory(BaseMemory): if ( self.reflection_threshold is not None and self.aggregate_importance > self.reflection_threshold + and not self.reflecting ): + self.reflecting = True self.pause_to_reflect() # Hack to clear the importance from reflection self.aggregate_importance = 0.0 + self.reflecting = False return result def fetch_memories(self, observation: str) -> List[Document]: