jt-memory 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
jt_memory/__init__.py
ADDED
jt_memory/core.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import time
|
|
3
|
+
from dataclasses import dataclass, field, asdict
|
|
4
|
+
from typing import List, Dict, Any, Optional
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class MemoryNode:
|
|
8
|
+
content: str
|
|
9
|
+
context: str
|
|
10
|
+
expected_outcome: float
|
|
11
|
+
actual_outcome: float
|
|
12
|
+
emotional_intensity: float
|
|
13
|
+
importance_score: float = field(init=False)
|
|
14
|
+
timestamp: float = field(default_factory=time.time)
|
|
15
|
+
is_consolidated: bool = False
|
|
16
|
+
|
|
17
|
+
def __post_init__(self):
|
|
18
|
+
prediction_error = abs(self.expected_outcome - self.actual_outcome)
|
|
19
|
+
self.importance_score = round(prediction_error * self.emotional_intensity, 4)
|
|
20
|
+
|
|
21
|
+
class JTMemoryEngine:
|
|
22
|
+
"""
|
|
23
|
+
Emotion-Guided Continual Memory Engine for AI Chatbots & Agents.
|
|
24
|
+
Prevents catastrophic forgetting and memory bloat via Importance Gating and Sleep Pruning.
|
|
25
|
+
"""
|
|
26
|
+
def __init__(self, importance_threshold: float = 5.0, max_long_term_nodes: int = 100):
|
|
27
|
+
self.importance_threshold = importance_threshold
|
|
28
|
+
self.max_long_term_nodes = max_long_term_nodes
|
|
29
|
+
self.short_term_memory: List[MemoryNode] = []
|
|
30
|
+
self.long_term_memory: List[MemoryNode] = []
|
|
31
|
+
|
|
32
|
+
def observe(self, content: str, expected: float, actual: float, intensity: float, context: str = "general") -> MemoryNode:
|
|
33
|
+
node = MemoryNode(content, context, expected, actual, intensity)
|
|
34
|
+
if node.importance_score >= self.importance_threshold:
|
|
35
|
+
node.is_consolidated = True
|
|
36
|
+
self._consolidate(node)
|
|
37
|
+
else:
|
|
38
|
+
self.short_term_memory.append(node)
|
|
39
|
+
return node
|
|
40
|
+
|
|
41
|
+
def _consolidate(self, node: MemoryNode) -> None:
|
|
42
|
+
for idx, existing in enumerate(self.long_term_memory):
|
|
43
|
+
if existing.context == node.context and existing.content == node.content:
|
|
44
|
+
self.long_term_memory[idx] = node
|
|
45
|
+
return
|
|
46
|
+
self.long_term_memory.append(node)
|
|
47
|
+
if len(self.long_term_memory) > self.max_long_term_nodes:
|
|
48
|
+
self.long_term_memory.sort(key=lambda x: x.importance_score, reverse=True)
|
|
49
|
+
self.long_term_memory.pop()
|
|
50
|
+
|
|
51
|
+
def sleep_and_prune(self) -> int:
|
|
52
|
+
purged = len(self.short_term_memory)
|
|
53
|
+
self.short_term_memory.clear()
|
|
54
|
+
return purged
|
|
55
|
+
|
|
56
|
+
def recall(self, context: Optional[str] = None, top_k: int = 5) -> List[Dict[str, Any]]:
|
|
57
|
+
memories = [m for m in self.long_term_memory if not context or m.context == context]
|
|
58
|
+
sorted_m = sorted(memories, key=lambda x: x.importance_score, reverse=True)[:top_k]
|
|
59
|
+
return [{"content": m.content, "context": m.context, "importance": m.importance_score} for m in sorted_m]
|
|
60
|
+
|
|
61
|
+
def save(self, filepath: str = "jt_memory_store.json") -> None:
|
|
62
|
+
"""Saves consolidated long-term memory to disk."""
|
|
63
|
+
data = {
|
|
64
|
+
"importance_threshold": self.importance_threshold,
|
|
65
|
+
"max_long_term_nodes": self.max_long_term_nodes,
|
|
66
|
+
"long_term_memory": [asdict(m) for m in self.long_term_memory]
|
|
67
|
+
}
|
|
68
|
+
with open(filepath, "w", encoding="utf-8") as f:
|
|
69
|
+
json.dump(data, f, indent=2)
|
|
70
|
+
|
|
71
|
+
def load(self, filepath: str = "jt_memory_store.json") -> None:
|
|
72
|
+
"""Loads consolidated long-term memory from disk."""
|
|
73
|
+
with open(filepath, "r", encoding="utf-8") as f:
|
|
74
|
+
data = json.load(f)
|
|
75
|
+
self.importance_threshold = data.get("importance_threshold", self.importance_threshold)
|
|
76
|
+
self.max_long_term_nodes = data.get("max_long_term_nodes", self.max_long_term_nodes)
|
|
77
|
+
self.long_term_memory = []
|
|
78
|
+
for item in data.get("long_term_memory", []):
|
|
79
|
+
node = MemoryNode(
|
|
80
|
+
content=item["content"],
|
|
81
|
+
context=item["context"],
|
|
82
|
+
expected_outcome=item["expected_outcome"],
|
|
83
|
+
actual_outcome=item["actual_outcome"],
|
|
84
|
+
emotional_intensity=item["emotional_intensity"],
|
|
85
|
+
timestamp=item.get("timestamp", time.time()),
|
|
86
|
+
is_consolidated=item.get("is_consolidated", True)
|
|
87
|
+
)
|
|
88
|
+
self.long_term_memory.append(node)
|
|
89
|
+
|
|
90
|
+
def stats(self) -> Dict[str, int]:
|
|
91
|
+
return {"short_term": len(self.short_term_memory), "long_term": len(self.long_term_memory)}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jt-memory
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Emotion-guided continual memory engine for AI chatbots and agents.
|
|
5
|
+
Author-email: Shrishant Jha <shrishant@example.com>
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
|
|
9
|
+
# jt-memory (by Jha Technologies)
|
|
10
|
+
Emotion-Guided Continual Memory Engine for AI Chatbots & Agents.
|
|
11
|
+
Formula: `Importance = |Expected - Actual| * Emotional Intensity`
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
jt_memory/__init__.py,sha256=u2CuSVbfWHzU9RWFZnBqYMkt070pRLhK7vqOYUr0GgU,115
|
|
2
|
+
jt_memory/core.py,sha256=5VXDAe5NZ1hBWUIPFapn7zfZhYoeM5lNC84zpQWDN0I,4153
|
|
3
|
+
jt_memory-0.1.0.dist-info/METADATA,sha256=dZDGJOcLspvKw9mRco3BQA3Vx3XbYAFBrTW_1WacEVk,410
|
|
4
|
+
jt_memory-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
jt_memory-0.1.0.dist-info/top_level.txt,sha256=AM5nYdIpe-6KJ9L9mSCjWptOyyUZ4f30U8BQOhvX5yM,10
|
|
6
|
+
jt_memory-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
jt_memory
|