gaur3009 commited on
Commit
76ea7e5
Β·
verified Β·
1 Parent(s): 9e97937

Create memory.py

Browse files
Files changed (1) hide show
  1. memory.py +23 -0
memory.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class ConversationMemory:
2
+ def __init__(self, max_length=2000):
3
+ self.history = []
4
+ self.max_length = max_length
5
+
6
+ def add_exchange(self, question, answer):
7
+ self.history.append({"user": question, "assistant": answer['response']})
8
+ self._trim_history()
9
+
10
+ def get_context(self):
11
+ """Format last 3 exchanges as context"""
12
+ context = ""
13
+ for exchange in self.history[-3:]:
14
+ context += f"User: {exchange['user']}\nAssistant: {exchange['assistant']}\n\n"
15
+ return context.strip()
16
+
17
+ def _trim_history(self):
18
+ """Keep conversation within token limit"""
19
+ total_length = sum(len(exchange['user']) + len(exchange['assistant'])
20
+ for exchange in self.history)
21
+ while total_length > self.max_length and self.history:
22
+ removed = self.history.pop(0)
23
+ total_length -= len(removed['user']) + len(removed['assistant'])