Files
courses/real_world_prompting/02_medical_prompt.ipynb
2024-08-13 21:05:26 -06:00

1968 lines
83 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lesson 2: A real-world prompt\n",
"\n",
"In the previous lesson, we discussed several key prompting tips and saw an example of how to use each in isolation. Let's now try writing a much larger prompt that incorporates many of the techniques we just covered.\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Our prompting goal\n",
"This lesson will focus on writing a medical record summarizer prompt that takes in long medical records and generates a summary containing important information to assist doctors in preparing for upcoming appointments."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each patient medical record looks something like this: \n",
"\n",
"```\n",
"Patient Name: Evelyn Thompson\n",
"Age: 78\n",
"Medical Record:\n",
"\n",
"1985: Diagnosed with type 2 diabetes, started on metformin\n",
"1992: Developed hypertension, prescribed lisinopril\n",
"1998: Total hip replacement (right) due to osteoarthritis\n",
"2000: Diagnosed with hypothyroidism, started on levothyroxine\n",
"2003: Cataract surgery (both eyes)\n",
"2005: Admitted for atrial fibrillation, started on warfarin\n",
"2008: Vitamin B12 deficiency diagnosed, monthly injections started\n",
"2010: Increased metformin dose due to rising A1C levels\n",
"2011: Admitted for transient ischemic attack (TIA), added aspirin to regimen\n",
"2013: Diagnosed with stage 2 breast cancer, underwent lumpectomy and radiation\n",
"2014: Started on anastrozole for breast cancer recurrence prevention\n",
"2015: Developed chronic kidney disease (CKD) stage 3, metformin adjusted\n",
"2017: Total knee replacement (left) due to osteoarthritis\n",
"2018: Hospitalized for pneumonia, treated with IV antibiotics\n",
"2019: Mild cognitive impairment noted, started on donepezil\n",
"2020: Lisinopril dosage increased due to refractory hypertension\n",
"2021: Recurrent UTIs, prescribed low-dose prophylactic antibiotics\n",
"2022: Annual mammogram clear, but eGFR shows worsening kidney function\n",
"2023: Mobility declining, started physical therapy and home health aide visits\n",
"```\n",
"\n",
"Our end goal is to generate consistent record summaries to help providers prepare for upcoming appointments. Each summary should contain key pieces of information including: \n",
"\n",
"* The patient's name\n",
"* The patient's age\n",
"* Key diagnoses\n",
"* A list of medications the patient is prescribed \n",
"* Recent concerns\n",
"* Action items for the provider\n",
"\n",
" An example output for the above medical record might look something like this: \n",
"\n",
"```\n",
"Name: Evelyn Thompson\n",
"Age: 78\n",
"\n",
"Key Diagnoses:\n",
"- Type 2 Diabetes (1985)\n",
"- Hypertension (1992)\n",
"- Osteoarthritis (Hip and Knee Replacements in 1998 and 2017)\n",
"- Hypothyroidism (2000)\n",
"- Atrial Fibrillation (2005)\n",
"- Vitamin B12 Deficiency (2008)\n",
"- Transient Ischemic Attack (TIA) (2011)\n",
"- Breast Cancer (2013)\n",
"- Chronic Kidney Disease (CKD) Stage 3 (2015)\n",
"- Pneumonia (2018)\n",
"- Mild Cognitive Impairment (2019)\n",
"- Recurrent Urinary Tract Infections (UTIs) (2021)\n",
"\n",
"Medications:\n",
"- Metformin (Diabetes)\n",
"- Lisinopril (Hypertension)\n",
"- Levothyroxine (Hypothyroidism)\n",
"- Warfarin (Atrial Fibrillation)\n",
"- Aspirin (Antiplatelet)\n",
"- Anastrozole (Breast Cancer Recurrence Prevention)\n",
"- Donepezil (Cognitive Impairment)\n",
"- Low-dose Prophylactic Antibiotics (Recurrent UTIs)\n",
"\n",
"Other Treatments:\n",
"- Total Hip Replacement (1998)\n",
"- Cataract Surgery (2003)\n",
"- Vitamin B12 Injections (2008)\n",
"- Lumpectomy and Radiation (Breast Cancer, 2013)\n",
"- Total Knee Replacement (2017)\n",
"- Physical Therapy and Home Health Aide (2023)\n",
"\n",
"Recent Concerns:\n",
"- Worsening Kidney Function (eGFR Decline in 2022)\n",
"- Declining Mobility (2023)\n",
"\n",
"Action Items:\n",
"- Monitor Kidney Function and Adjust Medications as Needed\n",
"- Continue Physical Therapy and Home Health Support\n",
"- Evaluate for Cognitive Decline and Adjust Treatment Plan\n",
"- Address Mobility Issues and Fall Risk\n",
"- Ensure Adherence to Recommended Cancer Screening\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's a Python list containing 5 medical records that we'll try our prompt with:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"patient_records = [\n",
" \"\"\"\n",
"Patient Name: Evelyn Thompson\n",
"Age: 78\n",
"Medical Record:\n",
"\n",
"1985: Diagnosed with type 2 diabetes, started on metformin\n",
"1992: Developed hypertension, prescribed lisinopril\n",
"1998: Total hip replacement (right) due to osteoarthritis\n",
"2000: Diagnosed with hypothyroidism, started on levothyroxine\n",
"2003: Cataract surgery (both eyes)\n",
"2005: Admitted for atrial fibrillation, started on warfarin\n",
"2008: Vitamin B12 deficiency diagnosed, monthly injections started\n",
"2010: Increased metformin dose due to rising A1C levels\n",
"2011: Admitted for transient ischemic attack (TIA), added aspirin to regimen\n",
"2013: Diagnosed with stage 2 breast cancer, underwent lumpectomy and radiation\n",
"2014: Started on anastrozole for breast cancer recurrence prevention\n",
"2015: Developed chronic kidney disease (CKD) stage 3, metformin adjusted\n",
"2017: Total knee replacement (left) due to osteoarthritis\n",
"2018: Hospitalized for pneumonia, treated with IV antibiotics\n",
"2019: Mild cognitive impairment noted, started on donepezil\n",
"2020: Lisinopril dosage increased due to refractory hypertension\n",
"2021: Recurrent UTIs, prescribed low-dose prophylactic antibiotics\n",
"2022: Annual mammogram clear, but eGFR shows worsening kidney function\n",
"2023: Mobility declining, started physical therapy and home health aide visits\n",
" \"\"\",\n",
" \"\"\"\n",
"Patient Name: Marcus Reyes\n",
"Age: 42\n",
"Medical Record:\n",
"\n",
"2001: Diagnosed with generalized anxiety disorder (GAD), started on paroxetine\n",
"2003: Diagnosed with major depressive disorder (MDD), added bupropion\n",
"2005: Hospitalized for suicidal ideation, added cognitive behavioral therapy (CBT)\n",
"2007: Diagnosed with attention-deficit/hyperactivity disorder (ADHD), started on methylphenidate\n",
"2009: Reported side effects from paroxetine, switched to escitalopram\n",
"2012: Diagnosed with obstructive sleep apnea (OSA), started CPAP therapy\n",
"2014: Diagnosed with hypertension, started on losartan\n",
"2015: Weight gain noted, referred to nutritionist\n",
"2016: Diagnosed with type 2 diabetes, started on metformin\n",
"2017: Hospitalized for diabetic ketoacidosis (DKA), insulin therapy initiated\n",
"2018: Reported nightmares, switched from bupropion to venlafaxine\n",
"2019: Gastroesophageal reflux disease (GERD) diagnosis, started on omeprazole\n",
"2020: Divorce, increased therapy sessions, added dialectical behavior therapy (DBT)\n",
"2021: Developed plantar fasciitis, prescribed orthotics and physical therapy\n",
"2022: Admitted for panic attack, mistaken for myocardial infarction, cardiac workup negative\n",
"2023: Attempted suicide, inpatient psychiatric treatment for 30 days\n",
"2023: Post-discharge, started on new antipsychotic (quetiapine) and mood stabilizer (lamotrigine)\n",
"2024: Reports improvement in mood and sleep, weight loss noted\n",
"2024: A1C levels improved, insulin dose decreased\n",
" \"\"\",\n",
" \"\"\"\n",
"Patient Name: Lily Chen\n",
"Age: 8\n",
"Medical Record:\n",
"\n",
"2016 (Birth): Born at 34 weeks, diagnosed with Tetralogy of Fallot (TOF)\n",
" - Immediate surgery to place a shunt for increased pulmonary blood flow\n",
"2016 (3 months): Echocardiogram showed worsening right ventricular hypertrophy\n",
"2017 (8 months): Complete repair of TOF (VSD closure, pulmonary valve replacement, RV outflow tract repair)\n",
"2017 (10 months): Developed post-operative arrhythmias, started on amiodarone\n",
"2018 (14 months): Developmental delay noted, referred to early intervention services\n",
"2018 (18 months): Speech therapy initiated for delayed language development\n",
"2019 (2 years): Diagnosed with failure to thrive, started on high-calorie diet\n",
"2019 (2.5 years): Occupational therapy started for fine motor skill delays\n",
"2020 (3 years): Cardiac catheterization showed mild pulmonary stenosis\n",
"2020 (3.5 years): Diagnosed with sensory processing disorder (SPD)\n",
"2021 (4 years): Started integrated preschool program with IEP (Individualized Education Plan)\n",
"2021 (4.5 years): Hospitalized for RSV bronchiolitis, required brief oxygen support\n",
"2022 (5 years): Echocardiogram showed progression of pulmonary stenosis, balloon valvuloplasty performed\n",
"2022 (5.5 years): Diagnosed with attention-deficit/hyperactivity disorder (ADHD), started behavioral therapy\n",
"2023 (6 years): Cochlear implant surgery for sensorineural hearing loss\n",
"2023 (7 years): Started mainstream school with continued IEP support\n",
"2024 (7.5 years): Occupational therapy discontinued, met fine motor skill goals\n",
"2024 (8 years): Periodic cardiac follow-up shows stable pulmonary valve function\n",
"2024 (8 years): Speech development progressing well, ongoing therapy\n",
" \"\"\",\n",
" \"\"\"\n",
"Patient Name: Jason Tran\n",
"Age: 25\n",
"Medical Record:\n",
"\n",
"2010 (11 yrs): Diagnosed with asthma, started on albuterol inhaler\n",
"2012 (13 yrs): First football concussion, brief loss of consciousness\n",
"2013 (14 yrs): Fractured right tibia during soccer, surgical fixation\n",
"2014 (15 yrs): Second concussion, resulting in post-concussion syndrome\n",
" - Symptoms: headaches, dizziness, memory problems\n",
" - Referred to pediatric neurologist, cognitive rehabilitation therapy\n",
"2015 (16 yrs): Developed anxiety and depression, started on fluoxetine\n",
"2016 (17 yrs): ACL tear (left knee) during basketball, reconstructive surgery\n",
" - 6-month rehabilitation, switched to non-contact sports\n",
"2017 (18 yrs): Graduated high school, started college on academic scholarship\n",
"2018 (19 yrs): Diagnosed with PTSD related to sports injuries\n",
" - Started cognitive-behavioral therapy (CBT)\n",
"2019 (20 yrs): Tried to return to basketball, experienced panic attack\n",
" - Increased therapy sessions, added exposure therapy\n",
"2020 (21 yrs): COVID-19 pandemic, remote learning, reported increased anxiety\n",
" - Started mindfulness meditation and yoga\n",
"2021 (22 yrs): Diagnosed with sleep apnea, started CPAP therapy\n",
" - Sleep study suggested link between concussions and sleep disorder\n",
"2022 (23 yrs): Gradual return to low-impact sports (swimming, cycling)\n",
" - Reported improved mood and sleep quality\n",
"2023 (24 yrs): Graduated college, started job in sports analytics\n",
" - Continuing therapy, now biweekly\n",
" - Volunteering with youth concussion awareness program\n",
"2024 (25 yrs): Annual check-up - asthma well-controlled, mental health stable\n",
" - No sports-related injuries in past 2 years\n",
" - Training for first half-marathon\n",
" \"\"\",\n",
" \"\"\"\n",
"Patient Name: Amira Khan\n",
"Age: 36\n",
"Medical Record:\n",
"\n",
"2011: Recurrent joint pain and fatigue, initial diagnosis of fibromyalgia\n",
" - Started on pregabalin and physical therapy\n",
"2012: Developed persistent rash and photosensitivity\n",
" - Dermatologist diagnosed cutaneous lupus (CLE)\n",
" - Started on topical corticosteroids and sunscreen\n",
"2013: Complained of severe fatigue, hair loss, and cognitive issues (\"brain fog\")\n",
" - Blood tests showed positive ANA and anti-dsDNA antibodies\n",
" - Diagnosed with Systemic Lupus Erythematosus (SLE)\n",
" - Started on hydroxychloroquine and low-dose prednisone\n",
"2014: Hospitalized for lupus nephritis (class III)\n",
" - Renal biopsy confirmed diagnosis\n",
" - Started on mycophenolate mofetil and increased prednisone\n",
"2015: Developed interstitial lung disease (ILD) secondary to SLE\n",
" - Started on cyclophosphamide pulse therapy\n",
" - Required home oxygen therapy\n",
"2016: Diagnosed with secondary Sjögren's syndrome\n",
" - Symptoms: dry eyes, dry mouth\n",
" - Started on pilocarpine and artificial tears\n",
"2017: Hospitalized for lupus cerebritis\n",
" - Symptoms: seizures, confusion, memory loss\n",
" - MRI showed brain inflammation\n",
" - Treated with high-dose steroids and rituximab\n",
"2018: Developed avascular necrosis (AVN) of hip, steroid-induced\n",
" - Underwent total hip replacement surgery\n",
"2019: Started on belimumab to reduce flare frequency\n",
" - Gradual reduction in prednisone dosage\n",
"2020: COVID-19 pandemic, self-isolated due to immunosuppression\n",
" - Telemedicine follow-ups, home infusions\n",
"2021: Diagnosed with steroid-induced diabetes\n",
" - Started on metformin and insulin\n",
" - Referred to endocrinologist and nutritionist\n",
"2022: Flare-up of lupus, increased joint pain and fatigue\n",
" - Adjusted medications: increased mycophenolate, added abatacept\n",
"2023: Cardiovascular screening due to long-term steroid use\n",
" - Echo showed early signs of diastolic dysfunction\n",
" - Started on ACE inhibitors, referred to cardio-rehab\n",
"2024: Improvement noted in all organ systems\n",
" - Tapering immunosuppressants, monitoring closely\n",
" - Continues physical therapy, yoga for joint health\n",
" - Planning pregnancy, consulted with high-risk OB\n",
" \"\"\"\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's take a look at one, just to make sure our list working properly:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\nPatient Name: Lily Chen\\nAge: 8\\nMedical Record:\\n\\n2016 (Birth): Born at 34 weeks, diagnosed with Tetralogy of Fallot (TOF)\\n - Immediate surgery to place a shunt for increased pulmonary blood flow\\n2016 (3 months): Echocardiogram showed worsening right ventricular hypertrophy\\n2017 (8 months): Complete repair of TOF (VSD closure, pulmonary valve replacement, RV outflow tract repair)\\n2017 (10 months): Developed post-operative arrhythmias, started on amiodarone\\n2018 (14 months): Developmental delay noted, referred to early intervention services\\n2018 (18 months): Speech therapy initiated for delayed language development\\n2019 (2 years): Diagnosed with failure to thrive, started on high-calorie diet\\n2019 (2.5 years): Occupational therapy started for fine motor skill delays\\n2020 (3 years): Cardiac catheterization showed mild pulmonary stenosis\\n2020 (3.5 years): Diagnosed with sensory processing disorder (SPD)\\n2021 (4 years): Started integrated preschool program with IEP (Individualized Education Plan)\\n2021 (4.5 years): Hospitalized for RSV bronchiolitis, required brief oxygen support\\n2022 (5 years): Echocardiogram showed progression of pulmonary stenosis, balloon valvuloplasty performed\\n2022 (5.5 years): Diagnosed with attention-deficit/hyperactivity disorder (ADHD), started behavioral therapy\\n2023 (6 years): Cochlear implant surgery for sensorineural hearing loss\\n2023 (7 years): Started mainstream school with continued IEP support\\n2024 (7.5 years): Occupational therapy discontinued, met fine motor skill goals\\n2024 (8 years): Periodic cardiac follow-up shows stable pulmonary valve function\\n2024 (8 years): Speech development progressing well, ongoing therapy\\n '"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"patient_records[2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Our initial \"bad\" prompt\n",
"\n",
"Let's start with a very simple prompt that asks Claude to generate us a summary. A very simple first attempt might look something like this:\n",
"\n",
"> I have this patient medical record. Can you summarize it for me?\n",
"> \n",
"> {medical record goes here}\n",
"> \n",
"> I need this for a quick review before the patient's appointment tomorrow.\n",
"\n",
"Let's formalize this into a specific prompt that we can use with a dynamically inserted medical record:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"initial_prompt = \"\"\"\n",
"I have this patient medical record. Can you summarize it for me?\n",
"\n",
"{record}\n",
"\n",
"I need this for a quick review before the patient's appointment tomorrow.\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, let's write a simple function that will accept a single medical record, insert it into our prompt, and get Claude to generate a summary:"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"from anthropic import Anthropic\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv()\n",
"client = Anthropic()\n",
"\n",
"def generate_summary_with_bad_prompt(patient_record):\n",
" prompt_with_record = initial_prompt.format(record=patient_record)\n",
" response = client.messages.create(\n",
" model=\"claude-3-sonnet-20240229\",\n",
" max_tokens=4096,\n",
" messages=[{\"role\": \"user\", \"content\": prompt_with_record}]\n",
" )\n",
" print(\"===============================\")\n",
" print(response.content[0].text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try it will all 5 of the medical records we have in the `patient_records` list:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"Here is a summary of Evelyn Thompson's 78-year-old medical record:\n",
"\n",
"Chronic Conditions:\n",
"- Type 2 diabetes (since 1985) - on metformin, dose increased in 2010\n",
"- Hypertension (since 1992) - on lisinopril, dose increased in 2020\n",
"- Hypothyroidism (since 2000) - on levothyroxine \n",
"- Atrial fibrillation (since 2005) - on warfarin\n",
"- Vitamin B12 deficiency (since 2008) - receiving monthly injections\n",
"- Chronic kidney disease stage 3 (since 2015) - metformin adjusted\n",
"- Mild cognitive impairment (since 2019) - on donepezil\n",
"\n",
"Surgical History:\n",
"- Total hip replacement (1998) - right side, due to osteoarthritis\n",
"- Cataract surgery (2003) - both eyes\n",
"- Lumpectomy and radiation (2013) - for stage 2 breast cancer \n",
"- Total knee replacement (2017) - left side, due to osteoarthritis\n",
"\n",
"Cancer History: \n",
"- Breast cancer (2013) - currently on anastrozole for recurrence prevention\n",
"\n",
"Recent Issues:\n",
"- Recurrent UTIs (2021) - on prophylactic antibiotics\n",
"- Worsening kidney function per eGFR (2022)\n",
"- Declining mobility (2023) - started physical therapy and home health aide\n",
"\n",
"Overall, an elderly patient with multiple chronic conditions requiring polypharmacy and close monitoring, especially for diabetes, hypertension, kidney disease, and cancer recurrence.\n"
]
}
],
"source": [
"generate_summary_with_bad_prompt(patient_records[0])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"Here's a summary of Marcus Reyes' (age 42) medical record:\n",
"\n",
"Mental Health:\n",
"- Diagnosed with generalized anxiety disorder in 2001, treated with paroxetine\n",
"- Major depressive disorder diagnosed in 2003, bupropion added\n",
"- Hospitalized for suicidal ideation in 2005, started cognitive behavioral therapy (CBT) \n",
"- ADHD diagnosis in 2007, started methylphenidate\n",
"- Medications changed over time due to side effects/inefficacy (escitalopram, venlafaxine, quetiapine, lamotrigine)\n",
"- Attempted suicide in 2023, required 30-day inpatient psychiatric treatment\n",
"- Divorce in 2020 led to increased therapy (added dialectical behavior therapy)\n",
"\n",
"Other Conditions:\n",
"- Obstructive sleep apnea in 2012, started CPAP therapy\n",
"- Hypertension in 2014, started losartan \n",
"- Weight gain, referred to nutritionist in 2015\n",
"- Type 2 diabetes diagnosed in 2016, metformin started\n",
"- Hospitalized for diabetic ketoacidosis in 2017, insulin initiated\n",
"- GERD diagnosed in 2019, started omeprazole\n",
"- Plantar fasciitis in 2021, prescribed orthotics and physical therapy\n",
"- Panic attack in 2022, mistaken for heart attack\n",
"\n",
"Recent Updates: \n",
"- 2024: Mood and sleep improved, weight loss noted\n",
"- 2024: A1C levels improved, insulin dose decreased\n",
"\n",
"The patient has a complex medical history involving multiple psychiatric conditions managed with medications and therapy. He also has several chronic conditions like diabetes, hypertension, and sleep apnea that require management.\n"
]
}
],
"source": [
"generate_summary_with_bad_prompt(patient_records[1])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"Here is a summary of Lily Chen's 8-year medical record:\n",
"\n",
"Lily was born prematurely at 34 weeks in 2016 with Tetralogy of Fallot (TOF), a congenital heart defect. She underwent immediate surgery for a shunt and then complete TOF repair at 8 months old in 2017. Post-operatively, she developed arrhythmias requiring medication.\n",
"\n",
"Starting around 1 year old, Lily experienced developmental delays - failure to thrive, speech/language delays requiring therapy, fine motor skill delays requiring occupational therapy, and a sensory processing disorder diagnosis. An IEP was started in preschool for extra support.\n",
"\n",
"Cardiac issues continued with mild pulmonary stenosis at 3 years old, requiring a balloon valvuloplasty at 5 years. She was also diagnosed with ADHD at 5.5 years and started behavioral therapy.\n",
"\n",
"Other medical events included an RSV bronchiolitis hospitalization at 4.5 years requiring oxygen, and cochlear implant surgery at 6 years for sensorineural hearing loss.\n",
"\n",
"Now at 8 years old, Lily has stable cardiac function but continues speech therapy. She has met fine motor goals and receives mainstream schooling with an IEP.\n"
]
}
],
"source": [
"generate_summary_with_bad_prompt(patient_records[2])"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"Here is a summary of Jason Tran's 25-year-old medical record:\n",
"\n",
"Asthma & Sports Injuries\n",
"- Diagnosed with asthma at age 11, prescribed albuterol inhaler\n",
"- Multiple sports concussions starting at age 13, with post-concussion syndrome \n",
"- Fracture and knee injuries requiring surgery in teens\n",
"\n",
"Mental Health\n",
"- Developed anxiety and depression at 16, started fluoxetine\n",
"- Diagnosed with PTSD at 19 related to sports injuries, started CBT and exposure therapy\n",
"- Experienced panic attack at 20 trying to return to basketball\n",
"- Increased anxiety during COVID-19 pandemic at 21, started mindfulness practices\n",
"\n",
"Sleep Issues\n",
"- Diagnosed with sleep apnea at 22 potentially linked to concussions, started CPAP\n",
"\n",
"Recent Updates\n",
"- Gradual return to low-impact sports at 23 with improved mood/sleep\n",
"- Graduated college at 24, working in sports analytics \n",
"- Continuing biweekly therapy and youth concussion awareness program\n",
"- Training for first half-marathon at 25, asthma and mental health stable\n",
"\n",
"Key Issues\n",
"- History of asthma\n",
"- Multiple concussions and sports injuries \n",
"- Anxiety, depression, PTSD \n",
"- Sleep apnea\n",
"\n",
"I've highlighted the key medical issues, mental health conditions, and recent progress to prepare for tomorrow's appointment. Let me know if you need any clarification or have additional details to add.\n"
]
}
],
"source": [
"generate_summary_with_bad_prompt(patient_records[3])"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"Here is a summary of Amira Khan's lengthy and complex medical history:\n",
"\n",
"Amira Khan is a 36-year-old woman with a long-standing diagnosis of Systemic Lupus Erythematosus (SLE). Her initial symptoms in 2011 were joint pain and fatigue. After developing rashes and photosensitivity in 2012, she was diagnosed with cutaneous lupus and started on topical steroids. In 2013, positive autoantibodies and multi-system involvement led to the SLE diagnosis.\n",
"\n",
"Over the years, Amira has experienced numerous lupus complications affecting multiple organs:\n",
"- Lupus nephritis requiring immunosuppressants (2014)\n",
"- Interstitial lung disease requiring cyclophosphamide and oxygen (2015) \n",
"- Secondary Sjogren's syndrome (2016)\n",
"- Lupus cerebritis with brain inflammation (2017)\n",
"- Avascular necrosis of hip requiring joint replacement (2018)\n",
"\n",
"She has required intensive treatment with hydroxychloroquine, mycophenolate, cyclophosphamide, rituximab, belimumab, and long-term corticosteroids leading to side effects like diabetes and diastolic dysfunction. Despite treatment challenges, her condition has gradually improved with multi-disciplinary care.\n",
"\n",
"Currently, she is on abatacept, mycophenolate, ACE inhibitors, and trying to taper corticosteroids. She continues physical therapy and is interested in a future pregnancy consultation. Close monitoring is needed for her complex multi-system disease and medication side effects.\n"
]
}
],
"source": [
"generate_summary_with_bad_prompt(patient_records[4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we take a closer look at the summaries we got from Claude, they're all over the place! Some summaries are just long paragraphs of text. Other summaries are broken down into bulleted lists. The actual content of each summary differs radically. The summary content isn't terrible, but the formatting, length, and types of summary data are inconsistent and would be difficult to use programmatically. Let's improve it!\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Improving the prompt\n",
"\n",
"As we learned in the previous lesson, there are a clear set of prompting techniques that could help us get better and more consistent results, including:\n",
"\n",
"* Adding a clear system prompt to set the context and role for the assistant, such as a medical professional or a healthcare data analyst\n",
"* Structuring the input data using XML tags (e.g., `<patient_record>`) to make it clear what part of the prompt is the input data\n",
"* Provide clear, specific instructions on what the summary should focus on, such as diagnoses, medications, and recent health concerns\n",
"* Specify the desired format for the summary, such as bullet points or a structured format with specific sections\n",
"* Include an example of a well-formatted summary to guide the assistant's response\n",
"* Use some sort of output XML tag to structure the output, making it easy for the user to find the relevant information quickly\n",
"\n",
"**In upcoming lessons, we'll discuss a specific, nuanced approach to prompt engineering and selecting prompting techniques. In this lesson we'll take a \"shotgun\" approach and use all of them at once.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding a system prompt\n",
"Let's tackle these suggestions one at a time. We'll begin by defining a system prompt to set the context and role for the assistant. In our experience, this is the only information that should go in the system prompt.\n",
"\n",
"For our use case, our system prompt should tell Claude that its role is to act as a medical professional that is talented at translating long patient histories into concise summaries.\n",
"\n",
"Here's one approach:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"system = \"\"\"\n",
"You are a highly experienced medical professional with a specialty in translating complex patient histories into concise, actionable summaries. \n",
"Your role is to analyze patient records, identify critical information, and present it in a clear, structured format that aids in diagnosis and treatment planning. \n",
"Your summaries are invaluable for busy healthcare providers who need quick insights into a patient's medical history before appointments.\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Structuring input data\n",
"One of the most important prompting tips when working with Claude is to clearly label your input data using XML tags. In our current prompt, we're providing a medical history as the main input like this: \n",
"\n",
"> I have this patient medical record. Can you summarize it for me?\n",
"> \n",
"> {medical record goes here}\n",
"> \n",
"> I need this for a quick review before the patient's appointment tomorrow.\n",
"\n",
"A simple addition that can lead to substantial improvement is to use XML tags to wrap our input data. Let's update our prompt to do this using `<patient_record>` tags"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"#System Prompt\n",
"system = \"\"\"\n",
"You are a highly experienced medical professional with a specialty in translating complex patient histories into concise, actionable summaries. \n",
"Your role is to analyze patient records, identify critical information, and present it in a clear, structured format that aids in diagnosis and treatment planning. \n",
"Your summaries are invaluable for busy healthcare providers who need quick insights into a patient's medical history before appointments.\n",
"\"\"\"\n",
"\n",
"#Improved Prompt (still a work in progress!)\n",
"updated_prompt = \"\"\"\n",
"<patient_record>\n",
"{record}\n",
"</patient_record>\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Provide clear instructions\n",
"When working with Claude, providing clear and direct instructions is essential for achieving the best results. Just as when we instruct a human for the first time on a task, the more we explain exactly what we want in a straightforward manner, the better and more accurate Claudes response will be.\n",
"\n",
"In our initial \"bad\" prompt, we had the following instructions:\n",
"\n",
"> I have this patient medical record. Can you summarize it for me?\n",
"\n",
"This is woefully vague! We need to come up with a set of clear and specific instructions. What exactly do we want the summary to look like? \n",
"\n",
"Let's say that we want the summary to include the following distinct categories of information:\n",
"\n",
"* The patient's name\n",
"* The patient's age\n",
"* A list of key diagnoses\n",
"* A list of medications the patient is prescribed\n",
"* A list of non-medication treatments (physical therapy, CBT, etc.)\n",
"* A list of recent concerns\n",
"* A list of important action items for a physician\n",
"\n",
"\n",
"It's also important that we specify the exact output format that we're after, so we'll make sure to ask Claude for bulleted lists instead of just a generic \"list.\"\n",
"\n",
"Here's an updated version of our prompt:"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"#System Prompt\n",
"system = \"\"\"\n",
"You are a highly experienced medical professional with a specialty in translating complex patient histories into concise, actionable summaries. \n",
"Your role is to analyze patient records, identify critical information, and present it in a clear, structured format that aids in diagnosis and treatment planning. \n",
"Your summaries are invaluable for busy healthcare providers who need quick insights into a patient's medical history before appointments.\n",
"\"\"\"\n",
"\n",
"#Improved Prompt (still a work in progress!)\n",
"updated_prompt = \"\"\"\n",
"I need your help summarizing patient medical records for our team of doctors. \n",
"We have a series of follow-up appointments tomorrow, and the doctors need quick, insightful summaries to prepare. \n",
"\n",
"Each summary should include the following elements in this order:\n",
"- The patient's name\n",
"- The patients age\n",
"- A bulleted list of key diagnoses in chronological order\n",
"- A bulleted list of medications the patient is prescribed\n",
"- A bulleted list of other treatments: non-medication treatments like CBT or physical therapy\n",
"- A short bulleted list of recent concerns\n",
"- A bulleted list of key action items to help our doctors prepare for the upcoming patient visit\n",
"\n",
"<patient_record>\n",
"{record}\n",
"</patient_record>\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### Adding examples\n",
"\n",
"Examples are one of the most powerful tools for enhancing Claudes performance and guiding it to produce a desired output. By providing a few well-crafted examples in our prompt, we can significantly improve the accuracy, consistency, and quality of Claudes responses. This technique is particularly effective for tasks that are highly detailed or require structured outputs or adherence to specific formats.\n",
"\n",
"At the moment, our prompt does not include any examples at all. To keep things brief, we'll include a single example, though it's often better to have at least two. \n",
"\n",
"We'll base our example input and output on this medical history: \n",
"\n",
"```\n",
"Patient Name: Ethan Blackwood\n",
"Age: 55\n",
"Medical Record:\n",
"\n",
"2010: Annual check-up, mild hypertension noted\n",
" - Started on lifestyle modifications (diet, exercise)\n",
"2012: Diagnosed with moderate depression following job loss\n",
" - Started on sertraline and cognitive-behavioral therapy (CBT)\n",
"2014: New job, reported improved mood\n",
" - Continued sertraline, reduced CBT sessions\n",
"2015: Mild back pain, diagnosed with early degenerative disc disease\n",
" - Physical therapy and over-the-counter NSAIDs prescribed\n",
"2016: Hypertension worsened, started on lisinopril\n",
"2017: Routine colonoscopy showed benign polyps, removed during procedure\n",
"2018: Developed persistent cough, chest X-ray clear\n",
" - Diagnosed with Gastroesophageal Reflux Disease (GERD)\n",
" - Started on omeprazole\n",
"2019: Diagnosed with obstructive sleep apnea (OSA)\n",
" - Started CPAP therapy, reported improved energy levels\n",
"2020: COVID-19 pandemic, worked from home\n",
" - Reported increased anxiety, CBT sessions resumed (telehealth)\n",
" - COVID-19 vaccination (Moderna, both doses)\n",
"2021: Mild knee pain, MRI showed minor meniscus tear\n",
" - Arthroscopic surgery recommended, patient opted for conservative management\n",
"2022: Annual check-up showed pre-diabetes (A1C: 6.1%)\n",
" - Intensified lifestyle modifications, referred to nutritionist\n",
"- Discontinued omeprazole due to resolved GERD symptoms\n",
"2023: Blood tests showed elevated PSA (Prostate-Specific Antigen)\n",
" - Prostate biopsy performed, results negative for cancer\n",
"- Knee pain worsened, agreed to arthroscopic surgery\n",
"2024: Post-op knee recovery: good, continuing physical therapy\n",
" - A1C levels improved (5.8%), pre-diabetes resolved\n",
" - Stress test normal, but mild LVH on echocardiogram\n",
" - Started on low-dose ACE inhibitor for cardioprotection\n",
"```\n",
"\n",
"Given this example input, a well-structured output that follows our prompt's rules might look like this: \n",
"\n",
"```\n",
"Name: Ethan Blackwood\n",
"Age: 55 \n",
"\n",
"Key Diagnoses:\n",
"- Hypertension (2010)\n",
"- Depression (2012)\n",
"- Degenerative Disc Disease (2015) \n",
"- Gastroesophageal Reflux Disease (GERD) (2018)\n",
"- Obstructive Sleep Apnea (OSA) (2019)\n",
"- Pre-diabetes (2022)\n",
"- Meniscus Tear (2021)\n",
"- Left Ventricular Hypertrophy (LVH) (2024)\n",
"\n",
"Medications: \n",
"- Sertraline (depression)\n",
"- Lisinopril (hypertension)\n",
"- Omeprazole (GERD) - discontinued in 2022\n",
"- Low-dose ACE inhibitor (cardioprotection - 2024)\n",
"\n",
"Other Treatments:\n",
"- Cognitive Behavioral Therapy (CBT) (depression)\n",
"- Physical therapy (back pain, post-op knee recovery)\n",
"- CPAP therapy (OSA)\n",
"- Arthroscopic knee surgery (2023)\n",
"\n",
"Recent Concerns:\n",
"- Worsening knee pain\n",
"- Elevated PSA (2023) \n",
"- Left ventricular hypertrophy on echocardiogram (2024)\n",
"\n",
"Action Items: \n",
"- Follow up on post-op knee recovery and physical therapy \n",
"- Monitor PSA levels and prostate health\n",
"- Optimize blood pressure and hypertension management\n",
"- Assess need for further cardiac workup after LVH finding\n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's update our prompt to include this example input and output. To make things extra clear to Claude, we'll make sure to wrap our example inside of `<example>` tags:"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"#System Prompt\n",
"system = \"\"\"\n",
"You are a highly experienced medical professional with a specialty in translating complex patient histories into concise, actionable summaries. \n",
"Your role is to analyze patient records, identify critical information, and present it in a clear, structured format that aids in diagnosis and treatment planning. \n",
"Your summaries are invaluable for busy healthcare providers who need quick insights into a patient's medical history before appointments.\n",
"\"\"\"\n",
"\n",
"#Improved Prompt (still a work in progress!)\n",
"updated_prompt = \"\"\"\n",
"I need your help summarizing patient medical records for our team of doctors. \n",
"We have a series of follow-up appointments tomorrow, and the doctors need quick, insightful summaries to prepare. \n",
"\n",
"Each summary should include the following elements in this order:\n",
"- The patient's name\n",
"- The patients age\n",
"- A bulleted list of key diagnoses in chronological order\n",
"- A bulleted list of medications the patient is prescribed\n",
"- A bulleted list of other treatments: non-medication treatments like CBT or physical therapy\n",
"- A short bulleted list of recent concerns\n",
"- A bulleted list of key action items to help our doctors prepare for the upcoming patient visit\n",
"\n",
"Here's an example of how we'd like the summaries formatted:\n",
"\n",
"<example>\n",
"<patient_record>\n",
"Patient Name: Ethan Blackwood\n",
"Age: 55\n",
"Medical Record:\n",
"\n",
"2010: Annual check-up, mild hypertension noted\n",
" - Started on lifestyle modifications (diet, exercise)\n",
"2012: Diagnosed with moderate depression following job loss\n",
" - Started on sertraline and cognitive-behavioral therapy (CBT)\n",
"2014: New job, reported improved mood\n",
" - Continued sertraline, reduced CBT sessions\n",
"2015: Mild back pain, diagnosed with early degenerative disc disease\n",
" - Physical therapy and over-the-counter NSAIDs prescribed\n",
"2016: Hypertension worsened, started on lisinopril\n",
"2017: Routine colonoscopy showed benign polyps, removed during procedure\n",
"2018: Developed persistent cough, chest X-ray clear\n",
" - Diagnosed with Gastroesophageal Reflux Disease (GERD)\n",
" - Started on omeprazole\n",
"2019: Diagnosed with obstructive sleep apnea (OSA)\n",
" - Started CPAP therapy, reported improved energy levels\n",
"2020: COVID-19 pandemic, worked from home\n",
" - Reported increased anxiety, CBT sessions resumed (telehealth)\n",
" - COVID-19 vaccination (Moderna, both doses)\n",
"2021: Mild knee pain, MRI showed minor meniscus tear\n",
" - Arthroscopic surgery recommended, patient opted for conservative management\n",
"2022: Annual check-up showed pre-diabetes (A1C: 6.1%)\n",
" - Intensified lifestyle modifications, referred to nutritionist\n",
"- Discontinued omeprazole due to resolved GERD symptoms\n",
"2023: Blood tests showed elevated PSA (Prostate-Specific Antigen)\n",
" - Prostate biopsy performed, results negative for cancer\n",
"- Knee pain worsened, agreed to arthroscopic surgery\n",
"2024: Post-op knee recovery: good, continuing physical therapy\n",
" - A1C levels improved (5.8%), pre-diabetes resolved\n",
" - Stress test normal, but mild LVH on echocardiogram\n",
" - Started on low-dose ACE inhibitor for cardioprotection\n",
"</patient_record>\n",
"\n",
"\n",
"Your output: \n",
"\n",
"Name: Ethan Blackwood\n",
"Age: 55 \n",
"\n",
"Key Diagnoses:\n",
"- Hypertension (2010)\n",
"- Depression (2012)\n",
"- Degenerative Disc Disease (2015) \n",
"- Gastroesophageal Reflux Disease (GERD) (2018)\n",
"- Obstructive Sleep Apnea (OSA) (2019)\n",
"- Pre-diabetes (2022)\n",
"- Meniscus Tear (2021)\n",
"- Left Ventricular Hypertrophy (LVH) (2024)\n",
"\n",
"Medications: \n",
"- Sertraline (depression)\n",
"- Lisinopril (hypertension)\n",
"- Omeprazole (GERD) - discontinued in 2022\n",
"- Low-dose ACE inhibitor (cardioprotection - 2024)\n",
"\n",
"Other Treatments:\n",
"- Cognitive Behavioral Therapy (CBT) (depression)\n",
"- Physical therapy (back pain, post-op knee recovery)\n",
"- CPAP therapy (OSA)\n",
"- Arthroscopic knee surgery (2023)\n",
"\n",
"Recent Concerns:\n",
"- Worsening knee pain\n",
"- Elevated PSA (2023) \n",
"- Left ventricular hypertrophy on echocardiogram (2024)\n",
"\n",
"Action Items: \n",
"- Follow up on post-op knee recovery and physical therapy \n",
"- Monitor PSA levels and prostate health\n",
"- Optimize blood pressure and hypertension management\n",
"- Assess need for further cardiac workup after LVH finding\n",
"</example>\n",
"\n",
"Now, please summarize the following patient record in the same format:\n",
"<patient_record>\n",
"{record}\n",
"</patient_record>\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Output XML structure\n",
"\n",
"A common prompting strategy is to ask Claude to use XML tags in its responses. This makes it easier to eventually extract the exact content we care about, even if Claude decides to give us a preamble like \"Ok, here's the summary you requested!\" or adds in additional content. \n",
"\n",
"In this case, it might make sense to get Claude to generate the medical record summary inside of `<summary>` tags. One easy implementation involves updating our example update to show Claude our desired format. \n",
"\n",
"Currently, the example portion of our prompt looks like this: \n",
"\n",
"```\n",
"Here's an example of how we'd like the summaries formatted:\n",
"\n",
"<example>\n",
"<patient_record>\n",
"Patient Name: Ethan Blackwood\n",
"Age: 55\n",
"Medical Record:\n",
"\n",
"2010: Annual check-up, mild hypertension noted\n",
" - Started on lifestyle modifications (diet, exercise)\n",
"medical record truncated to keep the text short here...\n",
"</patient_record>\n",
"\n",
"Your output: \n",
"\n",
"Name: Ethan Blackwood\n",
"Age: 55 \n",
"\n",
"Key Diagnoses:\n",
"- Hypertension (2010)\n",
"summary truncated to keep the text short here...\n",
"</example>\n",
"```\n",
"\n",
"With one simple change, we can indicate to Claude that it should wrap the output inside of `<summary>` tags:\n",
"\n",
"```\n",
"Here's an example of how we'd like the summaries formatted:\n",
"\n",
"<example>\n",
"<patient_record>\n",
"Patient Name: Ethan Blackwood\n",
"Age: 55\n",
"Medical Record:\n",
"\n",
"2010: Annual check-up, mild hypertension noted\n",
" - Started on lifestyle modifications (diet, exercise)\n",
"medical record truncated to keep the text short here...\n",
"</patient_record>\n",
"\n",
"<summary>\n",
"Name: Ethan Blackwood\n",
"Age: 55 \n",
"\n",
"Key Diagnoses:\n",
"- Hypertension (2010)\n",
"summary truncated to keep the text short here...\n",
"</summary>\n",
"</example>\n",
"```\n",
"\n",
"Let's update the example section of our prompt to reflect this change:\n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"#System Prompt\n",
"system = \"\"\"\n",
"You are a highly experienced medical professional with a specialty in translating complex patient histories into concise, actionable summaries. \n",
"Your role is to analyze patient records, identify critical information, and present it in a clear, structured format that aids in diagnosis and treatment planning. \n",
"Your summaries are invaluable for busy healthcare providers who need quick insights into a patient's medical history before appointments.\n",
"\"\"\"\n",
"\n",
"#Improved Prompt (still a work in progress!)\n",
"updated_prompt = \"\"\"\n",
"I need your help summarizing patient medical records for our team of doctors. \n",
"We have a series of follow-up appointments tomorrow, and the doctors need quick, insightful summaries to prepare. \n",
"\n",
"Each summary should include the following elements in this order:\n",
"- The patient's name\n",
"- The patients age\n",
"- A bulleted list of key diagnoses in chronological order\n",
"- A bulleted list of medications the patient is prescribed\n",
"- A bulleted list of other treatments: non-medication treatments like CBT or physical therapy\n",
"- A short bulleted list of recent concerns\n",
"- A bulleted list of key action items to help our doctors prepare for the upcoming patient visit\n",
"\n",
"Here's an example of how we'd like the summaries formatted:\n",
"\n",
"<example>\n",
"<patient_record>\n",
"Patient Name: Ethan Blackwood\n",
"Age: 55\n",
"Medical Record:\n",
"\n",
"2010: Annual check-up, mild hypertension noted\n",
" - Started on lifestyle modifications (diet, exercise)\n",
"2012: Diagnosed with moderate depression following job loss\n",
" - Started on sertraline and cognitive-behavioral therapy (CBT)\n",
"2014: New job, reported improved mood\n",
" - Continued sertraline, reduced CBT sessions\n",
"2015: Mild back pain, diagnosed with early degenerative disc disease\n",
" - Physical therapy and over-the-counter NSAIDs prescribed\n",
"2016: Hypertension worsened, started on lisinopril\n",
"2017: Routine colonoscopy showed benign polyps, removed during procedure\n",
"2018: Developed persistent cough, chest X-ray clear\n",
" - Diagnosed with Gastroesophageal Reflux Disease (GERD)\n",
" - Started on omeprazole\n",
"2019: Diagnosed with obstructive sleep apnea (OSA)\n",
" - Started CPAP therapy, reported improved energy levels\n",
"2020: COVID-19 pandemic, worked from home\n",
" - Reported increased anxiety, CBT sessions resumed (telehealth)\n",
" - COVID-19 vaccination (Moderna, both doses)\n",
"2021: Mild knee pain, MRI showed minor meniscus tear\n",
" - Arthroscopic surgery recommended, patient opted for conservative management\n",
"2022: Annual check-up showed pre-diabetes (A1C: 6.1%)\n",
" - Intensified lifestyle modifications, referred to nutritionist\n",
"- Discontinued omeprazole due to resolved GERD symptoms\n",
"2023: Blood tests showed elevated PSA (Prostate-Specific Antigen)\n",
" - Prostate biopsy performed, results negative for cancer\n",
"- Knee pain worsened, agreed to arthroscopic surgery\n",
"2024: Post-op knee recovery: good, continuing physical therapy\n",
" - A1C levels improved (5.8%), pre-diabetes resolved\n",
" - Stress test normal, but mild LVH on echocardiogram\n",
" - Started on low-dose ACE inhibitor for cardioprotection\n",
"</patient_record>\n",
"\n",
"\n",
"<summary>\n",
"Name: Ethan Blackwood\n",
"Age: 55 \n",
"\n",
"Key Diagnoses:\n",
"- Hypertension (2010)\n",
"- Depression (2012)\n",
"- Degenerative Disc Disease (2015) \n",
"- Gastroesophageal Reflux Disease (GERD) (2018)\n",
"- Obstructive Sleep Apnea (OSA) (2019)\n",
"- Pre-diabetes (2022)\n",
"- Meniscus Tear (2021)\n",
"- Left Ventricular Hypertrophy (LVH) (2024)\n",
"\n",
"Medications: \n",
"- Sertraline (depression)\n",
"- Lisinopril (hypertension)\n",
"- Omeprazole (GERD) - discontinued in 2022\n",
"- Low-dose ACE inhibitor (cardioprotection - 2024)\n",
"\n",
"Other Treatments:\n",
"- Cognitive Behavioral Therapy (CBT) (depression)\n",
"- Physical therapy (back pain, post-op knee recovery)\n",
"- CPAP therapy (OSA)\n",
"- Arthroscopic knee surgery (2023)\n",
"\n",
"Recent Concerns:\n",
"- Worsening knee pain\n",
"- Elevated PSA (2023) \n",
"- Left ventricular hypertrophy on echocardiogram (2024)\n",
"\n",
"Action Items: \n",
"- Follow up on post-op knee recovery and physical therapy \n",
"- Monitor PSA levels and prostate health\n",
"- Optimize blood pressure and hypertension management\n",
"- Assess need for further cardiac workup after LVH finding\n",
"</summary>\n",
"</example>\n",
"\n",
"Now, please summarize the following patient record in the same format:\n",
"\n",
"<patient_record>\n",
"{record}\n",
"</patient_record>\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Recap of the prompt changes\n",
"Before we try out our new prompt, let's take a look at the changes we made: \n",
"\n",
"* We added a clear system prompt to set the context and give Claude a clear role.\n",
"* We structured the input data using XML tags, `<patient_record>` in our case, to make it clear what part of the prompt is the input data.\n",
"* We provided clear, specific instructions on what the summary should focus on, such as diagnoses, medications, and recent health concerns.\n",
"* We specified the desired format for the summary.\n",
"* We added in an example input and corresponding well-formatted summary to guide Claude's reply.\n",
"* We asked Claude to generate the summary inside of `<summary>` tags.\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Testing out the new prompt\n",
"Let's try using our new `system` and `updated_prompt` prompts with the same 5 medical records in our `patient_records` list:"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"def generate_summary_with_improved_prompt(patient_record):\n",
" prompt_with_record = updated_prompt.format(record=patient_record) #use our rewritten prompt!\n",
" response = client.messages.create(\n",
" model=\"claude-3-sonnet-20240229\",\n",
" max_tokens=4096,\n",
" system=system, #add in our system prompt!\n",
" messages=[{\"role\": \"user\", \"content\": prompt_with_record}]\n",
" )\n",
" print(\"===============================\")\n",
" print(response.content[0].text)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"<summary>\n",
"Name: Evelyn Thompson\n",
"Age: 78\n",
"\n",
"Key Diagnoses:\n",
"- Type 2 Diabetes (1985)\n",
"- Hypertension (1992)\n",
"- Osteoarthritis (Hip and Knee Replacements in 1998 and 2017)\n",
"- Hypothyroidism (2000)\n",
"- Atrial Fibrillation (2005)\n",
"- Vitamin B12 Deficiency (2008)\n",
"- Transient Ischemic Attack (TIA) (2011)\n",
"- Breast Cancer (2013)\n",
"- Chronic Kidney Disease (CKD) Stage 3 (2015)\n",
"- Pneumonia (2018)\n",
"- Mild Cognitive Impairment (2019)\n",
"- Recurrent Urinary Tract Infections (UTIs) (2021)\n",
"\n",
"Medications:\n",
"- Metformin (Diabetes)\n",
"- Lisinopril (Hypertension)\n",
"- Levothyroxine (Hypothyroidism)\n",
"- Warfarin (Atrial Fibrillation)\n",
"- Aspirin (Antiplatelet)\n",
"- Anastrozole (Breast Cancer Recurrence Prevention)\n",
"- Donepezil (Cognitive Impairment)\n",
"- Low-dose Prophylactic Antibiotics (Recurrent UTIs)\n",
"\n",
"Other Treatments:\n",
"- Total Hip Replacement (1998)\n",
"- Cataract Surgery (2003)\n",
"- Vitamin B12 Injections (2008)\n",
"- Lumpectomy and Radiation (Breast Cancer, 2013)\n",
"- Total Knee Replacement (2017)\n",
"- Physical Therapy and Home Health Aide (2023)\n",
"\n",
"Recent Concerns:\n",
"- Worsening Kidney Function (eGFR Decline in 2022)\n",
"- Declining Mobility (2023)\n",
"\n",
"Action Items:\n",
"- Monitor Kidney Function and Adjust Medications as Needed\n",
"- Continue Physical Therapy and Home Health Support\n",
"- Evaluate for Cognitive Decline and Adjust Treatment Plan\n",
"- Address Mobility Issues and Fall Risk\n",
"- Ensure Adherence to Recommended Cancer Screening\n",
"</summary>\n"
]
}
],
"source": [
"generate_summary_with_improved_prompt(patient_records[0])"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"<summary>\n",
"Name: Marcus Reyes\n",
"Age: 42\n",
"\n",
"Key Diagnoses:\n",
"- Generalized Anxiety Disorder (GAD) (2001) \n",
"- Major Depressive Disorder (MDD) (2003)\n",
"- Attention-Deficit/Hyperactivity Disorder (ADHD) (2007)\n",
"- Obstructive Sleep Apnea (OSA) (2012)\n",
"- Hypertension (2014)\n",
"- Type 2 Diabetes (2016)\n",
"- Gastroesophageal Reflux Disease (GERD) (2019)\n",
"- Plantar Fasciitis (2021)\n",
"\n",
"Medications:\n",
"- Paroxetine (GAD) - switched to escitalopram in 2009\n",
"- Bupropion (MDD) - switched to venlafaxine in 2018 \n",
"- Methylphenidate (ADHD)\n",
"- Losartan (hypertension)\n",
"- Metformin (diabetes)\n",
"- Insulin therapy (diabetes) - initiated in 2017\n",
"- Omeprazole (GERD) \n",
"- Quetiapine (antipsychotic) (2023)\n",
"- Lamotrigine (mood stabilizer) (2023)\n",
"\n",
"Other Treatments: \n",
"- Cognitive Behavioral Therapy (CBT)\n",
"- Dialectical Behavior Therapy (DBT) (2020)\n",
"- CPAP therapy (OSA)\n",
"- Orthotics and physical therapy (plantar fasciitis)\n",
"- Inpatient psychiatric treatment (2023)\n",
"\n",
"Recent Concerns:\n",
"- Suicide attempt (2023) \n",
"- Panic attack (2022)\n",
"- Weight gain and poor diabetes control\n",
"\n",
"Action Items:\n",
"- Monitor mental health closely, ensure compliance with new medication regimen\n",
"- Follow up on diabetes management, lifestyle modifications for weight loss \n",
"- Continue CBT/DBT and evaluate need for increased therapy sessions\n",
"- Screen for potential medication side effects impacting sleep, weight, anxiety\n",
"</summary>\n"
]
}
],
"source": [
"generate_summary_with_improved_prompt(patient_records[1])"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"<summary>\n",
"Name: Lily Chen\n",
"Age: 8\n",
"\n",
"Key Diagnoses:\n",
"- Tetralogy of Fallot (TOF) (2016 - Birth)\n",
"- Developmental Delay (2018)\n",
"- Failure to Thrive (2019) \n",
"- Sensory Processing Disorder (SPD) (2020)\n",
"- Attention-Deficit/Hyperactivity Disorder (ADHD) (2022)\n",
"- Sensorineural Hearing Loss (2023)\n",
"\n",
"Medications:\n",
"- Amiodarone (post-operative arrhythmias - 2017)\n",
"\n",
"Other Treatments:\n",
"- Surgical Repair of TOF (2017)\n",
"- Early Intervention Services (2018) \n",
"- Speech Therapy (2018 - ongoing)\n",
"- Occupational Therapy (2019 - 2024) \n",
"- Integrated Preschool Program with IEP (2021)\n",
"- Behavioral Therapy for ADHD (2022)\n",
"- Cochlear Implant Surgery (2023)\n",
"- Mainstream School with IEP Support (2023 - ongoing)\n",
"- Balloon Valvuloplasty for Pulmonary Stenosis (2022)\n",
"\n",
"Recent Concerns: \n",
"- Progression of Pulmonary Stenosis requiring intervention (2022)\n",
"- Ongoing speech development delays\n",
"\n",
"Action Items:\n",
"- Monitor cardiac status, pulmonary valve function\n",
"- Continue speech and language therapy \n",
"- Assess need for ongoing ADHD treatment and IEP accommodations\n",
"- Ensure appropriate developmental support in mainstream education setting\n",
"</summary>\n"
]
}
],
"source": [
"generate_summary_with_improved_prompt(patient_records[2])"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"<summary>\n",
"Name: Jason Tran\n",
"Age: 25\n",
"\n",
"Key Diagnoses:\n",
"- Asthma (2010)\n",
"- Concussions (2012, 2014)\n",
"- Post-Concussion Syndrome (2014)\n",
"- Depression and Anxiety (2015)\n",
"- ACL Tear (2016)\n",
"- PTSD (2018)\n",
"- Sleep Apnea (2021)\n",
"\n",
"Medications:\n",
"- Albuterol inhaler (asthma)\n",
"- Fluoxetine (depression, anxiety)\n",
"\n",
"Other Treatments:\n",
"- Cognitive Rehabilitation Therapy (post-concussion syndrome)\n",
"- Cognitive-Behavioral Therapy (CBT) (PTSD, anxiety)\n",
"- Exposure Therapy (PTSD)\n",
"- Mindfulness Meditation and Yoga (anxiety)\n",
"- CPAP Therapy (sleep apnea)\n",
"- Surgical Fixation (fractured tibia, 2013)\n",
"- ACL Reconstruction Surgery (2016)\n",
"- Physical Rehabilitation (post-ACL surgery)\n",
"\n",
"Recent Concerns:\n",
"- Returning to sports and managing anxiety/PTSD\n",
"- Sleep quality and sleep apnea\n",
"\n",
"Action Items:\n",
"- Monitor asthma control and medication adherence\n",
"- Continue CBT and exposure therapy for PTSD/anxiety\n",
"- Assess need for ongoing CPAP therapy for sleep apnea\n",
"- Encourage gradual return to low-impact sports and training\n",
"- Support youth concussion awareness efforts\n",
"</summary>\n"
]
}
],
"source": [
"generate_summary_with_improved_prompt(patient_records[3])"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===============================\n",
"<summary>\n",
"Name: Amira Khan\n",
"Age: 36\n",
"\n",
"Key Diagnoses:\n",
"- Fibromyalgia (2011)\n",
"- Cutaneous Lupus Erythematosus (CLE) (2012)\n",
"- Systemic Lupus Erythematosus (SLE) (2013)\n",
"- Lupus Nephritis (2014)\n",
"- Interstitial Lung Disease (ILD) (2015)\n",
"- Sjögren's Syndrome (2016)\n",
"- Lupus Cerebritis (2017)\n",
"- Avascular Necrosis (AVN) of Hip (2018)\n",
"- Steroid-induced Diabetes (2021)\n",
"- Diastolic Dysfunction (2023)\n",
"\n",
"Medications:\n",
"- Pregabalin (fibromyalgia)\n",
"- Hydroxychloroquine (SLE)\n",
"- Prednisone (SLE)\n",
"- Mycophenolate Mofetil (lupus nephritis)\n",
"- Cyclophosphamide (ILD)\n",
"- Pilocarpine (Sjögren's syndrome)\n",
"- Rituximab (lupus cerebritis)\n",
"- Belimumab (reduce flare frequency)\n",
"- Metformin and Insulin (steroid-induced diabetes)\n",
"- Abatacept (SLE flare)\n",
"- ACE Inhibitors (diastolic dysfunction)\n",
"\n",
"Other Treatments:\n",
"- Physical Therapy (fibromyalgia, joint health)\n",
"- Topical Corticosteroids and Sunscreen (CLE)\n",
"- Home Oxygen Therapy (ILD)\n",
"- Total Hip Replacement Surgery (AVN)\n",
"- Yoga (joint health)\n",
"- Cardio-rehab (diastolic dysfunction)\n",
"\n",
"Recent Concerns:\n",
"- SLE Flare-up (2022)\n",
"- Early Diastolic Dysfunction (2023)\n",
"- Planning Pregnancy (2024)\n",
"\n",
"Action Items:\n",
"- Optimize SLE management, monitor disease activity\n",
"- Continue tapering immunosuppressants with close monitoring\n",
"- Lifestyle modifications for diabetes and cardiac health \n",
"- Evaluate fertility and pregnancy plans with high-risk OB\n",
"- Monitor for potential medication interactions or teratogenicity\n",
"</summary>\n"
]
}
],
"source": [
"generate_summary_with_improved_prompt(patient_records[4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's working great! Each of the outputs follows the rules and format that we specified. \n",
"Scroll back to the top to see the types of outputs Claude generated for us with our initial prompt and compare those to our new consistent outputs!\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Switching things up: JSON!\n",
"\n",
"We might be perfectly happy to have Claude generate the text summaries that our current prompt results in, but often we want specific structured responses that are easier to programmatically digest. The most common approach is to use JSON. \n",
"\n",
"**Note: The easiest way to 'force' a JSON response is through Claude's tool use functionality, [which we cover in a separate lesson in our tool-use course](https://github.com/anthropics/courses/blob/master/ToolUse/03_structured_outputs.ipynb). The point of this demonstration is to show the impact that changing a prompt can have.**\n",
"\n",
"To adapt our current prompt so that it generates a JSON response, we need to make a few changes:\n",
"* Explicitly tell Claude we want a JSON result and specify what the JSON output should include.\n",
"* Update our example summary to be a JSON output."
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"# Unchanged System Prompt\n",
"system = \"\"\"\n",
"You are a highly experienced medical professional with a specialty in translating complex patient histories into concise, actionable summaries. \n",
"Your role is to analyze patient records, identify critical information, and present it in a clear, structured format that aids in diagnosis and treatment planning. \n",
"Your summaries are invaluable for busy healthcare providers who need quick insights into a patient's medical history before appointments.\n",
"\"\"\"\n",
"\n",
"#Prompt updated to generate JSON\n",
"updated_json_prompt = \"\"\"\n",
"I need your help summarizing patient medical records for our team of doctors.\n",
"We have a series of follow-up appointments tomorrow, and the doctors need quick, insightful summaries to prepare. \n",
"\n",
"Please provide these summaries in JSON format with the following structure:\n",
"{\n",
" \"name\": \"Patient's full name\",\n",
" \"age\": patient's age as an integer,\n",
" \"key_diagnoses\": [\n",
" {\n",
" \"diagnosis\": \"Primary diagnosis\",\n",
" \"year\": year of diagnosis as an integer\n",
" },\n",
" ...\n",
" ],\n",
" \"medications\": [\n",
" {\n",
" \"name\": \"Medication name\",\n",
" \"purpose\": \"Brief description of what it's for\"\n",
" },\n",
" ...\n",
" ],\n",
" \"other_treatments\": [\n",
" {\n",
" \"treatment\": \"Treatment name\",\n",
" \"purpose\": \"Brief description of what it's for\"\n",
" },\n",
" ...\n",
" ],\n",
" \"recent_concerns\": [\n",
" \"Brief statement of recent health issue or concern\"\n",
" ],\n",
" \"action_items\": [\n",
" \"Action item 1\",\n",
" \"Action item 2\",\n",
" ...\n",
" ]\n",
"}\n",
"\n",
"Here's an example of how we'd like the summaries formatted:\n",
"\n",
"<example>\n",
"<patient_record>\n",
"Patient Name: Ethan Blackwood\n",
"Age: 55\n",
"Medical Record:\n",
"\n",
"2010: Annual check-up, mild hypertension noted\n",
" - Started on lifestyle modifications (diet, exercise)\n",
"2012: Diagnosed with moderate depression following job loss\n",
" - Started on sertraline and cognitive-behavioral therapy (CBT)\n",
"2014: New job, reported improved mood\n",
" - Continued sertraline, reduced CBT sessions\n",
"2015: Mild back pain, diagnosed with early degenerative disc disease\n",
" - Physical therapy and over-the-counter NSAIDs prescribed\n",
"2016: Hypertension worsened, started on lisinopril\n",
"2017: Routine colonoscopy showed benign polyps, removed during procedure\n",
"2018: Developed persistent cough, chest X-ray clear\n",
" - Diagnosed with Gastroesophageal Reflux Disease (GERD)\n",
" - Started on omeprazole\n",
"2019: Diagnosed with obstructive sleep apnea (OSA)\n",
" - Started CPAP therapy, reported improved energy levels\n",
"2020: COVID-19 pandemic, worked from home\n",
" - Reported increased anxiety, CBT sessions resumed (telehealth)\n",
" - COVID-19 vaccination (Moderna, both doses)\n",
"2021: Mild knee pain, MRI showed minor meniscus tear\n",
" - Arthroscopic surgery recommended, patient opted for conservative management\n",
"2022: Annual check-up showed pre-diabetes (A1C: 6.1%)\n",
" - Intensified lifestyle modifications, referred to nutritionist\n",
"- Discontinued omeprazole due to resolved GERD symptoms\n",
"2023: Blood tests showed elevated PSA (Prostate-Specific Antigen)\n",
" - Prostate biopsy performed, results negative for cancer\n",
"- Knee pain worsened, agreed to arthroscopic surgery\n",
"2024: Post-op knee recovery: good, continuing physical therapy\n",
" - A1C levels improved (5.8%), pre-diabetes resolved\n",
" - Stress test normal, but mild LVH on echocardiogram\n",
" - Started on low-dose ACE inhibitor for cardioprotection\n",
"</patient_record>\n",
"\n",
"\n",
"<summary>\n",
"{\n",
" \"name\": \"Ethan Blackwood\",\n",
" \"age\": 55,\n",
" \"key_diagnoses\": [\n",
" {\n",
" \"diagnosis\": \"Hypertension\",\n",
" \"year\": 2010\n",
" },\n",
" {\n",
" \"diagnosis\": \"Depression\",\n",
" \"year\": 2012\n",
" },\n",
" {\n",
" \"diagnosis\": \"Degenerative Disc Disease\",\n",
" \"year\": 2015\n",
" },\n",
" {\n",
" \"diagnosis\": \"Gastroesophageal Reflux Disease (GERD)\",\n",
" \"year\": 2018\n",
" },\n",
" {\n",
" \"diagnosis\": \"Obstructive Sleep Apnea (OSA)\",\n",
" \"year\": 2019\n",
" },\n",
" {\n",
" \"diagnosis\": \"Pre-diabetes\",\n",
" \"year\": 2022\n",
" },\n",
" {\n",
" \"diagnosis\": \"Left Ventricular Hypertrophy (LVH)\",\n",
" \"year\": 2024\n",
" }\n",
" ],\n",
" \"medications\": [\n",
" {\n",
" \"name\": \"Lisinopril\",\n",
" \"purpose\": \"Hypertension management\"\n",
" },\n",
" {\n",
" \"name\": \"Sertraline\",\n",
" \"purpose\": \"Depression\"\n",
" },\n",
" {\n",
" \"name\": \"ACE inhibitor (low-dose)\",\n",
" \"purpose\": \"Cardioprotection\"\n",
" }\n",
" ],\n",
" \"other_treatments\": [\n",
" {\n",
" \"treatment\": \"Cognitive Behavioral Therapy (CBT)\",\n",
" \"purpose\": \"Depression management\"\n",
" },\n",
" {\n",
" \"treatment\": \"Physical therapy\",\n",
" \"purpose\": \"Back pain and knee recovery\"\n",
" },\n",
" {\n",
" \"treatment\": \"CPAP therapy\",\n",
" \"purpose\": \"Obstructive Sleep Apnea\"\n",
" },\n",
" {\n",
" \"treatment\": \"Arthroscopic knee surgery\",\n",
" \"purpose\": \"Meniscus tear repair\"\n",
" }\n",
" ],\n",
" \"recent_concerns\": [\n",
" \"Worsening knee pain, addressed with surgery\",\n",
" \"Elevated PSA (2023), biopsy negative\",\n",
" \"Mild left ventricular hypertrophy (2024)\"\n",
" ],\n",
" \"action_items\": [\n",
" \"Follow up on post-op knee recovery and PT progress\",\n",
" \"Monitor PSA levels and prostate health\",\n",
" \"Assess cardiac health post-LVH finding\",\n",
" \"Review blood pressure management\"\n",
" ]\n",
"}\n",
"</summary>\n",
"</example>\n",
"\n",
"Now, please summarize the following patient record in the same format. Output your JSON sumary inside of <summary> tags\n",
"\"\"\"\n",
"\n",
"#Broke this part into its own variable to make it easier to use the `format` method later\n",
"# (the JSON curly braces in the above prompt variable cause problems when using `format` )\n",
"medical_record_input_prompt = \"\"\"\n",
"<patient_record>\n",
"{record}\n",
"</patient_record>\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's test it out using the following function:"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"def generate_summary_with_json_prompt(patient_record):\n",
" final_prompt_part = medical_record_input_prompt.format(record=patient_record) #add the medical record to the final prompt piece\n",
" complete_prompt = updated_json_prompt + final_prompt_part\n",
" response = client.messages.create(\n",
" model=\"claude-3-sonnet-20240229\",\n",
" max_tokens=4096,\n",
" system=system, #add in our system prompt!\n",
" messages=[{\"role\": \"user\", \"content\": complete_prompt}]\n",
" )\n",
" print(response.content[0].text)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<summary>\n",
"{\n",
" \"name\": \"Evelyn Thompson\",\n",
" \"age\": 78,\n",
" \"key_diagnoses\": [\n",
" {\n",
" \"diagnosis\": \"Type 2 Diabetes\",\n",
" \"year\": 1985\n",
" },\n",
" {\n",
" \"diagnosis\": \"Hypertension\",\n",
" \"year\": 1992\n",
" },\n",
" {\n",
" \"diagnosis\": \"Osteoarthritis\",\n",
" \"year\": 1998\n",
" },\n",
" {\n",
" \"diagnosis\": \"Hypothyroidism\",\n",
" \"year\": 2000\n",
" },\n",
" {\n",
" \"diagnosis\": \"Atrial Fibrillation\",\n",
" \"year\": 2005\n",
" },\n",
" {\n",
" \"diagnosis\": \"Vitamin B12 Deficiency\",\n",
" \"year\": 2008\n",
" },\n",
" {\n",
" \"diagnosis\": \"Transient Ischemic Attack (TIA)\",\n",
" \"year\": 2011\n",
" },\n",
" {\n",
" \"diagnosis\": \"Breast Cancer (Stage 2)\",\n",
" \"year\": 2013\n",
" },\n",
" {\n",
" \"diagnosis\": \"Chronic Kidney Disease (CKD Stage 3)\",\n",
" \"year\": 2015\n",
" },\n",
" {\n",
" \"diagnosis\": \"Mild Cognitive Impairment\",\n",
" \"year\": 2019\n",
" }\n",
" ],\n",
" \"medications\": [\n",
" {\n",
" \"name\": \"Metformin\",\n",
" \"purpose\": \"Diabetes management\"\n",
" },\n",
" {\n",
" \"name\": \"Lisinopril\",\n",
" \"purpose\": \"Hypertension management\"\n",
" },\n",
" {\n",
" \"name\": \"Levothyroxine\",\n",
" \"purpose\": \"Hypothyroidism treatment\"\n",
" },\n",
" {\n",
" \"name\": \"Warfarin\",\n",
" \"purpose\": \"Anticoagulant for atrial fibrillation\"\n",
" },\n",
" {\n",
" \"name\": \"Vitamin B12 Injections\",\n",
" \"purpose\": \"B12 deficiency treatment\"\n",
" },\n",
" {\n",
" \"name\": \"Aspirin\",\n",
" \"purpose\": \"Antiplatelet therapy after TIA\"\n",
" },\n",
" {\n",
" \"name\": \"Anastrozole\",\n",
" \"purpose\": \"Breast cancer recurrence prevention\"\n",
" },\n",
" {\n",
" \"name\": \"Donepezil\",\n",
" \"purpose\": \"Treatment for mild cognitive impairment\"\n",
" },\n",
" {\n",
" \"name\": \"Prophylactic Antibiotics\",\n",
" \"purpose\": \"Prevention of recurrent UTIs\"\n",
" }\n",
" ],\n",
" \"other_treatments\": [\n",
" {\n",
" \"treatment\": \"Total Hip Replacement\",\n",
" \"purpose\": \"Osteoarthritis management\"\n",
" },\n",
" {\n",
" \"treatment\": \"Cataract Surgery\",\n",
" \"purpose\": \"Vision improvement\"\n",
" },\n",
" {\n",
" \"treatment\": \"Lumpectomy and Radiation\",\n",
" \"purpose\": \"Breast cancer treatment\"\n",
" },\n",
" {\n",
" \"treatment\": \"Total Knee Replacement\",\n",
" \"purpose\": \"Osteoarthritis management\"\n",
" },\n",
" {\n",
" \"treatment\": \"IV Antibiotics\",\n",
" \"purpose\": \"Treatment for pneumonia\"\n",
" },\n",
" {\n",
" \"treatment\": \"Physical Therapy\",\n",
" \"purpose\": \"Mobility and functional improvement\"\n",
" },\n",
" {\n",
" \"treatment\": \"Home Health Aide\",\n",
" \"purpose\": \"Assistance with activities of daily living\"\n",
" }\n",
" ],\n",
" \"recent_concerns\": [\n",
" \"Declining mobility and functional status\",\n",
" \"Worsening kidney function (eGFR)\",\n",
" \"Recurrent urinary tract infections (UTIs)\"\n",
" ],\n",
" \"action_items\": [\n",
" \"Review diabetes and hypertension management\",\n",
" \"Monitor kidney function and adjust medications as needed\",\n",
" \"Optimize mobility and functional status through PT and home care\",\n",
" \"Assess cognitive status and efficacy of donepezil\",\n",
" \"Evaluate risk for falls and need for additional safety measures\"\n",
" ]\n",
"}\n",
"</summary>\n"
]
}
],
"source": [
"generate_summary_with_json_prompt(patient_records[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Great, we're getting the JSON we want! \n",
"To demonstrate how easy our new JSON responses are to work with, let's write a function that generates patient summaries for a batch of patients and then outputs a list of all the \"action items\" that a given physician might need for a day of appointments:"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"import json\n",
"\n",
"def generate_summary_with_json_prompt(patient_record):\n",
" final_prompt_part = medical_record_input_prompt.format(record=patient_record) #add the medical record to the final prompt piece\n",
" complete_prompt = updated_json_prompt + final_prompt_part\n",
" response = client.messages.create(\n",
" model=\"claude-3-sonnet-20240229\",\n",
" max_tokens=4096,\n",
" system=system, #add in our system prompt!\n",
" messages=[{\"role\": \"user\", \"content\": complete_prompt}]\n",
" )\n",
" return response.content[0].text\n",
"\n",
"def extract_json_from_summary_tags(summary_string):\n",
" # Use regular expressions to find content between <summary> tags\n",
" match = re.search(r'<summary>\\s*(.*?)\\s*</summary>', summary_string, re.DOTALL)\n",
" if not match:\n",
" print(\"No <summary> tags found in the input string.\")\n",
" return\n",
"\n",
" # Extract the JSON string\n",
" return match.group(1)\n",
"\n",
"def extract_action_items(model_response):\n",
" summary = extract_json_from_summary_tags(model_response)\n",
" try:\n",
" # Parse the JSON string\n",
" summary_data = json.loads(summary)\n",
"\n",
" # Extract and print action items\n",
" action_items = summary_data.get('action_items', [])\n",
" patient_name = summary_data.get('name', \"\")\n",
" print(f\"Action Items for {patient_name}:\")\n",
" for idx, item in enumerate(action_items, 1):\n",
" print(f\"{idx}. {item}\")\n",
" except json.JSONDecodeError:\n",
" print(\"Failed to parse JSON. Make sure the content between <summary> tags is valid JSON.\")\n",
"\n",
"def generate_daily_action_items(patient_records):\n",
" # Takes a list of patient records, generates a summary for each, and prints the action items:\n",
" for record in patient_records:\n",
" summary = generate_summary_with_json_prompt(record)\n",
" extract_action_items(summary)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Action Items for Evelyn Thompson:\n",
"1. Monitor kidney function and adjust medications as needed\n",
"2. Evaluate cognitive status and optimize treatment\n",
"3. Assess mobility and home safety, consider assistive devices\n",
"4. Follow up on breast cancer surveillance and prevention\n",
"Action Items for Marcus Reyes:\n",
"1. Monitor mental health closely, ensure adequate support system\n",
"2. Evaluate effectiveness of updated medication regimen\n",
"3. Continue lifestyle and diabetes management (diet, exercise)\n",
"4. Follow-up physical therapy for plantar fasciitis\n",
"5. Provide resources for patient and family related to mental health\n",
"Action Items for Lily Chen:\n",
"1. Evaluate current educational and therapy needs\n",
"2. Monitor hearing and speech progress post-cochlear implant\n",
"3. Assess cardiac status and need for future interventions\n",
"4. Continue close developmental monitoring and support\n",
"Action Items for Jason Tran:\n",
"1. Monitor asthma control and medication adherence\n",
"2. Assess mental health status and therapy progress\n",
"3. Evaluate sleep quality and CPAP compliance\n",
"4. Encourage gradual return to low-impact sports\n",
"5. Discuss potential for sports-related head injuries\n",
"Action Items for Amira Khan:\n",
"1. Monitor organ system involvement and adjust treatments\n",
"2. Manage steroid-induced diabetes and cardiovascular risk factors\n",
"3. Provide pre-conception counseling and coordinate high-risk OB care\n",
"4. Optimize physical therapy and lifestyle modifications\n"
]
}
],
"source": [
"generate_daily_action_items(patient_records)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "py311",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}