[docs]classChatbotManagerAgent:""" Central chatbot class that coordinates the different agents to generate a response to the user. The chatbot is composed of the following agents: - IntentRecognizerAgent: Determines the intent of the user input. - ConversationAgent: Generates a response to the user input for "NORMAL" intents. - AssesmentAgent: Generates a response to the user input for the rest of the intents and start and assesment. Attributes: intent_recognizer (IntentRecognizerAgent): The shared singleton instance of the intent classification agent. conversation_agent (ConversationAgent): The agent responsible for handling general, non-assessment conversation. assesment_agent (AssesmentAgent): The agent responsible for administering mental health questionnaires. assessment_repo (AssessmentRepository): Repository for assessment data access, used to check the current assessment state. """CHATBOT_CONFIDENCE_ASSESMENT_THRESHOLD=0.5CHATBOT_ASSESSMENT_AGENT_TYPE="Assesment"CHATBOT_CONVERSATION_AGENT_TYPE="Conversation"def__init__(self):self.intent_recognizer=IntentRecognizerAgent()self.conversation_agent=ConversationAgent()self.assesment_agent=AssesmentAgent()self.assessment_repo=AssessmentRepository()
[docs]defgenerate_bot_response(self,user_input,user_id,conv_id):""" Orchestrates agents to generate a bot response based on the user input and the current conversation. Args: user_input: The message sent by the user. conv_id: ID of the current conversation. assesment_active: Boolean indicating if an assessment is currently active for the user. Returns: response: JSON chatbot response. """assesment_active=self.assessment_repo.is_assessment_active(user_id)response={"intent_detected":None,"agent_response":None,"agent_type":self.CHATBOT_ASSESSMENT_AGENT_TYPE}# By default, response is when assessment is active.ifassesment_active:response["agent_response"]=self.assesment_agent.generate_response(user_input,conv_id)else:agent_response=self._handle_response_when_no_assesment(user_input,conv_id)response["intent_detected"]=agent_response["intent_detected"]response["agent_response"]=agent_response["response"]response["agent_type"]=agent_response["type"]returnresponse
[docs]def_handle_response_when_no_assesment(self,user_input,conversation_id):""" Handles the response generation when there is no active assessment. Determines the intent of the user input and decides whether to start an assessment or return a normal response. Args: user_input: The message sent by the user. conversation_id: ID of the current conversation. Returns: agent_response: JSON response containing the intent detected, response generated, and agent type. """# If there is no current assessment, we need to get the intent of the user input.# To know if we need to start an assessment or just return a normal response.intent,confidence=self.intent_recognizer.predict_intent(user_input)agent_response={"intent_detected":intent,"response":None,"type":None}# Check for conditions to START an assessmentshould_start_assessment=(confidence>=self.CHATBOT_CONFIDENCE_ASSESMENT_THRESHOLDandintent!="Normal"andself.assesment_agent.map_intent_to_assessment(intent)isnotNone)# Handle the two cases based on the check aboveifshould_start_assessment:agent_response["response"]=self.assesment_agent.generate_response(user_input,conversation_id,intent)agent_response["type"]=self.CHATBOT_ASSESSMENT_AGENT_TYPEelse:agent_response["response"]=self.conversation_agent.generate_response(user_input,conversation_id,intent)agent_response["type"]=self.CHATBOT_CONVERSATION_AGENT_TYPEreturnagent_response