Spaces:
Running
Running
Fix: targeted retrieval for safety context + sharper extraction prompt
Browse files
app.py
CHANGED
|
@@ -79,18 +79,34 @@ def _read_file(path: str) -> str:
|
|
| 79 |
|
| 80 |
|
| 81 |
def _build_full_context() -> str:
|
| 82 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 83 |
if not _retriever.chunks:
|
| 84 |
return ""
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
for c in _retriever.chunks:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
return "\n\n".join(parts)
|
| 94 |
|
| 95 |
|
| 96 |
# ── HF Hub trace sharing ───────────────────────────────────────────────────────
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
def _build_full_context() -> str:
|
| 82 |
+
"""
|
| 83 |
+
Build safety-check context via targeted retrieval for medications and allergies.
|
| 84 |
+
This guarantees we get the relevant chunks even if they appear late in documents.
|
| 85 |
+
"""
|
| 86 |
if not _retriever.chunks:
|
| 87 |
return ""
|
| 88 |
+
|
| 89 |
+
# Retrieve chunks specifically about meds and allergies
|
| 90 |
+
med_chunks = _retriever.retrieve("medications drugs prescribed dose frequency", top_k=6)
|
| 91 |
+
allergy_chunks = _retriever.retrieve("allergies allergic reactions contraindications", top_k=6)
|
| 92 |
+
|
| 93 |
+
seen: set = set()
|
| 94 |
+
ordered = []
|
| 95 |
+
for c in med_chunks + allergy_chunks:
|
| 96 |
+
uid = (c.source, c.index)
|
| 97 |
+
if uid not in seen:
|
| 98 |
+
seen.add(uid)
|
| 99 |
+
ordered.append(c)
|
| 100 |
+
|
| 101 |
+
# Also add any remaining chunks not yet included
|
| 102 |
for c in _retriever.chunks:
|
| 103 |
+
uid = (c.source, c.index)
|
| 104 |
+
if uid not in seen:
|
| 105 |
+
seen.add(uid)
|
| 106 |
+
ordered.append(c)
|
| 107 |
+
|
| 108 |
+
parts = [f"[{c.source}]\n{c.text}" for c in ordered]
|
| 109 |
+
return "\n\n---\n\n".join(parts)
|
| 110 |
|
| 111 |
|
| 112 |
# ── HF Hub trace sharing ───────────────────────────────────────────────────────
|