Spaces:
Sleeping
Sleeping
File size: 1,055 Bytes
b814c5a | 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import json
import os
def load_memory(agent_id):
path = f"agents/{agent_id}/memory.json"
if not os.path.exists(path):
return []
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
""""
def save_memory(agent_id, memory):
path = f"agents/{agent_id}/memory.json"
with open(path, "w", encoding="utf-8") as f:
json.dump(
memory,
f,
indent=2
)
"""
## folder exsist
def save_memory(agent_id, memory):
folder = f"agents/{agent_id}"
os.makedirs(folder, exist_ok=True)
path = f"{folder}/memory.json"
with open(path, "w", encoding="utf-8") as f:
json.dump(
memory,
f,
indent=2,
ensure_ascii=False
)
def add_message(agent_id, role, content):
memory = load_memory(agent_id)
memory.append({
"role": role,
"content": content
})
memory = memory[-10:]
save_memory(agent_id, memory) |