Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
.DS_Store
dist/
README_hidden.md
**/__pycache__/
**/__pycache__/
.idea
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ memary will default to the locally run models unless explicitly specified.
```
OPENAI_API_KEY="YOUR_API_KEY"
NEO4J_PW="YOUR_NEO4J_PW"
NEO4J_USER="YOUR_NEO4J_USER"
NEO4J_DB="YOUR_NEO4J_DATABASE"
NEO4J_URL="YOUR_NEO4J_URL"
PERPLEXITY_API_KEY="YOUR_API_KEY"
GOOGLEMAPS_API_KEY="YOUR_API_KEY"
Expand Down
9 changes: 7 additions & 2 deletions src/memary/agent/base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ def __init__(
pplx_api_key = os.getenv("PERPLEXITY_API_KEY")

# Neo4j credentials
self.neo4j_username = "neo4j"
# self.neo4j_username = "neo4j"
# Rajib - Neo4J user was hardcoded.
# Moved it to .env
self.neo4j_username = os.getenv("NEO4J_USER")
self.neo4j_password = os.getenv("NEO4J_PW")
self.neo4j_url = os.getenv("NEO4J_URL")
database = "neo4j"
# Adding the database also as env variable
database = os.getenv("NEO4J_DB")
# database = "neo4j"

# initialize APIs
self.load_llm_model(llm_model_name)
Expand Down
24 changes: 24 additions & 0 deletions src/memary/memory/base_memory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import math

from abc import ABC, abstractmethod
from datetime import datetime, timedelta
Expand Down Expand Up @@ -77,6 +78,29 @@ def remove_memory_by_index(self, index):
logging.info("Invalid index. Memory item not removed.")
return False

def calculate_retention_score(self, strength:int,days_elapsed_since_last_used:int):
"""
strength: This parameter denotes the strength of the memory.The strenght of the memory
is equivalent to the number of times the memory item has been accessed. In case of entity,
this will be the number of times(count), the entity has been used
days_elapsed_since_last_used: This is the duration in days that has elapsed from today till the
memory was last accessed.
"""
score = math.exp(-days_elapsed_since_last_used / strength)

return score

def forget_memory(self,threshold_retention_score):
"""
This function calcualtes the retention score for each memory item
and then keeps the one whose score is more than threshold
"""
today = datetime.today()
self.memory = [item for item in self.return_memory if
self.calculate_retention_score(item.count, (today - item.date).days) >= threshold_retention_score]

logging.info("Old memory has been forgotten.")

def clear_memory(self):
self.memory = []
if self.file_name:
Expand Down
9 changes: 2 additions & 7 deletions src/memary/memory/entity_knowledge_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def init_memory(self):
def return_memory(self):
return self.knowledge_memory


def load_memory_from_file(self):
try:
with open(self.file_name, 'r') as file:
Expand All @@ -46,10 +45,9 @@ def add_memory(self, memory_stream: list[MemoryItem]):
Args:
memory_stream (list): list of MemoryItem
"""
knowledge_meory = self._convert_memory_to_knowledge_memory(
knowledge_memory = self._convert_memory_to_knowledge_memory(
memory_stream)
self._update_knowledge_memory(knowledge_meory)

self._update_knowledge_memory(knowledge_memory)

def _update_knowledge_memory(self, knowledge_memory: list):
"""update self.knowledge memory with new knowledge memory items
Expand Down Expand Up @@ -87,6 +85,3 @@ def _convert_memory_to_knowledge_memory(

def get_memory(self) -> list[KnowledgeMemoryItem]:
return self.knowledge_memory