Spaces:
Sleeping
Sleeping
Added support for conversation history in soft medical triage. The conduct_triage method now accepts a chat_history parameter to take into account the context of previous messages. Updated relevant prompts to improve patient interaction. Changes have also been made to lifestyle_app.py to pass conversation history. All tests passed successfully.
Browse files- core_classes.py +33 -3
- lifestyle_app.py +10 -3
- prompts.py +15 -14
core_classes.py
CHANGED
|
@@ -359,11 +359,21 @@ class SoftMedicalTriage:
|
|
| 359 |
def __init__(self, api: GeminiAPI):
|
| 360 |
self.api = api
|
| 361 |
|
| 362 |
-
def conduct_triage(self, user_message: str, clinical_background: ClinicalBackground
|
| 363 |
-
|
|
|
|
| 364 |
|
| 365 |
system_prompt = SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE
|
| 366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
return self.api.generate_response(
|
| 369 |
system_prompt, user_prompt,
|
|
@@ -372,6 +382,26 @@ class SoftMedicalTriage:
|
|
| 372 |
agent_name="SoftMedicalTriage"
|
| 373 |
)
|
| 374 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
class MedicalAssistant:
|
| 376 |
def __init__(self, api: GeminiAPI):
|
| 377 |
self.api = api
|
|
|
|
| 359 |
def __init__(self, api: GeminiAPI):
|
| 360 |
self.api = api
|
| 361 |
|
| 362 |
+
def conduct_triage(self, user_message: str, clinical_background: ClinicalBackground,
|
| 363 |
+
chat_history: List[ChatMessage] = None) -> str:
|
| 364 |
+
"""Проводить м'який медичний тріаж З УРАХУВАННЯМ КОНТЕКСТУ"""
|
| 365 |
|
| 366 |
system_prompt = SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE
|
| 367 |
+
|
| 368 |
+
# Додаємо історію розмови
|
| 369 |
+
history_text = ""
|
| 370 |
+
if chat_history and len(chat_history) > 1: # Якщо є попередні повідомлення
|
| 371 |
+
recent_history = chat_history[-4:] # Останні 4 повідомлення
|
| 372 |
+
history_text = "\n".join([f"{msg.role}: {msg.message}" for msg in recent_history[:-1]]) # Виключаємо поточне
|
| 373 |
+
|
| 374 |
+
user_prompt = PROMPT_SOFT_MEDICAL_TRIAGE_WITH_CONTEXT(
|
| 375 |
+
clinical_background, user_message, history_text
|
| 376 |
+
)
|
| 377 |
|
| 378 |
return self.api.generate_response(
|
| 379 |
system_prompt, user_prompt,
|
|
|
|
| 382 |
agent_name="SoftMedicalTriage"
|
| 383 |
)
|
| 384 |
|
| 385 |
+
def PROMPT_SOFT_MEDICAL_TRIAGE_WITH_CONTEXT(clinical_background, user_message, history_text):
|
| 386 |
+
context_section = ""
|
| 387 |
+
if history_text.strip():
|
| 388 |
+
context_section = f"""
|
| 389 |
+
CONVERSATION HISTORY:
|
| 390 |
+
{history_text}
|
| 391 |
+
|
| 392 |
+
"""
|
| 393 |
+
|
| 394 |
+
return f"""PATIENT: {clinical_background.patient_name}
|
| 395 |
+
|
| 396 |
+
MEDICAL CONTEXT:
|
| 397 |
+
- Active problems: {"; ".join(clinical_background.active_problems[:3]) if clinical_background.active_problems else "none"}
|
| 398 |
+
- Critical alerts: {"; ".join(clinical_background.critical_alerts) if clinical_background.critical_alerts else "none"}
|
| 399 |
+
|
| 400 |
+
{context_section}PATIENT'S CURRENT MESSAGE: "{user_message}"
|
| 401 |
+
|
| 402 |
+
ANALYSIS REQUIRED:
|
| 403 |
+
Conduct gentle medical triage considering the conversation context. If this is a continuation of an existing conversation, acknowledge it naturally without re-introducing yourself."""
|
| 404 |
+
|
| 405 |
class MedicalAssistant:
|
| 406 |
def __init__(self, api: GeminiAPI):
|
| 407 |
self.api = api
|
lifestyle_app.py
CHANGED
|
@@ -331,8 +331,11 @@ class ExtendedLifestyleJourneyApp:
|
|
| 331 |
lifestyle_mode = classification.get("V", "off")
|
| 332 |
|
| 333 |
if lifestyle_mode == "off":
|
| 334 |
-
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
| 336 |
return response, "medical"
|
| 337 |
|
| 338 |
elif lifestyle_mode == "on":
|
|
@@ -349,7 +352,11 @@ class ExtendedLifestyleJourneyApp:
|
|
| 349 |
|
| 350 |
else:
|
| 351 |
# Fallback to medical mode with soft triage
|
| 352 |
-
response = self.soft_medical_triage.conduct_triage(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
return response, "medical"
|
| 354 |
|
| 355 |
def _handle_hybrid_flow(self, message: str, classification: Dict) -> Tuple[str, str]:
|
|
|
|
| 331 |
lifestyle_mode = classification.get("V", "off")
|
| 332 |
|
| 333 |
if lifestyle_mode == "off":
|
| 334 |
+
response = self.soft_medical_triage.conduct_triage(
|
| 335 |
+
message,
|
| 336 |
+
self.clinical_background,
|
| 337 |
+
self.chat_history
|
| 338 |
+
)
|
| 339 |
return response, "medical"
|
| 340 |
|
| 341 |
elif lifestyle_mode == "on":
|
|
|
|
| 352 |
|
| 353 |
else:
|
| 354 |
# Fallback to medical mode with soft triage
|
| 355 |
+
response = self.soft_medical_triage.conduct_triage(
|
| 356 |
+
message,
|
| 357 |
+
self.clinical_background,
|
| 358 |
+
self.chat_history # Додано!
|
| 359 |
+
)
|
| 360 |
return response, "medical"
|
| 361 |
|
| 362 |
def _handle_hybrid_flow(self, message: str, classification: Dict) -> Tuple[str, str]:
|
prompts.py
CHANGED
|
@@ -251,33 +251,34 @@ Always prioritize patient safety over providing comprehensive answers."""
|
|
| 251 |
SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE = """You are a compassionate medical assistant conducting gentle patient check-ins.
|
| 252 |
|
| 253 |
TASK:
|
| 254 |
-
Provide a warm,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
SOFT TRIAGE APPROACH:
|
| 257 |
-
🤗 **
|
| 258 |
-
🩺 **Gentle health check** with 1-2 brief questions
|
| 259 |
💚 **Supportive readiness** to help with any concerns
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
TRIAGE PRINCIPLES:
|
| 262 |
- **Minimal questioning**: This is a check-in, not an interrogation
|
| 263 |
- **Patient comfort**: Maintain friendly, non-imposing tone
|
|
|
|
| 264 |
- **Safety awareness**: Watch for concerning symptoms
|
| 265 |
- **Transition readiness**: Prepared to move to lifestyle coaching when appropriate
|
| 266 |
|
| 267 |
-
RESPONSE STRUCTURE:
|
| 268 |
-
1. Acknowledge patient warmly
|
| 269 |
-
2. Ask 1-2 gentle questions about current wellbeing
|
| 270 |
-
3. Express availability to help
|
| 271 |
-
|
| 272 |
-
ESCALATION AWARENESS:
|
| 273 |
-
- Watch for urgent symptoms requiring immediate attention
|
| 274 |
-
- Note concerning changes that need medical follow-up
|
| 275 |
-
- Be ready to transition to full medical triage if needed
|
| 276 |
-
|
| 277 |
LANGUAGE MATCHING:
|
| 278 |
Always respond in the same language the patient uses in their message.
|
| 279 |
|
| 280 |
-
Keep responses brief, warm, and
|
| 281 |
|
| 282 |
def PROMPT_MEDICAL_ASSISTANT(clinical_background, active_problems, medications, recent_vitals, history_text, user_message):
|
| 283 |
return f"""PATIENT MEDICAL PROFILE ({clinical_background.patient_name}):
|
|
|
|
| 251 |
SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE = """You are a compassionate medical assistant conducting gentle patient check-ins.
|
| 252 |
|
| 253 |
TASK:
|
| 254 |
+
Provide a warm, contextually-aware health assessment during patient interactions.
|
| 255 |
+
|
| 256 |
+
CONTEXT AWARENESS:
|
| 257 |
+
🧠 **Consider conversation history** - if this is a continuation, acknowledge it naturally
|
| 258 |
+
🔄 **Avoid repetitive greetings** - don't re-introduce yourself if already conversing
|
| 259 |
+
💬 **Build on previous exchanges** - reference earlier topics when relevant
|
| 260 |
|
| 261 |
SOFT TRIAGE APPROACH:
|
| 262 |
+
🤗 **Contextual acknowledgment** of patient's message
|
| 263 |
+
🩺 **Gentle health check** with 1-2 brief questions (if needed)
|
| 264 |
💚 **Supportive readiness** to help with any concerns
|
| 265 |
|
| 266 |
+
RESPONSE LOGIC:
|
| 267 |
+
• **First interaction**: Warm greeting + gentle health check
|
| 268 |
+
• **Continuation**: Natural acknowledgment + focused response to current topic
|
| 269 |
+
• **Medical updates**: Acknowledge improvement/changes + check for other concerns
|
| 270 |
+
|
| 271 |
TRIAGE PRINCIPLES:
|
| 272 |
- **Minimal questioning**: This is a check-in, not an interrogation
|
| 273 |
- **Patient comfort**: Maintain friendly, non-imposing tone
|
| 274 |
+
- **Context-sensitive**: Adapt based on conversation flow
|
| 275 |
- **Safety awareness**: Watch for concerning symptoms
|
| 276 |
- **Transition readiness**: Prepared to move to lifestyle coaching when appropriate
|
| 277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
LANGUAGE MATCHING:
|
| 279 |
Always respond in the same language the patient uses in their message.
|
| 280 |
|
| 281 |
+
Keep responses brief, warm, and contextually appropriate for the conversation stage."""
|
| 282 |
|
| 283 |
def PROMPT_MEDICAL_ASSISTANT(clinical_background, active_problems, medications, recent_vitals, history_text, user_message):
|
| 284 |
return f"""PATIENT MEDICAL PROFILE ({clinical_background.patient_name}):
|