-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
27 lines (19 loc) · 722 Bytes
/
memory.py
File metadata and controls
27 lines (19 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from collections import deque
from typing import List, Dict, Deque
from config import MAX_HISTORY_ITEMS
class ConversationMemory:
def __init__(self, max_items: int = MAX_HISTORY_ITEMS):
self._history: Deque[Dict[str, str]] = deque(maxlen=max_items)
def add(self, role: str, content: str):
self._history.append({"role": role, "content": content})
def get(self) -> List[Dict[str, str]]:
return list(self._history)
def clear(self):
self._history.clear()
_memory = ConversationMemory()
def add_to_history(role: str, content: str):
_memory.add(role, content)
def get_history() -> List[Dict[str, str]]:
return _memory.get()
def clear_history():
_memory.clear()