Spaces:
Runtime error
Runtime error
invincible-jha
commited on
Commit
·
3aa86cd
1
Parent(s):
f76ecb6
Fix Pydantic model handling with PrivateAttr
Browse files- agents/conversation_agent.py +19 -12
agents/conversation_agent.py
CHANGED
@@ -1,17 +1,23 @@
|
|
1 |
-
from typing import Dict, List
|
2 |
from crewai import Agent, Task
|
3 |
import logging
|
4 |
from utils.log_manager import LogManager
|
|
|
5 |
|
6 |
class ConversationAgent(Agent):
|
7 |
"""Agent specialized in therapeutic conversations"""
|
8 |
|
|
|
|
|
|
|
|
|
|
|
9 |
def __init__(self, model_config: Dict, **kwargs):
|
10 |
-
|
11 |
-
self.
|
12 |
-
self.
|
13 |
|
14 |
-
# Initialize the CrewAI agent
|
15 |
super().__init__(
|
16 |
role="Therapeutic Conversation Specialist",
|
17 |
goal="Guide therapeutic conversations and provide emotional support",
|
@@ -20,14 +26,15 @@ class ConversationAgent(Agent):
|
|
20 |
appropriate boundaries and recognizing when to escalate to crisis intervention.""",
|
21 |
verbose=True,
|
22 |
allow_delegation=False,
|
23 |
-
tools=[] # Tools will be added as needed
|
|
|
24 |
)
|
25 |
|
26 |
-
self.
|
27 |
|
28 |
def execute_task(self, task: Task) -> str:
|
29 |
"""Execute a task assigned to the agent"""
|
30 |
-
self.
|
31 |
|
32 |
try:
|
33 |
# Process the task description as a message
|
@@ -35,16 +42,16 @@ class ConversationAgent(Agent):
|
|
35 |
return result["message"]
|
36 |
|
37 |
except Exception as e:
|
38 |
-
self.
|
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.
|
44 |
context = context or {}
|
45 |
|
46 |
try:
|
47 |
-
#
|
48 |
response = self._generate_therapeutic_response(message, context)
|
49 |
|
50 |
return {
|
@@ -54,7 +61,7 @@ class ConversationAgent(Agent):
|
|
54 |
}
|
55 |
|
56 |
except Exception as e:
|
57 |
-
self.
|
58 |
return {
|
59 |
"message": "I apologize, but I encountered an error. Let me try a different approach.",
|
60 |
"agent_type": "conversation",
|
|
|
1 |
+
from typing import Dict, List, Optional
|
2 |
from crewai import Agent, Task
|
3 |
import logging
|
4 |
from utils.log_manager import LogManager
|
5 |
+
from pydantic import Field, BaseModel, PrivateAttr
|
6 |
|
7 |
class ConversationAgent(Agent):
|
8 |
"""Agent specialized in therapeutic conversations"""
|
9 |
|
10 |
+
# Private attributes for non-pydantic fields
|
11 |
+
_log_manager: LogManager = PrivateAttr(default_factory=LogManager)
|
12 |
+
_logger: logging.Logger = PrivateAttr()
|
13 |
+
_model_config: Dict = PrivateAttr()
|
14 |
+
|
15 |
def __init__(self, model_config: Dict, **kwargs):
|
16 |
+
# Initialize private attributes
|
17 |
+
self._model_config = model_config
|
18 |
+
self._logger = self._log_manager.get_agent_logger("conversation")
|
19 |
|
20 |
+
# Initialize the CrewAI agent with required fields
|
21 |
super().__init__(
|
22 |
role="Therapeutic Conversation Specialist",
|
23 |
goal="Guide therapeutic conversations and provide emotional support",
|
|
|
26 |
appropriate boundaries and recognizing when to escalate to crisis intervention.""",
|
27 |
verbose=True,
|
28 |
allow_delegation=False,
|
29 |
+
tools=[], # Tools will be added as needed
|
30 |
+
**kwargs
|
31 |
)
|
32 |
|
33 |
+
self._logger.info("Conversation Agent initialized")
|
34 |
|
35 |
def execute_task(self, task: Task) -> str:
|
36 |
"""Execute a task assigned to the agent"""
|
37 |
+
self._logger.info(f"Executing task: {task.description}")
|
38 |
|
39 |
try:
|
40 |
# Process the task description as a message
|
|
|
42 |
return result["message"]
|
43 |
|
44 |
except Exception as e:
|
45 |
+
self._logger.error(f"Error executing task: {str(e)}")
|
46 |
return "I apologize, but I encountered an error processing your request."
|
47 |
|
48 |
def process_message(self, message: str, context: Dict = None) -> Dict:
|
49 |
"""Process a message and return a therapeutic response"""
|
50 |
+
self._logger.info("Processing message")
|
51 |
context = context or {}
|
52 |
|
53 |
try:
|
54 |
+
# Generate therapeutic response
|
55 |
response = self._generate_therapeutic_response(message, context)
|
56 |
|
57 |
return {
|
|
|
61 |
}
|
62 |
|
63 |
except Exception as e:
|
64 |
+
self._logger.error(f"Error processing message: {str(e)}")
|
65 |
return {
|
66 |
"message": "I apologize, but I encountered an error. Let me try a different approach.",
|
67 |
"agent_type": "conversation",
|