MiniMax Agent
Deploy Enhanced Medical AI Pipeline (2,867 lines) - Transform generic responses to professional medical analysis
1d3d2ee
| """ | |
| COMPREHENSIVE MEDICAL ANALYSIS PROMPT ENGINEERING FRAMEWORK | |
| Creates meaningful clinical insights across ALL medical categories | |
| This file provides enhanced prompt templates and structured output schemas for: | |
| - Cardiology/ECG Analysis | |
| - Radiology (X-ray, CT, MRI, Ultrasound) | |
| - Laboratory Medicine | |
| - Pathology (Biopsies, Cytology) | |
| - Clinical Documentation | |
| - General Medical Analysis | |
| Each prompt includes: | |
| 1. Domain-specific clinical context | |
| 2. Structured diagnostic framework | |
| 3. Professional medical terminology | |
| 4. Evidence-based recommendations | |
| 5. Clinical correlation requirements | |
| """ | |
| import json | |
| from typing import Dict, Any, List | |
| from datetime import datetime | |
| class ComprehensiveMedicalPromptEngine: | |
| """ | |
| Enhanced medical prompt engineering for meaningful clinical insights | |
| across all medical categories | |
| """ | |
| def __init__(self): | |
| self.medical_domains = self._initialize_medical_domains() | |
| self.output_schemas = self._initialize_output_schemas() | |
| def _initialize_medical_domains(self) -> Dict[str, Dict[str, Any]]: | |
| """Initialize comprehensive medical analysis domains with enhanced prompts""" | |
| return { | |
| # ===== CARDIOLOGY/ECG ANALYSIS ===== | |
| "cardiology_ecg": { | |
| "clinical_context": """You are a board-certified cardiologist specializing in electrocardiography and cardiac electrophysiology. | |
| Provide comprehensive ECG analysis with clinical expertise.""", | |
| "prompt_template": """CLINICAL SCENARIO: Comprehensive Electrocardiogram Analysis | |
| Patient Context: {patient_info} | |
| ECG Data: {ecg_data} | |
| Provide detailed cardiac electrophysiology assessment including: | |
| 1. **RHYTHM ANALYSIS & CARDIAC RATE** | |
| - Primary rhythm identification with confidence assessment | |
| - Heart rate analysis with normal/abnormal range determination | |
| - Sinus rhythm characteristics and any arrhythmic patterns | |
| 2. **CONDUCTION SYSTEM ASSESSMENT** | |
| - P wave morphology, duration, and timing analysis | |
| - PR interval interpretation (normal, prolonged, shortened) | |
| - QRS complex analysis (duration, morphology, axis determination) | |
| - QT/QTc interval measurement with clinical significance | |
| 3. **MYOCARDIAL ISCHEMIA/INFARCTION DETECTION** | |
| - ST-segment elevation/depression analysis with lead distribution | |
| - T wave abnormalities and their clinical significance | |
| - Q wave presence indicating prior infarction territory | |
| - Pathological Q waves vs physiological variants | |
| 4. **CLINICAL CORRELATION & RECOMMENDATIONS** | |
| - Risk stratification based on ECG findings | |
| - Correlation with clinical presentation and cardiac biomarkers | |
| - Evidence-based management recommendations | |
| - Follow-up testing requirements (stress test, echo, catheterization) | |
| Provide professional cardiac electrophysiology interpretation.""", | |
| "domain_expertise": "Cardiology, Electrophysiology, Clinical Medicine", | |
| "critical_elements": ["rhythm", "intervals", "ischemia", "axis", "recommendations"] | |
| }, | |
| # ===== RADIOLOGY ANALYSIS ===== | |
| "radiology_xray": { | |
| "clinical_context": """You are a board-certified radiologist specializing in diagnostic imaging interpretation. | |
| Provide comprehensive radiological analysis with clinical expertise.""", | |
| "prompt_template": """CLINICAL SCENARIO: Comprehensive Radiological Analysis | |
| Imaging Study: {imaging_study} | |
| Patient Context: {patient_context} | |
| Radiological Data: {image_data} | |
| Provide detailed radiological interpretation including: | |
| 1. **TECHNICAL ASSESSMENT** | |
| - Imaging modality and acquisition parameters | |
| - Image quality and technical adequacy | |
| - Anatomical coverage and positioning | |
| 2. **ANATOMICAL & PATHOLOGICAL FINDINGS** | |
| - Systematic review of anatomical structures | |
| - Primary pathological findings with specific descriptions | |
| - Differential diagnosis considerations based on imaging patterns | |
| - Critical findings requiring urgent attention | |
| 3. **CLINICAL SIGNIFICANCE & INTERPRETATION** | |
| - Correlation with clinical presentation and symptoms | |
| - Severity assessment and prognostic implications | |
| - Disease progression or treatment response indicators | |
| - Comparison with prior imaging when available | |
| 4. **MANAGEMENT RECOMMENDATIONS** | |
| - Additional imaging studies if indicated | |
| - Clinical correlation requirements | |
| - Treatment implications and monitoring needs | |
| - Follow-up imaging recommendations | |
| Provide expert radiological consultation with clinical correlation.""", | |
| "domain_expertise": "Radiology, Diagnostic Imaging, Clinical Medicine", | |
| "critical_elements": ["findings", "differential", "severity", "recommendations"] | |
| }, | |
| "radiology_ct": { | |
| "clinical_context": """You are a board-certified radiologist specializing in CT imaging and cross-sectional anatomy. | |
| Provide comprehensive CT analysis with clinical expertise.""", | |
| "prompt_template": """CLINICAL SCENARIO: Comprehensive CT Scan Analysis | |
| CT Study: {ct_study} | |
| Clinical Indication: {indication} | |
| Patient Context: {patient_context} | |
| Provide detailed CT interpretation including: | |
| 1. **TECHNICAL ASSESSMENT** | |
| - CT protocol and imaging parameters | |
| - Contrast enhancement status | |
| - Image quality and diagnostic adequacy | |
| 2. **SYSTEMATIC ORGAN REVIEW** | |
| - Brain/nervous system (if head CT) | |
| - Chest (lung parenchyma, mediastinum, pleural spaces) | |
| - Abdomen/pelvis (solid organs, bowel, vasculature) | |
| - Musculoskeletal structures as applicable | |
| 3. **PATHOLOGICAL FINDINGS & INTERPRETATION** | |
| - Primary lesion characterization (size, location, enhancement) | |
| - Secondary findings and metastatic assessment | |
| - Inflammatory, infectious, or neoplastic processes | |
| - Vascular abnormalities and perfusion deficits | |
| 4. **CLINICAL CORRELATION & RECOMMENDATIONS** | |
| - Findings correlation with clinical presentation | |
| - Differential diagnosis with probability assessment | |
| - Tissue sampling recommendations if indicated | |
| - Treatment planning and monitoring protocols | |
| Provide expert CT interpretation with clinical management guidance.""", | |
| "domain_expertise": "Radiology, Cross-sectional Imaging, Clinical Medicine", | |
| "critical_elements": ["systematic_review", "pathology", "differential", "management"] | |
| }, | |
| # ===== LABORATORY MEDICINE ===== | |
| "laboratory_chemistry": { | |
| "clinical_context": """You are a board-certified clinical pathologist specializing in laboratory medicine. | |
| Provide comprehensive laboratory interpretation with clinical expertise.""", | |
| "prompt_template": """CLINICAL SCENARIO: Comprehensive Laboratory Analysis | |
| Laboratory Data: {lab_data} | |
| Patient Context: {patient_context} | |
| Clinical Indication: {indication} | |
| Provide detailed laboratory interpretation including: | |
| 1. **NORMAL & ABNORMAL VALUE ASSESSMENT** | |
| - Reference range comparison with flagging of abnormal values | |
| - Critical value identification requiring immediate attention | |
| - Trending analysis for serial measurements | |
| - Hemolysis, lipemia, or other specimen quality issues | |
| 2. **CLINICAL SIGNIFICANCE & CORRELATION** | |
| - Clinical interpretation of abnormal findings | |
| - Correlation with patient symptoms and medical history | |
| - Drug-induced laboratory abnormalities | |
| - Organ-specific findings (hepatic, renal, cardiac markers) | |
| 3. **DIAGNOSTIC & PROGNOSTIC IMPLICATIONS** | |
| - Disease diagnosis support based on laboratory patterns | |
| - Prognostic indicators and risk stratification | |
| - Treatment monitoring and therapeutic drug levels | |
| - Baseline and follow-up testing requirements | |
| 4. **RECOMMENDATIONS & CLINICAL ACTION** | |
| - Repeat testing requirements or confirmatory studies | |
| - Additional laboratory testing for diagnosis/monitoring | |
| - Clinical correlation with other diagnostic modalities | |
| - Specialist consultation recommendations | |
| Provide expert clinical pathology interpretation.""", | |
| "domain_expertise": "Laboratory Medicine, Clinical Pathology, Clinical Medicine", | |
| "critical_elements": ["reference_ranges", "abnormalities", "significance", "recommendations"] | |
| }, | |
| # ===== PATHOLOGY ANALYSIS ===== | |
| "pathology_biopsy": { | |
| "clinical_context": """You are a board-certified pathologist specializing in histopathology and molecular pathology. | |
| Provide comprehensive pathological analysis with clinical expertise.""", | |
| "prompt_template": """CLINICAL SCENARIO: Comprehensive Pathology Analysis | |
| Specimen Type: {specimen_type} | |
| Anatomical Site: {site} | |
| Clinical Information: {clinical_info} | |
| Histopathological Data: {path_data} | |
| Provide detailed pathological interpretation including: | |
| 1. **MORPHOLOGICAL ASSESSMENT** | |
| - Specimen adequacy and diagnostic quality | |
| - Histological pattern analysis and architectural features | |
| - Cellular morphology, nuclear characteristics, and cytoplasmic features | |
| - Staining characteristics and immunohistochemical profile | |
| 2. **DIAGNOSTIC INTERPRETATION** | |
| - Primary diagnosis with confidence level | |
| - Differential diagnosis considerations | |
| - Grading and staging information when applicable | |
| - Molecular/immunohistochemical markers and their significance | |
| 3. **CLINICAL CORRELATION & PROGNOSIS** | |
| - Correlation with clinical presentation and imaging findings | |
| - Prognostic factors and risk stratification | |
| - Treatment response prediction and therapeutic targets | |
| - Genetic/molecular alterations with clinical implications | |
| 4. **CLINICAL MANAGEMENT RECOMMENDATIONS** | |
| - Surgical margin assessment and adequacy | |
| - Additional staining or molecular testing recommendations | |
| - Treatment planning implications | |
| - Follow-up protocols and surveillance recommendations | |
| Provide expert pathological consultation with clinical management guidance.""", | |
| "domain_expertise": "Pathology, Histopathology, Molecular Pathology, Clinical Medicine", | |
| "critical_elements": ["morphology", "diagnosis", "prognosis", "management"] | |
| }, | |
| # ===== CLINICAL DOCUMENTATION ===== | |
| "clinical_notes": { | |
| "clinical_context": """You are an experienced clinical physician reviewing medical documentation. | |
| Provide comprehensive clinical assessment with medical expertise.""", | |
| "prompt_template": """CLINICAL SCENARIO: Clinical Documentation Review | |
| Medical Document: {document_data} | |
| Document Type: {doc_type} | |
| Patient Context: {patient_context} | |
| Provide comprehensive clinical analysis including: | |
| 1. **DOCUMENT STRUCTURE & CONTENT ASSESSMENT** | |
| - Chief complaint and presenting problem analysis | |
| - History of present illness extraction and analysis | |
| - Past medical, surgical, and social history review | |
| - Physical examination findings and documentation quality | |
| 2. **CLINICAL ASSESSMENT & DIAGNOSTIC REASONING** | |
| - Primary assessment and clinical impression extraction | |
| - Differential diagnosis considerations and reasoning | |
| - Problem prioritization and clinical decision-making | |
| - Evidence-based diagnostic approach assessment | |
| 3. **TREATMENT PLAN & MANAGEMENT** | |
| - Therapeutic interventions and medication orders | |
| - Diagnostic testing recommendations and rationale | |
| - Follow-up care and monitoring protocols | |
| - Patient education and discharge planning | |
| 4. **CLINICAL QUALITY ASSURANCE** | |
| - Documentation completeness and accuracy | |
| - Clinical reasoning adequacy and decision-making quality | |
| - Standard of care compliance and best practice adherence | |
| - Areas for improvement and education recommendations | |
| Provide professional clinical assessment and quality review.""", | |
| "domain_expertise": "Clinical Medicine, Internal Medicine, Medical Documentation", | |
| "critical_elements": ["assessment", "reasoning", "management", "quality"] | |
| }, | |
| # ===== EMERGENCY MEDICINE ===== | |
| "emergency_medicine": { | |
| "clinical_context": """You are an emergency medicine physician specializing in acute care assessment. | |
| Provide comprehensive emergency medicine evaluation with clinical expertise.""", | |
| "prompt_template": """CLINICAL SCENARIO: Emergency Medicine Assessment | |
| Emergency Presentation: {emergency_data} | |
| Patient Context: {patient_context} | |
| Clinical Scenario: {scenario} | |
| Provide comprehensive emergency medicine evaluation including: | |
| 1. **ACUTE PRESENTATION ASSESSMENT** | |
| - Chief complaint and triage priority assessment | |
| - Vital signs analysis and stability determination | |
| - Acute symptom progression and severity | |
| - Risk stratification and immediate threats | |
| 2. **EMERGENCY CLINICAL FINDINGS** | |
| - Critical diagnostic findings requiring immediate attention | |
| - Organ system dysfunction assessment | |
| - Pain assessment and management needs | |
| - Environmental and trauma considerations | |
| 3. **EMERGENCY MANAGEMENT PROTOCOL** | |
| - Immediate life-saving interventions required | |
| - Diagnostic testing priorities (CT, labs, ECG) | |
| - Specialist consultation requirements | |
| - Admission vs discharge decisions | |
| 4. **DISPOSITION & FOLLOW-UP** | |
| - Admission criteria and level of care determination | |
| - Outpatient follow-up requirements | |
| - Patient education and discharge instructions | |
| - Emergency re-evaluation triggers | |
| Provide expert emergency medicine consultation with acute care protocols.""", | |
| "domain_expertise": "Emergency Medicine, Acute Care, Critical Care", | |
| "critical_elements": ["triage", "critical_findings", "management", "disposition"] | |
| } | |
| } | |
| def _initialize_output_schemas(self) -> Dict[str, Dict[str, Any]]: | |
| """Initialize structured output schemas for each medical domain""" | |
| return { | |
| "cardiology_ecg": { | |
| "required_fields": [ | |
| "rhythm_analysis", "heart_rate", "conduction_intervals", | |
| "ischemia_findings", "clinical_significance", "recommendations" | |
| ], | |
| "output_structure": { | |
| "rhythm_analysis": "Primary rhythm identification and characteristics", | |
| "heart_rate": "Rate analysis with clinical interpretation", | |
| "conduction_intervals": "PR, QRS, QT intervals with significance", | |
| "ischemia_findings": "ST-T changes, Q waves, infarct location", | |
| "clinical_significance": "Risk assessment and correlation", | |
| "recommendations": "Evidence-based management and follow-up" | |
| } | |
| }, | |
| "radiology_xray": { | |
| "required_fields": [ | |
| "technical_assessment", "anatomical_findings", "pathological_lesions", | |
| "differential_diagnosis", "clinical_correlation", "recommendations" | |
| ], | |
| "output_structure": { | |
| "technical_assessment": "Image quality, positioning, adequacy", | |
| "anatomical_findings": "Systematic review of structures", | |
| "pathological_lesions": "Primary findings with descriptions", | |
| "differential_diagnosis": "List of possibilities with rationale", | |
| "clinical_correlation": "Symptom correlation and significance", | |
| "recommendations": "Additional imaging, treatment, follow-up" | |
| } | |
| }, | |
| "laboratory_chemistry": { | |
| "required_fields": [ | |
| "normal_values", "abnormal_values", "critical_values", | |
| "clinical_significance", "trending_analysis", "recommendations" | |
| ], | |
| "output_structure": { | |
| "normal_values": "Results within reference range", | |
| "abnormal_values": "Out of range results with interpretation", | |
| "critical_values": "Life-threatening values requiring action", | |
| "clinical_significance": "Interpretation of abnormalities", | |
| "trending_analysis": "Pattern recognition and changes", | |
| "recommendations": "Repeat testing, additional studies" | |
| } | |
| }, | |
| "pathology_biopsy": { | |
| "required_fields": [ | |
| "morphological_assessment", "diagnostic_interpretation", | |
| "grading_staging", "prognosis_factors", "treatment_implications" | |
| ], | |
| "output_structure": { | |
| "morphological_assessment": "Histological pattern and cellular features", | |
| "diagnostic_interpretation": "Primary diagnosis and differential", | |
| "grading_staging": "Severity assessment and classification", | |
| "prognosis_factors": "Risk factors and outcome prediction", | |
| "treatment_implications": "Therapeutic targets and monitoring" | |
| } | |
| }, | |
| "clinical_notes": { | |
| "required_fields": [ | |
| "chief_complaint", "assessment_plan", "clinical_reasoning", | |
| "diagnostic_approach", "treatment_recommendations", "documentation_quality" | |
| ], | |
| "output_structure": { | |
| "chief_complaint": "Primary problem and presentation", | |
| "assessment_plan": "Clinical impression and assessment", | |
| "clinical_reasoning": "Diagnostic logic and decision-making", | |
| "diagnostic_approach": "Testing and evaluation strategy", | |
| "treatment_recommendations": "Therapeutic interventions", | |
| "documentation_quality": "Completeness and accuracy assessment" | |
| } | |
| } | |
| } | |
| def generate_enhanced_prompt(self, domain: str, data: Dict[str, Any]) -> str: | |
| """Generate enhanced medical prompt for specific domain""" | |
| if domain not in self.medical_domains: | |
| return self._generate_general_medical_prompt(data) | |
| domain_config = self.medical_domains[domain] | |
| prompt_template = domain_config["prompt_template"] | |
| # Format the prompt with available data | |
| formatted_prompt = prompt_template.format(**data) | |
| # Add structured output requirements | |
| if domain in self.output_schemas: | |
| schema = self.output_schemas[domain] | |
| formatted_prompt += f""" | |
| STRUCTURED OUTPUT REQUIREMENTS: | |
| Provide your analysis in the following structured format: | |
| """ | |
| for field in schema["required_fields"]: | |
| formatted_prompt += f"• **{field.replace('_', ' ').title()}**: [Detailed analysis]\n" | |
| # Add clinical correlation requirement | |
| formatted_prompt += """ | |
| CLINICAL CORRELATION REQUIREMENTS: | |
| 1. Correlate all findings with patient presentation and medical context | |
| 2. Use professional medical terminology and clinical expertise | |
| 3. Provide evidence-based recommendations with clinical justification | |
| 4. Include risk stratification and management priorities | |
| 5. Suggest appropriate follow-up and monitoring protocols | |
| Professional medical analysis with clinical correlation required.""" | |
| return formatted_prompt | |
| def _generate_general_medical_prompt(self, data: Dict[str, Any]) -> str: | |
| """Generate general medical prompt for unspecified domains""" | |
| return f"""CLINICAL SCENARIO: Comprehensive Medical Document Analysis | |
| Medical Information: {data.get('text', 'N/A')} | |
| Provide comprehensive medical analysis including: | |
| 1. **CLINICAL FINDINGS** | |
| - Key medical findings and interpretations | |
| - Diagnostic considerations with clinical significance | |
| - Correlation with medical standards and guidelines | |
| 2. **CLINICAL ASSESSMENT** | |
| - Overall clinical impression and interpretation | |
| - Risk factors and prognostic indicators | |
| - Clinical decision-making guidance | |
| 3. **MANAGEMENT RECOMMENDATIONS** | |
| - Evidence-based treatment considerations | |
| - Follow-up protocols and monitoring requirements | |
| - Specialist consultation recommendations | |
| 4. **CLINICAL CORRELATION** | |
| - Integration with patient presentation and medical history | |
| - Quality assessment and clinical standards compliance | |
| Provide expert medical consultation with clinical correlation and professional medical interpretation.""" | |
| def get_domain_expertise(self, domain: str) -> str: | |
| """Get domain-specific medical expertise context""" | |
| return self.medical_domains.get(domain, {}).get("domain_expertise", "Clinical Medicine") | |
| def get_critical_elements(self, domain: str) -> List[str]: | |
| """Get critical analysis elements for domain""" | |
| return self.medical_domains.get(domain, {}).get("critical_elements", ["findings", "significance", "recommendations"]) | |
| # Global instance for medical analysis | |
| medical_prompt_engine = ComprehensiveMedicalPromptEngine() | |
| def generate_medical_analysis_prompt(domain: str, data: Dict[str, Any]) -> str: | |
| """Generate enhanced medical analysis prompt""" | |
| return medical_prompt_engine.generate_enhanced_prompt(domain, data) | |
| def get_medical_domain_expertise(domain: str) -> str: | |
| """Get medical domain expertise context""" | |
| return medical_prompt_engine.get_domain_expertise(domain) |