Spaces:
Runtime error
Runtime error
change format answer
Browse files- core/qa_pipeline.py +0 -57
core/qa_pipeline.py
CHANGED
|
@@ -241,63 +241,6 @@ def ask_ai_stream_delta(message: str, history: List, hybrid_retriever, cohort_ke
|
|
| 241 |
yield "Chào bạn 👋 Mình hỗ trợ tra cứu quy chế đào tạo. Bạn cần hỏi điều gì?"
|
| 242 |
return
|
| 243 |
|
| 244 |
-
# Chế độ chỉ liệt kê tài liệu cho câu hỏi đầu tiên (mặc định: tắt)
|
| 245 |
-
first_turn_docs_only = os.getenv("FIRST_TURN_DOCS_ONLY", "false").strip().lower() in {"1", "true", "yes", "on"}
|
| 246 |
-
if first_turn_docs_only and (not history or len(history) == 0):
|
| 247 |
-
logger.info(f"[FIRST TURN] CÂU HỎI GỐC: {message}")
|
| 248 |
-
question = message.strip()
|
| 249 |
-
processed_data = analyze_and_expand_query(question)
|
| 250 |
-
queries = processed_data.get('expanded_queries', [question])
|
| 251 |
-
|
| 252 |
-
# Chỉ tìm kiếm docs, không gọi LLM
|
| 253 |
-
all_docs: List = []
|
| 254 |
-
seen = set()
|
| 255 |
-
seen_lock = Lock()
|
| 256 |
-
|
| 257 |
-
def search_query(query: str):
|
| 258 |
-
current_alpha = 0.4 if "CNTT" in query.upper() else 0.5
|
| 259 |
-
return hybrid_retriever.search(
|
| 260 |
-
query,
|
| 261 |
-
k=TOP_K_RESULTS,
|
| 262 |
-
alpha=current_alpha,
|
| 263 |
-
cohort_key=cohort_key,
|
| 264 |
-
)
|
| 265 |
-
|
| 266 |
-
with ThreadPoolExecutor(max_workers=min(3, len(queries))) as executor:
|
| 267 |
-
futures = {executor.submit(search_query, q): q for q in queries}
|
| 268 |
-
for future in futures:
|
| 269 |
-
try:
|
| 270 |
-
docs = future.result(timeout=30)
|
| 271 |
-
for doc in docs:
|
| 272 |
-
content_hash = hashlib.sha256(doc.page_content.encode("utf-8")).hexdigest()
|
| 273 |
-
with seen_lock:
|
| 274 |
-
if content_hash not in seen:
|
| 275 |
-
seen.add(content_hash)
|
| 276 |
-
all_docs.append(doc)
|
| 277 |
-
except Exception:
|
| 278 |
-
logger.exception("Search error")
|
| 279 |
-
|
| 280 |
-
# Format documents thành văn bản trả về với markdown đúng
|
| 281 |
-
if all_docs:
|
| 282 |
-
result_text = "## 📚 Các tài liệu liên quan\n\n"
|
| 283 |
-
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"
|
| 284 |
-
|
| 285 |
-
for i, doc in enumerate(all_docs[:FINAL_TOP_K], 1):
|
| 286 |
-
source = doc.metadata.get("source") or "Không rõ"
|
| 287 |
-
content_preview = doc.page_content[:250] + ("..." if len(doc.page_content) > 250 else "")
|
| 288 |
-
# Định dạng markdown: bullet point, bold source
|
| 289 |
-
result_text += f"**{i}. Nguồn:** {source}\n"
|
| 290 |
-
result_text += f"{content_preview}\n\n"
|
| 291 |
-
|
| 292 |
-
result_text += "---\n\n"
|
| 293 |
-
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"
|
| 294 |
-
result_text += "*Ví dụ: \"Điều kiện để nhận học bổng là gì?\" thay vì \"Học bổng\"*"
|
| 295 |
-
|
| 296 |
-
yield result_text
|
| 297 |
-
else:
|
| 298 |
-
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!"
|
| 299 |
-
return
|
| 300 |
-
|
| 301 |
logger.info(f" CÂU HỎI GỐC: {message}")
|
| 302 |
question = generate_standalone_query(message, history)
|
| 303 |
|
|
|
|
| 241 |
yield "Chào bạn 👋 Mình hỗ trợ tra cứu quy chế đào tạo. Bạn cần hỏi điều gì?"
|
| 242 |
return
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
logger.info(f" CÂU HỎI GỐC: {message}")
|
| 245 |
question = generate_standalone_query(message, history)
|
| 246 |
|