Spaces:
Runtime error
Runtime error
invincible-jha
commited on
Commit
·
f76ecb6
1
Parent(s):
29e951a
Fix agent initialization and pydantic model handling
Browse files- agents/conversation_agent.py +44 -2
- agents/orchestrator.py +42 -9
agents/conversation_agent.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from typing import Dict, List
|
| 2 |
-
from crewai import Agent
|
| 3 |
import logging
|
| 4 |
from utils.log_manager import LogManager
|
| 5 |
|
|
@@ -25,6 +25,19 @@ class ConversationAgent(Agent):
|
|
| 25 |
|
| 26 |
self.logger.info("Conversation Agent initialized")
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def process_message(self, message: str, context: Dict = None) -> Dict:
|
| 29 |
"""Process a message and return a therapeutic response"""
|
| 30 |
self.logger.info("Processing message")
|
|
@@ -32,7 +45,7 @@ class ConversationAgent(Agent):
|
|
| 32 |
|
| 33 |
try:
|
| 34 |
# For now, return a simple response since we haven't set up the LLM
|
| 35 |
-
response =
|
| 36 |
|
| 37 |
return {
|
| 38 |
"message": response,
|
|
@@ -48,6 +61,35 @@ class ConversationAgent(Agent):
|
|
| 48 |
"task_type": "error_recovery"
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def get_status(self) -> Dict:
|
| 52 |
"""Get the current status of the agent"""
|
| 53 |
return {
|
|
|
|
| 1 |
from typing import Dict, List
|
| 2 |
+
from crewai import Agent, Task
|
| 3 |
import logging
|
| 4 |
from utils.log_manager import LogManager
|
| 5 |
|
|
|
|
| 25 |
|
| 26 |
self.logger.info("Conversation Agent initialized")
|
| 27 |
|
| 28 |
+
def execute_task(self, task: Task) -> str:
|
| 29 |
+
"""Execute a task assigned to the agent"""
|
| 30 |
+
self.logger.info(f"Executing task: {task.description}")
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# Process the task description as a message
|
| 34 |
+
result = self.process_message(task.description)
|
| 35 |
+
return result["message"]
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
self.logger.error(f"Error executing task: {str(e)}")
|
| 39 |
+
return "I apologize, but I encountered an error processing your request."
|
| 40 |
+
|
| 41 |
def process_message(self, message: str, context: Dict = None) -> Dict:
|
| 42 |
"""Process a message and return a therapeutic response"""
|
| 43 |
self.logger.info("Processing message")
|
|
|
|
| 45 |
|
| 46 |
try:
|
| 47 |
# For now, return a simple response since we haven't set up the LLM
|
| 48 |
+
response = self._generate_therapeutic_response(message, context)
|
| 49 |
|
| 50 |
return {
|
| 51 |
"message": response,
|
|
|
|
| 61 |
"task_type": "error_recovery"
|
| 62 |
}
|
| 63 |
|
| 64 |
+
def _generate_therapeutic_response(self, message: str, context: Dict) -> str:
|
| 65 |
+
"""Generate a therapeutic response based on the input message and context"""
|
| 66 |
+
# Basic response templates
|
| 67 |
+
responses = {
|
| 68 |
+
"greeting": "Hello! I'm here to support you. How are you feeling today?",
|
| 69 |
+
"emotion": "I hear that you're feeling {}. Would you like to tell me more about that?",
|
| 70 |
+
"support": "That sounds challenging. I'm here to listen and support you.",
|
| 71 |
+
"clarification": "Could you tell me more about what you mean by that?",
|
| 72 |
+
"validation": "It's completely normal to feel that way. Your feelings are valid.",
|
| 73 |
+
"default": "I understand and I'm here to help. Could you tell me more about how you're feeling?"
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
# Simple message analysis
|
| 77 |
+
message = message.lower()
|
| 78 |
+
|
| 79 |
+
if "hello" in message or "hi" in message:
|
| 80 |
+
return responses["greeting"]
|
| 81 |
+
elif any(word in message for word in ["sad", "angry", "scared", "happy", "anxious"]):
|
| 82 |
+
emotion = next(word for word in ["sad", "angry", "scared", "happy", "anxious"] if word in message)
|
| 83 |
+
return responses["emotion"].format(emotion)
|
| 84 |
+
elif any(word in message for word in ["help", "support", "difficult", "hard"]):
|
| 85 |
+
return responses["support"]
|
| 86 |
+
elif len(message.split()) < 3:
|
| 87 |
+
return responses["clarification"]
|
| 88 |
+
elif "feel" in message or "feeling" in message:
|
| 89 |
+
return responses["validation"]
|
| 90 |
+
else:
|
| 91 |
+
return responses["default"]
|
| 92 |
+
|
| 93 |
def get_status(self) -> Dict:
|
| 94 |
"""Get the current status of the agent"""
|
| 95 |
return {
|
agents/orchestrator.py
CHANGED
|
@@ -76,19 +76,52 @@ class WellnessOrchestrator:
|
|
| 76 |
context = context or {}
|
| 77 |
|
| 78 |
try:
|
| 79 |
-
#
|
| 80 |
if self._is_crisis(message):
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
self.logger.error(f"Error processing message: {str(e)}")
|
|
|
|
| 76 |
context = context or {}
|
| 77 |
|
| 78 |
try:
|
| 79 |
+
# Create a task based on message type
|
| 80 |
if self._is_crisis(message):
|
| 81 |
+
task = Task(
|
| 82 |
+
description=message,
|
| 83 |
+
agent=self.crisis_agent,
|
| 84 |
+
context=context,
|
| 85 |
+
expected_output="Crisis intervention response"
|
| 86 |
+
)
|
| 87 |
+
response = self.crisis_agent.execute_task(task)
|
| 88 |
+
agent_type = "crisis"
|
| 89 |
|
| 90 |
+
elif "assess" in message.lower() or "evaluate" in message.lower():
|
| 91 |
+
task = Task(
|
| 92 |
+
description=message,
|
| 93 |
+
agent=self.assessment_agent,
|
| 94 |
+
context=context,
|
| 95 |
+
expected_output="Mental health assessment response"
|
| 96 |
+
)
|
| 97 |
+
response = self.assessment_agent.execute_task(task)
|
| 98 |
+
agent_type = "assessment"
|
| 99 |
|
| 100 |
+
elif "meditate" in message.lower() or "mindful" in message.lower():
|
| 101 |
+
task = Task(
|
| 102 |
+
description=message,
|
| 103 |
+
agent=self.mindfulness_agent,
|
| 104 |
+
context=context,
|
| 105 |
+
expected_output="Mindfulness guidance response"
|
| 106 |
+
)
|
| 107 |
+
response = self.mindfulness_agent.execute_task(task)
|
| 108 |
+
agent_type = "mindfulness"
|
| 109 |
|
| 110 |
+
else:
|
| 111 |
+
task = Task(
|
| 112 |
+
description=message,
|
| 113 |
+
agent=self.conversation_agent,
|
| 114 |
+
context=context,
|
| 115 |
+
expected_output="Therapeutic conversation response"
|
| 116 |
+
)
|
| 117 |
+
response = self.conversation_agent.execute_task(task)
|
| 118 |
+
agent_type = "conversation"
|
| 119 |
+
|
| 120 |
+
return {
|
| 121 |
+
"message": response,
|
| 122 |
+
"agent_type": agent_type,
|
| 123 |
+
"task_type": task.expected_output
|
| 124 |
+
}
|
| 125 |
|
| 126 |
except Exception as e:
|
| 127 |
self.logger.error(f"Error processing message: {str(e)}")
|