Leonydis137 commited on
Commit
ac47337
·
verified ·
1 Parent(s): 3f1c563

Update memory.py

Browse files
Files changed (1) hide show
  1. memory.py +22 -9
memory.py CHANGED
@@ -1,14 +1,27 @@
1
- # memory.py
 
 
 
2
 
3
  def init_memory():
4
- # Initialize memory as a simple dict with a 'texts' list
5
- return {"texts": []}
6
-
7
- def add_to_memory(text, memory):
8
- # Append text to memory, keep last 100 entries
9
- memory["texts"].append(text)
10
- if len(memory["texts"]) > 100:
11
- memory["texts"] = memory["texts"][-100:]
 
 
 
 
 
 
 
 
 
 
12
 
13
  def search_memory(query, memory):
14
  # Placeholder: Return last few memory entries related to query (simple all for now)
 
1
+ import json
2
+ import os
3
+
4
+ MEMORY_FILE = "memory.json"
5
 
6
  def init_memory():
7
+ if not os.path.exists(MEMORY_FILE):
8
+ with open(MEMORY_FILE, "w") as f:
9
+ json.dump({"history": []}, f)
10
+ with open(MEMORY_FILE, "r") as f:
11
+ return json.load(f)
12
+
13
+ def save_memory(memory):
14
+ with open(MEMORY_FILE, "w") as f:
15
+ json.dump(memory, f, indent=2)
16
+
17
+ def add_to_memory(prompt, response):
18
+ memory = init_memory()
19
+ memory["history"].append({"prompt": prompt, "response": response})
20
+ memory["history"] = memory["history"][-50:] # limit to last 50
21
+ save_memory(memory)
22
+
23
+ def get_memory():
24
+ return init_memory()["history"]
25
 
26
  def search_memory(query, memory):
27
  # Placeholder: Return last few memory entries related to query (simple all for now)