Spaces:
Runtime error
Runtime error
Upload response_engine.py
Browse files- response_engine.py +25 -0
response_engine.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from knowledge_base import KnowledgeBase
|
3 |
+
from datetime import datetime
|
4 |
+
import random
|
5 |
+
|
6 |
+
class ResponseEngine:
|
7 |
+
def __init__(self):
|
8 |
+
self.kb = KnowledgeBase()
|
9 |
+
|
10 |
+
def format_response(self, user_input: str) -> str:
|
11 |
+
answer = self.kb.retrieve_answer(user_input)
|
12 |
+
|
13 |
+
if "don't know" in answer.lower():
|
14 |
+
fallback = [
|
15 |
+
"I'm not sure about that yet, but I'm learning!",
|
16 |
+
"That's a great question. I'll need to look into it.",
|
17 |
+
"I don't have that information, but I'll try to find out."
|
18 |
+
]
|
19 |
+
return random.choice(fallback)
|
20 |
+
|
21 |
+
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M')
|
22 |
+
return f"[{timestamp}] 🤖 Based on what I know: {answer}"
|
23 |
+
|
24 |
+
def close(self):
|
25 |
+
self.kb.close()
|