Spaces:
Running
Running
| """Clinical Guidelines Provider for USPSTF, CDC, and AHRQ guidelines.""" | |
| import logging | |
| from typing import List, Dict, Any, Callable | |
| import httpx | |
| from core.base_provider import BaseProvider | |
| from core.decorators import safe_json_return | |
| from providers import register_provider | |
| logger = logging.getLogger(__name__) | |
| # Evidence-based preventive care guidelines | |
| PREVENTIVE_GUIDELINES = { | |
| "diabetes_screening": [ | |
| { | |
| "title": "Type 2 Diabetes Screening", | |
| "recommendation": "Screen for prediabetes and type 2 diabetes in adults aged 35 to 70 years who have overweight or obesity", | |
| "grade": "B", | |
| "age_range": "35-70 years", | |
| "frequency": "Every 3 years if normal", | |
| "source": "USPSTF", | |
| "conditions": ["overweight", "obesity"], | |
| "citation": "USPSTF 2021" | |
| } | |
| ], | |
| "colorectal_screening": [ | |
| { | |
| "title": "Colorectal Cancer Screening", | |
| "recommendation": "Screen for colorectal cancer starting at age 45 years", | |
| "grade": "A", | |
| "age_range": "45-75 years", | |
| "frequency": "Every 10 years (colonoscopy) or annually (FIT)", | |
| "source": "USPSTF", | |
| "methods": ["Colonoscopy every 10 years", "FIT annually", "CT colonography every 5 years"], | |
| "citation": "USPSTF 2021" | |
| } | |
| ], | |
| "hypertension_screening": [ | |
| { | |
| "title": "Hypertension Screening", | |
| "recommendation": "Screen for high blood pressure in adults 18 years or older", | |
| "grade": "A", | |
| "age_range": "18+ years", | |
| "frequency": "Annually for adults 40+ or at increased risk; every 3-5 years for adults 18-39 with normal BP", | |
| "source": "USPSTF", | |
| "citation": "USPSTF 2021" | |
| } | |
| ], | |
| "cholesterol_screening": [ | |
| { | |
| "title": "Statin Use for Primary Prevention of CVD", | |
| "recommendation": "Screen for cardiovascular disease risk factors beginning at age 40", | |
| "grade": "B", | |
| "age_range": "40-75 years", | |
| "frequency": "Every 4-6 years", | |
| "source": "USPSTF", | |
| "conditions": ["1+ CVD risk factors"], | |
| "citation": "USPSTF 2016" | |
| } | |
| ] | |
| } | |
| # CDC vaccine schedule recommendations | |
| VACCINE_SCHEDULE = { | |
| "influenza": { | |
| "vaccine": "Influenza (Flu)", | |
| "recommendation": "Annual vaccination for all persons aged 6 months and older", | |
| "age_groups": ["6 months+"], | |
| "frequency": "Annually", | |
| "source": "CDC", | |
| "priority_groups": ["65+", "Pregnant", "Chronic conditions"] | |
| }, | |
| "covid19": { | |
| "vaccine": "COVID-19", | |
| "recommendation": "Vaccination recommended for all persons 6 months and older", | |
| "age_groups": ["6 months+"], | |
| "frequency": "Updated annually", | |
| "source": "CDC", | |
| "priority_groups": ["65+", "Immunocompromised", "Healthcare workers"] | |
| }, | |
| "pneumococcal": { | |
| "vaccine": "Pneumococcal (PCV/PPSV)", | |
| "recommendation": "Vaccination for adults 65 years or older and younger adults with certain conditions", | |
| "age_groups": ["65+", "19-64 with risk factors"], | |
| "frequency": "One-time or based on risk", | |
| "source": "CDC", | |
| "conditions": ["Chronic heart/lung disease", "Diabetes", "Immunocompromised"] | |
| }, | |
| "shingles": { | |
| "vaccine": "Shingles (Zoster)", | |
| "recommendation": "2-dose series for adults 50 years and older", | |
| "age_groups": ["50+"], | |
| "frequency": "2 doses, 2-6 months apart", | |
| "source": "CDC" | |
| }, | |
| "tdap": { | |
| "vaccine": "Tdap (Tetanus, Diphtheria, Pertussis)", | |
| "recommendation": "One dose of Tdap, then Td booster every 10 years", | |
| "age_groups": ["All adults"], | |
| "frequency": "Every 10 years", | |
| "source": "CDC" | |
| }, | |
| "hepatitis_b": { | |
| "vaccine": "Hepatitis B", | |
| "recommendation": "Vaccination for adults at increased risk", | |
| "age_groups": ["19-59 years (universal)", "60+ with risk factors"], | |
| "frequency": "2 or 3-dose series", | |
| "source": "CDC", | |
| "risk_factors": ["Healthcare workers", "Diabetes", "Chronic liver disease"] | |
| } | |
| } | |
| class ClinicalGuidelinesProvider(BaseProvider): | |
| """Provider for clinical guidelines and preventive care recommendations.""" | |
| def __init__(self, client: httpx.AsyncClient): | |
| super().__init__("clinical_guidelines", client) | |
| async def initialize(self) -> None: | |
| """Initialize clinical guidelines provider.""" | |
| logger.info("Clinical Guidelines provider initialized") | |
| def get_tools(self) -> List[Callable]: | |
| """Return all clinical guideline tools.""" | |
| return [ | |
| self.clinical_get_preventive_recommendations, | |
| self.clinical_get_vaccine_schedule, | |
| self.clinical_search_guidelines, | |
| self.clinical_get_lifestyle_recommendations, | |
| self.clinical_generate_care_plan, | |
| ] | |
| async def clinical_get_preventive_recommendations( | |
| self, | |
| age: int, | |
| sex: str, | |
| conditions: List[str] = None | |
| ) -> Dict[str, Any]: | |
| """ | |
| Get personalized preventive care recommendations based on USPSTF guidelines. | |
| Args: | |
| age: Patient age in years | |
| sex: Patient sex (male/female) | |
| conditions: List of existing conditions (e.g., ["diabetes", "overweight"]) | |
| Returns: | |
| Evidence-based screening and prevention recommendations with grades | |
| """ | |
| if conditions is None: | |
| conditions = [] | |
| recommendations = [] | |
| condition_lower = [c.lower() for c in conditions] | |
| # Diabetes screening | |
| if 35 <= age <= 70 and any(c in condition_lower for c in ["overweight", "obesity", "prediabetes"]): | |
| recommendations.append({ | |
| **PREVENTIVE_GUIDELINES["diabetes_screening"][0], | |
| "applicable": True, | |
| "rationale": "Age and BMI criteria met" | |
| }) | |
| # Colorectal cancer screening | |
| if 45 <= age <= 75: | |
| recommendations.append({ | |
| **PREVENTIVE_GUIDELINES["colorectal_screening"][0], | |
| "applicable": True, | |
| "rationale": "Age-based screening recommendation" | |
| }) | |
| # Hypertension screening | |
| if age >= 18: | |
| freq = "Annually" if age >= 40 else "Every 3-5 years" | |
| rec = PREVENTIVE_GUIDELINES["hypertension_screening"][0].copy() | |
| rec["frequency"] = freq | |
| rec["applicable"] = True | |
| rec["rationale"] = "Universal screening for adults" | |
| recommendations.append(rec) | |
| # Cholesterol/CVD screening | |
| if 40 <= age <= 75: | |
| recommendations.append({ | |
| **PREVENTIVE_GUIDELINES["cholesterol_screening"][0], | |
| "applicable": True, | |
| "rationale": "Age-based cardiovascular risk screening" | |
| }) | |
| # Sex-specific screenings | |
| if sex.lower() == "female": | |
| if 40 <= age <= 74: | |
| recommendations.append({ | |
| "title": "Breast Cancer Screening", | |
| "recommendation": "Biennial screening mammography for women aged 40-74", | |
| "grade": "B", | |
| "age_range": "40-74 years", | |
| "frequency": "Every 2 years", | |
| "source": "USPSTF", | |
| "applicable": True, | |
| "citation": "USPSTF 2024" | |
| }) | |
| if 21 <= age <= 65: | |
| recommendations.append({ | |
| "title": "Cervical Cancer Screening", | |
| "recommendation": "Screening every 3 years with cytology (21-29) or every 5 years with HPV testing (30-65)", | |
| "grade": "A", | |
| "age_range": "21-65 years", | |
| "frequency": "Every 3-5 years based on method", | |
| "source": "USPSTF", | |
| "applicable": True, | |
| "citation": "USPSTF 2018" | |
| }) | |
| elif sex.lower() == "male": | |
| if 55 <= age <= 80 and "smoking history" in condition_lower: | |
| recommendations.append({ | |
| "title": "Lung Cancer Screening", | |
| "recommendation": "Annual screening with low-dose CT for adults 50-80 with 20 pack-year smoking history", | |
| "grade": "B", | |
| "age_range": "50-80 years", | |
| "frequency": "Annually", | |
| "source": "USPSTF", | |
| "applicable": True, | |
| "conditions": ["20 pack-year smoking history"], | |
| "citation": "USPSTF 2021" | |
| }) | |
| return { | |
| "patient_age": age, | |
| "patient_sex": sex, | |
| "conditions": conditions, | |
| "recommendations": recommendations, | |
| "total_recommendations": len(recommendations) | |
| } | |
| async def clinical_get_vaccine_schedule( | |
| self, | |
| age: int, | |
| conditions: List[str] = None | |
| ) -> Dict[str, Any]: | |
| """ | |
| Get personalized vaccine recommendations based on CDC guidelines. | |
| Args: | |
| age: Patient age in years | |
| conditions: List of health conditions | |
| Returns: | |
| Recommended vaccines with timing and priority | |
| """ | |
| if conditions is None: | |
| conditions = [] | |
| recommended_vaccines = [] | |
| condition_lower = [c.lower() for c in conditions] | |
| # Universal vaccines | |
| recommended_vaccines.append({ | |
| **VACCINE_SCHEDULE["influenza"], | |
| "due": "Annually, preferably before flu season (Sept-Oct)", | |
| "priority": "High" if age >= 65 or len(conditions) > 0 else "Standard" | |
| }) | |
| recommended_vaccines.append({ | |
| **VACCINE_SCHEDULE["covid19"], | |
| "due": "Check for updated formulation annually", | |
| "priority": "High" if age >= 65 else "Standard" | |
| }) | |
| # Age-specific vaccines | |
| if age >= 65: | |
| recommended_vaccines.append({ | |
| **VACCINE_SCHEDULE["pneumococcal"], | |
| "due": "If not previously vaccinated", | |
| "priority": "High" | |
| }) | |
| if age >= 50: | |
| recommended_vaccines.append({ | |
| **VACCINE_SCHEDULE["shingles"], | |
| "due": "2-dose series if not previously vaccinated", | |
| "priority": "High" if age >= 60 else "Standard" | |
| }) | |
| # Condition-specific | |
| if any(c in condition_lower for c in ["diabetes", "heart disease", "lung disease", "immunocompromised"]): | |
| if age >= 19 and age < 65: | |
| recommended_vaccines.append({ | |
| **VACCINE_SCHEDULE["pneumococcal"], | |
| "due": "Based on risk factors", | |
| "priority": "High", | |
| "rationale": "Chronic medical condition" | |
| }) | |
| # Tdap (universal adult) | |
| recommended_vaccines.append({ | |
| **VACCINE_SCHEDULE["tdap"], | |
| "due": "If not received in last 10 years", | |
| "priority": "Standard" | |
| }) | |
| # Hepatitis B | |
| if 19 <= age <= 59: | |
| recommended_vaccines.append({ | |
| **VACCINE_SCHEDULE["hepatitis_b"], | |
| "due": "If not previously vaccinated", | |
| "priority": "Standard" | |
| }) | |
| return { | |
| "patient_age": age, | |
| "conditions": conditions, | |
| "recommended_vaccines": recommended_vaccines, | |
| "total_vaccines": len(recommended_vaccines) | |
| } | |
| async def clinical_search_guidelines( | |
| self, | |
| query: str, | |
| source: str = "All" | |
| ) -> Dict[str, Any]: | |
| """ | |
| Search clinical guidelines by condition or screening type. | |
| Args: | |
| query: Search term (condition, screening type, etc.) | |
| source: Filter by source (USPSTF, CDC, AHRQ, All) | |
| Returns: | |
| Matching guidelines with recommendations | |
| """ | |
| query_lower = query.lower() | |
| results = [] | |
| # Search preventive guidelines | |
| for key, guidelines in PREVENTIVE_GUIDELINES.items(): | |
| if query_lower in key: | |
| results.extend(guidelines) | |
| else: | |
| for g in guidelines: | |
| if query_lower in g.get("title", "").lower(): | |
| results.append(g) | |
| # Search vaccines | |
| for key, vaccine in VACCINE_SCHEDULE.items(): | |
| if query_lower in key or query_lower in vaccine.get("vaccine", "").lower(): | |
| results.append({ | |
| "title": vaccine["vaccine"], | |
| "recommendation": vaccine["recommendation"], | |
| "source": vaccine["source"], | |
| "type": "Immunization", | |
| "age_groups": vaccine.get("age_groups", []), | |
| "frequency": vaccine.get("frequency", "") | |
| }) | |
| # Filter by source if specified | |
| if source != "All": | |
| results = [r for r in results if r.get("source", "").upper() == source.upper()] | |
| return { | |
| "query": query, | |
| "source_filter": source, | |
| "results": results, | |
| "total": len(results) | |
| } | |
| async def clinical_get_lifestyle_recommendations( | |
| self, | |
| conditions: List[str], | |
| age: int | |
| ) -> Dict[str, Any]: | |
| """ | |
| Get evidence-based lifestyle and wellness recommendations. | |
| Args: | |
| conditions: List of health conditions | |
| age: Patient age | |
| Returns: | |
| Lifestyle modification recommendations | |
| """ | |
| if conditions is None: | |
| conditions = [] | |
| recommendations = [] | |
| condition_lower = [c.lower() for c in conditions] | |
| # Universal recommendations | |
| recommendations.append({ | |
| "category": "Physical Activity", | |
| "recommendation": "At least 150 minutes of moderate-intensity aerobic activity per week", | |
| "source": "CDC Physical Activity Guidelines", | |
| "evidence_grade": "A" | |
| }) | |
| recommendations.append({ | |
| "category": "Nutrition", | |
| "recommendation": "Mediterranean-style diet rich in fruits, vegetables, whole grains, and healthy fats", | |
| "source": "AHA Dietary Guidelines", | |
| "evidence_grade": "A" | |
| }) | |
| recommendations.append({ | |
| "category": "Sleep", | |
| "recommendation": "7-9 hours of sleep per night for adults", | |
| "source": "CDC Sleep Guidelines", | |
| "evidence_grade": "A" | |
| }) | |
| # Condition-specific | |
| if "diabetes" in condition_lower: | |
| recommendations.append({ | |
| "category": "Diabetes Management", | |
| "recommendation": "Monitor blood glucose regularly, aim for HbA1c <7% for most adults", | |
| "source": "ADA Standards of Care", | |
| "evidence_grade": "A", | |
| "specifics": ["Carbohydrate counting", "Regular foot examinations", "Annual eye exams"] | |
| }) | |
| if "hypertension" in condition_lower: | |
| recommendations.append({ | |
| "category": "Blood Pressure Management", | |
| "recommendation": "DASH diet, sodium restriction (<2300mg/day), regular BP monitoring", | |
| "source": "ACC/AHA Hypertension Guidelines", | |
| "evidence_grade": "A", | |
| "target": "BP <130/80 mmHg" | |
| }) | |
| if any("heart" in c or "cardiac" in c or "cardiovascular" in c for c in condition_lower): | |
| recommendations.append({ | |
| "category": "Cardiovascular Health", | |
| "recommendation": "Cardiac rehabilitation if eligible, cholesterol management, smoking cessation", | |
| "source": "AHA/ACC Guidelines", | |
| "evidence_grade": "A" | |
| }) | |
| if "obesity" in condition_lower or "overweight" in condition_lower: | |
| recommendations.append({ | |
| "category": "Weight Management", | |
| "recommendation": "5-10% weight loss through caloric reduction and increased physical activity", | |
| "source": "USPSTF Obesity Guidelines", | |
| "evidence_grade": "B" | |
| }) | |
| return { | |
| "conditions": conditions, | |
| "patient_age": age, | |
| "recommendations": recommendations, | |
| "total": len(recommendations) | |
| } | |
| async def clinical_generate_care_plan( | |
| self, | |
| age: int, | |
| sex: str, | |
| conditions: List[str] = None, | |
| current_medications: List[str] = None | |
| ) -> Dict[str, Any]: | |
| """ | |
| Generate comprehensive 6-month care plan with screening, vaccines, and lifestyle. | |
| Args: | |
| age: Patient age | |
| sex: Patient sex | |
| conditions: Existing conditions | |
| current_medications: Current medication list | |
| Returns: | |
| Structured 6-month care plan | |
| """ | |
| if conditions is None: | |
| conditions = [] | |
| if current_medications is None: | |
| current_medications = [] | |
| # Get all recommendation types | |
| preventive = await self.clinical_get_preventive_recommendations(age, sex, conditions) | |
| vaccines = await self.clinical_get_vaccine_schedule(age, conditions) | |
| lifestyle = await self.clinical_get_lifestyle_recommendations(conditions, age) | |
| # Build timeline | |
| care_plan = { | |
| "patient_profile": { | |
| "age": age, | |
| "sex": sex, | |
| "conditions": conditions, | |
| "current_medications": current_medications | |
| }, | |
| "preventive_screenings": preventive.get("recommendations", []), | |
| "immunizations": vaccines.get("recommended_vaccines", []), | |
| "lifestyle_modifications": lifestyle.get("recommendations", []), | |
| "timeline": { | |
| "month_1": ["Schedule overdue screenings", "Update immunizations"], | |
| "month_2": ["Follow up on screening results", "Begin lifestyle modifications"], | |
| "month_3": ["Medication review with provider", "Assess lifestyle progress"], | |
| "month_6": ["Comprehensive health review", "Update care plan"] | |
| }, | |
| "follow_up_schedule": { | |
| "primary_care": "Every 3-6 months" if conditions else "Annually", | |
| "specialist": "As indicated by conditions" if conditions else "Not required", | |
| "lab_work": "Per screening recommendations" | |
| } | |
| } | |
| return care_plan |