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

Update modules/orchestrator.py

Browse files
Files changed (1) hide show
  1. modules/orchestrator.py +94 -144
modules/orchestrator.py CHANGED
@@ -1,158 +1,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.1)
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
- # (This helper function remains unchanged)
26
- def _format_api_data_for_prompt(api_results: dict) -> dict[str, str]:
27
- formatted_strings = {}
28
- pubmed_data = api_results.get('pubmed', [])
29
- if isinstance(pubmed_data, list) and pubmed_data:
30
- lines = [f"- Title: {a.get('title', 'N/A')} (Journal: {a.get('journal', 'N/A')}, URL: {a.get('url')})" for a in pubmed_data]
31
- formatted_strings['pubmed'] = "\n".join(lines)
32
- else:
33
- formatted_strings['pubmed'] = "No relevant review articles were found on PubMed for this query."
34
- trials_data = api_results.get('trials', [])
35
- if isinstance(trials_data, list) and trials_data:
36
- lines = [f"- Title: {t.get('title', 'N/A')} (Status: {t.get('status', 'N/A')}, URL: {t.get('url')})" for t in trials_data]
37
- formatted_strings['trials'] = "\n".join(lines)
38
- else:
39
- formatted_strings['trials'] = "No actively recruiting clinical trials were found matching this query."
40
- fda_data = api_results.get('openfda', [])
41
- if isinstance(fda_data, list):
42
- all_events = list(chain.from_iterable(filter(None, fda_data)))
43
- if all_events:
44
- lines = [f"- {evt['term']} (Reported {evt['count']} times)" for evt in all_events]
45
- formatted_strings['openfda'] = "\n".join(lines)
46
- else:
47
- formatted_strings['openfda'] = "No specific adverse event data was found for this query."
48
- else:
49
- formatted_strings['openfda'] = "No specific adverse event data was found for this query."
50
- vision_data = api_results.get('vision', "")
51
- if isinstance(vision_data, str) and vision_data:
52
- formatted_strings['vision'] = vision_data
53
- elif isinstance(vision_data, Exception):
54
- formatted_strings['vision'] = f"An error occurred during image analysis: {vision_data}"
55
- else:
56
- formatted_strings['vision'] = ""
57
- return formatted_strings
58
-
59
-
60
- # --- FEATURE 1: Symptom Synthesizer Pipeline (v1.1) ---
61
-
62
- async def run_symptom_synthesis(user_query: str, image_input: Image.Image | None) -> str:
63
- """The complete, asynchronous pipeline for the Symptom Synthesizer tab."""
64
- if not user_query:
65
- return "Please enter a symptom description or a medical question to begin."
66
 
67
- # ==============================================================================
68
- # STEP 1 (V1.1 UPGRADE): AI-Powered Query Correction (The "Medical Translator")
69
- # ==============================================================================
70
- correction_prompt = prompts.get_query_correction_prompt(user_query)
71
- corrected_query = await gemini_handler.generate_text_response(correction_prompt)
72
- if not corrected_query:
73
- corrected_query = user_query # Fallback to original query if correction fails
 
 
 
 
74
 
75
- # ==============================================================================
76
- # STEP 2: AI-Powered Concept Extraction (now on the CLEANED query)
77
- # ==============================================================================
78
- term_prompt = prompts.get_term_extraction_prompt(corrected_query)
79
- concepts_str = await gemini_handler.generate_text_response(term_prompt)
80
- concepts = utils.safe_literal_eval(concepts_str)
81
- if not isinstance(concepts, list) or not concepts:
82
- concepts = [corrected_query] # Fallback if parsing fails
83
 
84
- # Use "OR" for a broader, more inclusive search across APIs
85
- search_query = " OR ".join(f'"{c}"' for c in concepts)
86
 
 
 
 
 
 
87
  # ==============================================================================
88
- # STEP 3: Massively Parallel Evidence Gathering
 
89
  # ==============================================================================
90
- async with aiohttp.ClientSession() as session:
91
- tasks = {
92
- "pubmed": pubmed_client.search_pubmed(session, search_query, max_results=3),
93
- "trials": clinicaltrials_client.find_trials(session, search_query, max_results=3),
94
- "openfda": asyncio.gather(*(openfda_client.get_adverse_events(session, c, top_n=3) for c in concepts)),
95
- }
96
- if image_input:
97
- tasks["vision"] = gemini_handler.analyze_image_with_text(
98
- "In the context of the user query, analyze this image objectively. Describe visual features like color, shape, texture, and patterns. Do not diagnose or offer medical advice.", image_input
99
- )
100
- raw_results = await asyncio.gather(*tasks.values(), return_exceptions=True)
101
- api_data = dict(zip(tasks.keys(), raw_results))
102
 
103
- # ==============================================================================
104
- # STEP 4: Data Formatting
105
- # ==============================================================================
106
- formatted_data = _format_api_data_for_prompt(api_data)
107
 
108
- # ==============================================================================
109
- # STEP 5: The Grand Synthesis
110
- # ==============================================================================
111
- synthesis_prompt = prompts.get_synthesis_prompt(
112
- user_query=user_query, # Pass original query for context
113
- concepts=concepts,
114
- pubmed_data=formatted_data['pubmed'],
115
- trials_data=formatted_data['trials'],
116
- fda_data=formatted_data['openfda'],
117
- vision_analysis=formatted_data['vision']
118
- )
119
- final_report = await gemini_handler.generate_text_response(synthesis_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
- # ==============================================================================
122
- # STEP 6: Final Delivery
123
- # ==============================================================================
124
- return f"{prompts.DISCLAIMER}\n\n{final_report}"
125
-
126
-
127
- # --- FEATURE 2: Drug Interaction & Safety Analyzer Pipeline ---
128
- # (This function remains unchanged)
129
- async def run_drug_interaction_analysis(drug_list_str: str) -> str:
130
- """The complete, asynchronous pipeline for the Drug Interaction Analyzer tab."""
131
- if not drug_list_str:
132
- return "Please enter a comma-separated list of medications."
133
- drug_names = [name.strip() for name in drug_list_str.split(',') if name.strip()]
134
- if len(drug_names) < 2:
135
- return "Please enter at least two medications to check for interactions."
136
- async with aiohttp.ClientSession() as session:
137
- tasks = {
138
- "interactions": rxnorm_client.run_interaction_check(drug_names),
139
- "safety_profiles": asyncio.gather(*(openfda_client.get_safety_profile(session, name) for name in drug_names))
140
- }
141
- raw_results = await asyncio.gather(*tasks.values(), return_exceptions=True)
142
- api_data = dict(zip(tasks.keys(), raw_results))
143
- interaction_data = api_data.get('interactions', [])
144
- if isinstance(interaction_data, Exception):
145
- interaction_data = [{"error": str(interaction_data)}]
146
- safety_profiles = api_data.get('safety_profiles', [])
147
- if isinstance(safety_profiles, Exception):
148
- safety_profiles = [{"error": str(safety_profiles)}]
149
- safety_data_dict = dict(zip(drug_names, safety_profiles))
150
- interaction_formatted = utils.format_list_as_markdown([str(i) for i in interaction_data]) if interaction_data else "No interactions found."
151
- safety_formatted = "\n".join([f"Profile for {drug}: {profile}" for drug, profile in safety_data_dict.items()])
152
- synthesis_prompt = prompts.get_drug_interaction_synthesis_prompt(
153
- drug_names=drug_names,
154
- interaction_data=interaction_formatted,
155
- safety_data=safety_formatted
156
- )
157
- final_report = await gemini_handler.generate_text_response(synthesis_prompt)
158
- return f"{prompts.DISCLAIMER}\n\n{final_report}"
 
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
+ """