mgbam commited on
Commit
4428435
·
verified ·
1 Parent(s): f869df3

Update modules/orchestrator.py

Browse files
Files changed (1) hide show
  1. modules/orchestrator.py +145 -92
modules/orchestrator.py CHANGED
@@ -1,108 +1,161 @@
1
- # modules/prompts.py
2
  """
3
- Central repository for all Gemini prompt engineering.
4
- This is the "soul" of the AI, defining its persona, tasks, and output structure.
5
- (v1.2 - The "Insight Engine" Upgrade)
 
 
6
  """
7
 
8
- # The non-negotiable disclaimer that precedes every major output.
9
- DISCLAIMER = (
10
- "**⚠️ IMPORTANT DISCLAIMER: This is an AI-powered informational tool and NOT a substitute for professional medical advice.** "
11
- "The information provided is for educational and research purposes only. "
12
- "It is generated by synthesizing publicly available data and may contain inaccuracies or be incomplete. "
13
- "**ALWAYS consult a qualified healthcare professional for diagnosis, treatment, or any medical concerns.** "
14
- "Never disregard professional medical advice because of something you have read here."
 
 
 
 
 
15
  )
16
 
17
-
18
- # (This function remains the same)
19
- def get_query_correction_prompt(user_text: str) -> str:
20
- return f"""
21
- You are an expert medical transcriptionist. Your task is to correct and clarify the following user query for a medical database search.
22
- - Correct all spelling and grammatical errors.
23
- - Translate colloquialisms or typos into proper medical terminology (e.g., "pin" -> "pain", "abdomian" -> "abdomen").
24
- - Rephrase as a clear statement or question.
25
- - Do not answer the question. Only return the corrected and clarified query.
26
- User Query: "{user_text}"
27
- Response:
28
  """
29
-
30
- # (This function remains the same)
31
- def get_term_extraction_prompt(user_text: str) -> str:
32
- return f"""
33
- From the user's corrected query below, extract the most relevant medical concepts, symptoms, or conditions.
34
- Return ONLY a Python-style list of strings.
35
- User Text: "{user_text}"
36
- Response:
37
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
  # ==============================================================================
41
- # V1.2 UPGRADE: The Symptom Synthesis prompt is now a "Narrative Briefing"
 
42
  # ==============================================================================
43
- def get_synthesis_prompt(user_query: str, concepts: list, pubmed_data: str, trials_data: str, fda_data: str, vision_analysis: str = "") -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- # ==============================================================================
46
- # CORRECTED LINE: The f-string logic is now handled correctly.
47
- vision_section = f"## Analysis of Uploaded Image\n{vision_analysis}" if vision_analysis else ""
48
- # ==============================================================================
49
-
50
- return f"""
51
- You are Asclepius, an expert medical information analyst. Your task is to transform raw medical data into a coherent, insightful, and beautifully formatted narrative report for a user.
52
-
53
- **YOUR DIRECTIVES:**
54
- 1. **START IMMEDIATELY with the provided mandatory disclaimer.** DO NOT add any other preamble, introduction, or disclaimer of your own. Your response must begin with "⚠️ IMPORTANT DISCLAIMER...".
55
- 2. **WRITE A NARRATIVE, NOT A LIST.** Do not use "1.", "2.", "3." to structure the main report. Use Markdown headings (##) for each section.
56
- 3. **SYNTHESIZE, DON'T JUST LIST.** For each section, provide a short introductory sentence that gives context, then present the data.
57
- 4. **BE HELPFUL WHEN DATA IS EMPTY.** If a data source is empty, state that no specific data was found and then provide a brief, high-level overview of the concept from your general knowledge.
58
-
59
- **REPORT STRUCTURE:**
60
-
61
- ## Overview
62
- (Start with a short, empathetic paragraph acknowledging the user's query about "{user_query}" and explaining that you have searched public health databases for information on the interpreted concepts: {concepts}.)
63
-
64
- ## Insights from Medical Research
65
- (Introduce this section by explaining you've looked for recent review articles on PubMed. Then, summarize the findings or state that none were found and give a general overview.)
66
- {pubmed_data if pubmed_data else "No specific review articles were found on PubMed for this query."}
67
-
68
- ## Current Clinical Trials
69
- (Introduce this section by explaining these are active studies from ClinicalTrials.gov. Then, list the trials or state that none were found.)
70
- {trials_data if trials_data else "No actively recruiting clinical trials were found matching this query."}
71
-
72
- ## Related Drug & Safety Data
73
- (Introduce this section by explaining this data comes from OpenFDA. Then, list the findings or state that none were found.)
74
- {fda_data if fda_data else "No specific adverse event data was found for this query."}
75
-
76
- {vision_section}
77
-
78
- **Begin your report now. Adhere strictly to these directives.**
79
- """
80
 
81
 
82
  # ==============================================================================
83
- # V1.2 UPGRADE: The Drug Interaction prompt is now an "Executive Safety Briefing"
 
84
  # ==============================================================================
85
- def get_drug_interaction_synthesis_prompt(drug_names: list[str], interaction_data: str, safety_data: str) -> str:
86
- return f"""
87
- You are a specialist AI focused on drug safety analysis. Your task is to act as a clear, cautious, and organized pharmacist, explaining raw API data to a user.
88
-
89
- **YOUR DIRECTIVES:**
90
- 1. **START IMMEDIATELY with the provided mandatory disclaimer.** DO NOT add any other preamble, introduction, or second disclaimer.
91
- 2. **WRITE A HUMAN-READABLE BRIEFING.** Do not use sterile numbering ("1.", "2.", "3."). Use descriptive Markdown headings (##).
92
- 3. **PROVIDE CONTEXT AND INSIGHT.** Your job is to explain what the data *means* in simple terms.
93
-
94
- **BRIEFING STRUCTURE:**
95
-
96
- ## Executive Summary
97
- (Write a concise, 1-2 sentence summary of the most important findings. For example: "A review of {', '.join(drug_names)} found no direct drug-drug interactions, but did identify several commonly reported side effects for each medication." or "A potentially significant interaction was identified between Drug A and Drug B. Details are provided below.")
98
-
99
- ## Drug-Drug Interaction Analysis
100
- (If interactions exist, list them here. For each interaction, **explain the consequence in simple terms.** For example: "Taking these together may increase the risk of...". If none, state clearly: "No direct drug-drug interactions were found among the provided list of medications based on the data available.")
101
- {interaction_data if interaction_data else "No direct drug-drug interactions were found."}
102
-
103
- ## Individual Drug Safety Profiles
104
- (Create a subsection for each drug using `### Drug Name`. Under each, summarize the data found in a user-friendly way.)
105
- {safety_data if safety_data else "No individual safety profiles were found."}
106
-
107
- **Begin your safety briefing now. Adhere strictly to these directives.**
108
- """
 
 
 
 
 
 
 
1
+ # modules/orchestrator.py
2
  """
3
+ The Central Nervous System of Project Asclepius.
4
+ This module is the master conductor, orchestrating high-performance, asynchronous
5
+ workflows for each of the application's features. It intelligently sequences
6
+ calls to API clients and the Gemini handler to transform user queries into
7
+ comprehensive, synthesized reports. (v1.2)
8
  """
9
 
10
+ import asyncio
11
+ import aiohttp
12
+ from itertools import chain
13
+ from PIL import Image
14
+
15
+ # Import all our specialized tools
16
+ from . import gemini_handler, prompts, utils
17
+ from api_clients import (
18
+ pubmed_client,
19
+ clinicaltrials_client,
20
+ openfda_client,
21
+ rxnorm_client
22
  )
23
 
24
+ # --- Internal Helper for Data Formatting ---
25
+ def _format_api_data_for_prompt(api_results: dict) -> dict[str, str]:
 
 
 
 
 
 
 
 
 
26
  """
27
+ Takes the raw dictionary of API results and formats each entry into a
28
+ clean, readable string suitable for injection into a Gemini prompt.
 
 
 
 
 
 
29
  """
30
+ formatted_strings = {}
31
+
32
+ # Format PubMed data
33
+ pubmed_data = api_results.get('pubmed', [])
34
+ if isinstance(pubmed_data, list) and pubmed_data:
35
+ lines = [f"- Title: {a.get('title', 'N/A')} (Journal: {a.get('journal', 'N/A')}, URL: {a.get('url')})" for a in pubmed_data]
36
+ formatted_strings['pubmed'] = "\n".join(lines)
37
+ else:
38
+ formatted_strings['pubmed'] = "No relevant review articles were found on PubMed for this query."
39
+
40
+ # Format Clinical Trials data
41
+ trials_data = api_results.get('trials', [])
42
+ if isinstance(trials_data, list) and trials_data:
43
+ lines = [f"- Title: {t.get('title', 'N/A')} (Status: {t.get('status', 'N/A')}, URL: {t.get('url')})" for t in trials_data]
44
+ formatted_strings['trials'] = "\n".join(lines)
45
+ else:
46
+ formatted_strings['trials'] = "No actively recruiting clinical trials were found matching this query."
47
+
48
+ # Format OpenFDA Adverse Events data
49
+ fda_data = api_results.get('openfda', [])
50
+ if isinstance(fda_data, list):
51
+ all_events = list(chain.from_iterable(filter(None, fda_data)))
52
+ if all_events:
53
+ lines = [f"- {evt['term']} (Reported {evt['count']} times)" for evt in all_events]
54
+ formatted_strings['openfda'] = "\n".join(lines)
55
+ else:
56
+ formatted_strings['openfda'] = "No specific adverse event data was found for this query."
57
+ else:
58
+ formatted_strings['openfda'] = "No specific adverse event data was found for this query."
59
+
60
+ # Format Vision analysis
61
+ vision_data = api_results.get('vision', "")
62
+ if isinstance(vision_data, str) and vision_data:
63
+ formatted_strings['vision'] = vision_data
64
+ elif isinstance(vision_data, Exception):
65
+ formatted_strings['vision'] = f"An error occurred during image analysis: {vision_data}"
66
+ else:
67
+ formatted_strings['vision'] = ""
68
+
69
+ return formatted_strings
70
 
71
 
72
  # ==============================================================================
73
+ # THIS IS THE FUNCTION THAT WAS REPORTED AS MISSING. PLEASE ENSURE IT EXISTS.
74
+ # --- FEATURE 1: Symptom Synthesizer Pipeline (v1.2) ---
75
  # ==============================================================================
76
+ async def run_symptom_synthesis(user_query: str, image_input: Image.Image | None) -> str:
77
+ """The complete, asynchronous pipeline for the Symptom Synthesizer tab."""
78
+ if not user_query:
79
+ return "Please enter a symptom description or a medical question to begin."
80
+
81
+ # STEP 1: AI-Powered Query Correction
82
+ correction_prompt = prompts.get_query_correction_prompt(user_query)
83
+ corrected_query = await gemini_handler.generate_text_response(correction_prompt)
84
+ if not corrected_query:
85
+ corrected_query = user_query
86
+
87
+ # STEP 2: AI-Powered Concept Extraction
88
+ term_prompt = prompts.get_term_extraction_prompt(corrected_query)
89
+ concepts_str = await gemini_handler.generate_text_response(term_prompt)
90
+ concepts = utils.safe_literal_eval(concepts_str)
91
+ if not isinstance(concepts, list) or not concepts:
92
+ concepts = [corrected_query]
93
+
94
+ search_query = " OR ".join(f'"{c}"' for c in concepts)
95
+
96
+ # STEP 3: Massively Parallel Evidence Gathering
97
+ async with aiohttp.ClientSession() as session:
98
+ tasks = {
99
+ "pubmed": pubmed_client.search_pubmed(session, search_query, max_results=3),
100
+ "trials": clinicaltrials_client.find_trials(session, search_query, max_results=3),
101
+ "openfda": asyncio.gather(*(openfda_client.get_adverse_events(session, c, top_n=3) for c in concepts)),
102
+ }
103
+ if image_input:
104
+ tasks["vision"] = gemini_handler.analyze_image_with_text(
105
+ "In the context of the user query, analyze this image objectively. Describe visual features. Do not diagnose.", image_input
106
+ )
107
+ raw_results = await asyncio.gather(*tasks.values(), return_exceptions=True)
108
+ api_data = dict(zip(tasks.keys(), raw_results))
109
+
110
+ # STEP 4: Data Formatting
111
+ formatted_data = _format_api_data_for_prompt(api_data)
112
+
113
+ # STEP 5: The Grand Synthesis
114
+ synthesis_prompt = prompts.get_synthesis_prompt(
115
+ user_query=user_query,
116
+ concepts=concepts,
117
+ pubmed_data=formatted_data['pubmed'],
118
+ trials_data=formatted_data['trials'],
119
+ fda_data=formatted_data['openfda'],
120
+ vision_analysis=formatted_data['vision']
121
+ )
122
+ final_report = await gemini_handler.generate_text_response(synthesis_prompt)
123
 
124
+ # STEP 6: Final Delivery
125
+ return f"{prompts.DISCLAIMER}\n\n{final_report}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
 
128
  # ==============================================================================
129
+ # THIS FUNCTION IS ALSO REQUIRED BY APP.PY. PLEASE ENSURE IT EXISTS.
130
+ # --- FEATURE 2: Drug Interaction & Safety Analyzer Pipeline ---
131
  # ==============================================================================
132
+ async def run_drug_interaction_analysis(drug_list_str: str) -> str:
133
+ """The complete, asynchronous pipeline for the Drug Interaction Analyzer tab."""
134
+ if not drug_list_str:
135
+ return "Please enter a comma-separated list of medications."
136
+ drug_names = [name.strip() for name in drug_list_str.split(',') if name.strip()]
137
+ if len(drug_names) < 2:
138
+ return "Please enter at least two medications to check for interactions."
139
+ async with aiohttp.ClientSession() as session:
140
+ tasks = {
141
+ "interactions": rxnorm_client.run_interaction_check(drug_names),
142
+ "safety_profiles": asyncio.gather(*(openfda_client.get_safety_profile(session, name) for name in drug_names))
143
+ }
144
+ raw_results = await asyncio.gather(*tasks.values(), return_exceptions=True)
145
+ api_data = dict(zip(tasks.keys(), raw_results))
146
+ interaction_data = api_data.get('interactions', [])
147
+ if isinstance(interaction_data, Exception):
148
+ interaction_data = [{"error": str(interaction_data)}]
149
+ safety_profiles = api_data.get('safety_profiles', [])
150
+ if isinstance(safety_profiles, Exception):
151
+ safety_profiles = [{"error": str(safety_profiles)}]
152
+ safety_data_dict = dict(zip(drug_names, safety_profiles))
153
+ interaction_formatted = utils.format_list_as_markdown([str(i) for i in interaction_data]) if interaction_data else "No interactions found."
154
+ safety_formatted = "\n".join([f"Profile for {drug}: {profile}" for drug, profile in safety_data_dict.items()])
155
+ synthesis_prompt = prompts.get_drug_interaction_synthesis_prompt(
156
+ drug_names=drug_names,
157
+ interaction_data=interaction_formatted,
158
+ safety_data=safety_formatted
159
+ )
160
+ final_report = await gemini_handler.generate_text_response(synthesis_prompt)
161
+ return f"{prompts.DISCLAIMER}\n\n{final_report}"