Commit
·
f160233
1
Parent(s):
a820b5b
feat: Enhance research workflow with embedding service integration
Browse files- Updated graph nodes to accept an optional EmbeddingService for improved evidence handling and deduplication.
- Refactored search, judge, resolve, and synthesize nodes to utilize the embedding service for enhanced functionality.
- Modified the research graph creation to bind the embedding service to worker nodes, ensuring seamless integration.
- Added logging for better traceability during node execution.
- Expanded unit and integration tests to cover new embedding service interactions and ensure robust functionality.
src/agents/graph/nodes.py
CHANGED
|
@@ -1,18 +1,31 @@
|
|
| 1 |
"""Graph node implementations for DeepBoner research."""
|
| 2 |
|
| 3 |
-
import asyncio
|
| 4 |
from typing import Any, Literal
|
| 5 |
|
|
|
|
| 6 |
from langchain_core.language_models.chat_models import BaseChatModel
|
| 7 |
from langchain_core.messages import AIMessage
|
| 8 |
from langchain_core.output_parsers import PydanticOutputParser
|
| 9 |
from langchain_core.prompts import ChatPromptTemplate
|
| 10 |
from pydantic import BaseModel, Field
|
|
|
|
| 11 |
|
|
|
|
| 12 |
from src.agents.graph.state import Hypothesis, ResearchState
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
from src.tools.clinicaltrials import ClinicalTrialsTool
|
| 14 |
from src.tools.europepmc import EuropePMCTool
|
| 15 |
from src.tools.pubmed import PubMedTool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
# --- Supervisor Output Schema ---
|
|
@@ -28,92 +41,206 @@ class SupervisorDecision(BaseModel):
|
|
| 28 |
# --- Nodes ---
|
| 29 |
|
| 30 |
|
| 31 |
-
async def search_node(
|
|
|
|
|
|
|
| 32 |
"""Execute search across all sources."""
|
| 33 |
query = state["query"]
|
|
|
|
| 34 |
|
| 35 |
# Initialize tools
|
| 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 |
return {
|
| 62 |
-
"
|
| 63 |
-
|
| 64 |
}
|
| 65 |
|
| 66 |
|
| 67 |
-
async def judge_node(
|
|
|
|
|
|
|
| 68 |
"""Evaluate evidence and update hypothesis confidence."""
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
return {
|
| 81 |
-
"hypotheses":
|
| 82 |
-
"messages": [AIMessage(content="Judge:
|
|
|
|
| 83 |
}
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
|
| 89 |
-
async def resolve_node(
|
|
|
|
|
|
|
| 90 |
"""Handle open conflicts."""
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
|
|
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
"""Generate final report."""
|
| 97 |
-
|
| 98 |
-
return {
|
| 99 |
-
"messages": [AIMessage(content="# Final Report\n\nResearch complete.")],
|
| 100 |
-
"next_step": "finish",
|
| 101 |
-
}
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
if state["iteration_count"] >= state["max_iterations"]:
|
| 113 |
return {"next_step": "synthesize", "iteration_count": state["iteration_count"]}
|
| 114 |
|
| 115 |
if llm is None:
|
| 116 |
-
# Fallback for tests/default
|
| 117 |
return {"next_step": "search", "iteration_count": state["iteration_count"] + 1}
|
| 118 |
|
| 119 |
parser = PydanticOutputParser(pydantic_object=SupervisorDecision)
|
|
@@ -142,6 +269,7 @@ async def supervisor_node(state: ResearchState, llm: BaseChatModel | None = None
|
|
| 142 |
chain = prompt | llm | parser
|
| 143 |
|
| 144 |
try:
|
|
|
|
| 145 |
decision: SupervisorDecision = await chain.ainvoke(
|
| 146 |
{
|
| 147 |
"query": state["query"],
|
|
@@ -158,10 +286,8 @@ async def supervisor_node(state: ResearchState, llm: BaseChatModel | None = None
|
|
| 158 |
"messages": [AIMessage(content=f"Supervisor: {decision.reasoning}")],
|
| 159 |
}
|
| 160 |
except Exception as e:
|
| 161 |
-
# Fallback on error (e.g. parsing failure)
|
| 162 |
-
# We default to 'judge' if we have data, or 'synthesize' if we are stuck
|
| 163 |
return {
|
| 164 |
-
"next_step": "synthesize",
|
| 165 |
"iteration_count": state["iteration_count"] + 1,
|
| 166 |
"messages": [AIMessage(content=f"Supervisor Error: {e!s}. Proceeding to synthesis.")],
|
| 167 |
}
|
|
|
|
| 1 |
"""Graph node implementations for DeepBoner research."""
|
| 2 |
|
|
|
|
| 3 |
from typing import Any, Literal
|
| 4 |
|
| 5 |
+
import structlog
|
| 6 |
from langchain_core.language_models.chat_models import BaseChatModel
|
| 7 |
from langchain_core.messages import AIMessage
|
| 8 |
from langchain_core.output_parsers import PydanticOutputParser
|
| 9 |
from langchain_core.prompts import ChatPromptTemplate
|
| 10 |
from pydantic import BaseModel, Field
|
| 11 |
+
from pydantic_ai import Agent
|
| 12 |
|
| 13 |
+
from src.agent_factory.judges import get_model
|
| 14 |
from src.agents.graph.state import Hypothesis, ResearchState
|
| 15 |
+
from src.prompts.hypothesis import SYSTEM_PROMPT as HYPOTHESIS_SYSTEM_PROMPT
|
| 16 |
+
from src.prompts.hypothesis import format_hypothesis_prompt
|
| 17 |
+
from src.prompts.report import SYSTEM_PROMPT as REPORT_SYSTEM_PROMPT
|
| 18 |
+
from src.prompts.report import format_report_prompt
|
| 19 |
+
from src.services.embeddings import EmbeddingService
|
| 20 |
+
from src.tools.base import SearchTool
|
| 21 |
from src.tools.clinicaltrials import ClinicalTrialsTool
|
| 22 |
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 Citation, Evidence, HypothesisAssessment, ResearchReport
|
| 27 |
+
|
| 28 |
+
logger = structlog.get_logger()
|
| 29 |
|
| 30 |
|
| 31 |
# --- Supervisor Output Schema ---
|
|
|
|
| 41 |
# --- Nodes ---
|
| 42 |
|
| 43 |
|
| 44 |
+
async def search_node(
|
| 45 |
+
state: ResearchState, embedding_service: EmbeddingService | None = None
|
| 46 |
+
) -> dict[str, Any]:
|
| 47 |
"""Execute search across all sources."""
|
| 48 |
query = state["query"]
|
| 49 |
+
logger.info("search_node: executing search", query=query)
|
| 50 |
|
| 51 |
# Initialize tools
|
| 52 |
+
tools: list[SearchTool] = [PubMedTool(), ClinicalTrialsTool(), EuropePMCTool()]
|
| 53 |
+
handler = SearchHandler(tools=tools)
|
| 54 |
+
|
| 55 |
+
# Execute search
|
| 56 |
+
result = await handler.execute(query)
|
| 57 |
+
|
| 58 |
+
new_evidence_count = 0
|
| 59 |
+
new_ids = []
|
| 60 |
+
|
| 61 |
+
if embedding_service and result.evidence:
|
| 62 |
+
# Deduplicate and store
|
| 63 |
+
unique_evidence = await embedding_service.deduplicate(result.evidence)
|
| 64 |
|
| 65 |
+
for ev in unique_evidence:
|
| 66 |
+
ev_id = ev.citation.url
|
| 67 |
+
await embedding_service.add_evidence(
|
| 68 |
+
evidence_id=ev_id,
|
| 69 |
+
content=ev.content,
|
| 70 |
+
metadata={
|
| 71 |
+
"source": ev.citation.source,
|
| 72 |
+
"title": ev.citation.title,
|
| 73 |
+
"date": ev.citation.date,
|
| 74 |
+
"authors": ",".join(ev.citation.authors or []),
|
| 75 |
+
"url": ev.citation.url,
|
| 76 |
+
},
|
| 77 |
+
)
|
| 78 |
+
new_ids.append(ev_id)
|
| 79 |
+
|
| 80 |
+
new_evidence_count = len(unique_evidence)
|
| 81 |
+
else:
|
| 82 |
+
new_evidence_count = len(result.evidence)
|
| 83 |
+
|
| 84 |
+
message = (
|
| 85 |
+
f"Search completed. Found {result.total_found} total, "
|
| 86 |
+
f"{new_evidence_count} unique new papers."
|
| 87 |
+
)
|
| 88 |
+
if result.errors:
|
| 89 |
+
message += f" Errors: {'; '.join(result.errors)}"
|
| 90 |
|
| 91 |
return {
|
| 92 |
+
"evidence_ids": new_ids,
|
| 93 |
+
"messages": [AIMessage(content=message)],
|
| 94 |
}
|
| 95 |
|
| 96 |
|
| 97 |
+
async def judge_node(
|
| 98 |
+
state: ResearchState, embedding_service: EmbeddingService | None = None
|
| 99 |
+
) -> dict[str, Any]:
|
| 100 |
"""Evaluate evidence and update hypothesis confidence."""
|
| 101 |
+
logger.info("judge_node: evaluating evidence")
|
| 102 |
+
|
| 103 |
+
evidence_context: list[Evidence] = []
|
| 104 |
+
if embedding_service:
|
| 105 |
+
scored_points = await embedding_service.search_similar(state["query"], n_results=20)
|
| 106 |
+
for p in scored_points:
|
| 107 |
+
meta = p.get("metadata", {})
|
| 108 |
+
authors = meta.get("authors", "")
|
| 109 |
+
author_list = authors.split(",") if authors else []
|
| 110 |
+
|
| 111 |
+
evidence_context.append(
|
| 112 |
+
Evidence(
|
| 113 |
+
content=p.get("content", ""),
|
| 114 |
+
citation=Citation(
|
| 115 |
+
url=p.get("id", ""),
|
| 116 |
+
title=meta.get("title", "Unknown"),
|
| 117 |
+
source=meta.get("source", "Unknown"),
|
| 118 |
+
date=meta.get("date", ""),
|
| 119 |
+
authors=author_list,
|
| 120 |
+
),
|
| 121 |
+
)
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
agent = Agent(
|
| 125 |
+
model=get_model(),
|
| 126 |
+
output_type=HypothesisAssessment,
|
| 127 |
+
system_prompt=HYPOTHESIS_SYSTEM_PROMPT,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
prompt = await format_hypothesis_prompt(
|
| 131 |
+
query=state["query"], evidence=evidence_context, embeddings=embedding_service
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
try:
|
| 135 |
+
result = await agent.run(prompt)
|
| 136 |
+
assessment = result.output
|
| 137 |
+
|
| 138 |
+
new_hypotheses = []
|
| 139 |
+
for h in assessment.hypotheses:
|
| 140 |
+
new_hypotheses.append(
|
| 141 |
+
Hypothesis(
|
| 142 |
+
id=h.drug,
|
| 143 |
+
statement=f"{h.drug} -> {h.target} -> {h.pathway} -> {h.effect}",
|
| 144 |
+
status="proposed",
|
| 145 |
+
confidence=h.confidence,
|
| 146 |
+
supporting_evidence_ids=[],
|
| 147 |
+
contradicting_evidence_ids=[],
|
| 148 |
+
)
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
return {
|
| 152 |
+
"hypotheses": new_hypotheses,
|
| 153 |
+
"messages": [AIMessage(content=f"Judge: Generated {len(new_hypotheses)} hypotheses.")],
|
| 154 |
+
"next_step": "resolve",
|
| 155 |
}
|
| 156 |
+
except Exception as e:
|
| 157 |
+
logger.error("judge_node failed", error=str(e))
|
| 158 |
+
return {"messages": [AIMessage(content=f"Judge Error: {e!s}")], "next_step": "search"}
|
| 159 |
|
| 160 |
|
| 161 |
+
async def resolve_node(
|
| 162 |
+
state: ResearchState, embedding_service: EmbeddingService | None = None
|
| 163 |
+
) -> dict[str, Any]:
|
| 164 |
"""Handle open conflicts."""
|
| 165 |
+
messages = []
|
| 166 |
+
|
| 167 |
+
# Access attributes with dot notation because items are Pydantic models
|
| 168 |
+
high_conf = [h for h in state["hypotheses"] if h.confidence > 0.8]
|
| 169 |
+
|
| 170 |
+
if high_conf:
|
| 171 |
+
messages.append(
|
| 172 |
+
AIMessage(
|
| 173 |
+
content=(
|
| 174 |
+
f"Resolver: Found {len(high_conf)} high confidence hypotheses. "
|
| 175 |
+
"Conflicts resolved."
|
| 176 |
+
)
|
| 177 |
+
)
|
| 178 |
+
)
|
| 179 |
+
else:
|
| 180 |
+
messages.append(AIMessage(content="Resolver: No high confidence hypotheses yet."))
|
| 181 |
|
| 182 |
+
return {"messages": messages}
|
| 183 |
|
| 184 |
+
|
| 185 |
+
async def synthesize_node(
|
| 186 |
+
state: ResearchState, embedding_service: EmbeddingService | None = None
|
| 187 |
+
) -> dict[str, Any]:
|
| 188 |
"""Generate final report."""
|
| 189 |
+
logger.info("synthesize_node: generating report")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
evidence_context: list[Evidence] = []
|
| 192 |
+
if embedding_service:
|
| 193 |
+
scored_points = await embedding_service.search_similar(state["query"], n_results=50)
|
| 194 |
+
for p in scored_points:
|
| 195 |
+
meta = p.get("metadata", {})
|
| 196 |
+
authors = meta.get("authors", "")
|
| 197 |
+
author_list = authors.split(",") if authors else []
|
| 198 |
|
| 199 |
+
evidence_context.append(
|
| 200 |
+
Evidence(
|
| 201 |
+
content=p.get("content", ""),
|
| 202 |
+
citation=Citation(
|
| 203 |
+
url=p.get("id", ""),
|
| 204 |
+
title=meta.get("title", "Unknown"),
|
| 205 |
+
source=meta.get("source", "Unknown"),
|
| 206 |
+
date=meta.get("date", ""),
|
| 207 |
+
authors=author_list,
|
| 208 |
+
),
|
| 209 |
+
)
|
| 210 |
+
)
|
| 211 |
|
| 212 |
+
agent = Agent(
|
| 213 |
+
model=get_model(),
|
| 214 |
+
output_type=ResearchReport,
|
| 215 |
+
system_prompt=REPORT_SYSTEM_PROMPT,
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
prompt = await format_report_prompt(
|
| 219 |
+
query=state["query"],
|
| 220 |
+
evidence=evidence_context,
|
| 221 |
+
hypotheses=[], # Relies on evidence for now as state mapping is complex
|
| 222 |
+
assessment={}, # Pass empty dict instead of None
|
| 223 |
+
metadata={"sources": list(set(e.citation.source for e in evidence_context))},
|
| 224 |
+
embeddings=embedding_service,
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
try:
|
| 228 |
+
result = await agent.run(prompt)
|
| 229 |
+
report = result.output
|
| 230 |
+
report = validate_references(report, evidence_context)
|
| 231 |
+
|
| 232 |
+
return {"messages": [AIMessage(content=report.to_markdown())], "next_step": "finish"}
|
| 233 |
+
except Exception as e:
|
| 234 |
+
logger.error("synthesize_node failed", error=str(e))
|
| 235 |
+
return {"messages": [AIMessage(content=f"Synthesis Error: {e!s}")], "next_step": "finish"}
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
async def supervisor_node(state: ResearchState, llm: BaseChatModel | None = None) -> dict[str, Any]:
|
| 239 |
+
"""Route to next node based on state using robust Pydantic parsing."""
|
| 240 |
if state["iteration_count"] >= state["max_iterations"]:
|
| 241 |
return {"next_step": "synthesize", "iteration_count": state["iteration_count"]}
|
| 242 |
|
| 243 |
if llm is None:
|
|
|
|
| 244 |
return {"next_step": "search", "iteration_count": state["iteration_count"] + 1}
|
| 245 |
|
| 246 |
parser = PydanticOutputParser(pydantic_object=SupervisorDecision)
|
|
|
|
| 269 |
chain = prompt | llm | parser
|
| 270 |
|
| 271 |
try:
|
| 272 |
+
# Note: state["conflicts"] contains Pydantic models, so use dot notation
|
| 273 |
decision: SupervisorDecision = await chain.ainvoke(
|
| 274 |
{
|
| 275 |
"query": state["query"],
|
|
|
|
| 286 |
"messages": [AIMessage(content=f"Supervisor: {decision.reasoning}")],
|
| 287 |
}
|
| 288 |
except Exception as e:
|
|
|
|
|
|
|
| 289 |
return {
|
| 290 |
+
"next_step": "synthesize",
|
| 291 |
"iteration_count": state["iteration_count"] + 1,
|
| 292 |
"messages": [AIMessage(content=f"Supervisor Error: {e!s}. Proceeding to synthesis.")],
|
| 293 |
}
|
src/agents/graph/workflow.py
CHANGED
|
@@ -15,29 +15,55 @@ from src.agents.graph.nodes import (
|
|
| 15 |
synthesize_node,
|
| 16 |
)
|
| 17 |
from src.agents.graph.state import ResearchState
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
def create_research_graph(
|
| 21 |
-
llm: BaseChatModel | None = None,
|
|
|
|
|
|
|
| 22 |
) -> CompiledStateGraph: # type: ignore
|
| 23 |
"""Build the research state graph.
|
| 24 |
|
| 25 |
Args:
|
| 26 |
llm: The language model for the supervisor node.
|
| 27 |
checkpointer: Optional persistence layer.
|
|
|
|
| 28 |
"""
|
| 29 |
graph = StateGraph(ResearchState)
|
| 30 |
|
| 31 |
# --- Nodes ---
|
| 32 |
# Bind the LLM to the supervisor node using partial
|
| 33 |
-
# This injects the model dependency while keeping the node signature clean for the graph
|
| 34 |
bound_supervisor = partial(supervisor_node, llm=llm) if llm else supervisor_node
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
graph.add_node("supervisor", bound_supervisor)
|
| 37 |
-
graph.add_node("search",
|
| 38 |
-
graph.add_node("judge",
|
| 39 |
-
graph.add_node("resolve",
|
| 40 |
-
graph.add_node("synthesize",
|
| 41 |
|
| 42 |
# --- Edges ---
|
| 43 |
# All worker nodes report back to supervisor
|
|
|
|
| 15 |
synthesize_node,
|
| 16 |
)
|
| 17 |
from src.agents.graph.state import ResearchState
|
| 18 |
+
from src.services.embeddings import EmbeddingService
|
| 19 |
|
| 20 |
|
| 21 |
def create_research_graph(
|
| 22 |
+
llm: BaseChatModel | None = None,
|
| 23 |
+
checkpointer: Any = None,
|
| 24 |
+
embedding_service: EmbeddingService | None = None,
|
| 25 |
) -> CompiledStateGraph: # type: ignore
|
| 26 |
"""Build the research state graph.
|
| 27 |
|
| 28 |
Args:
|
| 29 |
llm: The language model for the supervisor node.
|
| 30 |
checkpointer: Optional persistence layer.
|
| 31 |
+
embedding_service: Service for evidence storage and retrieval.
|
| 32 |
"""
|
| 33 |
graph = StateGraph(ResearchState)
|
| 34 |
|
| 35 |
# --- Nodes ---
|
| 36 |
# Bind the LLM to the supervisor node using partial
|
|
|
|
| 37 |
bound_supervisor = partial(supervisor_node, llm=llm) if llm else supervisor_node
|
| 38 |
|
| 39 |
+
# Bind embedding service to worker nodes
|
| 40 |
+
# We use partial to inject the service dependency while keeping the node signature clean
|
| 41 |
+
bound_search = (
|
| 42 |
+
partial(search_node, embedding_service=embedding_service)
|
| 43 |
+
if embedding_service
|
| 44 |
+
else search_node
|
| 45 |
+
)
|
| 46 |
+
bound_judge = (
|
| 47 |
+
partial(judge_node, embedding_service=embedding_service)
|
| 48 |
+
if embedding_service
|
| 49 |
+
else judge_node
|
| 50 |
+
)
|
| 51 |
+
bound_resolve = (
|
| 52 |
+
partial(resolve_node, embedding_service=embedding_service)
|
| 53 |
+
if embedding_service
|
| 54 |
+
else resolve_node
|
| 55 |
+
)
|
| 56 |
+
bound_synthesize = (
|
| 57 |
+
partial(synthesize_node, embedding_service=embedding_service)
|
| 58 |
+
if embedding_service
|
| 59 |
+
else synthesize_node
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
graph.add_node("supervisor", bound_supervisor)
|
| 63 |
+
graph.add_node("search", bound_search)
|
| 64 |
+
graph.add_node("judge", bound_judge)
|
| 65 |
+
graph.add_node("resolve", bound_resolve)
|
| 66 |
+
graph.add_node("synthesize", bound_synthesize)
|
| 67 |
|
| 68 |
# --- Edges ---
|
| 69 |
# All worker nodes report back to supervisor
|
src/orchestrators/langgraph_orchestrator.py
CHANGED
|
@@ -10,6 +10,7 @@ from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
|
|
| 10 |
from src.agents.graph.state import ResearchState
|
| 11 |
from src.agents.graph.workflow import create_research_graph
|
| 12 |
from src.orchestrators.base import OrchestratorProtocol
|
|
|
|
| 13 |
from src.utils.config import settings
|
| 14 |
from src.utils.models import AgentEvent
|
| 15 |
|
|
@@ -32,8 +33,9 @@ class LangGraphOrchestrator(OrchestratorProtocol):
|
|
| 32 |
# Ensure we have an API key
|
| 33 |
api_key = settings.hf_token
|
| 34 |
if not api_key:
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
self.llm_endpoint = HuggingFaceEndpoint( # type: ignore
|
| 39 |
repo_id=repo_id,
|
|
@@ -46,6 +48,8 @@ class LangGraphOrchestrator(OrchestratorProtocol):
|
|
| 46 |
|
| 47 |
async def run(self, query: str) -> AsyncGenerator[AgentEvent, None]:
|
| 48 |
"""Execute research workflow with structured state."""
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Setup checkpointer (SQLite for dev)
|
| 51 |
if self._checkpoint_path:
|
|
@@ -62,9 +66,17 @@ class LangGraphOrchestrator(OrchestratorProtocol):
|
|
| 62 |
async def get_graph_context(saver_instance: Any) -> AsyncIterator[Any]:
|
| 63 |
if saver_instance:
|
| 64 |
async with saver_instance as s:
|
| 65 |
-
yield create_research_graph(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
-
yield create_research_graph(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
async with get_graph_context(saver) as graph:
|
| 70 |
# Initialize state
|
|
|
|
| 10 |
from src.agents.graph.state import ResearchState
|
| 11 |
from src.agents.graph.workflow import create_research_graph
|
| 12 |
from src.orchestrators.base import OrchestratorProtocol
|
| 13 |
+
from src.services.embeddings import EmbeddingService
|
| 14 |
from src.utils.config import settings
|
| 15 |
from src.utils.models import AgentEvent
|
| 16 |
|
|
|
|
| 33 |
# Ensure we have an API key
|
| 34 |
api_key = settings.hf_token
|
| 35 |
if not api_key:
|
| 36 |
+
raise ValueError(
|
| 37 |
+
"HF_TOKEN (Hugging Face API Token) is required for God Mode to use Llama 3.1."
|
| 38 |
+
)
|
| 39 |
|
| 40 |
self.llm_endpoint = HuggingFaceEndpoint( # type: ignore
|
| 41 |
repo_id=repo_id,
|
|
|
|
| 48 |
|
| 49 |
async def run(self, query: str) -> AsyncGenerator[AgentEvent, None]:
|
| 50 |
"""Execute research workflow with structured state."""
|
| 51 |
+
# Initialize embedding service for this specific run (ensures isolation)
|
| 52 |
+
embedding_service = EmbeddingService()
|
| 53 |
|
| 54 |
# Setup checkpointer (SQLite for dev)
|
| 55 |
if self._checkpoint_path:
|
|
|
|
| 66 |
async def get_graph_context(saver_instance: Any) -> AsyncIterator[Any]:
|
| 67 |
if saver_instance:
|
| 68 |
async with saver_instance as s:
|
| 69 |
+
yield create_research_graph(
|
| 70 |
+
llm=self.chat_model,
|
| 71 |
+
checkpointer=s,
|
| 72 |
+
embedding_service=embedding_service,
|
| 73 |
+
)
|
| 74 |
else:
|
| 75 |
+
yield create_research_graph(
|
| 76 |
+
llm=self.chat_model,
|
| 77 |
+
checkpointer=None,
|
| 78 |
+
embedding_service=embedding_service,
|
| 79 |
+
)
|
| 80 |
|
| 81 |
async with get_graph_context(saver) as graph:
|
| 82 |
# Initialize state
|
tests/integration/graph/test_workflow.py
CHANGED
|
@@ -6,8 +6,46 @@ from src.agents.graph.workflow import create_research_graph
|
|
| 6 |
|
| 7 |
|
| 8 |
@pytest.mark.asyncio
|
| 9 |
-
async def test_graph_execution_flow():
|
| 10 |
"""Test the graph runs from start to finish (simulated)."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Create graph without LLM (will use fallback supervisor logic -> search -> synthesize)
|
| 12 |
graph = create_research_graph(llm=None)
|
| 13 |
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
@pytest.mark.asyncio
|
| 9 |
+
async def test_graph_execution_flow(mocker):
|
| 10 |
"""Test the graph runs from start to finish (simulated)."""
|
| 11 |
+
# Mock Agent.run to avoid API calls
|
| 12 |
+
mock_run = mocker.patch("pydantic_ai.Agent.run")
|
| 13 |
+
# Return dummy report/assessment
|
| 14 |
+
mock_result = mocker.Mock()
|
| 15 |
+
mock_result.output = mocker.Mock() # generic output
|
| 16 |
+
# For judge: output.hypotheses = []
|
| 17 |
+
mock_result.output.hypotheses = []
|
| 18 |
+
# For report: validate_references needs specific structure?
|
| 19 |
+
# Actually validate_references expects a ResearchReport.
|
| 20 |
+
# Let's mock the return of validate_references too if needed, or make report valid.
|
| 21 |
+
# Or just mock the node logic? No, we want to test the graph wiring.
|
| 22 |
+
|
| 23 |
+
# Minimal valid report
|
| 24 |
+
from src.utils.models import ReportSection, ResearchReport
|
| 25 |
+
|
| 26 |
+
dummy_section = ReportSection(title="Dummy", content="Content")
|
| 27 |
+
|
| 28 |
+
mock_report = ResearchReport(
|
| 29 |
+
title="Test Report",
|
| 30 |
+
executive_summary="Summary " * 20, # Ensure > 100 chars
|
| 31 |
+
research_question="Question",
|
| 32 |
+
methodology=dummy_section,
|
| 33 |
+
hypotheses_tested=[],
|
| 34 |
+
mechanistic_findings=dummy_section,
|
| 35 |
+
clinical_findings=dummy_section,
|
| 36 |
+
drug_candidates=[],
|
| 37 |
+
limitations=["None"],
|
| 38 |
+
conclusion="Conclusion",
|
| 39 |
+
references=[],
|
| 40 |
+
confidence_score=0.5,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Since fallback supervisor skips Judge and goes Search -> Synthesize,
|
| 44 |
+
# Agent.run is only called once by SynthesizeNode.
|
| 45 |
+
# It expects a ResearchReport.
|
| 46 |
+
mock_result.output = mock_report
|
| 47 |
+
mock_run.return_value = mock_result
|
| 48 |
+
|
| 49 |
# Create graph without LLM (will use fallback supervisor logic -> search -> synthesize)
|
| 50 |
graph = create_research_graph(llm=None)
|
| 51 |
|
tests/unit/graph/test_nodes.py
CHANGED
|
@@ -7,8 +7,26 @@ from src.agents.graph.state import ResearchState
|
|
| 7 |
|
| 8 |
|
| 9 |
@pytest.mark.asyncio
|
| 10 |
-
async def test_judge_node_initialization():
|
| 11 |
"""Test judge creates initial hypothesis if none exist."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
state: ResearchState = {
|
| 13 |
"query": "Does coffee cause cancer?",
|
| 14 |
"hypotheses": [],
|
|
@@ -24,7 +42,7 @@ async def test_judge_node_initialization():
|
|
| 24 |
|
| 25 |
assert "hypotheses" in update
|
| 26 |
assert len(update["hypotheses"]) == 1
|
| 27 |
-
assert update["hypotheses"][0].id == "
|
| 28 |
assert update["hypotheses"][0].status == "proposed"
|
| 29 |
|
| 30 |
|
|
@@ -67,4 +85,5 @@ async def test_search_node_execution(mocker):
|
|
| 67 |
|
| 68 |
update = await search_node(state)
|
| 69 |
assert "messages" in update
|
| 70 |
-
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
@pytest.mark.asyncio
|
| 10 |
+
async def test_judge_node_initialization(mocker):
|
| 11 |
"""Test judge creates initial hypothesis if none exist."""
|
| 12 |
+
# Mock pydantic_ai Agent
|
| 13 |
+
mock_run = mocker.patch("pydantic_ai.Agent.run")
|
| 14 |
+
|
| 15 |
+
# Create a mock assessment with attributes
|
| 16 |
+
mock_hypothesis = mocker.Mock()
|
| 17 |
+
mock_hypothesis.drug = "Caffeine"
|
| 18 |
+
mock_hypothesis.target = "Adenosine"
|
| 19 |
+
mock_hypothesis.pathway = "CNS"
|
| 20 |
+
mock_hypothesis.effect = "Alertness"
|
| 21 |
+
mock_hypothesis.confidence = 0.8
|
| 22 |
+
|
| 23 |
+
mock_assessment = mocker.Mock()
|
| 24 |
+
mock_assessment.hypotheses = [mock_hypothesis]
|
| 25 |
+
|
| 26 |
+
mock_result = mocker.Mock()
|
| 27 |
+
mock_result.output = mock_assessment
|
| 28 |
+
mock_run.return_value = mock_result
|
| 29 |
+
|
| 30 |
state: ResearchState = {
|
| 31 |
"query": "Does coffee cause cancer?",
|
| 32 |
"hypotheses": [],
|
|
|
|
| 42 |
|
| 43 |
assert "hypotheses" in update
|
| 44 |
assert len(update["hypotheses"]) == 1
|
| 45 |
+
assert update["hypotheses"][0].id == "Caffeine"
|
| 46 |
assert update["hypotheses"][0].status == "proposed"
|
| 47 |
|
| 48 |
|
|
|
|
| 85 |
|
| 86 |
update = await search_node(state)
|
| 87 |
assert "messages" in update
|
| 88 |
+
# Matches "Found 0 total, 0 unique new papers."
|
| 89 |
+
assert "0 unique new papers" in update["messages"][0].content
|