mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-19 13:26:32 -04:00
Adding support to save multiple memories at a time. Cuts save time by … (#5172)
# Adding support to save multiple memories at a time. Cuts save time by
more then half
<!--
Thank you for contributing to LangChain! Your PR will appear in our next
release under the title you set. Please make sure it highlights your
valuable contribution.
Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.
After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.
-->
## Who can review?
Community members can review the PR once tests pass. Tag
maintainers/contributors who might be interested:
<!-- For a quicker response, figure out the right person to tag with @
@hwchase17 - project lead
Tracing / Callbacks
- @agola11
Async
- @agola11
DataLoaders
- @eyurtsev
Models
- @hwchase17
- @agola11
-
VectorStores / Retrievers / Memory
- @dev2049
-->
@dev2049
@vowelparrot
---------
Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
This commit is contained in:
committed by
GitHub
parent
625717daa8
commit
1a9ac3b1f9
@@ -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]:
|
||||
|
||||
Reference in New Issue
Block a user