Update mcp/orchestrator.py
Browse files- mcp/orchestrator.py +76 -23
mcp/orchestrator.py
CHANGED
@@ -1,37 +1,90 @@
|
|
1 |
# mcp/orchestrator.py
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
from
|
5 |
-
|
6 |
-
from mcp.
|
7 |
-
from mcp.
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from mcp.openai_utils import ai_summarize, ai_qa
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
pubmed_task = asyncio.create_task(fetch_pubmed(query))
|
16 |
arxiv_results, pubmed_results = await asyncio.gather(arxiv_task, pubmed_task)
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
summary = await ai_summarize(paper_text)
|
26 |
-
links
|
|
|
27 |
return {
|
28 |
-
"papers":
|
29 |
-
"umls":
|
30 |
-
"drug_safety":
|
31 |
-
"ai_summary": summary,
|
32 |
"suggested_reading": links,
|
|
|
|
|
|
|
|
|
|
|
33 |
}
|
34 |
|
35 |
-
|
|
|
|
|
36 |
answer = await ai_qa(question, context)
|
37 |
return {"answer": answer}
|
|
|
1 |
# mcp/orchestrator.py
|
2 |
+
"""
|
3 |
+
Orchestrates retrieval, enrichment, and AI synthesis for a user query.
|
4 |
+
"""
|
5 |
|
6 |
+
import asyncio
|
7 |
+
from typing import Dict, Any, List
|
8 |
+
|
9 |
+
from mcp.arxiv import fetch_arxiv
|
10 |
+
from mcp.pubmed import fetch_pubmed
|
11 |
+
from mcp.nlp import extract_keywords
|
12 |
+
from mcp.umls import lookup_umls
|
13 |
+
from mcp.openfda import fetch_drug_safety
|
14 |
+
from mcp.ncbi import search_gene, get_mesh_definition
|
15 |
+
from mcp.disgenet import disease_to_genes
|
16 |
+
from mcp.clinicaltrials import search_trials
|
17 |
from mcp.openai_utils import ai_summarize, ai_qa
|
18 |
|
19 |
+
# ---------------------------------------------------------------------
|
20 |
+
async def _gene_and_mesh_enrichment(keywords: List[str]) -> Dict[str, Any]:
|
21 |
+
"""Run NCBI and DisGeNET on keywords in parallel."""
|
22 |
+
tasks = []
|
23 |
+
for kw in keywords:
|
24 |
+
tasks.append(search_gene(kw))
|
25 |
+
tasks.append(get_mesh_definition(kw))
|
26 |
+
tasks.append(disease_to_genes(kw))
|
27 |
+
results = await asyncio.gather(*tasks, return_exceptions=True)
|
28 |
|
29 |
+
genes, meshes, disgen = [], [], []
|
30 |
+
for i, res in enumerate(results):
|
31 |
+
if isinstance(res, Exception):
|
32 |
+
continue
|
33 |
+
# Cycle: 0 gene, 1 mesh, 2 disgenet, repeat …
|
34 |
+
mod = i % 3
|
35 |
+
if mod == 0:
|
36 |
+
genes.extend(res)
|
37 |
+
elif mod == 1:
|
38 |
+
meshes.append(res)
|
39 |
+
else:
|
40 |
+
disgen.extend(res)
|
41 |
+
return {"genes": genes, "meshes": meshes, "disgenet": disgen}
|
42 |
+
|
43 |
+
# ---------------------------------------------------------------------
|
44 |
+
async def orchestrate_search(query: str) -> Dict[str, Any]:
|
45 |
+
"""Main entry—returns a rich result dict for app UI."""
|
46 |
+
# -------- literature retrieval in parallel --------
|
47 |
+
arxiv_task = asyncio.create_task(fetch_arxiv(query))
|
48 |
pubmed_task = asyncio.create_task(fetch_pubmed(query))
|
49 |
arxiv_results, pubmed_results = await asyncio.gather(arxiv_task, pubmed_task)
|
50 |
+
papers = arxiv_results + pubmed_results
|
51 |
+
|
52 |
+
# -------- keyword extraction --------
|
53 |
+
paper_text = " ".join(p["summary"] for p in papers)
|
54 |
+
keywords = extract_keywords(paper_text)[:8]
|
55 |
+
|
56 |
+
# -------- enrichment tasks in parallel --------
|
57 |
+
umls_tasks = [lookup_umls(k) for k in keywords]
|
58 |
+
fda_tasks = [fetch_drug_safety(k) for k in keywords]
|
59 |
+
enrich_task = asyncio.create_task(_gene_and_mesh_enrichment(keywords))
|
60 |
+
trials_task = asyncio.create_task(search_trials(query, max_studies=10))
|
61 |
+
|
62 |
+
umls, fda, enrich, trials = await asyncio.gather(
|
63 |
+
asyncio.gather(*umls_tasks),
|
64 |
+
asyncio.gather(*fda_tasks),
|
65 |
+
enrich_task,
|
66 |
+
trials_task,
|
67 |
+
)
|
68 |
+
|
69 |
+
# -------- AI summary --------
|
70 |
summary = await ai_summarize(paper_text)
|
71 |
+
links = [p["link"] for p in papers[:3]]
|
72 |
+
|
73 |
return {
|
74 |
+
"papers" : papers,
|
75 |
+
"umls" : umls,
|
76 |
+
"drug_safety" : fda,
|
77 |
+
"ai_summary" : summary,
|
78 |
"suggested_reading": links,
|
79 |
+
# new fields
|
80 |
+
"genes" : enrich["genes"],
|
81 |
+
"mesh_definitions": enrich["meshes"],
|
82 |
+
"gene_disease" : enrich["disgenet"],
|
83 |
+
"clinical_trials" : trials,
|
84 |
}
|
85 |
|
86 |
+
# ---------------------------------------------------------------------
|
87 |
+
async def answer_ai_question(question: str, context: str = "") -> Dict[str, str]:
|
88 |
+
"""Free-form Q&A using OpenAI."""
|
89 |
answer = await ai_qa(question, context)
|
90 |
return {"answer": answer}
|