def detect_intent_with_parameters(project_id, session_id, query_params, language_code, user_input): """Returns the result of detect intent with texts as inputs. Using the same `session_id` between requests allows continuation of the conversaion.""" session_client = dialogflow.SessionsClient() session = session_client.session_path(project_id, session_id) print('Session path: {}\n'.format(session)) text = user_input text_input = TextInput(text=text, language_code=language_code) query_input = QueryInput(text=text_input) response = session_client.detect_intent( session=session, query_input=query_input, query_params=query_params ) print('=' * 20) print('Query text: {}'.format(response.query_result.query_text)) print('Detected intent: {} (confidence: {})\n'.format( response.query_result.intent.display_name, response.query_result.intent_detection_confidence)) print('Fulfillment text: {}\n'.format( response.query_result.fulfillment_text)) return response
def detect_intent_texts(project_id, session_id, text, language_code): session_client = dialogflow.SessionsClient() session = session_client.session_path(project_id, session_id) if text: text_input = TextInput(text=text, language_code=language_code) query_input = QueryInput(text=text_input) response = session_client.detect_intent(session=session, query_input=query_input) return response.query_result.fulfillment_text
def detect_intent_texts(project_id, session_id, text, language_code): session_client = SessionsClient() session = session_client.session_path(project_id, session_id) text_input = TextInput(text=text, language_code=language_code) query_input = QueryInput(text=text_input) response = session_client.detect_intent(session=session, query_input=query_input) if not response.query_result.intent.is_fallback: return response.query_result.fulfillment_text
def detect_intent_text(self, msg): """Use the Dialogflow API to detect a user's intent. Goto the Dialogflow console to define intents and params. :param msg: DialogflowRequest msg :return query_result: Dialogflow's query_result with action parameters :rtype: DialogflowResult """ # Create the Query Input text_input = TextInput(text=msg.query_text, language_code=self._language_code) query_input = QueryInput(text=text_input) # Create QueryParameters user_contexts = utils.converters.contexts_msg_to_struct(msg.contexts) self.last_contexts = utils.converters.contexts_msg_to_struct( self.last_contexts) contexts = self.last_contexts + user_contexts params = QueryParameters(contexts=contexts) try: self.contaChiamateApi += 1 #incremento il contatore chiamate Api ''' response = self._session_cli.detect_intent( session=self._session, query_input=query_input, query_params=params, output_audio_config=self._output_audio_config) ''' response = self._session_cli.detect_intent(session=self._session, query_input=query_input, query_params=params) except google.api_core.exceptions.Cancelled as c: rospy.logwarn("DF_CLIENT: Caught a Google API Client cancelled " "exception. Check request format!:\n{}".format(c)) except google.api_core.exceptions.Unknown as u: rospy.logwarn("DF_CLIENT: Unknown Exception Caught:\n{}".format(u)) except google.api_core.exceptions.ServiceUnavailable: rospy.logwarn( "DF_CLIENT: Deadline exceeded exception caught. The response " "took too long or you aren't connected to the internet!") except google.api_core.exceptions.DeadlineExceeded as u: rospy.logwarn( "DF_CLIENT: DeadlineExceeded Exception Caught:\n{}".format(u)) else: if response is None: rospy.logwarn("DF_CLIENT: No response received!") return None # Salvo il contesto self.last_contexts = utils.converters.contexts_struct_to_msg( response.query_result.output_contexts) # Ritorno la struttura dell'Intent riconosciuto return response.query_result
def detect_intent_texts(text, id): session_client = dialogflow.SessionsClient() project_id = "choco-bot-dfyxjw" session_id = id language_code = "ko" session = session_client.session_path(project_id, session_id) text_input = TextInput(text=text, language_code=language_code) query_input = QueryInput(text=text_input) response = session_client.detect_intent(session=session, query_input=query_input) res = response.query_result.fulfillment_text return res
def send_input(project_id, session_id, text, language_code, expected_intent, expected_params, threshold_confidence=0.6, verbose=0): """Sends a text input and matches detected intent with expected intent Returns True if 'detected intent' matches expected_intent, and is above threshold_confidence, else returns False """ session_client = dialogflow.SessionsClient() session_path = session_client.session_path(project_id, session_id) text_input = TextInput(text=text, language_code=language_code) query_input = QueryInput(text=text_input) response = session_client.detect_intent(session=session_path, query_input=query_input) detected_params = response.query_result.parameters result_intent = (response.query_result.intent.display_name == expected_intent and response.query_result.intent_detection_confidence > threshold_confidence) if (verbose > 0): print('Session path: {}'.format(session_path)) print('Query text: {}'.format(response.query_result.query_text)) print('Detected intent: {} (confidence: {})'.format( response.query_result.intent.display_name, response.query_result.intent_detection_confidence)) print('Expected intent: {} (confidence > {})'.format( expected_intent, threshold_confidence)) # print('Fulfillment text: {}'.format(response.query_result.fulfillment_text)) print( 'Intent matching: {}'.format('Pass' if result_intent else 'Fail')) result_params = check_params(detected_params, expected_params, verbose) return result_intent, result_params
def detect_intent_text(self, msg): """Use the Dialogflow API to detect a user's intent. Goto the Dialogflow console to define intents and params. :param msg: DialogflowRequest msg :return query_result: Dialogflow's query_result with action parameters :rtype: DialogflowResult """ # Create the Query Input text_input = TextInput(text=msg.query_text, language_code=self._language_code) query_input = QueryInput(text=text_input) # Create QueryParameters user_contexts = converters.contexts_msg_to_struct(msg.contexts) self.last_contexts = converters.contexts_msg_to_struct( self.last_contexts) contexts = self.last_contexts + user_contexts params = QueryParameters(contexts=contexts) try: response = self._session_cli.detect_intent( session=self._session, query_input=query_input, query_params=params, output_audio_config=self._output_audio_config) except google.api_core.exceptions.ServiceUnavailable: rospy.logwarn( "DF_CLIENT: Deadline exceeded exception caught. The response " "took too long or you aren't connected to the internet!") else: # Store context for future use self.last_contexts = converters.contexts_struct_to_msg( response.query_result.output_contexts) df_msg = converters.result_struct_to_msg(response.query_result) self._results_pub.publish(df_msg) rospy.loginfo(output.print_result(response.query_result)) # Play audio if self.PLAY_AUDIO: self._play_stream(response.output_audio) return df_msg