fix: solve the infinite loop caused by 'add_memory' function when run… (#4318)

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
This commit is contained in:
ImmortalZ 2023-05-09 06:13:23 +08:00 committed by GitHub
parent aa11f7c89b
commit b04d84f6b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,6 +44,7 @@ class GenerativeAgentMemory(BaseMemory):
relevant_memories_key: str = "relevant_memories" relevant_memories_key: str = "relevant_memories"
relevant_memories_simple_key: str = "relevant_memories_simple" relevant_memories_simple_key: str = "relevant_memories_simple"
most_recent_memories_key: str = "most_recent_memories" most_recent_memories_key: str = "most_recent_memories"
reflecting: bool = False
def chain(self, prompt: PromptTemplate) -> LLMChain: def chain(self, prompt: PromptTemplate) -> LLMChain:
return LLMChain(llm=self.llm, prompt=prompt, verbose=self.verbose) return LLMChain(llm=self.llm, prompt=prompt, verbose=self.verbose)
@ -136,10 +137,13 @@ class GenerativeAgentMemory(BaseMemory):
if ( if (
self.reflection_threshold is not None self.reflection_threshold is not None
and self.aggregate_importance > self.reflection_threshold and self.aggregate_importance > self.reflection_threshold
and not self.reflecting
): ):
self.reflecting = True
self.pause_to_reflect() self.pause_to_reflect()
# Hack to clear the importance from reflection # Hack to clear the importance from reflection
self.aggregate_importance = 0.0 self.aggregate_importance = 0.0
self.reflecting = False
return result return result
def fetch_memories(self, observation: str) -> List[Document]: def fetch_memories(self, observation: str) -> List[Document]: