| # 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 | |
| } |