-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
27 lines (20 loc) · 721 Bytes
/
Copy pathmemory.py
File metadata and controls
27 lines (20 loc) · 721 Bytes
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
import json
import os
MAX_MESSAGES = 5
def save_memory(messages, filepath = "memory/session.json"):
if not os.path.exists("memory"):
os.makedirs("memory")
with open(filepath, "w") as f:
json.dump(messages, f, indent=2, default=str)
def load_memory(filepath = "memory/session.json"):
try:
with open(filepath, "r") as f:
messages = json.load(f)
if len(messages) > MAX_MESSAGES:
return [messages[0], *messages[-MAX_MESSAGES: ]]
else:
return messages
except FileNotFoundError:
return [{
"role" : "system", "content" : "You are an helpful weather assistant."
}]