Update mcp/orchestrator.py
Browse files- mcp/orchestrator.py +120 -154
mcp/orchestrator.py
CHANGED
|
@@ -1,161 +1,127 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
from mcp.
|
| 12 |
-
from mcp.
|
| 13 |
-
from mcp.
|
| 14 |
-
from mcp.
|
| 15 |
-
from mcp.
|
| 16 |
-
from mcp.
|
| 17 |
-
from mcp.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
llm: Literal['openai', 'gemini'] = 'openai',
|
| 64 |
-
max_papers: int = 7,
|
| 65 |
-
max_trials: int = 10,
|
| 66 |
-
) -> Dict[str, Any]:
|
| 67 |
-
"""
|
| 68 |
-
Perform a comprehensive biomedical search pipeline with fault tolerance:
|
| 69 |
-
- Extract UMLS concepts and fetch definitions
|
| 70 |
-
- Literature (PubMed + arXiv)
|
| 71 |
-
- Drug safety, gene & variant info, disease-gene mapping
|
| 72 |
-
- Clinical trials, cBioPortal data
|
| 73 |
-
- AI-driven summary
|
| 74 |
-
|
| 75 |
-
Returns a dict with structured results ready for UI/graph building.
|
| 76 |
-
"""
|
| 77 |
-
# 1) Extract concepts and perform UMLS lookups
|
| 78 |
-
raw_concepts = await asyncio.to_thread(extract_umls_concepts, query)
|
| 79 |
-
umls_tasks = [
|
| 80 |
-
asyncio.create_task(
|
| 81 |
-
_safe_call(
|
| 82 |
-
lookup_umls,
|
| 83 |
-
term,
|
| 84 |
-
default={
|
| 85 |
-
'term': term,
|
| 86 |
-
'cui': None,
|
| 87 |
-
'name': None,
|
| 88 |
-
'definition': None,
|
| 89 |
-
},
|
| 90 |
-
)
|
| 91 |
-
)
|
| 92 |
-
for term in raw_concepts
|
| 93 |
-
]
|
| 94 |
-
|
| 95 |
-
# 2) Launch parallel data-fetch tasks (excluding UMLS)
|
| 96 |
-
tasks = {
|
| 97 |
-
'pubmed': asyncio.create_task(fetch_pubmed(query, max_results=max_papers)),
|
| 98 |
-
'arxiv': asyncio.create_task(fetch_arxiv(query, max_results=max_papers)),
|
| 99 |
-
'drug_safety': asyncio.create_task(_safe_call(fetch_drug_safety, query, default=[])),
|
| 100 |
-
'ncbi_gene': asyncio.create_task(_safe_call(search_gene, query, default=[])),
|
| 101 |
-
'mygene': asyncio.create_task(_safe_call(fetch_gene_info, query, default=[])),
|
| 102 |
-
'ensembl': asyncio.create_task(_safe_call(fetch_ensembl, query, default=[])),
|
| 103 |
-
'opentargets': asyncio.create_task(_safe_call(fetch_ot, query, default=[])),
|
| 104 |
-
'mesh': asyncio.create_task(_safe_call(get_mesh_definition, query, default="")),
|
| 105 |
-
'trials': asyncio.create_task(_safe_call(search_trials, query, default=[], max_studies=max_trials)),
|
| 106 |
-
'cbio': asyncio.create_task(_safe_call(fetch_cbio, query, default=[])),
|
| 107 |
-
'disgenet': asyncio.create_task(_safe_call(disease_to_genes, query, default=[])),
|
| 108 |
}
|
| 109 |
|
| 110 |
-
# 3) Await all tasks
|
| 111 |
-
results = await _gather_tasks(list(tasks.values()))
|
| 112 |
-
data = dict(zip(tasks.keys(), results))
|
| 113 |
-
umls_results = await asyncio.gather(*umls_tasks)
|
| 114 |
-
|
| 115 |
-
# 4) Consolidate gene sources
|
| 116 |
-
gene_sources = [data['ncbi_gene'], data['mygene'], data['ensembl'], data['opentargets']]
|
| 117 |
-
genes = _flatten_unique(gene_sources)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
#
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
return {
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
}
|
| 143 |
|
| 144 |
-
|
| 145 |
-
async def answer_ai_question(
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
"""
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
return {'answer': answer}
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
MedGenesis β dual-LLM orchestrator
|
| 3 |
+
----------------------------------
|
| 4 |
+
β’ Accepts llm = "openai" | "gemini" (falls back to OpenAI)
|
| 5 |
+
β’ Returns one unified dict the UI can rely on.
|
| 6 |
+
"""
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
import asyncio, itertools, logging
|
| 9 |
+
from typing import Dict, Any, List, Tuple
|
| 10 |
+
|
| 11 |
+
from mcp.arxiv import fetch_arxiv
|
| 12 |
+
from mcp.pubmed import fetch_pubmed
|
| 13 |
+
from mcp.ncbi import search_gene, get_mesh_definition
|
| 14 |
+
from mcp.mygene import fetch_gene_info
|
| 15 |
+
from mcp.ensembl import fetch_ensembl
|
| 16 |
+
from mcp.opentargets import fetch_ot
|
| 17 |
+
from mcp.umls import lookup_umls
|
| 18 |
+
from mcp.openfda import fetch_drug_safety
|
| 19 |
+
from mcp.disgenet import disease_to_genes
|
| 20 |
+
from mcp.clinicaltrials import search_trials
|
| 21 |
+
from mcp.cbio import fetch_cbio
|
| 22 |
+
from mcp.openai_utils import ai_summarize, ai_qa
|
| 23 |
+
from mcp.gemini import gemini_summarize, gemini_qa
|
| 24 |
+
|
| 25 |
+
log = logging.getLogger(__name__)
|
| 26 |
+
_DEF = "openai" # default engine
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# βββββββββββββββββββββββββββββββββββ helpers βββββββββββββββββββββββββββββββββββ
|
| 30 |
+
def _llm_router(engine: str = _DEF) -> Tuple:
|
| 31 |
+
if engine.lower() == "gemini":
|
| 32 |
+
return gemini_summarize, gemini_qa, "gemini"
|
| 33 |
+
return ai_summarize, ai_qa, "openai"
|
| 34 |
+
|
| 35 |
+
async def _gather_safely(*aws, as_list: bool = True):
|
| 36 |
+
"""await gather() that converts Exception β RuntimeError placeholder"""
|
| 37 |
+
out = await asyncio.gather(*aws, return_exceptions=True)
|
| 38 |
+
if as_list:
|
| 39 |
+
# filter exceptions β keep structure but drop failures
|
| 40 |
+
return [x for x in out if not isinstance(x, Exception)]
|
| 41 |
+
return out
|
| 42 |
+
|
| 43 |
+
async def _gene_enrichment(keys: List[str]) -> Dict[str, Any]:
|
| 44 |
+
jobs = []
|
| 45 |
+
for k in keys:
|
| 46 |
+
jobs += [
|
| 47 |
+
search_gene(k), # basic gene info
|
| 48 |
+
get_mesh_definition(k), # MeSH definitions
|
| 49 |
+
fetch_gene_info(k), # MyGene
|
| 50 |
+
fetch_ensembl(k), # Ensembl x-refs
|
| 51 |
+
fetch_ot(k), # Open Targets associations
|
| 52 |
+
]
|
| 53 |
+
res = await _gather_safely(*jobs, as_list=False)
|
| 54 |
+
|
| 55 |
+
# slice & compress five-way fan-out
|
| 56 |
+
combo = lambda idx: [r for i, r in enumerate(res) if i % 5 == idx and r]
|
| 57 |
+
return {
|
| 58 |
+
"ncbi" : combo(0),
|
| 59 |
+
"mesh" : combo(1),
|
| 60 |
+
"mygene" : combo(2),
|
| 61 |
+
"ensembl" : combo(3),
|
| 62 |
+
"ot_assoc" : combo(4),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# βββββββββββββββββββββββββββββββββ orchestrator ββββββββββββββββββββββββββββββββ
|
| 67 |
+
async def orchestrate_search(query: str, *, llm: str = _DEF) -> Dict[str, Any]:
|
| 68 |
+
"""Main entry β returns dict for the Streamlit UI"""
|
| 69 |
+
# 1 Literature β run in parallel
|
| 70 |
+
arxiv_task = asyncio.create_task(fetch_arxiv(query))
|
| 71 |
+
pubmed_task = asyncio.create_task(fetch_pubmed(query))
|
| 72 |
+
papers_raw = await _gather_safely(arxiv_task, pubmed_task)
|
| 73 |
+
papers = list(itertools.chain.from_iterable(papers_raw))[:30] # keep β€30
|
| 74 |
+
|
| 75 |
+
# 2 Keyword extraction (very light β only from abstracts)
|
| 76 |
+
kws = {w for p in papers for w in (p["summary"][:500].split()) if w.isalpha()}
|
| 77 |
+
kws = list(kws)[:10] # coarse, fast -> 10 seeds
|
| 78 |
+
|
| 79 |
+
# 3 Bio-enrichment fan-out
|
| 80 |
+
umls_f = [_safe_task(lookup_umls, k) for k in kws]
|
| 81 |
+
fda_f = [_safe_task(fetch_drug_safety, k) for k in kws]
|
| 82 |
+
gene_bundle = asyncio.create_task(_gene_enrichment(kws))
|
| 83 |
+
trials_task = asyncio.create_task(search_trials(query, max_studies=20))
|
| 84 |
+
cbio_task = asyncio.create_task(fetch_cbio(kws[0] if kws else ""))
|
| 85 |
+
|
| 86 |
+
umls, fda, gene_dat, trials, variants = await asyncio.gather(
|
| 87 |
+
_gather_safely(*umls_f),
|
| 88 |
+
_gather_safely(*fda_f),
|
| 89 |
+
gene_bundle,
|
| 90 |
+
trials_task,
|
| 91 |
+
cbio_task,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# 4 LLM summary
|
| 95 |
+
summarise_fn, _, engine = _llm_router(llm)
|
| 96 |
+
summary = await summarise_fn(" ".join(p["summary"] for p in papers)[:12000])
|
| 97 |
|
| 98 |
return {
|
| 99 |
+
"papers" : papers,
|
| 100 |
+
"umls" : umls,
|
| 101 |
+
"drug_safety" : fda,
|
| 102 |
+
"ai_summary" : summary,
|
| 103 |
+
"llm_used" : engine,
|
| 104 |
+
"genes" : gene_dat["ncbi"] + gene_dat["ensembl"] + gene_dat["mygene"],
|
| 105 |
+
"mesh_defs" : gene_dat["mesh"],
|
| 106 |
+
"gene_disease" : gene_dat["ot_assoc"],
|
| 107 |
+
"clinical_trials" : trials,
|
| 108 |
+
"variants" : variants or [],
|
| 109 |
}
|
| 110 |
|
| 111 |
+
# βββββββββββββββββββββββββββββββ follow-up QA βββββββββββββββββββββββββββββββββ
|
| 112 |
+
async def answer_ai_question(question: str, *, context: str, llm: str = _DEF) -> Dict[str, str]:
|
| 113 |
+
"""Follow-up QA using chosen LLM."""
|
| 114 |
+
_, qa_fn, _ = _llm_router(llm)
|
| 115 |
+
return {"answer": await qa_fn(f"Q: {question}\nContext: {context}\nA:")}
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# βββββββββββββββββββββββββββ internal util βββββββββββββββββββββββββββββββββββ
|
| 119 |
+
def _safe_task(fn, *args):
|
| 120 |
+
"""Helper to wrap callable β Task returning RuntimeError on exception."""
|
| 121 |
+
async def _wrapper():
|
| 122 |
+
try:
|
| 123 |
+
return await fn(*args)
|
| 124 |
+
except Exception as exc:
|
| 125 |
+
log.warning("background task %s failed: %s", fn.__name__, exc)
|
| 126 |
+
return RuntimeError(str(exc))
|
| 127 |
+
return asyncio.create_task(_wrapper())
|
|
|