File size: 946 Bytes
5bc6c3a
 
4473ebc
 
 
 
 
 
 
 
5bc6c3a
4473ebc
 
5bc6c3a
 
4473ebc
 
5bc6c3a
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from sentence_transformers import SentenceTransformer
import json, os, numpy as np

class MemorySummarizer:
    def __init__(self, memory_path="semantic_memory.json"):
        self.memory_path = memory_path
        self.model = SentenceTransformer("all-MiniLM-L6-v2")

    def summarize(self):
        if not os.path.exists(self.memory_path):
            return "No memory yet."
        with open(self.memory_path, "r") as f:
            data = json.load(f)
        texts = [d["text"] for d in data]
        if not texts:
            return "Memory empty."

        embeddings = self.model.encode(texts)
        centroid = np.mean(embeddings, axis=0)
        sims = np.dot(embeddings, centroid) / (
            np.linalg.norm(embeddings, axis=1) * np.linalg.norm(centroid)
        )
        top = np.argsort(sims)[-5:][::-1]
        key_lines = [texts[i] for i in top]
        return "Here’s what I know about you:\n- " + "\n- ".join(key_lines)