Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import anthropic
|
|
| 4 |
|
| 5 |
# Initialize the Anthropic client with your API key
|
| 6 |
client = anthropic.Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))
|
| 7 |
-
MODEL = "claude-3-5-sonnet-
|
| 8 |
|
| 9 |
SYSTEM_PROMPT = """
|
| 10 |
You are a compassionate AI medical assistant that uses Logic-of-Thought (LoT) and Self-Consistency (SC) reasoning techniques to analyze patient symptoms and provide medical education. Your goal is to deliver clear, empathetic explanations of possible conditions, treatments, and next steps based on the information provided. You will only offer a differential diagnosis if there is enough detail to make an informed response. Always remind the patient to consult a healthcare provider for a formal diagnosis and treatment when necessary.
|
|
@@ -23,7 +23,7 @@ When the patient provides new information in subsequent messages, incorporate it
|
|
| 23 |
Provide Educational Explanations:
|
| 24 |
|
| 25 |
Offer possible explanations based on the symptoms and data provided. If enough information is available, present the most likely causes in clear, understandable language. If not, request additional information.
|
| 26 |
-
Avoid jumping to conclusions when there are insufficient details. Ask relevant follow-up questions to clarify the patient
|
| 27 |
Offer General Treatment Suggestions:
|
| 28 |
|
| 29 |
Present over-the-counter remedies, lifestyle changes, or home management strategies based on the symptoms described. Emphasize that these are general educational suggestions and should not replace a healthcare provider's advice.
|
|
@@ -34,7 +34,7 @@ Internal Reasoning Process (Not Shown to the Patient):
|
|
| 34 |
|
| 35 |
Extract Key Information (Logical Propositions):
|
| 36 |
|
| 37 |
-
Internally capture the patient
|
| 38 |
Apply Logical Reasoning Principles (Logic-of-Thought):
|
| 39 |
|
| 40 |
Based on the gathered information, explore relationships between symptoms and potential conditions. If not enough data is provided, ask for more detail before proceeding with any diagnosis.
|
|
@@ -65,24 +65,29 @@ def chat_with_claude(message, history):
|
|
| 65 |
messages = []
|
| 66 |
response_content = ""
|
| 67 |
|
| 68 |
-
# Process the conversation history
|
| 69 |
-
for
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Add the new user message
|
| 78 |
-
messages.append({
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# Stream response from Claude with system prompt at top level
|
| 81 |
with client.messages.stream(
|
| 82 |
max_tokens=5000,
|
| 83 |
system=SYSTEM_PROMPT,
|
| 84 |
messages=messages,
|
| 85 |
-
model=MODEL
|
| 86 |
) as stream:
|
| 87 |
for text in stream.text_stream:
|
| 88 |
response_content += text
|
|
@@ -133,9 +138,8 @@ demo = gr.ChatInterface(
|
|
| 133 |
"Tell me about iron deficiency",
|
| 134 |
"How does a cell make energy?"
|
| 135 |
],
|
| 136 |
-
theme=gr.themes.Base(),
|
| 137 |
css=custom_css,
|
| 138 |
-
type='messages'
|
| 139 |
)
|
| 140 |
|
| 141 |
# Launch the interface
|
|
|
|
| 4 |
|
| 5 |
# Initialize the Anthropic client with your API key
|
| 6 |
client = anthropic.Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))
|
| 7 |
+
MODEL = "claude-3-5-sonnet-20241022"
|
| 8 |
|
| 9 |
SYSTEM_PROMPT = """
|
| 10 |
You are a compassionate AI medical assistant that uses Logic-of-Thought (LoT) and Self-Consistency (SC) reasoning techniques to analyze patient symptoms and provide medical education. Your goal is to deliver clear, empathetic explanations of possible conditions, treatments, and next steps based on the information provided. You will only offer a differential diagnosis if there is enough detail to make an informed response. Always remind the patient to consult a healthcare provider for a formal diagnosis and treatment when necessary.
|
|
|
|
| 23 |
Provide Educational Explanations:
|
| 24 |
|
| 25 |
Offer possible explanations based on the symptoms and data provided. If enough information is available, present the most likely causes in clear, understandable language. If not, request additional information.
|
| 26 |
+
Avoid jumping to conclusions when there are insufficient details. Ask relevant follow-up questions to clarify the patient's situation.
|
| 27 |
Offer General Treatment Suggestions:
|
| 28 |
|
| 29 |
Present over-the-counter remedies, lifestyle changes, or home management strategies based on the symptoms described. Emphasize that these are general educational suggestions and should not replace a healthcare provider's advice.
|
|
|
|
| 34 |
|
| 35 |
Extract Key Information (Logical Propositions):
|
| 36 |
|
| 37 |
+
Internally capture the patient's symptoms and medical history as logical propositions (e.g., P1: stomach pain for 2 days, P2: diarrhea). Use these details to form your reasoning but do not display this internal process.
|
| 38 |
Apply Logical Reasoning Principles (Logic-of-Thought):
|
| 39 |
|
| 40 |
Based on the gathered information, explore relationships between symptoms and potential conditions. If not enough data is provided, ask for more detail before proceeding with any diagnosis.
|
|
|
|
| 65 |
messages = []
|
| 66 |
response_content = ""
|
| 67 |
|
| 68 |
+
# Process the conversation history with proper message formatting
|
| 69 |
+
for user_msg, assistant_msg in history:
|
| 70 |
+
messages.append({
|
| 71 |
+
"role": "user",
|
| 72 |
+
"content": user_msg
|
| 73 |
+
})
|
| 74 |
+
messages.append({
|
| 75 |
+
"role": "assistant",
|
| 76 |
+
"content": assistant_msg
|
| 77 |
+
})
|
| 78 |
|
| 79 |
# Add the new user message
|
| 80 |
+
messages.append({
|
| 81 |
+
"role": "user",
|
| 82 |
+
"content": str(message)
|
| 83 |
+
})
|
| 84 |
|
| 85 |
# Stream response from Claude with system prompt at top level
|
| 86 |
with client.messages.stream(
|
| 87 |
max_tokens=5000,
|
| 88 |
system=SYSTEM_PROMPT,
|
| 89 |
messages=messages,
|
| 90 |
+
model=MODEL
|
| 91 |
) as stream:
|
| 92 |
for text in stream.text_stream:
|
| 93 |
response_content += text
|
|
|
|
| 138 |
"Tell me about iron deficiency",
|
| 139 |
"How does a cell make energy?"
|
| 140 |
],
|
| 141 |
+
theme=gr.themes.Base(),
|
| 142 |
css=custom_css,
|
|
|
|
| 143 |
)
|
| 144 |
|
| 145 |
# Launch the interface
|