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