Spaces:
Sleeping
Sleeping
File size: 803 Bytes
4aa868c |
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 26 27 28 29 30 31 32 33 34 |
import json
from pathlib import Path
MEMORY_FILE = Path("data/memory.json")
# Load memory
def load_memory():
if MEMORY_FILE.exists():
with open(MEMORY_FILE, "r") as f:
return json.load(f)
return {}
# Save memory
def save_memory(memory):
with open(MEMORY_FILE, "w") as f:
json.dump(memory, f, indent=2)
# Remember a message/response for an agent
def remember(agent_id, message, response):
memory = load_memory()
if agent_id not in memory:
memory[agent_id] = []
memory[agent_id].append({
"message": message,
"response": response
})
save_memory(memory)
# Recall last n interactions
def recall(agent_id, n=5):
memory = load_memory()
if agent_id not in memory:
return []
return memory[agent_id][-n:] |