Spaces:
Runtime error
Runtime error
Update memory.py
Browse files
memory.py
CHANGED
@@ -6,22 +6,25 @@ MEMORY_FILE = "memory.json"
|
|
6 |
def init_memory():
|
7 |
if not os.path.exists(MEMORY_FILE):
|
8 |
with open(MEMORY_FILE, "w") as f:
|
9 |
-
json.dump({
|
10 |
with open(MEMORY_FILE, "r") as f:
|
11 |
return json.load(f)
|
12 |
|
13 |
-
def save_memory(
|
14 |
with open(MEMORY_FILE, "w") as f:
|
15 |
-
json.dump(
|
16 |
|
17 |
-
def add_to_memory(prompt, response):
|
18 |
memory = init_memory()
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
save_memory(memory)
|
22 |
|
23 |
-
def get_memory():
|
24 |
-
|
|
|
25 |
|
26 |
def search_memory(query, memory):
|
27 |
# Placeholder: Return last few memory entries related to query (simple all for now)
|
|
|
6 |
def init_memory():
|
7 |
if not os.path.exists(MEMORY_FILE):
|
8 |
with open(MEMORY_FILE, "w") as f:
|
9 |
+
json.dump({}, f)
|
10 |
with open(MEMORY_FILE, "r") as f:
|
11 |
return json.load(f)
|
12 |
|
13 |
+
def save_memory(data):
|
14 |
with open(MEMORY_FILE, "w") as f:
|
15 |
+
json.dump(data, f, indent=2)
|
16 |
|
17 |
+
def add_to_memory(session_id, prompt, response):
|
18 |
memory = init_memory()
|
19 |
+
if session_id not in memory:
|
20 |
+
memory[session_id] = []
|
21 |
+
memory[session_id].append({"prompt": prompt, "response": response})
|
22 |
+
memory[session_id] = memory[session_id][-50:]
|
23 |
save_memory(memory)
|
24 |
|
25 |
+
def get_memory(session_id):
|
26 |
+
memory = init_memory()
|
27 |
+
return memory.get(session_id, [])
|
28 |
|
29 |
def search_memory(query, memory):
|
30 |
# Placeholder: Return last few memory entries related to query (simple all for now)
|