Spaces:
Build error
Build error
Create memory_core.py
Browse files- memory_core.py +26 -0
memory_core.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# memory_core.py
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
MEMORY_FILE = "agent_memory.json"
|
| 6 |
+
|
| 7 |
+
def load_memory():
|
| 8 |
+
"""Load stored agent memory if it exists."""
|
| 9 |
+
if not os.path.exists(MEMORY_FILE):
|
| 10 |
+
return {}
|
| 11 |
+
with open(MEMORY_FILE, "r") as f:
|
| 12 |
+
return json.load(f)
|
| 13 |
+
|
| 14 |
+
def save_memory(memory):
|
| 15 |
+
"""Save updated memory."""
|
| 16 |
+
with open(MEMORY_FILE, "w") as f:
|
| 17 |
+
json.dump(memory, f, indent=2)
|
| 18 |
+
|
| 19 |
+
def update_memory(agent_id, key, value):
|
| 20 |
+
"""Store learning or result per agent."""
|
| 21 |
+
memory = load_memory()
|
| 22 |
+
if agent_id not in memory:
|
| 23 |
+
memory[agent_id] = {}
|
| 24 |
+
memory[agent_id][key] = value
|
| 25 |
+
save_memory(memory)
|
| 26 |
+
print(f"[Memory] {agent_id} learned {key}: {value}")
|