Skip to content

Latest commit

 

History

History
 
 

intelligence-LUIS

LUIS Bot Sample

A sample bot using LuisDialog to integrate with a LUIS.ai application.

[![Deploy to Azure][Deploy Button]][Deploy CSharp/LUIS] [Deploy Button]: https://azuredeploy.net/deploybutton.png [Deploy CSharp/LUIS]: https://azuredeploy.net

Prerequisites

The minimum prerequisites to run this sample are:

  • The latest update of Visual Studio 2015. You can download the community version here for free.
  • The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.

LUIS Application

You can test this sample as-is, using the hosted sample application, or you can import the pre-built LuisBot.json file to your own LUIS account.

In the case you choose to import the application, the first step to using LUIS is to create or import an application. Go to the home page, www.luis.ai, and log in. After creating your LUIS account you'll be able to Import an Existing Application where can you can select a local copy of the LuisBot.json file an import it. For more information, take a look at Importing and Exporting Applications

Import an Existing Application

Once you imported the application you'll need to "train" the model (Training) before you can "Publish" the model in an HTTP endpoint. For more information, take a look at Publishing a Model.

Finally, edit the RootLuisDialog.cs file and update the LuisModel attribute placeholders with the values corresponding to your Subscription and Application. Also, you must comment out the #define useSampleModel line on top of the file.

#define useSampleModel

    [Serializable]
#if useSampleModel
    [LuisModel("162bf6ee-379b-4ce4-a519-5f5af90086b5", "11be6373fca44ded80fbe2afa8597c18")]
#else
    [LuisModel("YourModelId", "YourSubscriptionKey")]
#endif
    public class RootLuisDialog : LuisDialog<object>

Where to find the Application ID and Subscription Key

You'll need these two values to configure the LuisDialog through the LuisModel attribute:

  1. Application ID In the LUIS application's dashboard, select the App Settings option in the right side bar and copy the App ID field value.

    App Settings

  2. Subscription Key In the user's settings page, select the Subscription Keys option and copy the Programmatic API Key.

    Programmatic API Key

Code Highlights

One of the key problems in human-computer interactions is the ability of the computer to understand what a person wants, and to find the pieces of information that are relevant to their intent. In the LUIS application, you will bundle together the intents and entities that are important to your task. Read more about Creating an Application in the LUIS Help Docs. Check out the use of LuisIntent attributes decorating RootLuisDialog methods to handle LUIS Intents, for instance [LuisIntent("SearchHotels")].

[LuisIntent("SearchHotels")]
public async Task Search(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
{
    ...
}

Each intent handler method accepts the IDialogContext, the original incoming IMessageActivity message and the LuisResult including the matching Intents and Entities for the LUIS query. In the sample below, the RootLuisDialog class retrieves a city value from the processed pre-built entity.

EntityRecommendation cityEntityRecommendation;

if (result.TryFindEntity(EntityGeographyCity, out cityEntityRecommendation))
{
    cityEntityRecommendation.Type = "Destination";
}

You might notice the use of EntityRecommendation.Type = "Destination" in the code above. This is useful to map entity values to properties when reusing the LUIS captured entities for the FormDialog<HotelsQuery>. The properties mapped to entities will be pre-populated. In the case of the AirportCode this extra step is not required since the entity name already matches the property name.

var hotelsFormDialog = new FormDialog<HotelsQuery>(hotelsQuery, this.BuildHotelsForm, FormOptions.PromptInStart, result.Entities);

In addition, the AirportCode entity makes use of the LUIS Regex Features which helps LUIS infer entities based on an Regular Expression match, for instance, Airport Codes consist of three consecutive alphabetic characters. You can read more about Regex Features in the Improving Performance section of the LUIS Help Docs.

Edit Regex Feature

Another LUIS Model Feature used is Phrase List Features, for instance, the model includes a phrase list named Near which categorizes the words: near, around, close and nearby. Phrase list features work for both words and phrase and what LUIS learns about one phrase will automatically be applied to the others as well.

Note: Both RegEx and Phrase List are transparent from the Bot's implementation perspective. Think of model features as "hints" used by the Machine Learning algorithm to help categorize and recognize words that compound Entities and Intents.

Phrase List Feature

Spelling Correction

IF you want to enable spelling correction, set the IsSpellCorrectionEnabled key to true in the web.config file.

Microsoft Bing Spell Check API provides a module that allows you to to correct the spelling of the text. Check out the reference to know more about the modules available.

BingSpellCheckService.cs is the core component illustrating how to call the Bing Spell Check RESTful API.

In this sample we added spell correction before calling the dialog. Check out the usage in MessagesController.cs.

if (IsSpellCorrectionEnabled)
{
    try
    {
        activity.Text = await this.spellService.GetCorrectedTextAsync(activity.Text);
    }
    catch(Exception ex)
    {
        Trace.TraceError(ex.ToString());
    }
}

await Conversation.SendAsync(activity, () => new RootLuisDialog());

Outcome

You will see the following in the Bot Framework Emulator when opening and running the sample solution.

Sample Outcome

More Information

To get more information about how to get started in Bot Builder for .NET and Conversations please review the following resources:

Limitations
The functionality provided by the Bot Framework Activity can be used across many channels. Moreover, some special channel features can be unleashed using the ChannelData property.

The Bot Framework does its best to support the reuse of your Bot in as many channels as you want. However, due to the very nature of some of these channels, some features are not fully portable.

The features used in this sample are fully supported in the following channels:

  • Skype
  • Facebook
  • DirectLine
  • WebChat
  • Slack
  • GroupMe

They are also supported, with some limitations, in the following channels:

  • Kik
  • Email

On the other hand, they are not supported and the sample won't work as expected in the following channels:

  • Telegram
  • SMS