File size: 8,673 Bytes
0b4dd15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20d720d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ffbb9
20d720d
 
 
 
 
 
 
 
 
 
 
 
d10818d
0b4dd15
aeca024
37bbb29
 
 
 
 
 
 
 
d10818d
0b4dd15
 
 
 
 
 
 
37bbb29
0b4dd15
 
 
 
 
 
 
 
 
37bbb29
0b4dd15
 
 
 
37bbb29
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# from agents.tools.voice_tools import VoiceTools
# from agents.tools.llm_tools import LLMTools
# from agents.tools.knowledge_tools import KnowledgeTools
# from agents.tools.validation_tools import ValidationTools
# from crewai import Agent
# from utils.knowledge_base import KnowledgeBase

# class PersonalCoachCrew:
#     def __init__(self, config):
#         self.config = config 
#         # Centralized tool instances
#         self.voice_tools = VoiceTools(self.config)
#         self.llm_tools = LLMTools(self.config)
#         self.knowledge_tools = KnowledgeTools(self.config)
#         self.validation_tools = ValidationTools(self.config)

#         self.knowledge_base = KnowledgeBase(self.config)
#         self._initialize_agents()
#         #self._create_crew()

#     def _initialize_agents(self):
#         # ----- AGENT 1 -----
#         self.conversation_handler = Agent(
#             role="Empathetic Conversation Handler",
#             goal="Understand user's emotional state and needs through compassionate dialogue",
#             backstory="...",
#             verbose=self.config.crew.verbose,
#             allow_delegation=False,
#             tools=[
#                 self.voice_tools.transcribe_audio,
#                 self.voice_tools.detect_emotion,
#                 self.voice_tools.generate_reflective_questions,
#             ]
#         )
#         # ----- AGENT 2 -----
#         self.wisdom_advisor = Agent(
#             role="Wisdom Keeper and Spiritual Guide",
#             goal="Provide personalized guidance drawing from ancient wisdom and modern psychology",
#             backstory="...",
#             verbose=self.config.crew.verbose,
#             allow_delegation=False,
#             tools=[
#                 self.knowledge_tools.search_knowledge,
#                 self.knowledge_tools.extract_wisdom,
#                 self.knowledge_tools.suggest_practices,
#                 self.llm_tools.mistral_chat,
#                 self.llm_tools.generate_advice,
#             ]
#         )
#         # ----- AGENT 3 -----
#         self.response_validator = Agent(
#             role="Response Guardian and Quality Validator",
#             goal="Ensure all responses are safe, appropriate, and truly helpful",
#             backstory="...",
#             verbose=self.config.crew.verbose,
#             allow_delegation=False,
#             tools=[
#                 self.validation_tools.validate_response_tool
               
                
#             ]
#         )
#         # ----- AGENT 4 -----
#         self.interaction_manager = Agent(
#             role="Conversation Flow Manager",
#             goal="Create natural, engaging dialogue that helps users on their journey",
#             backstory="...",
#             verbose=self.config.crew.verbose,
#             allow_delegation=False,
#             tools=[
#                 self.llm_tools.summarize_conversation,
#             ]
#         )
#     def process(self, inputs: dict):
#         user_message = inputs.get("user_message", "")
#         # Optionally, add conversation history entries as prior messages.
#         messages = []
#         for his in inputs.get("conversation_history", []):
#             if len(his) == 2:
#                 messages.append({"role": "user", "content": his[0]})
#                 messages.append({"role": "assistant", "content": his[1]})
#         # Add current user message
#         messages.append({"role": "user", "content": user_message})

#         # 1. Empathetic dialog
#         conversation_response = self.conversation_handler.kickoff(messages)

#         # 2. Wisdom/advice — also provide messages (same as for conversation_handler)
#         wisdom_response = self.wisdom_advisor.kickoff(messages)

#         # Combine/mix as fits your logic
#         combined_response = f"{conversation_response}\n{wisdom_response}"

#         # For validation, create appropriate messages object
#         validation_messages = [{"role": "assistant", "content": combined_response}]
#         validator_result = self.response_validator.kickoff(validation_messages)

#         return {
#             "final_response": combined_response
#         }
from agents.tools.voice_tools import VoiceTools
from agents.tools.llm_tools import LLMTools
from agents.tools.knowledge_tools import KnowledgeTools
from agents.tools.validation_tools import ValidationTools
from crewai import Agent
from utils.knowledge_base import KnowledgeBase

class PersonalCoachCrew:
    def __init__(self, config):
        self.config = config 
        # Centralized tool instances
        self.voice_tools = VoiceTools(self.config)
        self.llm_tools = LLMTools(self.config)
        self.knowledge_tools = KnowledgeTools(self.config)
        self.validation_tools = ValidationTools(self.config)
        self.knowledge_base = KnowledgeBase(self.config)
        self._initialize_agents()

    def _initialize_agents(self):
        # ----- AGENT 1 -----
        self.conversation_handler = Agent(
            role="Empathetic Conversation Handler",
            goal="Understand user's emotional state and needs through compassionate dialogue",
            backstory="...",
            verbose=self.config.crew.verbose,
            allow_delegation=False,
            tools=[
                self.voice_tools.transcribe_audio,
                self.voice_tools.detect_emotion,
                self.voice_tools.generate_reflective_questions,
            ]
        )
        # ----- AGENT 2 -----
        self.wisdom_advisor = Agent(
            role="Wisdom Keeper and Spiritual Guide",
            goal="Provide personalized guidance drawing from ancient wisdom and modern psychology",
            backstory="...",
            verbose=self.config.crew.verbose,
            allow_delegation=False,
            tools=[
                self.knowledge_tools.search_knowledge,
                self.knowledge_tools.extract_wisdom,
                self.knowledge_tools.suggest_practices,
                self.llm_tools.mistral_chat,
                self.llm_tools.generate_advice,
            ]
        )
        # ----- AGENT 3 -----
        self.response_validator = Agent(
            role="Response Guardian and Quality Validator",
            goal="Ensure all responses are safe, appropriate, and truly helpful",
            backstory="...",
            verbose=self.config.crew.verbose,
            allow_delegation=False,
            tools=[
                self.validation_tools.validate_response_tool
            ]
        )
        # ----- AGENT 4 -----
        self.interaction_manager = Agent(
            role="Conversation Flow Manager",
            goal="Create natural, engaging dialogue that helps users on their journey",
            backstory="...",
            verbose=self.config.crew.verbose,
            allow_delegation=False,
            tools=[
                self.llm_tools.summarize_conversation,
            ]
        )

    def process(self, inputs: dict):
        user_message = inputs.get("user_message", "")
        # Optionally, add conversation history entries as prior messages.
        messages = []
        for his in inputs.get("conversation_history", []):
            if len(his) == 2:
                messages.append({"role": "user", "content": his[0]})
                messages.append({"role": "assistant", "content": his[1]})
        messages.append({"role": "user", "content": user_message})

        # Empathetic dialog
        conv_result = self.conversation_handler.kickoff(messages)
        # Accept either dict or string result
        if isinstance(conv_result, dict):
            conv_text = conv_result.get("output") or conv_result.get("text") or conv_result.get("final_answer") or str(conv_result)
        else:
            conv_text = str(conv_result).strip()

        # Wisdom/advisor
        wisdom_result = self.wisdom_advisor.kickoff(messages)
        if isinstance(wisdom_result, dict):
            wisdom_text = wisdom_result.get("output") or wisdom_result.get("text") or wisdom_result.get("final_answer") or str(wisdom_result)
        else:
            wisdom_text = str(wisdom_result).strip()
            
        # Final combined response (customize as necessary)
        combined_response = f"{conv_text}\n{wisdom_text}"

        # === VALIDATION: Pass only a string as 'response', never a dict ===
        # Compose tool input as expected by ValidateResponseTool
        validation_tool_input = [{"role": "user", "content": combined_response}]
        _ = self.response_validator.kickoff(validation_tool_input)

        return {
            "final_response": combined_response
        }