Spaces:
Runtime error
Runtime error
change format answer
Browse files- core/qa_pipeline.py +33 -6
core/qa_pipeline.py
CHANGED
|
@@ -276,14 +276,23 @@ def ask_ai_stream_delta(message: str, history: List, hybrid_retriever, cohort_ke
|
|
| 276 |
except Exception:
|
| 277 |
logger.exception("Search error")
|
| 278 |
|
| 279 |
-
# Format documents thành văn bản trả về
|
| 280 |
if all_docs:
|
| 281 |
-
result_text = "📚
|
|
|
|
|
|
|
| 282 |
for i, doc in enumerate(all_docs[:FINAL_TOP_K], 1):
|
| 283 |
source = doc.metadata.get("source") or "Không rõ"
|
| 284 |
-
content_preview = doc.page_content[:
|
| 285 |
-
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
else:
|
| 288 |
yield "❌ Không tìm thấy tài liệu liên quan. Vui lòng hãy đặt câu hỏi cụ thể hơn!"
|
| 289 |
return
|
|
@@ -340,6 +349,7 @@ def ask_ai_stream_delta(message: str, history: List, hybrid_retriever, cohort_ke
|
|
| 340 |
final_docs = advanced_rerank(question, all_docs, top_k=FINAL_TOP_K)
|
| 341 |
|
| 342 |
context_parts = []
|
|
|
|
| 343 |
total_chars = 0
|
| 344 |
for doc in final_docs:
|
| 345 |
page = doc.metadata.get('page_number', 'N/A')
|
|
@@ -350,6 +360,11 @@ def ask_ai_stream_delta(message: str, history: List, hybrid_retriever, cohort_ke
|
|
| 350 |
break
|
| 351 |
total_chars += len(block)
|
| 352 |
context_parts.append(block)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
context = "\n\n---\n\n".join(context_parts)
|
| 355 |
topic_hint = processed_data.get('topic') or processed_data.get('root_question') or question
|
|
@@ -401,4 +416,16 @@ def ask_ai_stream_delta(message: str, history: List, hybrid_retriever, cohort_ke
|
|
| 401 |
logger.error(f"Lỗi Gemini: {e}")
|
| 402 |
|
| 403 |
if not success:
|
| 404 |
-
yield "Đã xảy ra lỗi hệ thống hoặc quá tải. Vui lòng thử lại sau giây lát!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
except Exception:
|
| 277 |
logger.exception("Search error")
|
| 278 |
|
| 279 |
+
# Format documents thành văn bản trả về với markdown đúng
|
| 280 |
if all_docs:
|
| 281 |
+
result_text = "## 📚 Các tài liệu liên quan\n\n"
|
| 282 |
+
result_text += "Tôi tìm thấy những tài liệu sau có thể hữu ích cho bạn:\n\n"
|
| 283 |
+
|
| 284 |
for i, doc in enumerate(all_docs[:FINAL_TOP_K], 1):
|
| 285 |
source = doc.metadata.get("source") or "Không rõ"
|
| 286 |
+
content_preview = doc.page_content[:250] + ("..." if len(doc.page_content) > 250 else "")
|
| 287 |
+
# Định dạng markdown: bullet point, bold source
|
| 288 |
+
result_text += f"**{i}. Nguồn:** {source}\n"
|
| 289 |
+
result_text += f"{content_preview}\n\n"
|
| 290 |
+
|
| 291 |
+
result_text += "---\n\n"
|
| 292 |
+
result_text += "**💡 Gợi ý:** Hãy đặt câu hỏi cụ thể hơn để tôi có thể hỗ trợ bạn tốt hơn!\n\n"
|
| 293 |
+
result_text += "*Ví dụ: \"Điều kiện để nhận học bổng là gì?\" thay vì \"Học bổng\"*"
|
| 294 |
+
|
| 295 |
+
yield result_text
|
| 296 |
else:
|
| 297 |
yield "❌ Không tìm thấy tài liệu liên quan. Vui lòng hãy đặt câu hỏi cụ thể hơn!"
|
| 298 |
return
|
|
|
|
| 349 |
final_docs = advanced_rerank(question, all_docs, top_k=FINAL_TOP_K)
|
| 350 |
|
| 351 |
context_parts = []
|
| 352 |
+
context_docs = [] # Lưu metadata để trích dẫn ở cuối
|
| 353 |
total_chars = 0
|
| 354 |
for doc in final_docs:
|
| 355 |
page = doc.metadata.get('page_number', 'N/A')
|
|
|
|
| 360 |
break
|
| 361 |
total_chars += len(block)
|
| 362 |
context_parts.append(block)
|
| 363 |
+
# Lưu metadata cho phần tài liệu tham khảo ở cuối
|
| 364 |
+
context_docs.append({
|
| 365 |
+
'source': file_name or "Không rõ",
|
| 366 |
+
'page': page
|
| 367 |
+
})
|
| 368 |
|
| 369 |
context = "\n\n---\n\n".join(context_parts)
|
| 370 |
topic_hint = processed_data.get('topic') or processed_data.get('root_question') or question
|
|
|
|
| 416 |
logger.error(f"Lỗi Gemini: {e}")
|
| 417 |
|
| 418 |
if not success:
|
| 419 |
+
yield "Đã xảy ra lỗi hệ thống hoặc quá tải. Vui lòng thử lại sau giây lát!"
|
| 420 |
+
return
|
| 421 |
+
|
| 422 |
+
# Thêm phần Tài liệu tham khảo ở cuối
|
| 423 |
+
if context_docs:
|
| 424 |
+
yield "\n\n---\n\n"
|
| 425 |
+
yield "## 📚 Tài liệu tham khảo\n\n"
|
| 426 |
+
seen_sources = set()
|
| 427 |
+
for i, doc_info in enumerate(context_docs, 1):
|
| 428 |
+
source_key = f"{doc_info['source']}_{doc_info['page']}"
|
| 429 |
+
if source_key not in seen_sources:
|
| 430 |
+
seen_sources.add(source_key)
|
| 431 |
+
yield f"- **{doc_info['source']}** (Trang {doc_info['page']})\n"
|