Spaces:
Sleeping
Sleeping
Upload short_term_memory.py with huggingface_hub
Browse files- short_term_memory.py +19 -0
short_term_memory.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# short_term_memory.py - Recent Chat Buffer
|
| 2 |
+
from collections import deque
|
| 3 |
+
|
| 4 |
+
class ShortTermMemory:
|
| 5 |
+
def __init__(self, max_size=10):
|
| 6 |
+
self.memory = deque(maxlen=max_size)
|
| 7 |
+
|
| 8 |
+
def add(self, user_input, ai_response):
|
| 9 |
+
self.memory.append({
|
| 10 |
+
"user": user_input,
|
| 11 |
+
"ai": ai_response,
|
| 12 |
+
"timestamp": datetime.now().isoformat()
|
| 13 |
+
})
|
| 14 |
+
|
| 15 |
+
def get_context(self):
|
| 16 |
+
return list(self.memory)
|
| 17 |
+
|
| 18 |
+
def clear(self):
|
| 19 |
+
self.memory.clear()
|