diff --git a/.gitignore b/.gitignore index 771e58d7..f80ec107 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ .DS_Store dist/ README_hidden.md -**/__pycache__/ \ No newline at end of file +**/__pycache__/ +.idea \ No newline at end of file diff --git a/README.md b/README.md index afa4e642..e3895275 100644 --- a/README.md +++ b/README.md @@ -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" diff --git a/src/memary/agent/base_agent.py b/src/memary/agent/base_agent.py index a63a12fa..10eedb41 100644 --- a/src/memary/agent/base_agent.py +++ b/src/memary/agent/base_agent.py @@ -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) diff --git a/src/memary/memory/base_memory.py b/src/memary/memory/base_memory.py index c2733aff..9d2a0a3d 100644 --- a/src/memary/memory/base_memory.py +++ b/src/memary/memory/base_memory.py @@ -1,5 +1,6 @@ import json import logging +import math from abc import ABC, abstractmethod from datetime import datetime, timedelta @@ -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: diff --git a/src/memary/memory/entity_knowledge_store.py b/src/memary/memory/entity_knowledge_store.py index ca08f2d7..6cca4427 100644 --- a/src/memary/memory/entity_knowledge_store.py +++ b/src/memary/memory/entity_knowledge_store.py @@ -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: @@ -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 @@ -87,6 +85,3 @@ def _convert_memory_to_knowledge_memory( def get_memory(self) -> list[KnowledgeMemoryItem]: return self.knowledge_memory - - -