Bot-docs: Type unsafe choice for recognizerResult.Entities (C#)?

Created on 20 Feb 2019  Â·  5Comments  Â·  Source: MicrosoftDocs/bot-docs

Understand this was just auto generated from a swagger rest api spec, and might be OK for JS.. But we might want a more type safe representation of the entities objects? Plus, the JSON schema for the entities isn't documented anywhere.
Also, the example helper function also covers simple type/score checks.. No documentation on say, currency type entities or other builtin entity types..


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

issue type new platform SDK v4

Most helpful comment

The corresponding sample code will be updated once 4.3 is released. I'll update this topic at that time, and use a strongly-typed class to receive the LUIS result.

All 5 comments

The corresponding sample code will be updated once 4.3 is released. I'll update this topic at that time, and use a strongly-typed class to receive the LUIS result.

Looks like the team is moving in this direction with respect to handling LUIS results:

public static async Task<BookingDetails> ExecuteLuisQuery(IConfiguration configuration, ILogger logger, ITurnContext turnContext, CancellationToken cancellationToken)
{
    var bookingDetails = new BookingDetails();

    try
    {
        // Create the LUIS client from configuration.
        var luisService = new LuisService
        {
            AppId = configuration["Luis-Booking-AppId"],
            AuthoringKey = configuration["Luis-Booking-AuthoringKey"],
            Region = configuration["Luis-Booking-Region"],
        };

        var recognizer = new LuisRecognizer(new LuisApplication(
            luisService.AppId,
            luisService.AuthoringKey,
            luisService.GetEndpoint()));

        // The actual call to LUIS
        var recognizerResult = await recognizer.RecognizeAsync(turnContext, cancellationToken);

        var (intent, score) = recognizerResult.GetTopScoringIntent();
        if (intent == "Book_flight")
        {
            // We need to get the result from the LUIS JSON which at every level returns an array.
            bookingDetails.Destination = recognizerResult.Entities["To"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();
            bookingDetails.Origin = recognizerResult.Entities["From"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();

            // 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.
            bookingDetails.TravelDate = recognizerResult.Entities["datetime"]?.FirstOrDefault()?["timex"]?.FirstOrDefault()?.ToString().Split('T')[0];
        }
    }
    catch (Exception e)
    {
        logger.LogWarning($"LUIS Exception: {e.Message} Check your LUIS configuration.");
    }

    return bookingDetails;
}

For this sample. The dev team decided to use the raw entities from the LUIS result. The article has been updated to detail their approach. I'll track down links to additional information on consuming LUIS entity output and add them to the article.

Adding this to the article. Let me know if additon links to external content would help this article.

For information on how entity information appears in a LUIS result, see Extract data from utterance text with intents and entities.

Closing. Please open a new doc issue if you feel there is more we need to do in the docs to explain this pattern. You can also open an issue in the botbuilder-dotnet or botbuilder-samples repos to request changes to the SDK or the samples, respectively.

Was this page helpful?
0 / 5 - 0 ratings