async def excecute_luis_query(configuration: dict, turn_context: TurnContext) -> BookingDetails: booking_details = BookingDetails() try: luis_application = LuisApplication( configuration['LuisApplication'], configuration['LuisAPIKey'], 'https://' + configuration['LuisAPIHostName']) recognizer = LuisRecognizer(luis_application) recognizer_result = await recognizer.recognize(turn_context) intent = sorted( recognizer_result.intents, key=recognizer_result.intents.get, reverse=True)[:1] if recognizer_result.intents else None if intent == 'Book_flight': # We need to get the result from the LUIS JSON which at every level returns an array. booking_details.destination = recognizer_result.entities.get( "To", {}).get("Airport", [])[:1][:1] booking_details.origin = recognizer_result.entities.get( "From", {}).get("Airport", [])[:1][:1] # This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part. # TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year. booking_details.travel_date = recognizer_result.entities.get( "datetime", {}).get("timex", [])[:1].split('T')[0] except Exception as e: print(e) return booking_details
async def execute_luis_query(configuration: dict, turn_context: TurnContext) -> BookingDetails: """Invoke LUIS service to perform prediction/evaluation of utterance.""" booking_details = BookingDetails() # pylint:disable=broad-except try: luis_application = LuisApplication( configuration['LUIS_APP_ID'], configuration['LUIS_API_KEY'], configuration['LUIS_API_HOST_NAME'] ) recognizer = LuisRecognizer(luis_application) recognizer_result = await recognizer.recognize(turn_context) if recognizer_result.intents: intent = sorted(recognizer_result.intents, key=recognizer_result.intents.get, reverse=True)[:1][0] if intent == 'Book_flight': # We need to get the result from the LUIS JSON which at every level returns an array. to_entities = recognizer_result.entities.get("$instance", {}).get("To", []) if to_entities: booking_details.destination = to_entities[0]['text'] from_entities = recognizer_result.entities.get("$instance", {}).get("From", []) if from_entities: booking_details.origin = from_entities[0]['text'] # This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part. # TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year. date_entities = recognizer_result.entities.get("$instance", {}).get("datetime", []) if date_entities: booking_details.travel_date = None # Set when we get a timex format except Exception as exception: print(exception) return booking_details
async def execute_luis_query( configuration, turn_context: TurnContext, telemetry_client: BotTelemetryClient = None, ) -> BookingDetails: """Invoke LUIS service to perform prediction/evaluation of utterance.""" booking_details = BookingDetails() # pylint:disable=broad-except try: luis_application = LuisApplication( configuration.get("LUIS_APP_ID"), configuration.get("LUIS_API_KEY"), configuration.get("LUIS_API_HOST_NAME"), ) options = LuisPredictionOptions() options.telemetry_client = ( telemetry_client if telemetry_client is not None else NullTelemetryClient() ) recognizer = LuisRecognizer(luis_application, prediction_options=options) recognizer_result = await recognizer.recognize(turn_context) print(f"Recognize Result: {recognizer_result}") if recognizer_result.intents: intent = sorted( recognizer_result.intents, key=recognizer_result.intents.get, reverse=True, )[:1][0] if intent == "Book_flight": # We need to get the result from the LUIS JSON which at every level returns an array. to_entities = recognizer_result.entities.get("$instance", {}).get( "To", [] ) if to_entities: booking_details.destination = to_entities[0]["text"] from_entities = recognizer_result.entities.get("$instance", {}).get( "From", [] ) if from_entities: booking_details.origin = from_entities[0]["text"] # This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part. # TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year. date_entities = recognizer_result.entities.get("$instance", {}).get( "datetime", [] ) if date_entities: booking_details.travel_date = ( None ) # Set when we get a timex format except Exception as exception: print(exception) return booking_details
async def execute_luis_query( luis_recognizer: LuisRecognizer, turn_context: TurnContext) -> (Intent, object): result = None intent = None try: recognizer_result = await luis_recognizer.recognize(turn_context) intent = (sorted( recognizer_result.intents, key=recognizer_result.intents.get, reverse=True, )[:1][0] if recognizer_result.intents else None) if intent == Intent.BOOK_FLIGHT.value: result = BookingDetails() # We need to get the result from the LUIS JSON which at every level returns an array. to_entities = recognizer_result.entities.get("$instance", {}).get("To", []) if len(to_entities) > 0: if recognizer_result.entities.get("To", [{ "$instance": {} }])[0]["$instance"]: result.destination = to_entities[0]["text"].capitalize( ) else: result.unsupported_airports.append( to_entities[0]["text"].capitalize()) from_entities = recognizer_result.entities.get( "$instance", {}).get("From", []) if len(from_entities) > 0: if recognizer_result.entities.get("From", [{ "$instance": {} }])[0]["$instance"]: result.origin = from_entities[0]["text"].capitalize() else: result.unsupported_airports.append( from_entities[0]["text"].capitalize()) # TODO: This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part. # TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year. date_entities = recognizer_result.entities.get( "$instance", {}).get("datetime", []) if len(date_entities) > 0: result.travel_date = None # TODO: Set when we get a timex format except Exception as e: print(e) return intent, result
async def act_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: if not self._luis_recognizer.is_configured: # LUIS is not configured, we just run the BookingDialog path with an empty BookingDetailsInstance. return await step_context.begin_dialog(self._booking_dialog_id, BookingDetails()) # Call LUIS and gather any potential booking details. (Note the TurnContext has the response to the prompt.) intent, luis_result = await LuisHelper.execute_luis_query( self._luis_recognizer, step_context.context) if intent == Intent.BOOK_FLIGHT.value and luis_result: # Show a warning for Origin and Destination if we can't resolve them. await MainDialog._show_warning_for_unsupported_cities( step_context.context, luis_result) # Run the BookingDialog giving it whatever details we have from the LUIS call. return await step_context.begin_dialog(self._booking_dialog_id, luis_result) if intent == Intent.CANCEL_BOOKING.value and luis_result: # Show a warning for Origin and Destination if we can't resolve them. await MainDialog._show_warning_for_unsupported_cities( step_context.context, luis_result) # Run the BookingDialog giving it whatever details we have from the LUIS call. return await step_context.begin_dialog( self._cancel_booking_dialog_id, luis_result) if intent == Intent.EDIT_BOOKING.value and luis_result: # Show a warning for Origin and Destination if we can't resolve them. await MainDialog._show_warning_for_unsupported_cities( step_context.context, luis_result) # Run the BookingDialog giving it whatever details we have from the LUIS call. return await step_context.begin_dialog( self._edit_booking_dialog_id, luis_result) if intent == Intent.GET_WEATHER.value: get_weather_text = "TODO: get weather flow here" get_weather_message = MessageFactory.text( get_weather_text, get_weather_text, InputHints.ignoring_input) await step_context.context.send_activity(get_weather_message) else: didnt_understand_text = ( "Sorry, I didn't get that. Please try asking in a different way" ) didnt_understand_message = MessageFactory.text( didnt_understand_text, didnt_understand_text, InputHints.ignoring_input) await step_context.context.send_activity(didnt_understand_message) return await step_context.next(None)
async def act_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: # Call LUIS and gather any potential booking details. (Note the TurnContext has the response to the prompt.) booking_details = await LuisHelper.excecute_luis_query( self._configuration, step_context.context ) if step_context.result is not None else BookingDetails() # In this sample we only have a single Intent we are concerned with. However, typically a scenario # will have multiple different Intents each corresponding to starting a different child Dialog. # Run the BookingDialog giving it whatever details we have from the LUIS call, it will fill out the remainder. return await step_context.begin_dialog(BookingDialog.__name__, booking_details)
async def act_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: """Use language understanding to gather details about booking.""" # In this sample we only have a single Intent we are concerned with. # However, typically a scenario will have multiple different Intents # each corresponding to starting a different child Dialog. booking_details = await LuisHelper.execute_luis_query(self._configuration,\ step_context.context) if step_context.result is not None else BookingDetails() # Run the BookingDialog giving it whatever details we have from the # model. The dialog will prompt to find out the remaining details. return await step_context.begin_dialog(BookingDialog.__name__, booking_details)
async def act_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: """Use language understanding to gather details about booking.""" # Call LUIS and gather any potential booking details. (Note the TurnContext # has the response to the prompt.) booking_details = await LuisHelper.execute_luis_query(self._configuration,\ step_context.context, self.telemetry_client) if step_context.result is not None\ else BookingDetails() # pylint: disable=bad-continuation # In this sample we only have a single Intent we are concerned with. However, # typically a scenario will have multiple different Intents each corresponding # to starting a different child Dialog. # Run the BookingDialog giving it whatever details we have from the # model. The dialog will prompt to find out the remaining details. return await step_context.begin_dialog(BookingDialog.__name__, booking_details)
async def act_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: if not self._luis_recognizer.is_configured: # LUIS is not configured, we just run the BookingDialog path with an empty BookingDetailsInstance. return await step_context.begin_dialog( self._booking_dialog_id, BookingDetails() ) # Call LUIS and gather any potential booking details. (Note the TurnContext has the response to the prompt.) intent, luis_result = await LuisHelper.execute_luis_query( self._luis_recognizer, step_context.context ) if intent == Intent.BOOK_FLIGHT.value and luis_result: # export luis "understanding" as a trace for performance improvement luis_understanding = {} luis_understanding["1 - Input"] = step_context.context.activity.text luis_understanding["2 - Destination airport"] = luis_result.destination luis_understanding["3 - Origin airport"] = luis_result.origin luis_understanding["4 - Departure date"] = luis_result.departure_date luis_understanding["5 - Return date"] = luis_result.return_date luis_understanding["6 - Budget"] = luis_result.budget self.telemetry_client.track_trace("Luis understanding", luis_understanding, "INFO") # Run the BookingDialog giving it whatever details we have from the LUIS call. return await step_context.begin_dialog(self._booking_dialog_id, luis_result) else: didnt_understand_text = ( "Sorry, I didn't get that. Please try asking in a different way" ) didnt_understand_message = MessageFactory.text( didnt_understand_text, didnt_understand_text, InputHints.ignoring_input ) await step_context.context.send_activity(didnt_understand_message) return await step_context.next(None)
async def execute_luis_query( luis_recognizer: LuisRecognizer, turn_context: TurnContext) -> (Intent, object): """ Returns an object with preformatted LUIS results for the bot's dialogs to consume. """ result = None intent = None try: recognizer_result = await luis_recognizer.recognize(turn_context) intent = (sorted( recognizer_result.intents, key=recognizer_result.intents.get, reverse=True, )[:1][0] if recognizer_result.intents else None) if intent == Intent.BOOK_BIKE.value: result = BookingDetails() # We need to get the result from the LUIS JSON which at every level returns an array. to_entities = recognizer_result.entities.get("$instance", {}).get("To", []) if len(to_entities) > 0: if recognizer_result.entities.get("To", [{ "$instance": {} }])[0]["$instance"]: result.destination = to_entities[0]["text"].capitalize( ) else: result.unsupported_airports.append( to_entities[0]["text"].capitalize()) from_entities = recognizer_result.entities.get( "$instance", {}).get("From", []) if len(from_entities) > 0: if recognizer_result.entities.get("From", [{ "$instance": {} }])[0]["$instance"]: result.origin = from_entities[0]["text"].capitalize() else: result.unsupported_airports.append( from_entities[0]["text"].capitalize()) # This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop # the Time part. TIMEX is a format that represents DateTime expressions that include some ambiguity. # e.g. missing a Year. date_entities = recognizer_result.entities.get("datetime", []) if date_entities: timex = date_entities[0]["timex"] if timex: datetime = timex[0].split("T")[0] result.travel_date = datetime else: result.travel_date = None except Exception as exception: print(exception) return intent, result
async def execute_luis_query( luis_recognizer: LuisRecognizer, turn_context: TurnContext) -> (Intent, object): """ Returns an object with preformatted LUIS results for the bot's dialogs to consume. """ result = None intent = None try: recognizer_result = await luis_recognizer.recognize(turn_context) intent = LuisRecognizer.top_intent(recognizer_result) #ajout --- #retult = recognizer_result.properties["recognizerResult"] #await turn_context.send_activity(f" Luis Result {recognizer_result.entities}") #------------------------- #intent = ( #sorted( #recognizer_result.intents, #key=recognizer_result.intents.get, #reverse=True, #)[:1][0] #if recognizer_result.intents #else None #) if intent == Intent.BOOK_FLIGHT.value: result = BookingDetails() # We need to get the result from the LUIS JSON which at every level returns an array. to_entities = recognizer_result.entities.get("$instance", {}).get("To", []) if len(to_entities) > 0: result.destination = to_entities[0]["text"].capitalize() from_entities = recognizer_result.entities.get( "$instance", {}).get("From", []) if len(from_entities) > 0: result.origin = from_entities[0]["text"].capitalize() budget_entities = recognizer_result.entities.get( "$instance", {}).get("budget", []) if len(budget_entities) > 0: result.budget = budget_entities[0]["text"] # This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop # the Time part. TIMEX is a format that represents DateTime expressions that include some ambiguity. # e.g. missing a Year. date_entities = recognizer_result.entities.get("str_date", []) if date_entities: timex = date_entities[0]["datetime"][0]["timex"] if timex: datetime = timex[0].split("T")[0] result.travel_date = datetime else: result.travel_date = None date_return_entities = recognizer_result.entities.get( "$instance", {}).get("end_date", []) if date_return_entities: timex = date_return_entities[0]["datetime"][0]["timex"] if timex: datetime = timex[0].split("T")[0] result.return_date = datetime else: result.return_date = None except Exception as exception: print(exception) return intent, result