from typing import List, Dict COMMON_RED_FLAGS = ["Severe pain", "Syncope", "Heavy bleeding", "Chest pain / SOB", "Persistent fever"] def compose_soap(narrative: str, retrieved: List[Dict]) -> Dict: text = (narrative or "").lower() assessment, plan = [], ["Safety-net advice and red-flag education.", "Follow-up in 3–7 days or earlier if worse."] if any(k in text for k in ["bleed","spotting","period","menorrhagia","aub"]): assessment.append("Abnormal uterine bleeding — structural vs hormonal.") plan.append("Urine pregnancy test; CBC; pelvic ultrasound if indicated.") if any(k in text for k in ["pelvic pain","cramp","lower abdominal pain"]): assessment.append("Pelvic pain — r/o infection/cyst/endometriosis.") plan.append("Trial NSAIDs; pelvic exam/US if persistent.") if any(k in text for k in ["chest pain","shortness of breath","sob"]): assessment.append("Chest pain — consider ACS/PE; triage immediately if red flags.") plan.append("ECG, vitals; urgent review if high risk.") if any(k in text for k in ["knee pain","joint","sprain","swelling","injury"]): assessment.append("MSK complaint — ortho evaluation.") plan.append("RICE; imaging if trauma/red flags.") if not assessment: assessment.append("Non-specific symptoms — conservative management and targeted testing.") citations = [it.get("title","(untitled)") for it in retrieved] return {"subjective": narrative, "objective": "Vitals stable (PoC)", "assessment": assessment, "plan": plan, "red_flags": COMMON_RED_FLAGS, "follow_up": "3–7 days or earlier if red flags.", "citations": citations}