Commit
Β·
7719726
1
Parent(s):
46b9533
fix: implement proper hypothesis conversion in synthesize_node
Browse filesThe previous implementation left hypotheses=[] in synthesize_node with
a comment "Relies on evidence for now as state mapping is complex".
This fix:
- Adds _convert_hypothesis_to_mechanism() helper function
- Properly converts state Hypothesis β MechanismHypothesis
- Parses "drug -> target -> pathway -> effect" format back to fields
- Enables report generation to use generated hypotheses
The conversion is actually straightforward - parse the statement format
and map supporting/contradicting evidence IDs.
- src/agents/graph/nodes.py +42 -3
src/agents/graph/nodes.py
CHANGED
|
@@ -23,11 +23,47 @@ from src.tools.europepmc import EuropePMCTool
|
|
| 23 |
from src.tools.pubmed import PubMedTool
|
| 24 |
from src.tools.search_handler import SearchHandler
|
| 25 |
from src.utils.citation_validator import validate_references
|
| 26 |
-
from src.utils.models import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
logger = structlog.get_logger()
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# --- Supervisor Output Schema ---
|
| 32 |
class SupervisorDecision(BaseModel):
|
| 33 |
"""The decision made by the supervisor."""
|
|
@@ -215,11 +251,14 @@ async def synthesize_node(
|
|
| 215 |
system_prompt=REPORT_SYSTEM_PROMPT,
|
| 216 |
)
|
| 217 |
|
|
|
|
|
|
|
|
|
|
| 218 |
prompt = await format_report_prompt(
|
| 219 |
query=state["query"],
|
| 220 |
evidence=evidence_context,
|
| 221 |
-
hypotheses=
|
| 222 |
-
assessment={},
|
| 223 |
metadata={"sources": list(set(e.citation.source for e in evidence_context))},
|
| 224 |
embeddings=embedding_service,
|
| 225 |
)
|
|
|
|
| 23 |
from src.tools.pubmed import PubMedTool
|
| 24 |
from src.tools.search_handler import SearchHandler
|
| 25 |
from src.utils.citation_validator import validate_references
|
| 26 |
+
from src.utils.models import (
|
| 27 |
+
Citation,
|
| 28 |
+
Evidence,
|
| 29 |
+
HypothesisAssessment,
|
| 30 |
+
MechanismHypothesis,
|
| 31 |
+
ResearchReport,
|
| 32 |
+
)
|
| 33 |
|
| 34 |
logger = structlog.get_logger()
|
| 35 |
|
| 36 |
|
| 37 |
+
def _convert_hypothesis_to_mechanism(h: Hypothesis) -> MechanismHypothesis:
|
| 38 |
+
"""Convert state Hypothesis to MechanismHypothesis for report generation.
|
| 39 |
+
|
| 40 |
+
The state Hypothesis stores the mechanism as a statement like:
|
| 41 |
+
"drug -> target -> pathway -> effect"
|
| 42 |
+
|
| 43 |
+
We parse this back into structured MechanismHypothesis fields.
|
| 44 |
+
"""
|
| 45 |
+
# Parse statement format: "drug -> target -> pathway -> effect"
|
| 46 |
+
parts = h.statement.split(" -> ")
|
| 47 |
+
if len(parts) >= 4:
|
| 48 |
+
drug, target, pathway, effect = parts[0], parts[1], parts[2], parts[3]
|
| 49 |
+
else:
|
| 50 |
+
# Fallback if format is unexpected
|
| 51 |
+
drug = h.id
|
| 52 |
+
target = "Unknown"
|
| 53 |
+
pathway = "Unknown"
|
| 54 |
+
effect = h.statement
|
| 55 |
+
|
| 56 |
+
return MechanismHypothesis(
|
| 57 |
+
drug=drug,
|
| 58 |
+
target=target,
|
| 59 |
+
pathway=pathway,
|
| 60 |
+
effect=effect,
|
| 61 |
+
confidence=h.confidence,
|
| 62 |
+
supporting_evidence=h.supporting_evidence_ids,
|
| 63 |
+
contradicting_evidence=h.contradicting_evidence_ids,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
# --- Supervisor Output Schema ---
|
| 68 |
class SupervisorDecision(BaseModel):
|
| 69 |
"""The decision made by the supervisor."""
|
|
|
|
| 251 |
system_prompt=REPORT_SYSTEM_PROMPT,
|
| 252 |
)
|
| 253 |
|
| 254 |
+
# Convert state hypotheses to MechanismHypothesis for report generation
|
| 255 |
+
mechanism_hypotheses = [_convert_hypothesis_to_mechanism(h) for h in state["hypotheses"]]
|
| 256 |
+
|
| 257 |
prompt = await format_report_prompt(
|
| 258 |
query=state["query"],
|
| 259 |
evidence=evidence_context,
|
| 260 |
+
hypotheses=mechanism_hypotheses,
|
| 261 |
+
assessment={},
|
| 262 |
metadata={"sources": list(set(e.citation.source for e in evidence_context))},
|
| 263 |
embeddings=embedding_service,
|
| 264 |
)
|