Spaces:
Runtime error
Runtime error
Update src/history.py
Browse files- src/history.py +42 -14
src/history.py
CHANGED
|
@@ -3,6 +3,30 @@ import os
|
|
| 3 |
from typing import List, Dict
|
| 4 |
from datetime import datetime
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
class ChatHistory:
|
| 7 |
def __init__(self, storage_path: str, max_entries: int = 10):
|
| 8 |
self.path = storage_path
|
|
@@ -12,19 +36,23 @@ class ChatHistory:
|
|
| 12 |
def _initialize_storage(self):
|
| 13 |
if not os.path.exists(self.path):
|
| 14 |
with open(self.path, 'w') as f:
|
| 15 |
-
json.dump(
|
| 16 |
|
| 17 |
-
def add_entry(self, user_message: str, bot_response: str):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
history
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
def get_history(self) -> List[Dict]:
|
| 29 |
-
|
| 30 |
-
return json.load(f)
|
|
|
|
| 3 |
from typing import List, Dict
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
+
def save_conversation(user_id: str, messages: List[Dict], file_path: str = "data/chat_history.json"):
|
| 7 |
+
"""Save conversation history to JSON file"""
|
| 8 |
+
try:
|
| 9 |
+
history = load_conversation(file_path)
|
| 10 |
+
history[user_id] = messages[-10:] # Keep last 10 messages
|
| 11 |
+
|
| 12 |
+
with open(file_path, 'w') as f:
|
| 13 |
+
json.dump(history, f, indent=2)
|
| 14 |
+
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error saving conversation: {e}")
|
| 17 |
+
|
| 18 |
+
def load_conversation(file_path: str = "data/chat_history.json") -> Dict[str, List[Dict]]:
|
| 19 |
+
"""Load conversation history from JSON file"""
|
| 20 |
+
try:
|
| 21 |
+
if not os.path.exists(file_path):
|
| 22 |
+
return {}
|
| 23 |
+
|
| 24 |
+
with open(file_path, 'r') as f:
|
| 25 |
+
return json.load(f)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Error loading conversation: {e}")
|
| 28 |
+
return {}
|
| 29 |
+
|
| 30 |
class ChatHistory:
|
| 31 |
def __init__(self, storage_path: str, max_entries: int = 10):
|
| 32 |
self.path = storage_path
|
|
|
|
| 36 |
def _initialize_storage(self):
|
| 37 |
if not os.path.exists(self.path):
|
| 38 |
with open(self.path, 'w') as f:
|
| 39 |
+
json.dump({}, f)
|
| 40 |
|
| 41 |
+
def add_entry(self, user_id: str, user_message: str, bot_response: str):
|
| 42 |
+
history = load_conversation(self.path)
|
| 43 |
+
if user_id not in history:
|
| 44 |
+
history[user_id] = []
|
| 45 |
+
|
| 46 |
+
history[user_id].append({
|
| 47 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 48 |
+
"user": user_message,
|
| 49 |
+
"bot": bot_response
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
# Keep only the last max_entries messages
|
| 53 |
+
history[user_id] = history[user_id][-self.max_entries:]
|
| 54 |
+
|
| 55 |
+
save_conversation(user_id, history[user_id], self.path)
|
| 56 |
|
| 57 |
+
def get_history(self, user_id: str) -> List[Dict]:
|
| 58 |
+
return load_conversation(self.path).get(user_id, [])
|
|
|