Update src/model.py
Browse files- src/model.py +27 -18
src/model.py
CHANGED
|
@@ -119,7 +119,7 @@ def extract_and_link_sources(text, evidence_snippets):
|
|
| 119 |
|
| 120 |
# Process DOI citations
|
| 121 |
for doi in doi_matches:
|
| 122 |
-
|
| 123 |
# Check if this is a direct DOI match
|
| 124 |
if 'doi' in snippet and snippet['doi'] == doi:
|
| 125 |
source_map[f"DOI:{doi}"] = {
|
|
@@ -133,13 +133,13 @@ def extract_and_link_sources(text, evidence_snippets):
|
|
| 133 |
# Also check the ID field which might contain DOI
|
| 134 |
elif snippet.get("id") == f"DOI:{doi}":
|
| 135 |
source_map[f"DOI:{doi}"] = {
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
"citation": snippet["citation"],
|
| 140 |
"doi": doi
|
| 141 |
-
|
| 142 |
-
|
| 143 |
|
| 144 |
# Process other citation formats for backward compatibility
|
| 145 |
for source_id_match in source_matches:
|
|
@@ -147,15 +147,15 @@ def extract_and_link_sources(text, evidence_snippets):
|
|
| 147 |
for snippet in evidence_snippets:
|
| 148 |
if source_id_match == snippet["id"]:
|
| 149 |
source_map[source_id_match] = {
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
"citation": snippet["citation"],
|
| 154 |
"pmid": snippet.get("pmid", ""),
|
| 155 |
"doi": snippet.get("doi", "")
|
| 156 |
-
|
| 157 |
break
|
| 158 |
-
|
| 159 |
# Replace PMID citations with links
|
| 160 |
linked_text = text
|
| 161 |
for pmid_key in [f"PMID:{pmid}" for pmid in pmid_matches]:
|
|
@@ -186,10 +186,10 @@ def extract_and_link_sources(text, evidence_snippets):
|
|
| 186 |
# Replace other citation formats
|
| 187 |
for source_id_key, source_data in source_map.items():
|
| 188 |
if not (source_id_key.startswith("PMID:") or source_id_key.startswith("DOI:")):
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
|
| 194 |
# Handle generic [source_id] placeholder
|
| 195 |
if "source_id" in source_matches:
|
|
@@ -206,7 +206,7 @@ def extract_and_link_sources(text, evidence_snippets):
|
|
| 206 |
"doi": snippet.get("doi", "")
|
| 207 |
}
|
| 208 |
replacement = f"[{snippet['title']}]({snippet['url']})"
|
| 209 |
-
|
| 210 |
|
| 211 |
# Final fallback for any remaining placeholders
|
| 212 |
linked_text = re.sub(r'\[source_id\]', "[Medical Reference]", linked_text)
|
|
@@ -1283,6 +1283,9 @@ def orchestrator_chat(history, query, use_rag, is_follow_up=False):
|
|
| 1283 |
system = {"role": "system", "content": FOLLOW_UP_PROMPT}
|
| 1284 |
else:
|
| 1285 |
system = {"role": "system", "content": SYSTEM_PROMPT}
|
|
|
|
|
|
|
|
|
|
| 1286 |
|
| 1287 |
msgs = [system] + history
|
| 1288 |
|
|
@@ -1363,7 +1366,7 @@ def orchestrator_chat(history, query, use_rag, is_follow_up=False):
|
|
| 1363 |
• [DOI:10.xxxx/yyyy] format for Europe PMC articles without PMID
|
| 1364 |
|
| 1365 |
Use no more than 3 sources and no fewer than 2 sources.
|
| 1366 |
-
|
| 1367 |
**After your main response, ALWAYS include these sections:**
|
| 1368 |
- **Follow-up Questions**: Specific questions to gather additional information.
|
| 1369 |
- **Reasoning**: Bullet points detailing your clinical reasoning.
|
|
@@ -1432,11 +1435,14 @@ def orchestrator_chat(history, query, use_rag, is_follow_up=False):
|
|
| 1432 |
follow_up_questions = "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions) if q])
|
| 1433 |
else:
|
| 1434 |
follow_up_questions = questions
|
|
|
|
|
|
|
|
|
|
| 1435 |
else:
|
| 1436 |
# If RAG is disabled, just parse the response without source processing
|
| 1437 |
parsed_response = parse_doctor_response(response)
|
| 1438 |
main_response = response
|
| 1439 |
-
|
| 1440 |
# Extract reasoning
|
| 1441 |
reasoning = parsed_response.get("reasoning", [])
|
| 1442 |
if reasoning:
|
|
@@ -1453,6 +1459,9 @@ def orchestrator_chat(history, query, use_rag, is_follow_up=False):
|
|
| 1453 |
follow_up_questions = "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions) if q])
|
| 1454 |
else:
|
| 1455 |
follow_up_questions = questions
|
|
|
|
|
|
|
|
|
|
| 1456 |
|
| 1457 |
# Return four values: main response, explanation, follow-up questions, and evidence
|
| 1458 |
return main_response, explanation, follow_up_questions, evidence_snippets
|
|
|
|
| 119 |
|
| 120 |
# Process DOI citations
|
| 121 |
for doi in doi_matches:
|
| 122 |
+
for snippet in evidence_snippets:
|
| 123 |
# Check if this is a direct DOI match
|
| 124 |
if 'doi' in snippet and snippet['doi'] == doi:
|
| 125 |
source_map[f"DOI:{doi}"] = {
|
|
|
|
| 133 |
# Also check the ID field which might contain DOI
|
| 134 |
elif snippet.get("id") == f"DOI:{doi}":
|
| 135 |
source_map[f"DOI:{doi}"] = {
|
| 136 |
+
"id": snippet["id"],
|
| 137 |
+
"title": snippet["title"].strip(),
|
| 138 |
+
"url": snippet["url"],
|
| 139 |
"citation": snippet["citation"],
|
| 140 |
"doi": doi
|
| 141 |
+
}
|
| 142 |
+
break
|
| 143 |
|
| 144 |
# Process other citation formats for backward compatibility
|
| 145 |
for source_id_match in source_matches:
|
|
|
|
| 147 |
for snippet in evidence_snippets:
|
| 148 |
if source_id_match == snippet["id"]:
|
| 149 |
source_map[source_id_match] = {
|
| 150 |
+
"id": snippet["id"],
|
| 151 |
+
"title": snippet["title"].strip(),
|
| 152 |
+
"url": snippet["url"],
|
| 153 |
"citation": snippet["citation"],
|
| 154 |
"pmid": snippet.get("pmid", ""),
|
| 155 |
"doi": snippet.get("doi", "")
|
| 156 |
+
}
|
| 157 |
break
|
| 158 |
+
|
| 159 |
# Replace PMID citations with links
|
| 160 |
linked_text = text
|
| 161 |
for pmid_key in [f"PMID:{pmid}" for pmid in pmid_matches]:
|
|
|
|
| 186 |
# Replace other citation formats
|
| 187 |
for source_id_key, source_data in source_map.items():
|
| 188 |
if not (source_id_key.startswith("PMID:") or source_id_key.startswith("DOI:")):
|
| 189 |
+
safe_id = re.escape(source_id_key)
|
| 190 |
+
pattern = f"\\[{safe_id}\\]"
|
| 191 |
+
replacement = f"[{source_data['title']}]({source_data['url']})"
|
| 192 |
+
linked_text = re.sub(pattern, replacement, linked_text)
|
| 193 |
|
| 194 |
# Handle generic [source_id] placeholder
|
| 195 |
if "source_id" in source_matches:
|
|
|
|
| 206 |
"doi": snippet.get("doi", "")
|
| 207 |
}
|
| 208 |
replacement = f"[{snippet['title']}]({snippet['url']})"
|
| 209 |
+
linked_text = re.sub(r'\[source_id\]', replacement, linked_text)
|
| 210 |
|
| 211 |
# Final fallback for any remaining placeholders
|
| 212 |
linked_text = re.sub(r'\[source_id\]', "[Medical Reference]", linked_text)
|
|
|
|
| 1283 |
system = {"role": "system", "content": FOLLOW_UP_PROMPT}
|
| 1284 |
else:
|
| 1285 |
system = {"role": "system", "content": SYSTEM_PROMPT}
|
| 1286 |
+
|
| 1287 |
+
# Debug - Print prompt type
|
| 1288 |
+
print(f"Using {'FOLLOW_UP_PROMPT' if is_follow_up else 'SYSTEM_PROMPT'} with query: {query}")
|
| 1289 |
|
| 1290 |
msgs = [system] + history
|
| 1291 |
|
|
|
|
| 1366 |
• [DOI:10.xxxx/yyyy] format for Europe PMC articles without PMID
|
| 1367 |
|
| 1368 |
Use no more than 3 sources and no fewer than 2 sources.
|
| 1369 |
+
|
| 1370 |
**After your main response, ALWAYS include these sections:**
|
| 1371 |
- **Follow-up Questions**: Specific questions to gather additional information.
|
| 1372 |
- **Reasoning**: Bullet points detailing your clinical reasoning.
|
|
|
|
| 1435 |
follow_up_questions = "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions) if q])
|
| 1436 |
else:
|
| 1437 |
follow_up_questions = questions
|
| 1438 |
+
|
| 1439 |
+
# Debug: Print follow-up questions
|
| 1440 |
+
print(f"Follow-up questions generated: {follow_up_questions}")
|
| 1441 |
else:
|
| 1442 |
# If RAG is disabled, just parse the response without source processing
|
| 1443 |
parsed_response = parse_doctor_response(response)
|
| 1444 |
main_response = response
|
| 1445 |
+
|
| 1446 |
# Extract reasoning
|
| 1447 |
reasoning = parsed_response.get("reasoning", [])
|
| 1448 |
if reasoning:
|
|
|
|
| 1459 |
follow_up_questions = "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions) if q])
|
| 1460 |
else:
|
| 1461 |
follow_up_questions = questions
|
| 1462 |
+
|
| 1463 |
+
# Debug: Print follow-up questions
|
| 1464 |
+
print(f"Follow-up questions generated: {follow_up_questions}")
|
| 1465 |
|
| 1466 |
# Return four values: main response, explanation, follow-up questions, and evidence
|
| 1467 |
return main_response, explanation, follow_up_questions, evidence_snippets
|