Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to save multiple memories at a time. Cuts save time by … #5172

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions langchain/experimental/generative_agents/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,61 @@ def _score_memory_importance(self, memory_content: str) -> float:
return (float(match.group(1)) / 10) * self.importance_weight
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
Expand Down