Spaces:
Sleeping
Sleeping
Update rag_retrieval.py
Browse files66 unmodified lines676869707170717273747576771 unmodified line79808182838485868788899091929394959697989910010110210366 unmodified lines
def build_answer(user_text: str, rewrite: str, retrieved): """ Simple answer builder: - Show relevant snippets with citations. Answer builder with a simple 'no clear answer' heuristic.
- If scores are very low OR none of the retrieved snippets share meaningful words with the question, we return a graceful fallback. - Otherwise, we list relevant snippets with citations. """ if not retrieved: return (1 unmodified line [] )
# ---- Heuristic: check scores + keyword overlap ---- question_tokens = {t.lower() for t in user_text.split() if len(t) > 3}
def snippet_has_overlap(snippet: str) -> bool: words = {w.lower().strip(".,!?;:()[]") for w in snippet.split()} return len(question_tokens & words) > 0
best_score = max(r["score_combined"] for r in retrieved) any_overlap = any(snippet_has_overlap(r["text"]) for r in retrieved)
if best_score < 0.2 or not any_overlap: # Fallback: nothing strongly relevant in this thread return ( "Within this thread, I don’t see any email that clearly answers this question. " "You may need to search outside this thread or check other conversations.", [] )
# ---- Normal snippet-based answer ---- lines = [f"**Question:** {user_text}", "", "**Relevant information:**"] citations = []
rag_retrieval.py+24-2