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

Enhance memory management system #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
52 changes: 49 additions & 3 deletions Local Client Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "07cf336c-de93-449d-ab4e-440ccfbbc81e",
"id": "07cf336c-de93-449d-ab4e-440ccfbbc81a",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -560,13 +560,59 @@
"nb_print(response.messages)"
]
},
{
"cell_type": "markdown",
"id": "d5f9a5c9-4e3b-4b8e-8b8e-5f1c9a4cb9d",
"metadata": {},
"source": [
"## Section 6: Demonstrating Memory Categorization and Prioritization\n",
"In this section, we will demonstrate how to categorize and prioritize memories using the new memory management system."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f854c2d-fe1a-460f-9c55-392b14947e8c",
"id": "d5f9a5c9-4e3b-4b8e-8b8e-5f1c9a4cb9d",
"metadata": {},
"outputs": [],
"source": []
"source": [
"from helper import Memory, MemoryManager\n",
"\n",
"memory_manager = MemoryManager()\n",
"\n",
"memory1 = Memory(\"Learned about AI\", 10)\n",
"memory2 = Memory(\"Had lunch\", 5)\n",
"memory3 = Memory(\"Went for a walk\", 7)\n",
"\n",
"memory_manager.add_memory(memory1, \"short_term\")\n",
"memory_manager.add_memory(memory2, \"long_term\")\n",
"memory_manager.add_memory(memory3, \"episodic\")\n",
"\n",
"memory_manager.prioritize_memories()\n",
"print(\"Prioritized Memories:\", memory_manager.get_memories(\"short_term\"))"
]
},
{
"cell_type": "markdown",
"id": "0106a5c9-4e3b-4b8e-8b8e-5f1c9a4cb9d",
"metadata": {},
"source": [
"## Section 7: Demonstrating Memory Decay Mechanism\n",
"In this section, we will demonstrate how the decay mechanism works to gradually forget less important memories over time."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0106a5c9-4e3b-4b8e-8b8e-5f1c9a4cb9d",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"memory_manager.decay_memories()\n",
"print(\"Memories after decay:\", memory_manager.get_memories(\"short_term\"))"
]
}
],
"metadata": {
Expand Down
75 changes: 74 additions & 1 deletion helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import os
import re
import time

from dotenv import find_dotenv, load_dotenv
from IPython.display import HTML, display
Expand Down Expand Up @@ -130,4 +131,76 @@ def format_json(json_str):
formatted = re.sub(r": (true|false)", r': <span class="json-boolean">\1</span>', formatted)
return formatted
except json.JSONDecodeError:
return html.escape(json_str)
return html.escape(json_str)


# Memory management functions

class Memory:
def __init__(self, content, importance, timestamp=None):
self.content = content
self.importance = importance
self.timestamp = timestamp or time.time()

def __repr__(self):
return f"Memory(content={self.content}, importance={self.importance}, timestamp={self.timestamp})"


class MemoryManager:
def __init__(self):
self.short_term_memory = []
self.long_term_memory = []
self.episodic_memory = []

def add_memory(self, memory, category):
if category == "short_term":
self.short_term_memory.append(memory)
elif category == "long_term":
self.long_term_memory.append(memory)
elif category == "episodic":
self.episodic_memory.append(memory)

def prioritize_memories(self):
self.short_term_memory.sort(key=lambda x: x.importance, reverse=True)
self.long_term_memory.sort(key=lambda x: x.importance, reverse=True)
self.episodic_memory.sort(key=lambda x: x.importance, reverse=True)

def decay_memories(self, decay_rate=0.01):
current_time = time.time()
for memory in self.short_term_memory:
memory.importance -= decay_rate * (current_time - memory.timestamp)
for memory in self.long_term_memory:
memory.importance -= decay_rate * (current_time - memory.timestamp)
for memory in self.episodic_memory:
memory.importance -= decay_rate * (current_time - memory.timestamp)

self.short_term_memory = [m for m in self.short_term_memory if m.importance > 0]
self.long_term_memory = [m for m in self.long_term_memory if m.importance > 0]
self.episodic_memory = [m for m in self.episodic_memory if m.importance > 0]

def get_memories(self, category):
if category == "short_term":
return self.short_term_memory
elif category == "long_term":
return self.long_term_memory
elif category == "episodic":
return self.episodic_memory


# Example usage
if __name__ == "__main__":
memory_manager = MemoryManager()

memory1 = Memory("Learned about AI", 10)
memory2 = Memory("Had lunch", 5)
memory3 = Memory("Went for a walk", 7)

memory_manager.add_memory(memory1, "short_term")
memory_manager.add_memory(memory2, "long_term")
memory_manager.add_memory(memory3, "episodic")

memory_manager.prioritize_memories()
print("Prioritized Memories:", memory_manager.get_memories("short_term"))

memory_manager.decay_memories()
print("Memories after decay:", memory_manager.get_memories("short_term"))