Spaces:
Sleeping
Sleeping
| 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:] |