Spaces:
Running
Running
Commit
·
cd11dad
1
Parent(s):
cfb473d
refactor(examples): apply CodeRabbit feedback (validations, complexity, formatting)
Browse files
examples/README.md
CHANGED
|
@@ -107,7 +107,7 @@ uv run python examples/hypothesis_demo/run_hypothesis.py "sildenafil heart failu
|
|
| 107 |
|
| 108 |
---
|
| 109 |
|
| 110 |
-
### 6. Full
|
| 111 |
|
| 112 |
**THE COMPLETE PIPELINE** - All phases working together.
|
| 113 |
|
|
@@ -142,7 +142,7 @@ Output: Publication-quality research report with validated citations.
|
|
| 142 |
|
| 143 |
## Architecture
|
| 144 |
|
| 145 |
-
```
|
| 146 |
User Query
|
| 147 |
|
|
| 148 |
v
|
|
|
|
| 107 |
|
| 108 |
---
|
| 109 |
|
| 110 |
+
### 6. Full-Stack Demo (LLM Required)
|
| 111 |
|
| 112 |
**THE COMPLETE PIPELINE** - All phases working together.
|
| 113 |
|
|
|
|
| 142 |
|
| 143 |
## Architecture
|
| 144 |
|
| 145 |
+
```text
|
| 146 |
User Query
|
| 147 |
|
|
| 148 |
v
|
examples/full_stack_demo/run_full.py
CHANGED
|
@@ -77,6 +77,33 @@ async def _run_search_iteration(
|
|
| 77 |
return all_evidence
|
| 78 |
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
async def run_full_demo(query: str, max_iterations: int) -> None:
|
| 81 |
"""Run the REAL full stack pipeline."""
|
| 82 |
print_header("DeepCritical Full Stack Demo (REAL)")
|
|
@@ -124,22 +151,12 @@ async def run_full_demo(query: str, max_iterations: int) -> None:
|
|
| 124 |
_print_truncated(hyp_response.messages[0].text)
|
| 125 |
|
| 126 |
# Step 3: REAL Judge
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
print(f" Confidence: {assessment.confidence:.0%}")
|
| 132 |
-
print(f" Recommendation: {assessment.recommendation.upper()}")
|
| 133 |
-
|
| 134 |
-
if assessment.recommendation == "synthesize":
|
| 135 |
-
print("\n[Judge] Evidence sufficient! Proceeding to report generation...")
|
| 136 |
-
evidence_store["last_assessment"] = assessment.details.model_dump()
|
| 137 |
break
|
| 138 |
|
| 139 |
-
next_queries = assessment.next_search_queries[:2]
|
| 140 |
-
print(f"\n[Judge] Need more evidence. Next queries: {next_queries}")
|
| 141 |
-
query = assessment.next_search_queries[0] if assessment.next_search_queries else query
|
| 142 |
-
|
| 143 |
# Step 4: REAL Report generation
|
| 144 |
print_step(iteration + 1, "REPORT GENERATION (REAL LLM)")
|
| 145 |
report_agent = ReportAgent(evidence_store, embedding_service)
|
|
@@ -184,6 +201,10 @@ Examples:
|
|
| 184 |
|
| 185 |
args = parser.parse_args()
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
# Fail fast: require API key
|
| 188 |
if not (os.getenv("OPENAI_API_KEY") or os.getenv("ANTHROPIC_API_KEY")):
|
| 189 |
print("=" * 70)
|
|
|
|
| 77 |
return all_evidence
|
| 78 |
|
| 79 |
|
| 80 |
+
async def _handle_judge_step(
|
| 81 |
+
judge_handler: Any, query: str, all_evidence: list[Evidence], evidence_store: dict[str, Any]
|
| 82 |
+
) -> tuple[bool, str]:
|
| 83 |
+
"""Handle the judge assessment step. Returns (should_stop, next_query)."""
|
| 84 |
+
print("\n[Judge] Assessing evidence quality (REAL LLM)...")
|
| 85 |
+
assessment = await judge_handler.assess(query, all_evidence)
|
| 86 |
+
print(f" Mechanism Score: {assessment.details.mechanism_score}/10")
|
| 87 |
+
print(f" Clinical Score: {assessment.details.clinical_evidence_score}/10")
|
| 88 |
+
print(f" Confidence: {assessment.confidence:.0%}")
|
| 89 |
+
print(f" Recommendation: {assessment.recommendation.upper()}")
|
| 90 |
+
|
| 91 |
+
if assessment.recommendation == "synthesize":
|
| 92 |
+
print("\n[Judge] Evidence sufficient! Proceeding to report generation...")
|
| 93 |
+
evidence_store["last_assessment"] = assessment.details.model_dump()
|
| 94 |
+
return True, query
|
| 95 |
+
|
| 96 |
+
next_queries = assessment.next_search_queries[:2] if assessment.next_search_queries else []
|
| 97 |
+
if next_queries:
|
| 98 |
+
print(f"\n[Judge] Need more evidence. Next queries: {next_queries}")
|
| 99 |
+
return False, next_queries[0]
|
| 100 |
+
|
| 101 |
+
print(
|
| 102 |
+
"\n[Judge] Need more evidence but no suggested queries. " "Continuing with original query."
|
| 103 |
+
)
|
| 104 |
+
return False, query
|
| 105 |
+
|
| 106 |
+
|
| 107 |
async def run_full_demo(query: str, max_iterations: int) -> None:
|
| 108 |
"""Run the REAL full stack pipeline."""
|
| 109 |
print_header("DeepCritical Full Stack Demo (REAL)")
|
|
|
|
| 151 |
_print_truncated(hyp_response.messages[0].text)
|
| 152 |
|
| 153 |
# Step 3: REAL Judge
|
| 154 |
+
should_stop, query = await _handle_judge_step(
|
| 155 |
+
judge_handler, query, all_evidence, evidence_store
|
| 156 |
+
)
|
| 157 |
+
if should_stop:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
break
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
# Step 4: REAL Report generation
|
| 161 |
print_step(iteration + 1, "REPORT GENERATION (REAL LLM)")
|
| 162 |
report_agent = ReportAgent(evidence_store, embedding_service)
|
|
|
|
| 201 |
|
| 202 |
args = parser.parse_args()
|
| 203 |
|
| 204 |
+
if args.iterations < 1:
|
| 205 |
+
print("Error: iterations must be at least 1")
|
| 206 |
+
sys.exit(1)
|
| 207 |
+
|
| 208 |
# Fail fast: require API key
|
| 209 |
if not (os.getenv("OPENAI_API_KEY") or os.getenv("ANTHROPIC_API_KEY")):
|
| 210 |
print("=" * 70)
|
examples/hypothesis_demo/run_hypothesis.py
CHANGED
|
@@ -28,60 +28,68 @@ from src.tools.websearch import WebTool
|
|
| 28 |
|
| 29 |
async def run_hypothesis_demo(query: str) -> None:
|
| 30 |
"""Run the REAL hypothesis generation pipeline."""
|
| 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 |
-
for
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
async def main() -> None:
|
|
|
|
| 28 |
|
| 29 |
async def run_hypothesis_demo(query: str) -> None:
|
| 30 |
"""Run the REAL hypothesis generation pipeline."""
|
| 31 |
+
try:
|
| 32 |
+
print(f"\n{'='*60}")
|
| 33 |
+
print("DeepCritical Hypothesis Agent Demo (Phase 7)")
|
| 34 |
+
print(f"Query: {query}")
|
| 35 |
+
print("Mode: REAL (Live API calls)")
|
| 36 |
+
print(f"{'='*60}\n")
|
| 37 |
+
|
| 38 |
+
# Step 1: REAL Search
|
| 39 |
+
print("[Step 1] Searching PubMed + Web...")
|
| 40 |
+
search_handler = SearchHandler(tools=[PubMedTool(), WebTool()], timeout=30.0)
|
| 41 |
+
result = await search_handler.execute(query, max_results_per_tool=5)
|
| 42 |
+
|
| 43 |
+
print(f" Found {result.total_found} results from {result.sources_searched}")
|
| 44 |
+
if result.errors:
|
| 45 |
+
print(f" Warnings: {result.errors}")
|
| 46 |
+
|
| 47 |
+
if not result.evidence:
|
| 48 |
+
print("\nNo evidence found. Try a different query.")
|
| 49 |
+
return
|
| 50 |
+
|
| 51 |
+
# Step 2: REAL Embeddings - Deduplicate
|
| 52 |
+
print("\n[Step 2] Semantic deduplication...")
|
| 53 |
+
embedding_service = EmbeddingService()
|
| 54 |
+
unique_evidence = await embedding_service.deduplicate(result.evidence, threshold=0.85)
|
| 55 |
+
print(f" {len(result.evidence)} -> {len(unique_evidence)} unique papers")
|
| 56 |
+
|
| 57 |
+
# Show what we found
|
| 58 |
+
print("\n[Evidence collected]")
|
| 59 |
+
max_title_len = 50
|
| 60 |
+
for i, e in enumerate(unique_evidence[:5], 1):
|
| 61 |
+
raw_title = e.citation.title
|
| 62 |
+
if len(raw_title) > max_title_len:
|
| 63 |
+
title = raw_title[:max_title_len] + "..."
|
| 64 |
+
else:
|
| 65 |
+
title = raw_title
|
| 66 |
+
print(f" {i}. [{e.citation.source.upper()}] {title}")
|
| 67 |
+
|
| 68 |
+
# Step 3: REAL LLM - Generate hypotheses
|
| 69 |
+
print("\n[Step 3] Generating mechanistic hypotheses (LLM)...")
|
| 70 |
+
evidence_store: dict[str, Any] = {"current": unique_evidence, "hypotheses": []}
|
| 71 |
+
agent = HypothesisAgent(evidence_store, embedding_service)
|
| 72 |
+
|
| 73 |
+
print("-" * 60)
|
| 74 |
+
response = await agent.run(query)
|
| 75 |
+
print(response.messages[0].text)
|
| 76 |
+
print("-" * 60)
|
| 77 |
+
|
| 78 |
+
# Show stored hypotheses
|
| 79 |
+
hypotheses = evidence_store.get("hypotheses", [])
|
| 80 |
+
print(f"\n{len(hypotheses)} hypotheses stored")
|
| 81 |
+
|
| 82 |
+
if hypotheses:
|
| 83 |
+
print("\nGenerated search queries for further investigation:")
|
| 84 |
+
for h in hypotheses:
|
| 85 |
+
queries = h.to_search_queries()
|
| 86 |
+
print(f" {h.drug} -> {h.target}:")
|
| 87 |
+
for q in queries[:3]:
|
| 88 |
+
print(f" - {q}")
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"\n❌ Error during hypothesis generation: {e}")
|
| 92 |
+
raise
|
| 93 |
|
| 94 |
|
| 95 |
async def main() -> None:
|
examples/orchestrator_demo/run_agent.py
CHANGED
|
@@ -29,6 +29,8 @@ from src.tools.search_handler import SearchHandler
|
|
| 29 |
from src.tools.websearch import WebTool
|
| 30 |
from src.utils.models import OrchestratorConfig
|
| 31 |
|
|
|
|
|
|
|
| 32 |
|
| 33 |
async def main() -> None:
|
| 34 |
"""Run the REAL agent demo."""
|
|
@@ -51,6 +53,10 @@ Examples:
|
|
| 51 |
parser.add_argument("--iterations", type=int, default=3, help="Max iterations (default: 3)")
|
| 52 |
args = parser.parse_args()
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Fail fast: require API key
|
| 55 |
if not (os.getenv("OPENAI_API_KEY") or os.getenv("ANTHROPIC_API_KEY")):
|
| 56 |
print("=" * 60)
|
|
|
|
| 29 |
from src.tools.websearch import WebTool
|
| 30 |
from src.utils.models import OrchestratorConfig
|
| 31 |
|
| 32 |
+
MAX_ITERATIONS = 10
|
| 33 |
+
|
| 34 |
|
| 35 |
async def main() -> None:
|
| 36 |
"""Run the REAL agent demo."""
|
|
|
|
| 53 |
parser.add_argument("--iterations", type=int, default=3, help="Max iterations (default: 3)")
|
| 54 |
args = parser.parse_args()
|
| 55 |
|
| 56 |
+
if not 1 <= args.iterations <= MAX_ITERATIONS:
|
| 57 |
+
print(f"Error: iterations must be between 1 and {MAX_ITERATIONS}")
|
| 58 |
+
sys.exit(1)
|
| 59 |
+
|
| 60 |
# Fail fast: require API key
|
| 61 |
if not (os.getenv("OPENAI_API_KEY") or os.getenv("ANTHROPIC_API_KEY")):
|
| 62 |
print("=" * 60)
|