In this section we will have some hands on experience with LUIS.
The documentation explains LUIS with these words: Language Understanding Intelligent Service (LUIS) allows your application to understand what a person wants in their own words. LUIS uses machine learning to allow developers to build applications that can receive user input in natural language and extract meaning from it. A client application that converses with the user can pass user input to a LUIS app and receive relevant, detailed information back."
Please find the Documentation to LUIS in the link below; https://docs.microsoft.com/en-us/azure/cognitive-services/LUIS/Home
Please move on to the website below to start creating a LUIS App. LUIS has a EU website for the European Region so please make sure that you are on the eu page and not the www page.
From the main page click the "Sign In" button on the right top of the page. You can use your Avanade credentials to sign in to the LUIS app. After you login you will be forwarded to the My Apps page. If not please selecet the My Apps from the top menu. Now we will be creating a new app so please click New App
In the page that pops up enter the Name, Culture and Description(optional) details for your new LUIS app. Lastly click the Create button.
In the Image below you can find the Menu that should be visible on the left side of your screen once you create your LUIS App. You will be using this menu often while you are setting up and using your LUIS App.
Dashboard -> Facts & statistics about the app's data and the received endpoint hits at any period of time.
Intents -> A listing of intents in the application.
Entities -> Manage a list of entities in your application and track and control their instances within utterances.
Prebuilt domains -> Prebuilt domains are off-the-shelf collections of intents and entities that you can directly add and use in your application.
Features -> Use these advanced features to improve performance and avoid mistakes in identifying and interpreting utterances.
Train & Test -> Use this tool to test the current and published versions of your application, to check if you are progressing on the right track.
Publish App -> Use this section to Publish your LUIS app.
Lets go to our LUIS App page and select Intents from the left menu. An Intent represents actions that the user wants to perform. Our aim is to add two Intents to our App (Giving Feedback and Providing Idea).
After selecting the Intents option from the left manu, click Add Intent. On this pop up we will be naming our intent and then we will select Save.
After saving the intent we will be forwarded to the newly created Intents page. Here we need to add Utterances. You can also reach this page by clicking the Intent name after selecting Intents from the left menu.
Utterances -> This is input from the user that your app needs to interpret.
Filling in the Utterances are actually kind of guessing what the user might type in to reach the specific intent. Before we add the Utterances we first need to add Entitys. We will be coming back to this page after creating Entities.
Lets go to our LUIS App page and select Entities from the left menu. In the previous section we added and Intent named GiveIdea. We know that the user will give an Idea so this means that we can create a Entity named Idea so that we can use this entity in our Intents section. Once we are on the Entity page we can select Add custom entity. Enter a name (Idea) and entity type (Simple) then click Save.
Lets go to our LUIS App page and select Intents from the left menu. Now click the intent that you previously created so that we can add utterances to it.
In the box that says Type a new utterance & press Enter, we can enter a new utterance.
For example lets enter I have an idea and then press enter. Now you will see that this utterance has been added to the list.
For example lets enter why dont we have more holiday and then press enter. You will also see this in the list below. As you can see the part after why dont is an idea. So we can basically assign the words that come after the why dont to the Idea entity. We do this by clicking the first and last letter of the part of the sentence that we want to connect to the Entity. Once you have this part in the brackets there will be a hover menu asking you which Entity you want to select. From this hover menu lets select the Idea entity.
Don't forget to click the Save button after creating the utterances.
Once we have our Intents, Entities and Utterances it means that we are ready to Train & Test our LUIS App. We start this step by clicking the Train & Test header on the left. You should have a similar page to the image below. Now you can click Train or Train Application button.
Whenever you make updates in your current model, you’ll need to train your app before testing and publishing it. When you 'train' a model, LUIS generalizes from the examples you have labeled, and develops code to recognize relevant intents and entities in the future, thus improving its classification accuracy.
On the current page you are able to write sentences and send them to see if LUIS understands what you are talking about and also if it can capture your Entities.
Before you start testing, click Train Application to train the current model on the latest updates. Note that you'll not be able to train your app until you add utterances for all your intents.
In the scenario below LUIS understood that both sentences are about the GiveIdea Intent. The problem is that LUIS caught the $Idea in one sentence but it couldnt catch it on the second sentence. So this means I have to go back to the Utterances page to improve my logic.
Now let's login to Microsoft Azure with our Avanade credentials.
Please open the Resource Group that we created in the previous steps. Click the +Add button so that we can add a new service to our Resource Group. You can find this button in the image below.
In the search bar lets type in LUIS so that we can find the LUIS service. When you find the correct service as shown in the image below, click on it so that you have new blade with the description of the LUIS service. Let's click the Create button on the bottom of the new blade so that we can set up our new LUIS service.
In the new blade you will see empty fields similar to the image below.
Name: Enter a unique name for your LUIS Service.
Subscription: Select the correct subscription.
Location: Select West Europe. This is the closest service location to our current location.
Pricing tier: Select the F0 option. This is the free version and it is more then enough for our current event.
Resource group: Select the Use existing box and select the correct Resource Group from the drop-down.
Lastly, click the confirmation button and then click Create so that we can create our new LUIS Service.
Now its time to bind our Azure Service to our LUIS App.
Select Publish App from the main left menu of your LUIS App. You should have screen similar to the image below.
Now please select the Europe Regions and then click the Add Key button.
From the screen that pops up you will see a similar screen to the image below. You need to enter a Tenant name, Subscription Name and a Key. Things might get tricky here so continue reading the next part.
You might have several Tenant names in the drop-down or maybe only even one. Let's check if what our correct Tenant name is.
We go to Microsoft Azure. On the right top of the page you will see your email address and your current Directory. Make sure you are on the correct Directory. Now mouse over your email and you will see a hover screen similar to the image below.
Your Tenant name is the string next to your Directory name.
Make sure you select the correct Subscription name.
The Key in the drop-down should be the same name as your LUIS Service which you created on Microsoft Azure.
If we are all set you can click the Add Key button.
Once the steps above are completed you should see your new key is added on the Publish App page. Your screen should look similar to the one image below.
Select Publish App from the main left menu of your LUIS App. Make sure you added the new Key as explained in the previous section. You should have screen similar to the image below.
Now please click the Publish to production slot. If the publish is successful you should see a similer view to the image below on the top of your screen.
Now it's time to add our keys to our Microsoft Azure Web App. We always have the chance to save the keys to VS but for security reasons its always better to save the keys to the Microsoft Azure Web App and then give a reference to the Microsoft Azure Web App from VS. You should find two keys: App ID and Key 1
To find your App ID open your LUIS service on the the Dashboard page you should find your App ID string.
To find your Key 1 open your Microsoft Azure LUIS Service and click the Keys header from the side bar. In the page that opens you should find Key 1.
You should add these numbers to your Microsoft Azure Web App. Open the Microsoft Azure Web App you created in the previous steps. In the left menu bar find Application Settings under the Settings part. The left menu should be similar to the image below.
Once you save the two keys to your Microsoft Azure Web App, its time to add the keys to your VS project. We need to enter the keys that we previously entered to Microsoft Azure to the Web.config aswell. You should fill in the page that looks similar to this.
<appSettings>
<!-- ... -->
<add key="LuisAppId" value="" />
<add key="LuisKey" value="" />
</appSettings>
This is the last part of the LUIS section. Now that we have everything ready it's time to add some codes to VS so that we can forward the user to our wonderful LUIS App.
Firstly we need make sure that we have our LuisDialog() as Serializable. We do this because we are going to be moving back and forth from other apps and services and we want to make sure that VS keeps track of our latest state in the VS code. As you can see in the image below it is important to give reference to the Web.config file because that is where we stored our keys. Please check the image below to see the base part of the Luis Dialog.
[Serializable]
public class BasicLuisDialog : LuisDialog<object>
{
public BasicLuisDialog() : base(
new LuisService(
new LuisModelAttribute(
ConfigurationManager.AppSettings["LuisAppId"],
ConfigurationManager.AppSettings["LuisKey"],
LuisApiVersion.V2,
"westeurope.api.cognitive.microsoft.com")))
{
}
}
To explain how the communication between the LUIS App and VS is done in a simple way, we can say that the communication is done using the LUIS Intent. As you remember in the previous sections we entered Intent's to our LUIS App. Now we will be checking from VS if the received message by the user had any connection with the previously saved Intents to our LUIS App. We do this by adding a ** [LuisIntent("Intent Name")]** in front of the corresponding intent section in our code.
The context.Forward is used for passing the dialog to the child dialog. In this case the child dialog is the Luis Dialog. After the Luis Dialog is completed the context.Forward will forward our state to the Resume function. As you can also find below the Resume function puts us to the context.Wait stage. This is the stage where we wait for a new input from the user.
[LuisIntent("GiveFeedback")]
public async Task Feedback(IDialogContext context,IAwaitable<IMessageActivity> activity, LuisResult result)
{
var item = await activity;
await context.Forward(FeedbackDialog.BuildFormDialog(), Resume, item);
}
[LuisIntent("GiveIdea")]
public async Task Idea(IDialogContext context,IAwaitable<IMessageActivity> activity, LuisResult result)
{
var item = await activity;
await context.Forward(IdeaDialog.BuildFormDialog(), Resume, item);
}
private async Task Resume(IDialogContext context, IAwaitable<object> result)
{
await context.SayAsync("Can I help with something else?");
context.Wait(MessageReceived);
}