refactor message flow on purposes
Browse files- app/message_processor.py +72 -3
app/message_processor.py
CHANGED
|
@@ -182,9 +182,78 @@ class MessageProcessor:
|
|
| 182 |
if value not in (None, "", []) and conv.get(key) in (None, "", []):
|
| 183 |
conv[key] = value
|
| 184 |
logger.info(f"[DEBUG] Message history update cuối cùng: {conv}")
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
# 6. Gửi response và cập nhật final state
|
| 189 |
# Replace all occurrences of '**' with '*' before sending
|
| 190 |
response_to_send = format_for_facebook(response.replace('**', '*')) if isinstance(response, str) else response
|
|
|
|
| 182 |
if value not in (None, "", []) and conv.get(key) in (None, "", []):
|
| 183 |
conv[key] = value
|
| 184 |
logger.info(f"[DEBUG] Message history update cuối cùng: {conv}")
|
| 185 |
+
|
| 186 |
+
# 5. Rẽ nhánh xử lý theo mục đích (muc_dich)
|
| 187 |
+
# Lấy muc_dich từ history nếu có, hoặc từ message mới phân tích
|
| 188 |
+
muc_dich_to_use = None
|
| 189 |
+
if history and conv.get('originalpurpose'):
|
| 190 |
+
muc_dich_to_use = conv.get('originalpurpose')
|
| 191 |
+
else:
|
| 192 |
+
muc_dich_to_use = muc_dich
|
| 193 |
+
logger.info(f"[DEBUG] Định hướng mục đích xử lý: {muc_dich_to_use}")
|
| 194 |
+
|
| 195 |
+
response = None
|
| 196 |
+
if not command:
|
| 197 |
+
if muc_dich_to_use == "hỏi về mức phạt":
|
| 198 |
+
# Giữ nguyên luồng cũ cho hỏi về mức phạt
|
| 199 |
+
vehicle = conv.get('originalvehicle', '')
|
| 200 |
+
action = conv.get('originalaction', '')
|
| 201 |
+
keywords = [kw.strip() for kw in vehicle.split(',') if kw.strip()]
|
| 202 |
+
if keywords:
|
| 203 |
+
if action:
|
| 204 |
+
logger.info(f"[DEBUG] tạo embedding: {action}")
|
| 205 |
+
embedding = await self.channel.embedder.create_embedding(action)
|
| 206 |
+
logger.info(f"[DEBUG] embedding: {embedding[:5]} ... (total {len(embedding)})")
|
| 207 |
+
matches = self.channel.supabase.match_documents(
|
| 208 |
+
embedding,
|
| 209 |
+
vehicle_keywords=keywords,
|
| 210 |
+
user_question=action
|
| 211 |
+
)
|
| 212 |
+
logger.info(f"[DEBUG] matches: {matches}")
|
| 213 |
+
if matches:
|
| 214 |
+
response = await self.format_search_results(message_text, matches, page_token, sender_id)
|
| 215 |
+
else:
|
| 216 |
+
response = "Xin lỗi, tôi không tìm thấy thông tin phù hợp."
|
| 217 |
+
else:
|
| 218 |
+
logger.info(f"[DEBUG] Không có hành vi vi phạm: {message_text}")
|
| 219 |
+
response = "Xin lỗi, tôi không tìm thấy thông tin về hành vi vi phạm trong câu hỏi của bạn."
|
| 220 |
+
conv['isdone'] = True
|
| 221 |
+
else:
|
| 222 |
+
response = "Vui lòng cho biết loại phương tiện bạn cần tìm (xe máy, ô tô...)"
|
| 223 |
+
conv['isdone'] = False
|
| 224 |
+
elif muc_dich_to_use == "hỏi về quy tắc giao thông":
|
| 225 |
+
# Dùng LLM để trả lời câu hỏi về quy tắc giao thông
|
| 226 |
+
answer = await self.channel.llm.generate_text(message_text)
|
| 227 |
+
response = answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng trả lời về quy tắc giao thông sẽ sớm có mặt."
|
| 228 |
+
conv['isdone'] = True
|
| 229 |
+
elif muc_dich_to_use == "hỏi về báo hiệu đường bộ":
|
| 230 |
+
# Dùng LLM để trả lời câu hỏi về báo hiệu đường bộ
|
| 231 |
+
answer = await self.channel.llm.generate_text(message_text)
|
| 232 |
+
response = answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng trả lời về báo hiệu đường bộ sẽ sớm có mặt."
|
| 233 |
+
conv['isdone'] = True
|
| 234 |
+
elif muc_dich_to_use == "hỏi về quy trình xử lý vi phạm giao thông":
|
| 235 |
+
# Dùng LLM để trả lời câu hỏi về quy trình xử lý vi phạm giao thông
|
| 236 |
+
answer = await self.channel.llm.generate_text(message_text)
|
| 237 |
+
response = answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng trả lời về quy trình xử lý vi phạm giao thông sẽ sớm có mặt."
|
| 238 |
+
conv['isdone'] = True
|
| 239 |
+
else:
|
| 240 |
+
# Dùng LLM cho các mục đích khác
|
| 241 |
+
answer = await self.channel.llm.generate_text(message_text)
|
| 242 |
+
response = answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng này sẽ sớm có mặt."
|
| 243 |
+
conv['isdone'] = True
|
| 244 |
+
else:
|
| 245 |
+
# Có command
|
| 246 |
+
if command == "xong":
|
| 247 |
+
post_url = await self.create_facebook_post(page_token, conv['recipient_id'], [conv])
|
| 248 |
+
if post_url:
|
| 249 |
+
response = f"Bài viết đã được tạo thành công! Bạn có thể xem tại: {post_url}"
|
| 250 |
+
else:
|
| 251 |
+
response = "Đã xảy ra lỗi khi tạo bài viết. Vui lòng thử lại sau."
|
| 252 |
+
conv['isdone'] = True
|
| 253 |
+
else:
|
| 254 |
+
response = "Vui lòng cung cấp thêm thông tin và gõ lệnh \\xong khi hoàn tất."
|
| 255 |
+
conv['isdone'] = False
|
| 256 |
+
|
| 257 |
# 6. Gửi response và cập nhật final state
|
| 258 |
# Replace all occurrences of '**' with '*' before sending
|
| 259 |
response_to_send = format_for_facebook(response.replace('**', '*')) if isinstance(response, str) else response
|