The Language Understanding (LUIS) API allows you to take advantage of the deep learning capabilities of Azure’s Cognitive Services to incorporate natural language interactions into your bot. This lab focuses on configuring and integrating LUIS into our BackMeUp bot.
Upon completion, developers should have:
- Set up a LUIS application
- Connect the bot service to LUIS
- Manage synonyms with entities
- Use intents to tie utterances to dialogs or steps
- Test and release LUIS application using versions
- Continuously improve your LUIS app
Navigate to https://www.luis.ai/home
If you’re not logged in, you’ll need to sign in
Click on “Create new app”
Type “BackMeUp” in the “Name” field and click “Done”
Click “BackMeUp” to open
Click “Entities” and then “Create new entity”
Type “Command” for “Entity name” and “List” for “Entity type”. Then click “Done".
Under “Values” type “back pain” and hit enter
Your entry will appear below. Click “Synonyms” and type “back”. Hit “Enter”. Repeat for “lower back pain” and “upper back pain”.
Click “Intents” and then “Create new intent”
Type “Root Command” and click “Done”
In the text box under “Root Command”, type “help me with my back pain” and hit “Enter”
Note that this automatically identified the entity. This is because we added “back pain” to the entity’s list.
Add these utterances:
- “my back hurts”
- “I need help with my back”
- “can you help me with my back”
- “my back needs help”
Click “Train”
When it’s done training, click “Test”
Type “help me, my back hurts”. It should match up to “Root Command”
Click “Inspect”. The “Inspect” panel should expand. Note that the “Command” entity captures “back” as the entity value.
Click “Publish”
Keep the default “Environment” choice of “Production.” Click “Publish”
Open the solution used in Lab 2
Add the NuGet package “Microsoft.Bot.Builder.AI.Luis”
Open “BotConfiguration.bot” and paste in the following JSON at the end of the “services” array. Don’t forget the comma in the element above it.
{
"type": "luis",
"name": "BackMeUp",
"appId": "",
"version": "",
"authoringKey": "",
"subscriptionKey": "",
"region": "",
"id": "2"
}Return to the LUIS portal
Click the “Manage” tab, then select “Application information”. Click the copy button next to the “Application ID” and paste it into the “appId” field in “BotConfiguration.bot”
Click “Versions” and take note of the version name. Type that in the “version” field in “BotConfiguration.bot”
Click “Keys and Endpoints”. Copy “Key 1” and past it into “subscriptionKey” in “BotConfiguration.bot”. Type the “Region” value into the “region” field in “BotConfiguraiton.bot”
In “ConfigureServices” within the “Startup.cs” file, locate the switch statement that inspects “service.Type”. Add the following code
case ServiceTypes.Luis:
if (service is LuisService luisService)
{
services.AddSingleton(sp => new LuisApplication(
luisService.AppId ?? throw new InvalidOperationException("The LUIS configuration must include appId property"),
luisService.SubscriptionKey ?? throw new InvalidOperationException("The LUIS configuration must include subscriptionKey property"),
luisService.GetEndpoint()));
services.AddTransient(sp => new LuisRecognizer(
sp.GetService<LuisApplication>(),
new LuisPredictionOptions
{
IncludeAllIntents = true,
}));
}
break;Open up “BackMeUp.cs” and add the LuisApplication to the constructor, as a readonly field, and in the initialization code of the constructor body
In “BackMeUp.cs” add the private method code below
private static async Task ProcessCommandAsync(
ITurnContext turnContext,
RecognizerResult luisResult,
DialogContext dialogContext,
CancellationToken cancellationToken)
{
var commands = luisResult?.Entities["Command"]?.ToObject<string[][]>();
if (commands == null || commands.Length == 0 || commands[0].Length == 0)
{
var responseMessage = MessageFactory.Text(
"We're here to help. Try typing \"help me with back pain\"");
await turnContext.SendActivityAsync(responseMessage, cancellationToken);
return;
}
var theCommand = commands[0][0];
switch (theCommand)
{
case "back pain":
await dialogContext.BeginDialogAsync(
BackPainDialogFactory.DialogId,
cancellationToken: cancellationToken);
break;
default:
var responseMessage = MessageFactory.Text(
"I'm sorry. I can't help you with that. Try typing \"help me with back pain\"");
await turnContext.SendActivityAsync(responseMessage, cancellationToken);
break;
}
}In “BackMeUp.cs” at the top of “OnTurnAsync” just below the line
if (turnContext.Activity.Type == ActivityTypes.Message)
{add
var luisResult = turnContext.Activity.Text == null
? null
: await _luis.RecognizeAsync(turnContext, cancellationToken);
var (luisIntent, _) = luisResult?.GetTopScoringIntent() ?? (null, 0.0);In “BackMeUp.cs” replace the “else if” (and its body) that looks like this
else if (new[] { "back pain", "start" }.Any(t => t == activityText))
{
// start the dialog. We'll do better when we integrate LUIS
await dialogContext.BeginDialogAsync(
BackPainDialogFactory.DialogId,
cancellationToken: cancellationToken);
}with this
else if (luisIntent == "Root_Command")
{
await ProcessCommandAsync(turnContext, luisResult, dialogContext, cancellationToken);
}Run and test in the Bot Framework Emulator
In the LUIS portal, click “Intents” in the menu, and then “Create new intent”
Name it “Help” and click “Done”
Add the following utterances
| Image | Utterance |
|---|---|
![]() |
|
Train and test your changes
Publish your changes
In “OnTurnAsync” in the “BackMeUp.cs” file, after
var (luisIntent, _) = luisResult?.GetTopScoringIntent() ?? (null, 0.0);Add the lines
if (luisIntent == "Help")
{
var message = MessageFactory.Text("Right now, I can only help you with back pain. With time, I'll do more, but try typing \"Help me with back pain\"");
await turnContext.SendActivityAsync(message, cancellationToken);
return;
}Run your code and test your changes in Bot Framework Emulator
On the “Versions” tab, check the checkbox next to “0.1” then click “Clone”
Type “0.2” then click “Done”
You should now see a new version “0.2” that is the active version. This means edits made will be to version 0.2.
Change the active version back to “0.1” by clicking the checkbox next to “0.1” then clicking “Activate”
Switch between versions using the toggle at the top left. This has the same effect as step 4
While editing version “0.2”, return to the “Intents” screen on the “BUILD” tab. Click “Add prebuilt intent”
Type “AreYouListening” in the search box, click the checkbox next to “OnDevice.AreYouListening” and click “Done”
You should now see the prebuilt intent in your list of intents
Click “Train”
Click “Publish”
Select “Staging” then click “Publish”
In “appsettings.json”, add the following section
"LUIS": {
"Staging": true
}Update “ConfigureServices” in “Startup.cs”. Replace the LuisPredictionOptions initializer with the following
new LuisPredictionOptions
{
IncludeAllIntents = true,
Staging = Configuration.GetSection("LUIS").GetValue<bool>("Staging"),
}Optionally, you can update the “version” in the “luis” service of “BotConfiguration.bot”. It locates your service by slot, not version number.
In “BackMeUp.cs” add the following code under the code that handles the “Help” intent
if (luisIntent == "OnDevice_AreYouListening")
{
var message = MessageFactory.Text("Your well being is very important to me. I listen intently to everything you say.");
await turnContext.SendActivityAsync(message, cancellationToken);
return;
}Run your code and test it using Bot Framework Emulator.
Return to the “Entities” tab in LUIS, and select “Command”
Note that LUIS provides suggestions
You may see different options; pick one by clicking on it. Add a few more if you wish.
Train and publish to staging
In “ProcessCommandAsync” of “BackMeUp.cs”, replace the “default” code in the switch statement with the following
var responseMessage = MessageFactory.Text(
$"I'll be able to help you with your {theCommand} soon. For now, I can only help with back pain.");
await turnContext.SendActivityAsync(responseMessage, cancellationToken);
break;Run your code and test it using the Bot Framework Emulator
The code in these labs has evolved in a way that makes it easy to follow. To be maintainable, however, it needs some work. Try refactoring the code in “BackMeUp.cs” to help with maintainability.
You can see utterances that the endpoint received but was not able to match with an intent by clicking “Review endpoint utterances” in the “BUILD” tab. Take a look at the utterances you typed during testing. If you see one that can be matched to an intent, select the proper “Aligned intent” and then click the “Add to aligned intent” checkbox. Go back to “Intents” and observe what happened.
Read about phrase lists (https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-how-to-add-features).










































