File size: 11,126 Bytes
98a8ef8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from pathlib import Path

# Clean BOM
for path in Path("app").rglob("*.py"):
    text = path.read_text(encoding="utf-8-sig")
    text = text.replace("\ufeff", "")
    path.write_text(text, encoding="utf-8")

print("BOM cleanup completed.")


# =====================================================
# 1. Add answer quality enhancer
# =====================================================

Path("app/generation/answer_quality_enhancer.py").write_text(r'''
from typing import Any, Dict, List


SHORT_ANSWER_WORD_LIMIT = 70


def to_dict(obj: Any) -> Dict[str, Any]:
    if obj is None:
        return {}

    if isinstance(obj, dict):
        return obj

    if hasattr(obj, "model_dump"):
        try:
            return obj.model_dump()
        except Exception:
            pass

    if hasattr(obj, "dict"):
        try:
            return obj.dict()
        except Exception:
            pass

    if hasattr(obj, "__dict__"):
        try:
            return dict(obj.__dict__)
        except Exception:
            pass

    return {}


def value_from(data: Dict[str, Any], keys: List[str], default: str = "") -> str:
    for key in keys:
        value = data.get(key)
        if value not in [None, ""]:
            return str(value)

    metadata = data.get("metadata")

    if isinstance(metadata, dict):
        for key in keys:
            value = metadata.get(key)
            if value not in [None, ""]:
                return str(value)

    return default


def text_from_source(source: Dict[str, Any]) -> str:
    return value_from(
        source,
        [
            "text",
            "content",
            "chunk_text",
            "page_text",
            "cleaned_text",
            "raw_text",
            "text_preview",
            "preview",
            "chunk_preview",
            "body"
        ],
        ""
    )


def normalize_sources(raw_sources: Any, raw_citations: Any = None) -> List[Dict[str, Any]]:
    sources = []

    if isinstance(raw_sources, list):
        for item in raw_sources:
            sources.append(to_dict(item))

    if isinstance(raw_citations, list):
        for item in raw_citations:
            sources.append(to_dict(item))

    cleaned = []
    seen = set()

    for index, source in enumerate(sources):
        if not source:
            continue

        source_id = value_from(
            source,
            ["source_id", "citation_id", "id"],
            f"S{index + 1}"
        )

        chunk_id = value_from(
            source,
            ["chunk_id", "source_chunk_id", "chunk", "chunk_index", "id"],
            source_id
        )

        text = text_from_source(source)

        document_name = value_from(
            source,
            ["document_name", "source_file_name", "file_name", "filename", "document_title"],
            "Selected document"
        )

        page = value_from(
            source,
            ["page_number", "page", "page_no", "page_index"],
            "Not available"
        )

        key = f"{source_id}|{chunk_id}|{page}"

        if key in seen:
            continue

        seen.add(key)

        cleaned.append({
            "source_id": source_id,
            "chunk_id": chunk_id,
            "document_name": document_name,
            "page": page,
            "text": text,
            "raw": source
        })

    return cleaned[:6]


def is_answer_too_short(answer: str) -> bool:
    if not answer:
        return True

    word_count = len(answer.split())

    if word_count < SHORT_ANSWER_WORD_LIMIT:
        return True

    weak_phrases = [
        "i could not find",
        "not enough information",
        "maternity leave",
        "rag is retrieval-augmented generation",
        "the answer is"
    ]

    lower = answer.lower().strip()

    for phrase in weak_phrases:
        if lower == phrase or lower.startswith(phrase) and word_count < 90:
            return True

    return False


def source_label(index: int, source: Dict[str, Any]) -> str:
    sid = source.get("source_id") or f"S{index + 1}"

    if str(sid).upper().startswith("S"):
        return str(sid)

    return f"S{index + 1}"


def make_key_points_from_sources(query: str, sources: List[Dict[str, Any]]) -> List[str]:
    points = []

    for index, source in enumerate(sources[:4]):
        text = source.get("text", "").strip()
        label = source_label(index, source)

        if not text:
            continue

        cleaned = " ".join(text.split())

        if len(cleaned) > 290:
            cleaned = cleaned[:290].rsplit(" ", 1)[0] + "..."

        points.append(f"- {cleaned} [{label}]")

    return points


def build_detailed_evidence_answer(
    query: str,
    original_answer: str,
    sources: List[Dict[str, Any]]
) -> str:
    if not sources:
        return original_answer or "I could not find enough grounded evidence in the indexed document to answer this clearly."

    direct_answer = (original_answer or "").strip()

    if not direct_answer or is_answer_too_short(direct_answer):
        direct_answer = (
            "Based on the retrieved document evidence, the answer is connected to the points below. "
            "The indexed sources provide supporting context, but the final interpretation should be verified from the cited source chunks."
        )

    key_points = make_key_points_from_sources(query=query, sources=sources)

    evidence_lines = []

    for index, source in enumerate(sources[:5]):
        label = source_label(index, source)
        document_name = source.get("document_name", "Selected document")
        page = source.get("page", "Not available")
        chunk_id = source.get("chunk_id", label)

        evidence_lines.append(
            f"- [{label}] Document: {document_name}; Page: {page}; Chunk: {chunk_id}"
        )

    answer_parts = []

    answer_parts.append("Direct answer")
    answer_parts.append(direct_answer)

    if key_points:
        answer_parts.append("\nKey evidence from the document")
        answer_parts.extend(key_points)

    answer_parts.append("\nSources used")
    answer_parts.extend(evidence_lines)

    answer_parts.append(
        "\nNote\nThis answer is grounded in the retrieved chunks above. "
        "If a page number is unavailable, it means the parser did not expose page metadata for that source."
    )

    return "\n".join(answer_parts)


def safe_enhance_answer_for_response(local_vars: Dict[str, Any]) -> str:
    """
    Designed to be called from answer_service response dict using locals().
    It avoids crashing the /ask endpoint even if variable names differ.
    """

    try:
        answer = (
            local_vars.get("answer")
            or local_vars.get("final_answer")
            or local_vars.get("generated_answer")
            or local_vars.get("response_text")
            or ""
        )

        query = local_vars.get("query") or ""

        request_obj = local_vars.get("request")

        if not query and request_obj is not None:
            query = getattr(request_obj, "query", "")

        sources = (
            local_vars.get("sourced_results")
            or local_vars.get("cleaned_results")
            or local_vars.get("retrieved_results")
            or local_vars.get("results")
            or []
        )

        citations = local_vars.get("citations") or []

        normalized_sources = normalize_sources(sources, citations)

        if is_answer_too_short(answer):
            return build_detailed_evidence_answer(
                query=str(query),
                original_answer=str(answer),
                sources=normalized_sources
            )

        # If answer is okay but has no citation marker, add source summary.
        if normalized_sources and "[S" not in str(answer):
            source_refs = []

            for index, source in enumerate(normalized_sources[:3]):
                label = source_label(index, source)
                page = source.get("page", "Not available")
                source_refs.append(f"[{label}: page {page}]")

            return str(answer).strip() + "\n\nSources: " + ", ".join(source_refs)

        return str(answer)

    except Exception:
        return str(
            local_vars.get("answer")
            or local_vars.get("final_answer")
            or local_vars.get("generated_answer")
            or local_vars.get("response_text")
            or ""
        )
''', encoding="utf-8")


# =====================================================
# 2. Patch answer_service.py safely
# =====================================================

answer_path = Path("app/generation/answer_service.py")

if not answer_path.exists():
    print("WARNING: answer_service.py not found. Created enhancer only.")
else:
    text = answer_path.read_text(encoding="utf-8-sig")
    text = text.replace("\ufeff", "")

    if "from app.generation.answer_quality_enhancer import safe_enhance_answer_for_response" not in text:
        text = (
            "from app.generation.answer_quality_enhancer import safe_enhance_answer_for_response\n"
            + text
        )
        print("Added answer enhancer import.")

    replacements = {
        '"answer": answer,': '"answer": safe_enhance_answer_for_response(locals()),',
        "'answer': answer,": "'answer': safe_enhance_answer_for_response(locals()),",
        '"answer": final_answer,': '"answer": safe_enhance_answer_for_response(locals()),',
        "'answer': final_answer,": "'answer': safe_enhance_answer_for_response(locals()),",
        '"answer": generated_answer,': '"answer": safe_enhance_answer_for_response(locals()),',
        "'answer': generated_answer,": "'answer': safe_enhance_answer_for_response(locals()),",
    }

    changed = False

    for old, new in replacements.items():
        if old in text:
            text = text.replace(old, new)
            changed = True
            print(f"Replaced {old}")

    if not changed:
        print("WARNING: Could not find answer return pattern. Enhancer file created but answer_service not wired automatically.")

    answer_path.write_text(text, encoding="utf-8")


# =====================================================
# 3. Make UI default style more detailed
# =====================================================

hf_path = Path("app/deployment/hf_status.py")

if hf_path.exists():
    ui = hf_path.read_text(encoding="utf-8-sig")
    ui = ui.replace("\ufeff", "")

    ui = ui.replace(
        "Answer in a detailed but readable format. Start with a direct answer, then explain important points with evidence. Include citations after key claims.",
        "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."
    )

    ui = ui.replace(
        'top_k: 7,',
        'top_k: 8,'
    )

    ui = ui.replace(
        'graph_entity_limit: 10,',
        'graph_entity_limit: 12,'
    )

    hf_path.write_text(ui, encoding="utf-8")
    print("Updated UI answer instruction defaults.")

print("Phase 30 better answer quality backend patch complete.")