Leonydis137 commited on
Commit
3f94e37
·
verified ·
1 Parent(s): 81b9e60

Update memory.py

Browse files
Files changed (1) hide show
  1. memory.py +11 -8
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({"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)
 
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)