Skip to content

Commit b5622de

Browse files
committed
Draft of Claude symptom diagnosis article.
1 parent ab2c024 commit b5622de

File tree

2 files changed

+157
-121
lines changed

2 files changed

+157
-121
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
title: "Symptom Diagnosis with Claude LLM"
3+
date: 2024-04-25 15:18:25 -0500
4+
categories:
5+
- ai
6+
- ml
7+
- llm
8+
- python
9+
- claude
10+
- sklearn
11+
author: steven
12+
---
13+
14+
# Symptom Diagnosis with Claude LLM
15+
_Building an Interactive Diagnostic Tool with Claude's LLM API and Jupyter Widgets_
16+
17+
![](https://raw.githubusercontent.com/git-steven/git-steven.github.io/master/assets/images/claude-diagnose-md.png)
18+
19+
## Introduction
20+
In the field of healthcare, early and accurate diagnosis plays a crucial role in providing effective treatment to patients. With the advancements in artificial intelligence (AI) and natural language processing (NLP), it is now possible to create intelligent diagnostic tools that can assist medical professionals and empower patients. In this article, we will explore how to build a _basic_ interactive diagnostic tool using Claude's LLM API, the Python Anthropic package, and Jupyter Widgets.
21+
22+
## Prerequisites
23+
24+
### Claude account and API key
25+
Go to the [Anthropic Console](https://console.anthropic.com/settings/keys) to create your key. If you haven't created a [Claude](https://claude.ai/) account yet, do it when prompted to; it's easy!
26+
27+
### Python Packages
28+
To get started, make sure you have Python and Jupyter Notebook installed on your machine. You will also need the Anthropic package, which provides a convenient way to interact with Claude's LLM API. We'll also need jupyter widgets. They can all be installed via:
29+
30+
```bash
31+
pip install anthropic jupyterlab jupyterlab-widgets ipywidgets
32+
```
33+
34+
## Jupyter notebook
35+
Start jupyter lab and create a new Jupyter Notebook:
36+
```bash
37+
jupyter lab
38+
```
39+
40+
Create a new cell (as below) to:
41+
* Import necessary code from modules
42+
* Create the [anthropic](https://pypi.org/project/anthropic/) `Client` with your `API_KEY`. In this example, it is stored in an environment variable so it is not published with this example.
43+
* Create the basic `diagnose` function, which accepts a string of symptoms, which can be separated by commas. This function uses the [anthropic](https://pypi.org/project/anthropic/) `Client` to communicate with the Claude API to obtain a basic diagnosis.
44+
45+
46+
```python
47+
import os
48+
49+
import anthropic
50+
from ipywidgets import widgets
51+
from IPython.display import display
52+
53+
client = anthropic.Client(api_key=os.getenv('CLAUDE_API_SECRET'))
54+
55+
PROMPT = """
56+
Given the following symptoms: "{}", what are the possible diagnoses?
57+
"""
58+
def diagnose(symptoms):
59+
prompt = PROMPT.format(symptoms)
60+
message = client.messages.create(
61+
model="claude-3-opus-20240229",
62+
max_tokens=1024,
63+
messages=[
64+
{"role": "user", "content": prompt}
65+
]
66+
)
67+
return message.content
68+
```
69+
70+
## User Interface
71+
_Creating the User Interface with Jupyter Widgets_
72+
Now, let's create an interactive user interface using Jupyter Widgets to accept symptoms from the user and display the possible diagnoses.
73+
74+
In the cell below, we create a simple UI with 3 components:
75+
* a text area widget for entering symptoms
76+
* a submit widget for triggering the diagnosis via the local `on_submit_click` function.
77+
* an output widget for displaying the results
78+
79+
80+
```python
81+
DISCLAIMER_MSG = "Please note that this is not a definitive"
82+
DISCLAIMER_MSG += " diagnosis. Always consult a healthcare"
83+
DISCLAIMER_MSG += " a healthcare professional for proper evaluation"
84+
DISCLAIMER_MSG += " and treatment."
85+
SYMPTOM_LBL = "Describe your symptoms, separated by commas..."
86+
87+
def symptom_checker_widget():
88+
"""
89+
An interactive interface, created with Jupyter widgets
90+
"""
91+
92+
caption = widgets.HTML("<h3>Diagnostics via Claude</h3>")
93+
94+
symptom_input = widgets.Textarea(
95+
description='Symptoms',
96+
placeholder=SYMPTOM_LBL,
97+
layout={'width':'80%', 'height':'80px'}
98+
)
99+
100+
diagnose_button = widgets.Button(description="Diagnose")
101+
output = widgets.Output(layout={
102+
'border': '1px solid black',
103+
'min_height':'80px',
104+
})
105+
output.append_stdout('Diagnosis will appear here.')
106+
107+
def on_submit_click(b):
108+
with output:
109+
output.clear_output()
110+
print('Diagnosing...')
111+
symptoms = symptom_input.value
112+
diagnosis = diagnose(symptoms)
113+
possible_conditions = [m.text for m in diagnosis]
114+
for condition in possible_conditions:
115+
print(f"- {condition}")
116+
print(f"\n\nDISCLAIMER_MSG")
117+
118+
119+
diagnose_button.on_click(on_submit_click)
120+
121+
display(caption, symptom_input, diagnose_button, output)
122+
```
123+
124+
# Running the tool
125+
To run the interactive diagnostic tool, execute the following cell, which simply calls the `symptom_checker_widget` function defined above. The user interface will appear, allowing users to enter their symptoms and click the "Diagnose" button to receive possible diagnoses based on the symptoms provided.
126+
127+
128+
```python
129+
symptom_checker_widget()
130+
```
131+
132+
```bash
133+
HTML(value='<h3>Diagnostics via Claude</h3>')
134+
135+
136+
137+
Textarea(value='', description='Symptoms', layout=Layout(height='80px', width='80%'), placeholder='Describe yo…
138+
139+
140+
141+
Button(description='Diagnose', style=ButtonStyle())
142+
143+
144+
145+
Output(layout=Layout(border_bottom='1px solid black', border_left='1px solid black', border_right='1px solid b…
146+
```
147+
148+
## Conclusion
149+
In this article, we explored how to build an interactive diagnostic tool using Claude's LLM [API](https://docs.anthropic.com/claude/reference/client-sdks), the Python [Anthropic](https://pypi.org/project/anthropic/) package, and [Jupyter Widgets](https://ipywidgets.readthedocs.io/). By leveraging the power of AI and NLP, we created a simple interface that accepts symptoms and provides possible diagnoses.
150+
151+
This diagnostic tool can serve as a valuable resource for both medical professionals and patients, aiding in the early identification of potential health issues. However, it is essential to note that this tool is not a substitute for professional medical advice and should be used in conjunction with proper medical evaluation and treatment.
152+
153+
It is also by no means a complete diagnostics tool. As you continue to develop and refine the diagnostic tool, consider incorporating additional features such as symptom validation, medical terminology recognition, and integration with medical knowledge bases to enhance its accuracy and usability. You might also consider tracking the diagnostic session, so it can be conversational and used to refine a diagnosis. Another feature that might be useful is suggesting other symptoms that might appear along with the submitted ones. This is useful for a patient who doesn't remember all the symptoms, or might think they aren't worth mentioning.
154+
155+
Remember to handle user input securely, ensure the privacy of sensitive health information, and provide appropriate disclaimers regarding the limitations and intended use of the diagnostic tool.
156+
157+
By combining the power of Claude's LLM API, Python, and Jupyter Widgets, you can create innovative healthcare applications that have the potential to revolutionize patient care and empower individuals to take a more active role in managing their health.

_posts/2024-04-26-claude-llm-symptoms.md

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)