Create orchestrator.py
Browse files- mcp/orchestrator.py +37 -0
mcp/orchestrator.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mcp/orchestrator.py
|
| 2 |
+
|
| 3 |
+
from mcp.arxiv import fetch_arxiv
|
| 4 |
+
from mcp.pubmed import fetch_pubmed
|
| 5 |
+
from mcp.nlp import extract_keywords
|
| 6 |
+
from mcp.umls import lookup_umls
|
| 7 |
+
from mcp.openfda import fetch_drug_safety
|
| 8 |
+
from mcp.openai_utils import ai_summarize, ai_qa
|
| 9 |
+
|
| 10 |
+
async def orchestrate_search(query: str) -> dict:
|
| 11 |
+
# Fetch results in parallel (use asyncio.gather for speed)
|
| 12 |
+
arxiv_results = await fetch_arxiv(query)
|
| 13 |
+
pubmed_results = await fetch_pubmed(query)
|
| 14 |
+
all_papers = arxiv_results + pubmed_results
|
| 15 |
+
# Semantic ranking (use OpenAI embeddings or similar)
|
| 16 |
+
# ...
|
| 17 |
+
# NLP: extract keywords/drugs
|
| 18 |
+
keywords = extract_keywords(" ".join([p['summary'] for p in all_papers]))
|
| 19 |
+
# UMLS enrichment
|
| 20 |
+
umls_results = [await lookup_umls(k) for k in keywords]
|
| 21 |
+
# Drug safety
|
| 22 |
+
drug_data = [await fetch_drug_safety(k) for k in keywords]
|
| 23 |
+
# Summarization
|
| 24 |
+
summary = await ai_summarize(" ".join([p['summary'] for p in all_papers]))
|
| 25 |
+
# Suggest reading (top links)
|
| 26 |
+
links = [p['link'] for p in all_papers[:3]]
|
| 27 |
+
return {
|
| 28 |
+
"papers": all_papers,
|
| 29 |
+
"umls": umls_results,
|
| 30 |
+
"drug_safety": drug_data,
|
| 31 |
+
"ai_summary": summary,
|
| 32 |
+
"suggested_reading": links,
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
async def answer_ai_question(question: str, context: str = "") -> dict:
|
| 36 |
+
answer = await ai_qa(question, context)
|
| 37 |
+
return {"answer": answer}
|