Update mcp/orchestrator.py
Browse files- mcp/orchestrator.py +79 -74
mcp/orchestrator.py
CHANGED
@@ -1,117 +1,122 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
-
"""MedGenesis β
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
β’ Summarises with OpenAI or Gemini (userβselectable)
|
10 |
-
β’ Returns a single dict ready for Streamlit UI & pydantic schemas.
|
11 |
"""
|
12 |
from __future__ import annotations
|
13 |
|
14 |
import asyncio
|
15 |
from typing import Any, Dict, List
|
16 |
|
17 |
-
# ββ
|
18 |
-
from mcp.arxiv
|
19 |
-
from mcp.pubmed
|
20 |
-
from mcp.nlp
|
21 |
-
from mcp.umls
|
22 |
-
from mcp.openfda
|
23 |
-
from mcp.ncbi
|
24 |
-
from mcp.disgenet
|
25 |
-
from mcp.mygene
|
26 |
-
from mcp.ctgov
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
# LLM router
|
32 |
-
#
|
33 |
-
|
34 |
|
35 |
-
def
|
36 |
-
if
|
37 |
return gemini_summarize, gemini_qa, "gemini"
|
38 |
return ai_summarize, ai_qa, "openai"
|
39 |
|
40 |
-
#
|
41 |
-
#
|
42 |
-
#
|
43 |
async def _enrich_keywords(keys: List[str]) -> Dict[str, Any]:
|
44 |
-
tasks = []
|
45 |
for k in keys:
|
46 |
tasks += [search_gene(k), get_mesh_definition(k), disease_to_genes(k)]
|
47 |
|
48 |
-
|
49 |
|
50 |
genes, mesh_defs, disg = [], [], []
|
51 |
-
for idx,
|
52 |
-
if isinstance(
|
53 |
continue
|
54 |
bucket = idx % 3
|
55 |
if bucket == 0:
|
56 |
-
genes.extend(
|
57 |
elif bucket == 1:
|
58 |
-
mesh_defs.append(
|
59 |
else:
|
60 |
-
disg.extend(
|
61 |
return {"genes": genes, "meshes": mesh_defs, "disgenet": disg}
|
62 |
|
63 |
-
#
|
64 |
-
#
|
65 |
-
#
|
66 |
-
async def orchestrate_search(query: str, *, llm: str =
|
67 |
-
"""Run
|
68 |
# 1) Literature --------------------------------------------------
|
69 |
-
|
70 |
-
|
71 |
-
papers
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
75 |
keywords = extract_keywords(corpus)[:8]
|
76 |
|
77 |
-
# 3)
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
asyncio.gather(*
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
trials_task,
|
91 |
)
|
92 |
|
|
|
|
|
|
|
|
|
93 |
# 4) LLM summary -------------------------------------------------
|
94 |
-
|
95 |
-
ai_summary = await
|
96 |
|
97 |
-
# 5) Assemble
|
98 |
return {
|
99 |
"papers" : papers,
|
100 |
"umls" : umls,
|
101 |
"drug_safety" : fda,
|
102 |
"ai_summary" : ai_summary,
|
103 |
"llm_used" : engine,
|
104 |
-
|
105 |
-
|
106 |
-
"
|
107 |
-
"
|
108 |
-
"gene_disease" : ncbi_data["disgenet"],
|
109 |
-
|
110 |
# trials
|
111 |
"clinical_trials": trials,
|
112 |
}
|
113 |
|
114 |
-
|
115 |
-
async def answer_ai_question(question: str, *, context: str, llm: str =
|
116 |
-
|
|
|
117 |
return {"answer": await qa_fn(question, context)}
|
|
|
1 |
#!/usr/bin/env python3
|
2 |
+
"""MedGenesis β orchestrator (v4, resilient).
|
3 |
+
|
4 |
+
* Pulls PubMed + arXiv (async)
|
5 |
+
* spaCy keyword extraction β UMLS / openFDA / DisGeNET / MeSH fanβout
|
6 |
+
* Adds MyGene.info, ClinicalTrials.gov (v2 β v1 fallback)
|
7 |
+
* Filters out failed enrichment calls (exceptions) so UI never crashes
|
8 |
+
* Summarises with OpenAI *or* Gemini; router returns `llm_used`
|
|
|
|
|
9 |
"""
|
10 |
from __future__ import annotations
|
11 |
|
12 |
import asyncio
|
13 |
from typing import Any, Dict, List
|
14 |
|
15 |
+
# ββ async fetchers ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
16 |
+
from mcp.arxiv import fetch_arxiv
|
17 |
+
from mcp.pubmed import fetch_pubmed
|
18 |
+
from mcp.nlp import extract_keywords
|
19 |
+
from mcp.umls import lookup_umls
|
20 |
+
from mcp.openfda import fetch_drug_safety
|
21 |
+
from mcp.ncbi import search_gene, get_mesh_definition
|
22 |
+
from mcp.disgenet import disease_to_genes
|
23 |
+
from mcp.mygene import fetch_gene_info
|
24 |
+
from mcp.ctgov import search_trials
|
25 |
+
|
26 |
+
# ββ LLM helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
27 |
+
from mcp.openai_utils import ai_summarize, ai_qa
|
28 |
+
from mcp.gemini import gemini_summarize, gemini_qa
|
29 |
+
|
30 |
+
# ------------------------------------------------------------------
|
31 |
# LLM router
|
32 |
+
# ------------------------------------------------------------------
|
33 |
+
_DEF = "openai"
|
34 |
|
35 |
+
def _llm_router(name: str | None):
|
36 |
+
if name and name.lower() == "gemini":
|
37 |
return gemini_summarize, gemini_qa, "gemini"
|
38 |
return ai_summarize, ai_qa, "openai"
|
39 |
|
40 |
+
# ------------------------------------------------------------------
|
41 |
+
# Keyword enrichment bundle
|
42 |
+
# ------------------------------------------------------------------
|
43 |
async def _enrich_keywords(keys: List[str]) -> Dict[str, Any]:
|
44 |
+
tasks: List[asyncio.Future] = []
|
45 |
for k in keys:
|
46 |
tasks += [search_gene(k), get_mesh_definition(k), disease_to_genes(k)]
|
47 |
|
48 |
+
res = await asyncio.gather(*tasks, return_exceptions=True)
|
49 |
|
50 |
genes, mesh_defs, disg = [], [], []
|
51 |
+
for idx, r in enumerate(res):
|
52 |
+
if isinstance(r, Exception):
|
53 |
continue
|
54 |
bucket = idx % 3
|
55 |
if bucket == 0:
|
56 |
+
genes.extend(r)
|
57 |
elif bucket == 1:
|
58 |
+
mesh_defs.append(r)
|
59 |
else:
|
60 |
+
disg.extend(r)
|
61 |
return {"genes": genes, "meshes": mesh_defs, "disgenet": disg}
|
62 |
|
63 |
+
# ------------------------------------------------------------------
|
64 |
+
# Main orchestrator
|
65 |
+
# ------------------------------------------------------------------
|
66 |
+
async def orchestrate_search(query: str, *, llm: str = _DEF) -> Dict[str, Any]:
|
67 |
+
"""Run entire async pipeline; never raises."""
|
68 |
# 1) Literature --------------------------------------------------
|
69 |
+
arxiv_f = asyncio.create_task(fetch_arxiv(query, max_results=10))
|
70 |
+
pubmed_f = asyncio.create_task(fetch_pubmed(query, max_results=10))
|
71 |
+
papers = []
|
72 |
+
for fut in await asyncio.gather(arxiv_f, pubmed_f, return_exceptions=True):
|
73 |
+
if not isinstance(fut, Exception):
|
74 |
+
papers.extend(fut)
|
75 |
+
|
76 |
+
# 2) Keywords ----------------------------------------------------
|
77 |
+
corpus = " ".join(p.get("summary", "") for p in papers)
|
78 |
keywords = extract_keywords(corpus)[:8]
|
79 |
|
80 |
+
# 3) Fanβout enrichment -----------------------------------------
|
81 |
+
umls_f = [lookup_umls(k) for k in keywords]
|
82 |
+
fda_f = [fetch_drug_safety(k) for k in keywords]
|
83 |
+
ncbi_f = asyncio.create_task(_enrich_keywords(keywords))
|
84 |
+
mygene_f = asyncio.create_task(fetch_gene_info(query))
|
85 |
+
trials_f = asyncio.create_task(search_trials(query, max_studies=20))
|
86 |
+
|
87 |
+
umls, fda, ncbi, mygene, trials = await asyncio.gather(
|
88 |
+
asyncio.gather(*umls_f, return_exceptions=True),
|
89 |
+
asyncio.gather(*fda_f, return_exceptions=True),
|
90 |
+
ncbi_f,
|
91 |
+
mygene_f,
|
92 |
+
trials_f,
|
|
|
93 |
)
|
94 |
|
95 |
+
# ββ filter exception objects -----------------------------------
|
96 |
+
umls = [u for u in umls if isinstance(u, dict)]
|
97 |
+
fda = [d for d in fda if isinstance(d, (dict, list))]
|
98 |
+
|
99 |
# 4) LLM summary -------------------------------------------------
|
100 |
+
summarize, _, engine = _llm_router(llm)
|
101 |
+
ai_summary = await summarize(corpus) if corpus else ""
|
102 |
|
103 |
+
# 5) Assemble payload -------------------------------------------
|
104 |
return {
|
105 |
"papers" : papers,
|
106 |
"umls" : umls,
|
107 |
"drug_safety" : fda,
|
108 |
"ai_summary" : ai_summary,
|
109 |
"llm_used" : engine,
|
110 |
+
# gene context
|
111 |
+
"genes" : (ncbi["genes"] or []) + ([mygene] if mygene else []),
|
112 |
+
"mesh_defs" : ncbi["meshes"],
|
113 |
+
"gene_disease" : ncbi["disgenet"],
|
|
|
|
|
114 |
# trials
|
115 |
"clinical_trials": trials,
|
116 |
}
|
117 |
|
118 |
+
# ------------------------------------------------------------------
|
119 |
+
async def answer_ai_question(question: str, *, context: str, llm: str = _DEF) -> Dict[str, str]:
|
120 |
+
"""Followβup QA using chosen LLM."""
|
121 |
+
_, qa_fn, _ = _llm_router(llm)
|
122 |
return {"answer": await qa_fn(question, context)}
|