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.","Follow-up in 3–7 days."] if any(k in text for k in ["bleed","period","aub","spotting"]): assessment.append("Abnormal uterine bleeding — consider structural vs hormonal causes.") plan.append("Pregnancy test if indicated; CBC; consider pelvic ultrasound.") if any(k in text for k in ["pelvic pain","cramp","lower abdominal pain"]): assessment.append("Pelvic pain — consider dysmenorrhea, ovarian cyst, endometriosis, or infection.") plan.append("Trial NSAIDs; pelvic exam and ultrasound if persistent or severe.") if not assessment: assessment.append("Non-specific gynae symptoms — conservative management and targeted testing.") citations = [it.get("title","") for it in retrieved] return {"subjective": narrative,"objective":"Vitals stable","assessment":assessment,"plan":plan,"red_flags":COMMON_RED_FLAGS,"follow_up":"3–7 days","citations":citations}