Clinical note summarization with AI
Quick answer
Use a large language model like
gpt-4o-mini to summarize clinical notes by sending the note text as a prompt and requesting a concise summary. This can be done via the OpenAI Python SDK with prompt engineering to ensure clinical relevance and privacy compliance.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the openai Python package and set your API key as an environment variable for secure access.
pip install openai>=1.0 output
Collecting openai Downloading openai-1.x.x-py3-none-any.whl (xx kB) Installing collected packages: openai Successfully installed openai-1.x.x
Step by step
This example shows how to send a clinical note to the gpt-4o-mini model and receive a concise summary. The prompt instructs the model to focus on key clinical information.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
clinical_note = """
Patient is a 65-year-old male with a history of hypertension and diabetes mellitus type 2. He presents with chest pain and shortness of breath. ECG shows ST elevation in leads II, III, and aVF. Troponin levels are elevated. Treatment started with aspirin and nitroglycerin.
"""
prompt = f"Summarize the following clinical note focusing on diagnosis, treatment, and key findings:\n\n{clinical_note}\n\nSummary:"
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
max_tokens=150
)
summary = response.choices[0].message.content
print("Clinical note summary:\n", summary) output
Clinical note summary: Patient, a 65-year-old male with hypertension and type 2 diabetes, presents with chest pain and shortness of breath. ECG indicates ST elevation in leads II, III, and aVF, and elevated troponin confirms myocardial infarction. Treatment initiated with aspirin and nitroglycerin.
Common variations
You can use asynchronous calls for better performance in web apps, switch to streaming responses for real-time display, or try other models like claude-3-5-haiku-20241022 for different summarization styles.
import os
import asyncio
from openai import OpenAI
async def async_summarize(note: str):
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = f"Summarize the clinical note:\n\n{note}\n\nSummary:"
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
max_tokens=150
)
return response.choices[0].message.content
clinical_note = "Patient with fever and cough, diagnosed with pneumonia. Started antibiotics."
summary = asyncio.run(async_summarize(clinical_note))
print("Async summary:\n", summary) output
Async summary: Patient diagnosed with pneumonia presenting with fever and cough; treatment initiated with antibiotics.
Troubleshooting
- If you receive incomplete summaries, increase
max_tokensor refine your prompt for clarity. - Ensure your clinical data is de-identified to comply with HIPAA and privacy regulations.
- If API calls fail, verify your
OPENAI_API_KEYenvironment variable is set correctly and your network allows outbound HTTPS requests.
Key Takeaways
- Use
gpt-4o-miniwith prompt engineering to extract concise clinical summaries. - Always de-identify patient data before sending to AI APIs to maintain privacy compliance.
- Async and streaming API calls improve responsiveness in clinical applications.
- Adjust
max_tokensand prompt specificity to optimize summary quality.