From 1a9ac3b1f9676028afa7394d0bf91e2692ca1dac Mon Sep 17 00:00:00 2001 From: Abhijeet Malamkar Date: Mon, 5 Jun 2023 15:47:48 -0400 Subject: [PATCH] =?UTF-8?q?Adding=20support=20to=20save=20multiple=20memor?= =?UTF-8?q?ies=20at=20a=20time.=20Cuts=20save=20time=20by=20=E2=80=A6=20(#?= =?UTF-8?q?5172)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Adding support to save multiple memories at a time. Cuts save time by more then half ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @dev2049 @vowelparrot --------- Co-authored-by: Dev 2049 --- .../experimental/generative_agents/memory.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/langchain/experimental/generative_agents/memory.py b/langchain/experimental/generative_agents/memory.py index 3582caff..b828b05f 100644 --- a/langchain/experimental/generative_agents/memory.py +++ b/langchain/experimental/generative_agents/memory.py @@ -137,6 +137,64 @@ class GenerativeAgentMemory(BaseMemory): else: return 0.0 + def _score_memories_importance(self, memory_content: str) -> List[float]: + """Score the absolute importance of the given memory.""" + prompt = PromptTemplate.from_template( + "On the scale of 1 to 10, where 1 is purely mundane" + + " (e.g., brushing teeth, making bed) and 10 is" + + " extremely poignant (e.g., a break up, college" + + " acceptance), rate the likely poignancy of the" + + " following piece of memory. Always answer with only a list of numbers." + + " If just given one memory still respond in a list." + + " Memories are separated by semi colans (;)" + + "\Memories: {memory_content}" + + "\nRating: " + ) + scores = self.chain(prompt).run(memory_content=memory_content).strip() + + if self.verbose: + logger.info(f"Importance scores: {scores}") + + # Split into list of strings and convert to floats + scores_list = [float(x) for x in scores.split(";")] + + return scores_list + + def add_memories( + self, memory_content: str, now: Optional[datetime] = None + ) -> List[str]: + """Add an observations or memories to the agent's memory.""" + importance_scores = self._score_memories_importance(memory_content) + + self.aggregate_importance += max(importance_scores) + memory_list = memory_content.split(";") + documents = [] + + for i in range(len(memory_list)): + documents.append( + Document( + page_content=memory_list[i], + metadata={"importance": importance_scores[i]}, + ) + ) + + result = self.memory_retriever.add_documents(documents, current_time=now) + + # After an agent has processed a certain amount of memories (as measured by + # aggregate importance), it is time to reflect on recent events to add + # more synthesized memories to the agent's memory stream. + 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(now=now) + # Hack to clear the importance from reflection + self.aggregate_importance = 0.0 + self.reflecting = False + return result + def add_memory( self, memory_content: str, now: Optional[datetime] = None ) -> List[str]: