Spaces:
Runtime error
Runtime error
Improve answer quality with detailed source-grounded responses
Browse files
app/deployment/hf_status.py
CHANGED
|
@@ -3948,7 +3948,7 @@ function getAnswerStyleInstruction() {
|
|
| 3948 |
return "Answer in a research-style format: direct answer, evidence, interpretation, limitations. Include citations after key claims.";
|
| 3949 |
}
|
| 3950 |
|
| 3951 |
-
return "Answer in a detailed
|
| 3952 |
}
|
| 3953 |
|
| 3954 |
function buildContextualQuery(currentQuestion) {
|
|
@@ -4118,12 +4118,12 @@ async function sendMessage() {
|
|
| 4118 |
const payload = {
|
| 4119 |
query: buildContextualQuery(userText),
|
| 4120 |
document_id: doc.id,
|
| 4121 |
-
top_k:
|
| 4122 |
retrieval_mode: "hybrid",
|
| 4123 |
use_reranker: document.getElementById("useReranker").checked,
|
| 4124 |
use_llm: document.getElementById("useLLM").checked,
|
| 4125 |
use_graph: document.getElementById("useGraph").checked,
|
| 4126 |
-
graph_entity_limit:
|
| 4127 |
use_graph_retrieval: document.getElementById("useGraphRetrieval").checked,
|
| 4128 |
graph_retrieval_top_k: 6
|
| 4129 |
};
|
|
@@ -5055,7 +5055,7 @@ function getAnswerStyleInstruction() {
|
|
| 5055 |
return "Focus on comparison. Explain similarities, differences, and evidence clearly. Include citations after key claims.";
|
| 5056 |
}
|
| 5057 |
|
| 5058 |
-
return "Answer in a detailed
|
| 5059 |
}
|
| 5060 |
|
| 5061 |
function buildContextualQuery(currentQuestion, compareMode = false) {
|
|
@@ -5082,12 +5082,12 @@ function askPayload(query, documentId) {
|
|
| 5082 |
return {
|
| 5083 |
query: query,
|
| 5084 |
document_id: documentId,
|
| 5085 |
-
top_k:
|
| 5086 |
retrieval_mode: "hybrid",
|
| 5087 |
use_reranker: document.getElementById("useReranker").checked,
|
| 5088 |
use_llm: document.getElementById("useLLM").checked,
|
| 5089 |
use_graph: document.getElementById("useGraph").checked,
|
| 5090 |
-
graph_entity_limit:
|
| 5091 |
use_graph_retrieval: document.getElementById("useGraphRetrieval").checked,
|
| 5092 |
graph_retrieval_top_k: 6
|
| 5093 |
};
|
|
|
|
| 3948 |
return "Answer in a research-style format: direct answer, evidence, interpretation, limitations. Include citations after key claims.";
|
| 3949 |
}
|
| 3950 |
|
| 3951 |
+
return "Answer in a detailed, useful, and source-grounded format. Use this structure: Direct answer, Key points, Evidence from sources, and Limitations. Mention citations after important claims.";
|
| 3952 |
}
|
| 3953 |
|
| 3954 |
function buildContextualQuery(currentQuestion) {
|
|
|
|
| 4118 |
const payload = {
|
| 4119 |
query: buildContextualQuery(userText),
|
| 4120 |
document_id: doc.id,
|
| 4121 |
+
top_k: 8,
|
| 4122 |
retrieval_mode: "hybrid",
|
| 4123 |
use_reranker: document.getElementById("useReranker").checked,
|
| 4124 |
use_llm: document.getElementById("useLLM").checked,
|
| 4125 |
use_graph: document.getElementById("useGraph").checked,
|
| 4126 |
+
graph_entity_limit: 12,
|
| 4127 |
use_graph_retrieval: document.getElementById("useGraphRetrieval").checked,
|
| 4128 |
graph_retrieval_top_k: 6
|
| 4129 |
};
|
|
|
|
| 5055 |
return "Focus on comparison. Explain similarities, differences, and evidence clearly. Include citations after key claims.";
|
| 5056 |
}
|
| 5057 |
|
| 5058 |
+
return "Answer in a detailed, useful, and source-grounded format. Use this structure: Direct answer, Key points, Evidence from sources, and Limitations. Mention citations after important claims.";
|
| 5059 |
}
|
| 5060 |
|
| 5061 |
function buildContextualQuery(currentQuestion, compareMode = false) {
|
|
|
|
| 5082 |
return {
|
| 5083 |
query: query,
|
| 5084 |
document_id: documentId,
|
| 5085 |
+
top_k: 8,
|
| 5086 |
retrieval_mode: "hybrid",
|
| 5087 |
use_reranker: document.getElementById("useReranker").checked,
|
| 5088 |
use_llm: document.getElementById("useLLM").checked,
|
| 5089 |
use_graph: document.getElementById("useGraph").checked,
|
| 5090 |
+
graph_entity_limit: 12,
|
| 5091 |
use_graph_retrieval: document.getElementById("useGraphRetrieval").checked,
|
| 5092 |
graph_retrieval_top_k: 6
|
| 5093 |
};
|
app/generation/answer_quality_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|
| 13 |
+
return obj
|
| 14 |
+
|
| 15 |
+
if hasattr(obj, "model_dump"):
|
| 16 |
+
try:
|
| 17 |
+
return obj.model_dump()
|
| 18 |
+
except Exception:
|
| 19 |
+
pass
|
| 20 |
+
|
| 21 |
+
if hasattr(obj, "dict"):
|
| 22 |
+
try:
|
| 23 |
+
return obj.dict()
|
| 24 |
+
except Exception:
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
if hasattr(obj, "__dict__"):
|
| 28 |
+
try:
|
| 29 |
+
return dict(obj.__dict__)
|
| 30 |
+
except Exception:
|
| 31 |
+
pass
|
| 32 |
+
|
| 33 |
+
return {}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def value_from(data: Dict[str, Any], keys: List[str], default: str = "") -> str:
|
| 37 |
+
for key in keys:
|
| 38 |
+
value = data.get(key)
|
| 39 |
+
if value not in [None, ""]:
|
| 40 |
+
return str(value)
|
| 41 |
+
|
| 42 |
+
metadata = data.get("metadata")
|
| 43 |
+
|
| 44 |
+
if isinstance(metadata, dict):
|
| 45 |
+
for key in keys:
|
| 46 |
+
value = metadata.get(key)
|
| 47 |
+
if value not in [None, ""]:
|
| 48 |
+
return str(value)
|
| 49 |
+
|
| 50 |
+
return default
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def text_from_source(source: Dict[str, Any]) -> str:
|
| 54 |
+
return value_from(
|
| 55 |
+
source,
|
| 56 |
+
[
|
| 57 |
+
"text",
|
| 58 |
+
"content",
|
| 59 |
+
"chunk_text",
|
| 60 |
+
"page_text",
|
| 61 |
+
"cleaned_text",
|
| 62 |
+
"raw_text",
|
| 63 |
+
"text_preview",
|
| 64 |
+
"preview",
|
| 65 |
+
"chunk_preview",
|
| 66 |
+
"body"
|
| 67 |
+
],
|
| 68 |
+
""
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def normalize_sources(raw_sources: Any, raw_citations: Any = None) -> List[Dict[str, Any]]:
|
| 73 |
+
sources = []
|
| 74 |
+
|
| 75 |
+
if isinstance(raw_sources, list):
|
| 76 |
+
for item in raw_sources:
|
| 77 |
+
sources.append(to_dict(item))
|
| 78 |
+
|
| 79 |
+
if isinstance(raw_citations, list):
|
| 80 |
+
for item in raw_citations:
|
| 81 |
+
sources.append(to_dict(item))
|
| 82 |
+
|
| 83 |
+
cleaned = []
|
| 84 |
+
seen = set()
|
| 85 |
+
|
| 86 |
+
for index, source in enumerate(sources):
|
| 87 |
+
if not source:
|
| 88 |
+
continue
|
| 89 |
+
|
| 90 |
+
source_id = value_from(
|
| 91 |
+
source,
|
| 92 |
+
["source_id", "citation_id", "id"],
|
| 93 |
+
f"S{index + 1}"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
chunk_id = value_from(
|
| 97 |
+
source,
|
| 98 |
+
["chunk_id", "source_chunk_id", "chunk", "chunk_index", "id"],
|
| 99 |
+
source_id
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
text = text_from_source(source)
|
| 103 |
+
|
| 104 |
+
document_name = value_from(
|
| 105 |
+
source,
|
| 106 |
+
["document_name", "source_file_name", "file_name", "filename", "document_title"],
|
| 107 |
+
"Selected document"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
page = value_from(
|
| 111 |
+
source,
|
| 112 |
+
["page_number", "page", "page_no", "page_index"],
|
| 113 |
+
"Not available"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
key = f"{source_id}|{chunk_id}|{page}"
|
| 117 |
+
|
| 118 |
+
if key in seen:
|
| 119 |
+
continue
|
| 120 |
+
|
| 121 |
+
seen.add(key)
|
| 122 |
+
|
| 123 |
+
cleaned.append({
|
| 124 |
+
"source_id": source_id,
|
| 125 |
+
"chunk_id": chunk_id,
|
| 126 |
+
"document_name": document_name,
|
| 127 |
+
"page": page,
|
| 128 |
+
"text": text,
|
| 129 |
+
"raw": source
|
| 130 |
+
})
|
| 131 |
+
|
| 132 |
+
return cleaned[:6]
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def is_answer_too_short(answer: str) -> bool:
|
| 136 |
+
if not answer:
|
| 137 |
+
return True
|
| 138 |
+
|
| 139 |
+
word_count = len(answer.split())
|
| 140 |
+
|
| 141 |
+
if word_count < SHORT_ANSWER_WORD_LIMIT:
|
| 142 |
+
return True
|
| 143 |
+
|
| 144 |
+
weak_phrases = [
|
| 145 |
+
"i could not find",
|
| 146 |
+
"not enough information",
|
| 147 |
+
"maternity leave",
|
| 148 |
+
"rag is retrieval-augmented generation",
|
| 149 |
+
"the answer is"
|
| 150 |
+
]
|
| 151 |
+
|
| 152 |
+
lower = answer.lower().strip()
|
| 153 |
+
|
| 154 |
+
for phrase in weak_phrases:
|
| 155 |
+
if lower == phrase or lower.startswith(phrase) and word_count < 90:
|
| 156 |
+
return True
|
| 157 |
+
|
| 158 |
+
return False
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def source_label(index: int, source: Dict[str, Any]) -> str:
|
| 162 |
+
sid = source.get("source_id") or f"S{index + 1}"
|
| 163 |
+
|
| 164 |
+
if str(sid).upper().startswith("S"):
|
| 165 |
+
return str(sid)
|
| 166 |
+
|
| 167 |
+
return f"S{index + 1}"
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def make_key_points_from_sources(query: str, sources: List[Dict[str, Any]]) -> List[str]:
|
| 171 |
+
points = []
|
| 172 |
+
|
| 173 |
+
for index, source in enumerate(sources[:4]):
|
| 174 |
+
text = source.get("text", "").strip()
|
| 175 |
+
label = source_label(index, source)
|
| 176 |
+
|
| 177 |
+
if not text:
|
| 178 |
+
continue
|
| 179 |
+
|
| 180 |
+
cleaned = " ".join(text.split())
|
| 181 |
+
|
| 182 |
+
if len(cleaned) > 290:
|
| 183 |
+
cleaned = cleaned[:290].rsplit(" ", 1)[0] + "..."
|
| 184 |
+
|
| 185 |
+
points.append(f"- {cleaned} [{label}]")
|
| 186 |
+
|
| 187 |
+
return points
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def build_detailed_evidence_answer(
|
| 191 |
+
query: str,
|
| 192 |
+
original_answer: str,
|
| 193 |
+
sources: List[Dict[str, Any]]
|
| 194 |
+
) -> str:
|
| 195 |
+
if not sources:
|
| 196 |
+
return original_answer or "I could not find enough grounded evidence in the indexed document to answer this clearly."
|
| 197 |
+
|
| 198 |
+
direct_answer = (original_answer or "").strip()
|
| 199 |
+
|
| 200 |
+
if not direct_answer or is_answer_too_short(direct_answer):
|
| 201 |
+
direct_answer = (
|
| 202 |
+
"Based on the retrieved document evidence, the answer is connected to the points below. "
|
| 203 |
+
"The indexed sources provide supporting context, but the final interpretation should be verified from the cited source chunks."
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
key_points = make_key_points_from_sources(query=query, sources=sources)
|
| 207 |
+
|
| 208 |
+
evidence_lines = []
|
| 209 |
+
|
| 210 |
+
for index, source in enumerate(sources[:5]):
|
| 211 |
+
label = source_label(index, source)
|
| 212 |
+
document_name = source.get("document_name", "Selected document")
|
| 213 |
+
page = source.get("page", "Not available")
|
| 214 |
+
chunk_id = source.get("chunk_id", label)
|
| 215 |
+
|
| 216 |
+
evidence_lines.append(
|
| 217 |
+
f"- [{label}] Document: {document_name}; Page: {page}; Chunk: {chunk_id}"
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
answer_parts = []
|
| 221 |
+
|
| 222 |
+
answer_parts.append("Direct answer")
|
| 223 |
+
answer_parts.append(direct_answer)
|
| 224 |
+
|
| 225 |
+
if key_points:
|
| 226 |
+
answer_parts.append("\nKey evidence from the document")
|
| 227 |
+
answer_parts.extend(key_points)
|
| 228 |
+
|
| 229 |
+
answer_parts.append("\nSources used")
|
| 230 |
+
answer_parts.extend(evidence_lines)
|
| 231 |
+
|
| 232 |
+
answer_parts.append(
|
| 233 |
+
"\nNote\nThis answer is grounded in the retrieved chunks above. "
|
| 234 |
+
"If a page number is unavailable, it means the parser did not expose page metadata for that source."
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
return "\n".join(answer_parts)
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def safe_enhance_answer_for_response(local_vars: Dict[str, Any]) -> str:
|
| 241 |
+
"""
|
| 242 |
+
Designed to be called from answer_service response dict using locals().
|
| 243 |
+
It avoids crashing the /ask endpoint even if variable names differ.
|
| 244 |
+
"""
|
| 245 |
+
|
| 246 |
+
try:
|
| 247 |
+
answer = (
|
| 248 |
+
local_vars.get("answer")
|
| 249 |
+
or local_vars.get("final_answer")
|
| 250 |
+
or local_vars.get("generated_answer")
|
| 251 |
+
or local_vars.get("response_text")
|
| 252 |
+
or ""
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
+
query = local_vars.get("query") or ""
|
| 256 |
+
|
| 257 |
+
request_obj = local_vars.get("request")
|
| 258 |
+
|
| 259 |
+
if not query and request_obj is not None:
|
| 260 |
+
query = getattr(request_obj, "query", "")
|
| 261 |
+
|
| 262 |
+
sources = (
|
| 263 |
+
local_vars.get("sourced_results")
|
| 264 |
+
or local_vars.get("cleaned_results")
|
| 265 |
+
or local_vars.get("retrieved_results")
|
| 266 |
+
or local_vars.get("results")
|
| 267 |
+
or []
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
citations = local_vars.get("citations") or []
|
| 271 |
+
|
| 272 |
+
normalized_sources = normalize_sources(sources, citations)
|
| 273 |
+
|
| 274 |
+
if is_answer_too_short(answer):
|
| 275 |
+
return build_detailed_evidence_answer(
|
| 276 |
+
query=str(query),
|
| 277 |
+
original_answer=str(answer),
|
| 278 |
+
sources=normalized_sources
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
+
# If answer is okay but has no citation marker, add source summary.
|
| 282 |
+
if normalized_sources and "[S" not in str(answer):
|
| 283 |
+
source_refs = []
|
| 284 |
+
|
| 285 |
+
for index, source in enumerate(normalized_sources[:3]):
|
| 286 |
+
label = source_label(index, source)
|
| 287 |
+
page = source.get("page", "Not available")
|
| 288 |
+
source_refs.append(f"[{label}: page {page}]")
|
| 289 |
+
|
| 290 |
+
return str(answer).strip() + "\n\nSources: " + ", ".join(source_refs)
|
| 291 |
+
|
| 292 |
+
return str(answer)
|
| 293 |
+
|
| 294 |
+
except Exception:
|
| 295 |
+
return str(
|
| 296 |
+
local_vars.get("answer")
|
| 297 |
+
or local_vars.get("final_answer")
|
| 298 |
+
or local_vars.get("generated_answer")
|
| 299 |
+
or local_vars.get("response_text")
|
| 300 |
+
or ""
|
| 301 |
+
)
|
app/generation/answer_service.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from app.graph.graph_retrieval_fusion import fuse_retrieval_results_with_graph
|
| 2 |
from app.graph.graph_context_service import build_graph_context_for_query
|
| 3 |
import re
|
|
@@ -119,7 +120,7 @@ def answer_question(
|
|
| 119 |
|
| 120 |
return {
|
| 121 |
"query": query,
|
| 122 |
-
"answer":
|
| 123 |
"retrieval_mode": retrieval_mode,
|
| 124 |
"question_type": question_type,
|
| 125 |
"used_reranker": use_reranker,
|
|
@@ -197,7 +198,7 @@ def answer_question(
|
|
| 197 |
|
| 198 |
return {
|
| 199 |
"query": query,
|
| 200 |
-
"answer":
|
| 201 |
"retrieval_mode": retrieval_mode,
|
| 202 |
"question_type": question_type,
|
| 203 |
"used_reranker": use_reranker,
|
|
|
|
| 1 |
+
from app.generation.answer_quality_enhancer import safe_enhance_answer_for_response
|
| 2 |
from app.graph.graph_retrieval_fusion import fuse_retrieval_results_with_graph
|
| 3 |
from app.graph.graph_context_service import build_graph_context_for_query
|
| 4 |
import re
|
|
|
|
| 120 |
|
| 121 |
return {
|
| 122 |
"query": query,
|
| 123 |
+
"answer": safe_enhance_answer_for_response(locals()),
|
| 124 |
"retrieval_mode": retrieval_mode,
|
| 125 |
"question_type": question_type,
|
| 126 |
"used_reranker": use_reranker,
|
|
|
|
| 198 |
|
| 199 |
return {
|
| 200 |
"query": query,
|
| 201 |
+
"answer": safe_enhance_answer_for_response(locals()),
|
| 202 |
"retrieval_mode": retrieval_mode,
|
| 203 |
"question_type": question_type,
|
| 204 |
"used_reran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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
# Clean BOM
|
| 4 |
+
for path in Path("app").rglob("*.py"):
|
| 5 |
+
text = path.read_text(encoding="utf-8-sig")
|
| 6 |
+
text = text.replace("\ufeff", "")
|
| 7 |
+
path.write_text(text, encoding="utf-8")
|
| 8 |
+
|
| 9 |
+
print("BOM cleanup completed.")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# =====================================================
|
| 13 |
+
# 1. Add answer quality enhancer
|
| 14 |
+
# =====================================================
|
| 15 |
+
|
| 16 |
+
Path("app/generation/answer_quality_enhancer.py").write_text(r'''
|
| 17 |
+
from typing import Any, Dict, List
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
SHORT_ANSWER_WORD_LIMIT = 70
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def to_dict(obj: Any) -> Dict[str, Any]:
|
| 24 |
+
if obj is None:
|
| 25 |
+
return {}
|
| 26 |
+
|
| 27 |
+
if isinstance(obj, dict):
|
| 28 |
+
return obj
|
| 29 |
+
|
| 30 |
+
if hasattr(obj, "model_dump"):
|
| 31 |
+
try:
|
| 32 |
+
return obj.model_dump()
|
| 33 |
+
except Exception:
|
| 34 |
+
pass
|
| 35 |
+
|
| 36 |
+
if hasattr(obj, "dict"):
|
| 37 |
+
try:
|
| 38 |
+
return obj.dict()
|
| 39 |
+
except Exception:
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
if hasattr(obj, "__dict__"):
|
| 43 |
+
try:
|
| 44 |
+
return dict(obj.__dict__)
|
| 45 |
+
except Exception:
|
| 46 |
+
pass
|
| 47 |
+
|
| 48 |
+
return {}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def value_from(data: Dict[str, Any], keys: List[str], default: str = "") -> str:
|
| 52 |
+
for key in keys:
|
| 53 |
+
value = data.get(key)
|
| 54 |
+
if value not in [None, ""]:
|
| 55 |
+
return str(value)
|
| 56 |
+
|
| 57 |
+
metadata = data.get("metadata")
|
| 58 |
+
|
| 59 |
+
if isinstance(metadata, dict):
|
| 60 |
+
for key in keys:
|
| 61 |
+
value = metadata.get(key)
|
| 62 |
+
if value not in [None, ""]:
|
| 63 |
+
return str(value)
|
| 64 |
+
|
| 65 |
+
return default
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def text_from_source(source: Dict[str, Any]) -> str:
|
| 69 |
+
return value_from(
|
| 70 |
+
source,
|
| 71 |
+
[
|
| 72 |
+
"text",
|
| 73 |
+
"content",
|
| 74 |
+
"chunk_text",
|
| 75 |
+
"page_text",
|
| 76 |
+
"cleaned_text",
|
| 77 |
+
"raw_text",
|
| 78 |
+
"text_preview",
|
| 79 |
+
"preview",
|
| 80 |
+
"chunk_preview",
|
| 81 |
+
"body"
|
| 82 |
+
],
|
| 83 |
+
""
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def normalize_sources(raw_sources: Any, raw_citations: Any = None) -> List[Dict[str, Any]]:
|
| 88 |
+
sources = []
|
| 89 |
+
|
| 90 |
+
if isinstance(raw_sources, list):
|
| 91 |
+
for item in raw_sources:
|
| 92 |
+
sources.append(to_dict(item))
|
| 93 |
+
|
| 94 |
+
if isinstance(raw_citations, list):
|
| 95 |
+
for item in raw_citations:
|
| 96 |
+
sources.append(to_dict(item))
|
| 97 |
+
|
| 98 |
+
cleaned = []
|
| 99 |
+
seen = set()
|
| 100 |
+
|
| 101 |
+
for index, source in enumerate(sources):
|
| 102 |
+
if not source:
|
| 103 |
+
continue
|
| 104 |
+
|
| 105 |
+
source_id = value_from(
|
| 106 |
+
source,
|
| 107 |
+
["source_id", "citation_id", "id"],
|
| 108 |
+
f"S{index + 1}"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
chunk_id = value_from(
|
| 112 |
+
source,
|
| 113 |
+
["chunk_id", "source_chunk_id", "chunk", "chunk_index", "id"],
|
| 114 |
+
source_id
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
text = text_from_source(source)
|
| 118 |
+
|
| 119 |
+
document_name = value_from(
|
| 120 |
+
source,
|
| 121 |
+
["document_name", "source_file_name", "file_name", "filename", "document_title"],
|
| 122 |
+
"Selected document"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
page = value_from(
|
| 126 |
+
source,
|
| 127 |
+
["page_number", "page", "page_no", "page_index"],
|
| 128 |
+
"Not available"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
key = f"{source_id}|{chunk_id}|{page}"
|
| 132 |
+
|
| 133 |
+
if key in seen:
|
| 134 |
+
continue
|
| 135 |
+
|
| 136 |
+
seen.add(key)
|
| 137 |
+
|
| 138 |
+
cleaned.append({
|
| 139 |
+
"source_id": source_id,
|
| 140 |
+
"chunk_id": chunk_id,
|
| 141 |
+
"document_name": document_name,
|
| 142 |
+
"page": page,
|
| 143 |
+
"text": text,
|
| 144 |
+
"raw": source
|
| 145 |
+
})
|
| 146 |
+
|
| 147 |
+
return cleaned[:6]
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def is_answer_too_short(answer: str) -> bool:
|
| 151 |
+
if not answer:
|
| 152 |
+
return True
|
| 153 |
+
|
| 154 |
+
word_count = len(answer.split())
|
| 155 |
+
|
| 156 |
+
if word_count < SHORT_ANSWER_WORD_LIMIT:
|
| 157 |
+
return True
|
| 158 |
+
|
| 159 |
+
weak_phrases = [
|
| 160 |
+
"i could not find",
|
| 161 |
+
"not enough information",
|
| 162 |
+
"maternity leave",
|
| 163 |
+
"rag is retrieval-augmented generation",
|
| 164 |
+
"the answer is"
|
| 165 |
+
]
|
| 166 |
+
|
| 167 |
+
lower = answer.lower().strip()
|
| 168 |
+
|
| 169 |
+
for phrase in weak_phrases:
|
| 170 |
+
if lower == phrase or lower.startswith(phrase) and word_count < 90:
|
| 171 |
+
return True
|
| 172 |
+
|
| 173 |
+
return False
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def source_label(index: int, source: Dict[str, Any]) -> str:
|
| 177 |
+
sid = source.get("source_id") or f"S{index + 1}"
|
| 178 |
+
|
| 179 |
+
if str(sid).upper().startswith("S"):
|
| 180 |
+
return str(sid)
|
| 181 |
+
|
| 182 |
+
return f"S{index + 1}"
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def make_key_points_from_sources(query: str, sources: List[Dict[str, Any]]) -> List[str]:
|
| 186 |
+
points = []
|
| 187 |
+
|
| 188 |
+
for index, source in enumerate(sources[:4]):
|
| 189 |
+
text = source.get("text", "").strip()
|
| 190 |
+
label = source_label(index, source)
|
| 191 |
+
|
| 192 |
+
if not text:
|
| 193 |
+
continue
|
| 194 |
+
|
| 195 |
+
cleaned = " ".join(text.split())
|
| 196 |
+
|
| 197 |
+
if len(cleaned) > 290:
|
| 198 |
+
cleaned = cleaned[:290].rsplit(" ", 1)[0] + "..."
|
| 199 |
+
|
| 200 |
+
points.append(f"- {cleaned} [{label}]")
|
| 201 |
+
|
| 202 |
+
return points
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def build_detailed_evidence_answer(
|
| 206 |
+
query: str,
|
| 207 |
+
original_answer: str,
|
| 208 |
+
sources: List[Dict[str, Any]]
|
| 209 |
+
) -> str:
|
| 210 |
+
if not sources:
|
| 211 |
+
return original_answer or "I could not find enough grounded evidence in the indexed document to answer this clearly."
|
| 212 |
+
|
| 213 |
+
direct_answer = (original_answer or "").strip()
|
| 214 |
+
|
| 215 |
+
if not direct_answer or is_answer_too_short(direct_answer):
|
| 216 |
+
direct_answer = (
|
| 217 |
+
"Based on the retrieved document evidence, the answer is connected to the points below. "
|
| 218 |
+
"The indexed sources provide supporting context, but the final interpretation should be verified from the cited source chunks."
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
key_points = make_key_points_from_sources(query=query, sources=sources)
|
| 222 |
+
|
| 223 |
+
evidence_lines = []
|
| 224 |
+
|
| 225 |
+
for index, source in enumerate(sources[:5]):
|
| 226 |
+
label = source_label(index, source)
|
| 227 |
+
document_name = source.get("document_name", "Selected document")
|
| 228 |
+
page = source.get("page", "Not available")
|
| 229 |
+
chunk_id = source.get("chunk_id", label)
|
| 230 |
+
|
| 231 |
+
evidence_lines.append(
|
| 232 |
+
f"- [{label}] Document: {document_name}; Page: {page}; Chunk: {chunk_id}"
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
answer_parts = []
|
| 236 |
+
|
| 237 |
+
answer_parts.append("Direct answer")
|
| 238 |
+
answer_parts.append(direct_answer)
|
| 239 |
+
|
| 240 |
+
if key_points:
|
| 241 |
+
answer_parts.append("\nKey evidence from the document")
|
| 242 |
+
answer_parts.extend(key_points)
|
| 243 |
+
|
| 244 |
+
answer_parts.append("\nSources used")
|
| 245 |
+
answer_parts.extend(evidence_lines)
|
| 246 |
+
|
| 247 |
+
answer_parts.append(
|
| 248 |
+
"\nNote\nThis answer is grounded in the retrieved chunks above. "
|
| 249 |
+
"If a page number is unavailable, it means the parser did not expose page metadata for that source."
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
return "\n".join(answer_parts)
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def safe_enhance_answer_for_response(local_vars: Dict[str, Any]) -> str:
|
| 256 |
+
"""
|
| 257 |
+
Designed to be called from answer_service response dict using locals().
|
| 258 |
+
It avoids crashing the /ask endpoint even if variable names differ.
|
| 259 |
+
"""
|
| 260 |
+
|
| 261 |
+
try:
|
| 262 |
+
answer = (
|
| 263 |
+
local_vars.get("answer")
|
| 264 |
+
or local_vars.get("final_answer")
|
| 265 |
+
or local_vars.get("generated_answer")
|
| 266 |
+
or local_vars.get("response_text")
|
| 267 |
+
or ""
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
query = local_vars.get("query") or ""
|
| 271 |
+
|
| 272 |
+
request_obj = local_vars.get("request")
|
| 273 |
+
|
| 274 |
+
if not query and request_obj is not None:
|
| 275 |
+
query = getattr(request_obj, "query", "")
|
| 276 |
+
|
| 277 |
+
sources = (
|
| 278 |
+
local_vars.get("sourced_results")
|
| 279 |
+
or local_vars.get("cleaned_results")
|
| 280 |
+
or local_vars.get("retrieved_results")
|
| 281 |
+
or local_vars.get("results")
|
| 282 |
+
or []
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
citations = local_vars.get("citations") or []
|
| 286 |
+
|
| 287 |
+
normalized_sources = normalize_sources(sources, citations)
|
| 288 |
+
|
| 289 |
+
if is_answer_too_short(answer):
|
| 290 |
+
return build_detailed_evidence_answer(
|
| 291 |
+
query=str(query),
|
| 292 |
+
original_answer=str(answer),
|
| 293 |
+
sources=normalized_sources
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
# If answer is okay but has no citation marker, add source summary.
|
| 297 |
+
if normalized_sources and "[S" not in str(answer):
|
| 298 |
+
source_refs = []
|
| 299 |
+
|
| 300 |
+
for index, source in enumerate(normalized_sources[:3]):
|
| 301 |
+
label = source_label(index, source)
|
| 302 |
+
page = source.get("page", "Not available")
|
| 303 |
+
source_refs.append(f"[{label}: page {page}]")
|
| 304 |
+
|
| 305 |
+
return str(answer).strip() + "\n\nSources: " + ", ".join(source_refs)
|
| 306 |
+
|
| 307 |
+
return str(answer)
|
| 308 |
+
|
| 309 |
+
except Exception:
|
| 310 |
+
return str(
|
| 311 |
+
local_vars.get("answer")
|
| 312 |
+
or local_vars.get("final_answer")
|
| 313 |
+
or local_vars.get("generated_answer")
|
| 314 |
+
or local_vars.get("response_text")
|
| 315 |
+
or ""
|
| 316 |
+
)
|
| 317 |
+
''', encoding="utf-8")
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
# =====================================================
|
| 321 |
+
# 2. Patch answer_service.py safely
|
| 322 |
+
# =====================================================
|
| 323 |
+
|
| 324 |
+
answer_path = Path("app/generation/answer_service.py")
|
| 325 |
+
|
| 326 |
+
if not answer_path.exists():
|
| 327 |
+
print("WARNING: answer_service.py not found. Created enhancer only.")
|
| 328 |
+
else:
|
| 329 |
+
text = answer_path.read_text(encoding="utf-8-sig")
|
| 330 |
+
text = text.replace("\ufeff", "")
|
| 331 |
+
|
| 332 |
+
if "from app.generation.answer_quality_enhancer import safe_enhance_answer_for_response" not in text:
|
| 333 |
+
text = (
|
| 334 |
+
"from app.generation.answer_quality_enhancer import safe_enhance_answer_for_response\n"
|
| 335 |
+
+ text
|
| 336 |
+
)
|
| 337 |
+
print("Added answer enhancer import.")
|
| 338 |
+
|
| 339 |
+
replacements = {
|
| 340 |
+
'"answer": answer,': '"answer": safe_enhance_answer_for_response(locals()),',
|
| 341 |
+
"'answer': answer,": "'answer': safe_enhance_answer_for_response(locals()),",
|
| 342 |
+
'"answer": final_answer,': '"answer": safe_enhance_answer_for_response(locals()),',
|
| 343 |
+
"'answer': final_answer,": "'answer': safe_enhance_answer_for_response(locals()),",
|
| 344 |
+
'"answer": generated_answer,': '"answer": safe_enhance_answer_for_response(locals()),',
|
| 345 |
+
"'answer': generated_answer,": "'answer': safe_enhance_answer_for_response(locals()),",
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
changed = False
|
| 349 |
+
|
| 350 |
+
for old, new in replacements.items():
|
| 351 |
+
if old in text:
|
| 352 |
+
text = text.replace(old, new)
|
| 353 |
+
changed = True
|
| 354 |
+
print(f"Replaced {old}")
|
| 355 |
+
|
| 356 |
+
if not changed:
|
| 357 |
+
print("WARNING: Could not find answer return pattern. Enhancer file created but answer_service not wired automatically.")
|
| 358 |
+
|
| 359 |
+
answer_path.write_text(text, encoding="utf-8")
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
# =====================================================
|
| 363 |
+
# 3. Make UI default style more detailed
|
| 364 |
+
# =====================================================
|
| 365 |
+
|
| 366 |
+
hf_path = Path("app/deployment/hf_status.py")
|
| 367 |
+
|
| 368 |
+
if hf_path.exists():
|
| 369 |
+
ui = hf_path.read_text(encoding="utf-8-sig")
|
| 370 |
+
ui = ui.replace("\ufeff", "")
|
| 371 |
+
|
| 372 |
+
ui = ui.replace(
|
| 373 |
+
"Answer in a detailed but readable format. Start with a direct answer, then explain important points with evidence. Include citations after key claims.",
|
| 374 |
+
"Answer in a detailed, useful, and source-grounded format. Use this structure: Direct answer, Key points, Evidence from sources, and Limitations. Mention citations after important claims."
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
ui = ui.replace(
|
| 378 |
+
'top_k: 7,',
|
| 379 |
+
'top_k: 8,'
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
ui = ui.replace(
|
| 383 |
+
'graph_entity_limit: 10,',
|
| 384 |
+
'graph_entity_limit: 12,'
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
hf_path.write_text(ui, encoding="utf-8")
|
| 388 |
+
print("Updated UI answer instruction defaults.")
|
| 389 |
+
|
| 390 |
+
print("Phase 30 better answer quality backend patch complete.")
|