Davidtran99
commited on
Commit
·
3c30480
1
Parent(s):
895bb47
Fix: Cải thiện xử lý follow-up questions và off-topic questions
Browse files
backend/hue_portal/chatbot/chatbot.py
CHANGED
|
@@ -113,14 +113,105 @@ class Chatbot(CoreChatbot):
|
|
| 113 |
}
|
| 114 |
|
| 115 |
elif route_decision.route == IntentRoute.SMALL_TALK:
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
else: # IntentRoute.SEARCH
|
| 126 |
# Use core chatbot search for other intents
|
|
|
|
| 113 |
}
|
| 114 |
|
| 115 |
elif route_decision.route == IntentRoute.SMALL_TALK:
|
| 116 |
+
# Xử lý follow-up questions trong context
|
| 117 |
+
follow_up_keywords = ["có điều khoản", "liên quan", "khác", "nữa", "thêm", "tóm tắt", "tải file"]
|
| 118 |
+
query_lower = query.lower()
|
| 119 |
+
is_follow_up = any(kw in query_lower for kw in follow_up_keywords)
|
| 120 |
+
|
| 121 |
+
response = None
|
| 122 |
+
|
| 123 |
+
# Nếu là follow-up question, thử tìm context từ conversation trước
|
| 124 |
+
if is_follow_up and session_id:
|
| 125 |
+
try:
|
| 126 |
+
recent_messages = ConversationContext.get_recent_messages(session_id, limit=5)
|
| 127 |
+
# Tìm message bot cuối cùng có results
|
| 128 |
+
for msg in reversed(recent_messages):
|
| 129 |
+
if msg.role == "bot" and msg.intent == "search_legal":
|
| 130 |
+
# Có context về legal query trước đó, thử search lại với query mới
|
| 131 |
+
enhanced_query = f"{query} {msg.content[:100]}"
|
| 132 |
+
search_result = self.search_by_intent("search_legal", enhanced_query, limit=3)
|
| 133 |
+
if search_result["count"] > 0:
|
| 134 |
+
# Tìm thấy results, trả về
|
| 135 |
+
top_result = search_result["results"][0]
|
| 136 |
+
top_data = top_result.get("data", {})
|
| 137 |
+
doc_code = top_data.get("document_code", "")
|
| 138 |
+
doc_title = top_data.get("document_title", "văn bản pháp luật")
|
| 139 |
+
section_code = top_data.get("section_code", "")
|
| 140 |
+
section_title = top_data.get("section_title", "")
|
| 141 |
+
content = top_data.get("content", "") or top_data.get("excerpt", "")
|
| 142 |
+
|
| 143 |
+
if "tóm tắt" in query_lower:
|
| 144 |
+
content_preview = content[:400] + "..." if len(content) > 400 else content
|
| 145 |
+
message = (
|
| 146 |
+
f"**Tóm tắt {section_code}**: {section_title or 'Nội dung chính'}\n\n"
|
| 147 |
+
f"{content_preview}\n\n"
|
| 148 |
+
f"Nguồn: {doc_title}" + (f" ({doc_code})" if doc_code else "")
|
| 149 |
+
)
|
| 150 |
+
elif "tải" in query_lower or "download" in query_lower:
|
| 151 |
+
message = (
|
| 152 |
+
f"Bạn có thể tải file gốc của {doc_title}" + (f" ({doc_code})" if doc_code else "") +
|
| 153 |
+
f" từ link download trong kết quả tìm kiếm."
|
| 154 |
+
)
|
| 155 |
+
else:
|
| 156 |
+
# Câu hỏi "có điều khoản liên quan nào khác không?"
|
| 157 |
+
if search_result["count"] > 1:
|
| 158 |
+
message = (
|
| 159 |
+
f"Có, tôi tìm thấy {search_result['count']} điều khoản liên quan:\n\n"
|
| 160 |
+
)
|
| 161 |
+
for i, result in enumerate(search_result["results"][:3], 1):
|
| 162 |
+
data = result.get("data", {})
|
| 163 |
+
sec_code = data.get("section_code", "")
|
| 164 |
+
sec_title = data.get("section_title", "")
|
| 165 |
+
message += f"{i}. **{sec_code}**: {sec_title or 'Nội dung liên quan'}\n"
|
| 166 |
+
message += f"\nNguồn: {doc_title}" + (f" ({doc_code})" if doc_code else "")
|
| 167 |
+
else:
|
| 168 |
+
message = (
|
| 169 |
+
f"Tôi đã tìm thấy điều khoản liên quan:\n\n"
|
| 170 |
+
f"**{section_code}**: {section_title or 'Nội dung liên quan'}\n\n"
|
| 171 |
+
f"{content[:300]}...\n\n"
|
| 172 |
+
f"Nguồn: {doc_title}" + (f" ({doc_code})" if doc_code else "")
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
response = {
|
| 176 |
+
"message": message,
|
| 177 |
+
"intent": "search_legal",
|
| 178 |
+
"confidence": 0.85,
|
| 179 |
+
"results": search_result["results"][:3],
|
| 180 |
+
"count": search_result["count"],
|
| 181 |
+
"routing": "follow_up"
|
| 182 |
+
}
|
| 183 |
+
break
|
| 184 |
+
except Exception as e:
|
| 185 |
+
logger.warning(f"[FOLLOW_UP] Failed to process follow-up: {e}")
|
| 186 |
+
|
| 187 |
+
# Nếu không phải follow-up hoặc không tìm thấy context, trả về message thân thiện
|
| 188 |
+
if response is None:
|
| 189 |
+
# Detect off-topic questions (nấu ăn, chả trứng, etc.)
|
| 190 |
+
off_topic_keywords = ["nấu", "nau", "chả trứng", "cha trung", "món ăn", "mon an", "công thức", "cong thuc",
|
| 191 |
+
"cách làm", "cach lam", "đổ chả", "do cha", "trứng", "trung"]
|
| 192 |
+
is_off_topic = any(kw in query_lower for kw in off_topic_keywords)
|
| 193 |
+
|
| 194 |
+
if is_off_topic:
|
| 195 |
+
message = (
|
| 196 |
+
"Xin lỗi, tôi là chatbot chuyên về tra cứu các văn bản quy định pháp luật "
|
| 197 |
+
"về xử lí kỷ luật cán bộ đảng viên của Phòng Thanh Tra - Công An Thành Phố Huế.\n\n"
|
| 198 |
+
"Tôi không thể trả lời các câu hỏi về nấu ăn, công thức nấu ăn hay các chủ đề khác ngoài phạm vi pháp luật.\n\n"
|
| 199 |
+
"Bạn có muốn tra cứu thông tin về:\n"
|
| 200 |
+
"- Các quy định về xử lí kỷ luật cán bộ đảng viên\n"
|
| 201 |
+
"- Các điều khoản trong Thông tư 02 về xử lý điều lệnh trong CAND\n"
|
| 202 |
+
"- Hoặc các văn bản pháp luật liên quan khác?"
|
| 203 |
+
)
|
| 204 |
+
else:
|
| 205 |
+
message = "Tôi có thể giúp bạn tra cứu các văn bản quy định pháp luật về xử lí kỷ luật cán bộ đảng viên. Bạn muốn tìm gì?"
|
| 206 |
+
|
| 207 |
+
response = {
|
| 208 |
+
"message": message,
|
| 209 |
+
"intent": intent,
|
| 210 |
+
"confidence": confidence,
|
| 211 |
+
"results": [],
|
| 212 |
+
"count": 0,
|
| 213 |
+
"routing": "small_talk"
|
| 214 |
+
}
|
| 215 |
|
| 216 |
else: # IntentRoute.SEARCH
|
| 217 |
# Use core chatbot search for other intents
|