How to use LLM for medical question answering
Quick answer
Use a large language model like
gpt-4o-mini or claude-3-5-sonnet-20241022 with carefully crafted prompts to answer medical questions. Incorporate domain-specific context or retrieval-augmented generation (RAG) to improve accuracy and safety in medical question answering.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 Installing collected packages: openai Successfully installed openai-1.x.x
Step by step
This example shows how to use gpt-4o-mini to answer a medical question with a clear prompt and handle the response.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
medical_question = "What are the common symptoms of diabetes?"
messages = [
{"role": "system", "content": "You are a helpful medical assistant providing accurate and safe medical information."},
{"role": "user", "content": medical_question}
]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
max_tokens=256
)
answer = response.choices[0].message.content
print("Medical answer:", answer) output
Medical answer: Common symptoms of diabetes include increased thirst, frequent urination, unexplained weight loss, fatigue, blurred vision, and slow-healing sores. If you suspect diabetes, consult a healthcare professional for diagnosis and treatment.
Common variations
You can use claude-3-5-sonnet-20241022 from Anthropic for medical QA or implement retrieval-augmented generation (RAG) by combining LLMs with a medical document retriever for improved accuracy.
from anthropic import Anthropic
import os
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
medical_question = "What treatments are available for hypertension?"
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=256,
system="You are a knowledgeable medical assistant.",
messages=[{"role": "user", "content": medical_question}]
)
print("Medical answer:", response.content) output
Medical answer: Treatments for hypertension include lifestyle changes such as diet and exercise, medications like ACE inhibitors, beta-blockers, diuretics, and calcium channel blockers. Always consult a healthcare provider for personalized treatment.
Troubleshooting
- If the model returns vague or unsafe answers, refine your system prompt to emphasize accuracy and safety.
- Use retrieval-augmented generation to ground answers in trusted medical sources.
- Check API key environment variables if authentication errors occur.
Key Takeaways
- Use domain-specific system prompts to guide LLMs for safe medical answers.
- Combine LLMs with retrieval systems for higher accuracy in medical QA.
- Use
gpt-4o-miniorclaude-3-5-sonnet-20241022for best results in 2026. - Always validate medical answers with professional sources before clinical use.