Spaces:
Sleeping
Sleeping
feat: implement medical API call and enhance soft medical triage functionality
Browse files- src/core/ai_client.py +23 -0
- src/core/core_classes.py +46 -1
src/core/ai_client.py
CHANGED
|
@@ -442,6 +442,29 @@ class AIClientManager:
|
|
| 442 |
call_type="spiritual_analysis",
|
| 443 |
agent_name="SpiritualDistressAnalyzer"
|
| 444 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
|
| 446 |
# Factory function for easy client creation
|
| 447 |
def create_ai_client(agent_name: str) -> UniversalAIClient:
|
|
|
|
| 442 |
call_type="spiritual_analysis",
|
| 443 |
agent_name="SpiritualDistressAnalyzer"
|
| 444 |
)
|
| 445 |
+
|
| 446 |
+
def call_medical_api(self, system_prompt: str, user_prompt: str,
|
| 447 |
+
temperature: float = 0.3) -> str:
|
| 448 |
+
"""
|
| 449 |
+
Call AI API for medical assistance.
|
| 450 |
+
|
| 451 |
+
Uses the soft medical triage agent configuration.
|
| 452 |
+
|
| 453 |
+
Args:
|
| 454 |
+
system_prompt: System prompt for the AI
|
| 455 |
+
user_prompt: User prompt/message for medical guidance
|
| 456 |
+
temperature: Temperature for response generation
|
| 457 |
+
|
| 458 |
+
Returns:
|
| 459 |
+
AI response as string
|
| 460 |
+
"""
|
| 461 |
+
return self.generate_response(
|
| 462 |
+
system_prompt=system_prompt,
|
| 463 |
+
user_prompt=user_prompt,
|
| 464 |
+
temperature=temperature,
|
| 465 |
+
call_type="medical_assistance",
|
| 466 |
+
agent_name="SoftMedicalTriage"
|
| 467 |
+
)
|
| 468 |
|
| 469 |
# Factory function for easy client creation
|
| 470 |
def create_ai_client(agent_name: str) -> UniversalAIClient:
|
src/core/core_classes.py
CHANGED
|
@@ -85,7 +85,52 @@ class SoftMedicalTriage:
|
|
| 85 |
|
| 86 |
def conduct_triage(self, message: str, background: ClinicalBackground, history: List) -> str:
|
| 87 |
"""Conduct soft medical triage."""
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
import os
|
|
|
|
| 85 |
|
| 86 |
def conduct_triage(self, message: str, background: ClinicalBackground, history: List) -> str:
|
| 87 |
"""Conduct soft medical triage."""
|
| 88 |
+
try:
|
| 89 |
+
# Build conversation context
|
| 90 |
+
context = ""
|
| 91 |
+
if history:
|
| 92 |
+
recent_history = history[-6:] # Last 3 exchanges
|
| 93 |
+
context = "Recent conversation:\n"
|
| 94 |
+
for msg in recent_history:
|
| 95 |
+
context += f"{msg.role}: {msg.message}\n"
|
| 96 |
+
context += "\n"
|
| 97 |
+
|
| 98 |
+
# Build patient context
|
| 99 |
+
patient_context = f"""Patient: {background.patient_name}, {background.age} years old
|
| 100 |
+
Medical conditions: {', '.join(background.conditions)}
|
| 101 |
+
Primary goal: {background.primary_goal}
|
| 102 |
+
Exercise preferences: {', '.join(background.exercise_preferences)}
|
| 103 |
+
Exercise limitations: {', '.join(background.exercise_limitations)}"""
|
| 104 |
+
|
| 105 |
+
# Create prompt for medical assistant
|
| 106 |
+
prompt = f"""{context}Patient context:
|
| 107 |
+
{patient_context}
|
| 108 |
+
|
| 109 |
+
Current message: "{message}"
|
| 110 |
+
|
| 111 |
+
Provide helpful medical guidance and support."""
|
| 112 |
+
|
| 113 |
+
# Get system prompt for medical assistant
|
| 114 |
+
from src.config.prompts import SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE
|
| 115 |
+
|
| 116 |
+
# Call AI
|
| 117 |
+
response = self.api.call_medical_api(
|
| 118 |
+
system_prompt=SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE,
|
| 119 |
+
user_prompt=prompt
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
return response
|
| 123 |
+
|
| 124 |
+
except Exception as e:
|
| 125 |
+
# Fallback response
|
| 126 |
+
import logging
|
| 127 |
+
logging.error(f"Medical triage error: {e}")
|
| 128 |
+
|
| 129 |
+
# Detect language for fallback
|
| 130 |
+
if any(ord(c) >= 0x0400 and ord(c) <= 0x04FF for c in message):
|
| 131 |
+
return "Дякую, що звернулися. Як я можу вам допомогти з вашим здоров'ям сьогодні?"
|
| 132 |
+
else:
|
| 133 |
+
return "Thank you for reaching out. How can I help you with your health today?"
|
| 134 |
|
| 135 |
|
| 136 |
import os
|