Spaces:
Runtime error
Runtime error
Update memory_store.py
Browse files- memory_store.py +30 -20
memory_store.py
CHANGED
|
@@ -1,35 +1,45 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class MemoryStore:
|
| 4 |
def __init__(self, path="/mnt/data/memory.json"):
|
| 5 |
self.path = path
|
|
|
|
| 6 |
self.memories = []
|
| 7 |
-
|
| 8 |
-
with open(path, "r") as f:
|
| 9 |
-
try: self.memories = json.load(f)
|
| 10 |
-
except: self.memories = []
|
| 11 |
|
| 12 |
def add_memory(self, text, vector):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
if
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
self.save()
|
| 20 |
-
return "Updated existing memory."
|
| 21 |
-
self.memories.append({'text': text, 'vector': vector.tolist(), 'weight': 0.5})
|
| 22 |
self.save()
|
| 23 |
return "Saved new memory."
|
| 24 |
|
| 25 |
def retrieve_relevant(self, query_vector, top_k=5):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
return sorted(scored, key=lambda x: x[1], reverse=True)[:top_k]
|
| 32 |
|
| 33 |
def save(self):
|
|
|
|
| 34 |
with open(self.path, "w") as f:
|
| 35 |
-
json.dump(self.memories, f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
class MemoryStore:
|
| 6 |
def __init__(self, path="/mnt/data/memory.json"):
|
| 7 |
self.path = path
|
| 8 |
+
os.makedirs(os.path.dirname(self.path), exist_ok=True) # ensure folder exists
|
| 9 |
self.memories = []
|
| 10 |
+
self.load()
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def add_memory(self, text, vector):
|
| 13 |
+
# avoid duplicates based on cosine similarity
|
| 14 |
+
if self.memories:
|
| 15 |
+
sims = [self.cosine_similarity(vector, np.array(m["vector"])) for m in self.memories]
|
| 16 |
+
if max(sims) > 0.95:
|
| 17 |
+
return "Duplicate detected — memory already stored."
|
| 18 |
+
self.memories.append({"text": text, "vector": vector.tolist()})
|
|
|
|
|
|
|
|
|
|
| 19 |
self.save()
|
| 20 |
return "Saved new memory."
|
| 21 |
|
| 22 |
def retrieve_relevant(self, query_vector, top_k=5):
|
| 23 |
+
if not self.memories:
|
| 24 |
+
return []
|
| 25 |
+
scored = [
|
| 26 |
+
(m["text"], self.cosine_similarity(query_vector, np.array(m["vector"])))
|
| 27 |
+
for m in self.memories
|
| 28 |
+
]
|
| 29 |
return sorted(scored, key=lambda x: x[1], reverse=True)[:top_k]
|
| 30 |
|
| 31 |
def save(self):
|
| 32 |
+
os.makedirs(os.path.dirname(self.path), exist_ok=True)
|
| 33 |
with open(self.path, "w") as f:
|
| 34 |
+
json.dump(self.memories, f)
|
| 35 |
+
|
| 36 |
+
def load(self):
|
| 37 |
+
if os.path.exists(self.path):
|
| 38 |
+
with open(self.path, "r") as f:
|
| 39 |
+
self.memories = json.load(f)
|
| 40 |
+
else:
|
| 41 |
+
self.memories = []
|
| 42 |
+
|
| 43 |
+
@staticmethod
|
| 44 |
+
def cosine_similarity(a, b):
|
| 45 |
+
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|