adding real_world_prompting with vertex

This commit is contained in:
Elie Schoppik
2024-08-26 20:56:24 -04:00
parent 52f25e6c26
commit 891ba16451
127 changed files with 191 additions and 32854 deletions

View File

@@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lesson 2: A real-world prompt\n",
"# 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",
@@ -120,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@@ -291,7 +291,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -300,7 +300,7 @@
"'\\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,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
@@ -330,7 +330,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
@@ -352,20 +352,70 @@
},
{
"cell_type": "code",
"execution_count": 98,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n",
"Your browser has been opened to visit:\n",
"\n",
" https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fsqlservice.login&state=1tuG7GpenqE0tnrhJMJBgmtqMdUItk&access_type=offline&code_challenge=yhf6A8DPqH88gqJR8yLR16ZX_qyDSpfqQmEB8vKECro&code_challenge_method=S256\n",
"\n",
"\n",
"Credentials saved to file: [/Users/elie/.config/gcloud/application_default_credentials.json]\n",
"\n",
"These credentials will be used by any library that requests Application Default Credentials (ADC).\n",
"\n",
"Quota project \"anthropic\" was added to ADC which can be used by Google client libraries for billing and quota. Note that some services may still bill the project owning the resource.\n",
"\n",
"\n",
"Updates are available for some Google Cloud CLI components. To install them,\n",
"please run:\n",
" $ gcloud components update\n",
"\n"
]
}
],
"source": [
"from anthropic import Anthropic\n",
"%pip install -U --quiet python-dotenv google-cloud-aiplatform \"anthropic[vertex]\"\n",
"!gcloud auth application-default login"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"just-aloe-430520-u6 us-central1\n"
]
}
],
"source": [
"from anthropic import AnthropicVertex\n",
"from dotenv import load_dotenv\n",
"import os\n",
"\n",
"load_dotenv()\n",
"client = Anthropic()\n",
"\n",
"project_id = os.environ.get(\"PROJECT_ID\")\n",
"# Where the model is running. e.g. us-central1 or europe-west4 for haiku\n",
"region = os.environ.get(\"REGION\")\n",
"\n",
"client = AnthropicVertex(project_id=project_id, region=region)\n",
"\n",
"print(project_id, region)\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",
" model=\"claude-3-sonnet@20240229\",\n",
" max_tokens=4096,\n",
" messages=[{\"role\": \"user\", \"content\": prompt_with_record}]\n",
" )\n",
@@ -382,7 +432,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -390,32 +440,15 @@
"output_type": "stream",
"text": [
"===============================\n",
"Here is a summary of Evelyn Thompson's 78-year-old medical record:\n",
"Here is a summary of Evelyn Thompson's 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",
"Evelyn is a 78-year-old female with a long history of chronic medical conditions including type 2 diabetes since 1985 (on metformin), hypertension since 1992 (on lisinopril), hypothyroidism since 2000 (on levothyroxine), and atrial fibrillation since 2005 (on warfarin and aspirin). She has also had osteoarthritis requiring a right total hip replacement in 1998 and a left total knee replacement in 2017. \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",
"In 2003, she underwent cataract surgery bilaterally. She was diagnosed with vitamin B12 deficiency in 2008 requiring monthly injections. In 2011, she had a transient ischemic attack. She was diagnosed with stage 2 breast cancer in 2013, treated with lumpectomy, radiation, and anastrozole.\n",
"\n",
"Cancer History: \n",
"- Breast cancer (2013) - currently on anastrozole for recurrence prevention\n",
"She developed chronic kidney disease stage 3 in 2015 requiring metformin adjustment. Other issues include pneumonia in 2018 requiring hospitalization, mild cognitive impairment since 2019 (on donepezil), refractory hypertension in 2020, and recurrent UTIs in 2021 treated with prophylactic antibiotics.\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"
"Recent issues are worsening kidney function based on eGFR in 2022 and declining mobility in 2023 requiring physical therapy and home health aide.\n"
]
}
],
@@ -1151,7 +1184,7 @@
"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",
" 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",
@@ -1687,7 +1720,7 @@
" 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",
" 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",
@@ -1860,7 +1893,7 @@
" 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",
" 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",
@@ -1959,7 +1992,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
"version": "3.11.9"
}
},
"nbformat": 4,