Intelligent Chatbot with Microsoft LUIS

Suyan
6 min readMay 18, 2019

Hi guys, Alison here. In my last post I talked about buiding an easy and useful chatbot using Microsoft’s QnA Maker, if you haven’t seen that yet, it’s here. In this blog we’ll be continuing our chatbot quest, with LUIS!

Part 1 — What is LUIS?

Ok, so LUIS is short for Language Understanding Intelligent Service, it is one of Microsoft Azure’s Cognitive Services (a collection of Azure APIs that serves its purpose in 5 fields: Vision, Speech, Language, Knowledge, and Search, more on that in my other blog). Obviously, LUIS is a part of the Language Cognitive Services, and it can help teach your apps to understand commands from your users!

So how does LUIS work? First, we would need to talk a bit about some key concepts, namely Intent, Entity, and Utterance:

  1. Intents represent actions that the users want to perform, think of it as expressing the purpose or goal expressed in a user’s input. You can define and name the intents that correspond to certain actions.
  2. Utterances are units of speech. Every single user input (sentence) can be understood as an utterance.
  3. Entities represent detailed information (data) that is relevant in the utterance. LUIS helps you to choose the specific action to take to answer a user’s sentence by recognizing and labeling the entities that are mentioned in the user’s utterance.

See the following example “I want to book a flight from New York to LA” as user input. LUIS should be able to recognize the entire sentence as an utterance, flight booking as the intent, and “New York” and “LA (Los Angeles)” as entities, more specifically, locational entities.

Alright, now that you’ve gotten the key concepts and the main purpose of LUIS, let’s start making a LUIS app!

Part 2 — Creating a LUIS app on LUIS.ai

1. Login LUIS.ai with your Azure account.

2. Create new app, and choose a name for your application

3. You will be directed to the LUIS dashboard, there you can view endpoint hits once you publish and use your LUIS app. Go to Build, create intents and entities from App Assets in the left navigation menu (note: you can create entities first, and LUIS will be able to recognize these entities when you create intent utterances).

Example of intent (Book flight) and utterances:

Example of entity (Location):

4. Train and test your LUIS app, from the top navigation bar. One thing to point out, the LUIS model is not meant to generate a response, but to learn to understand the intent (purpose) of the sentence. The percentage associated with the result intent shows the correctness possibility of the intent given by LUIS, ranging from 0–1.

5. You can also view the insight of a particular input while testing, and modify it’s intent if the LUIS model didn’t give the correct result.

6. To extract contextual data, e.g. in the utterance “flight from New York to London”, label “New York” as origin and “London” as the destination, create roles “Destination” and “Origin” under the location entity, and in the Book flight intent, update all location to their specific roles.

7. To use the endpoint URL (in Manage->Keys and Endpoint), please remember to Publish your LUIS app first. Example endpoint URL from this demo:

https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/db7b9841-cf50-4c16-9be1-ba8e9e691b49?verbose=true&timezoneOffset=-360&subscription-key=9a06ee74cee94abcbb0ff49a4a1823ac&q=

Place your utterance after “q=” and paste the URL in a browser, you will receive a JSON returned from LUIS, such as the following: (if you finish the previous step, you can also see role values for entities). This JSON tells us that the LUIS model correctly identifies Book flight as the most possible intent, it also recognizes entity “LA”.

8. Click Publish to publish the LUIS app.

9. Go to Manage, find the Application ID (copy and save for later).

Part 2 — Integrating your LUIS app with an Azure bot

10. Go to your Azure portal, create new resource Web App Bot, enter Bot name, Resource group. For Bot template, in this tutorial, select SDK version 3 (SDK v3), SDK language Node.js, and Language understanding, click OK and Create.

11. Go to the new Web App Bot resource, under App Service Settings -> Configuration, update LuisAppId to be the Application ID in LUIS.AI. save your changes.

12. Go to Test in Web Chat, input something like “Hi”, if the output is as followings, it indicates that the Azure web bot has successfully connected to the LUIS API: the bot recognizes the intent of the utterance (aka the meaning of the sentence) and responds to it. First time loading app might take some time.

13. The following part will require a bit of programming knowledge :) don’t worry, I’ve prepared a short code segment that you can just copy and past to try out.

You may realize at this point that the Web App Bot is echoing back the user input, that’s because the bot hasn’t been told how to respond yet, it might not even contain the code to recognize your intents. For that, you can edit the code online, or download the bot source code and program it in Visual Studio to generate more customized responses.

This following code segment can be added to the end of the app.js file (in Azure Web App Bot resource, find Build, click Open online code editor, and find this file). The code looks for user input that matches the intent “Booking flight”.

This demonstrates how the bot can recognize data in utterances, and use waterfall conversation flow to ask user input in sequence (e.g. figure above).

Try to write your own bot.dialog code for different intents :)

bot.dialog(‘BookflightDialog’,[(session, args, next) => {var intent = args.intent;var location = builder.EntityRecognizer.findEntity(intent.entities, ‘Location’);var booking = session.dialogData.booking = {from: location ? location.entity : null,to:null,};if(!booking.from) {builder.Prompts.text(session, ‘So you wanna book a flight? Ok, where are you flying from?’);} else {next();}},(session, results, next) => {var booking = session.dialogData.booking;if(results.response){booking.from=results.response;}if(!booking.to) {builder.Prompts.text(session, ‘Where are you arriving to?’);}else{next();}},(session, results, next) => {var booking = session.dialogData.booking;if(results.response){booking.to=results.response;}if(!booking.time) {builder.Prompts.text(session, ‘What time do you want to book?’);}else{next();}},(session, results)=>{var booking = session.dialogData.bookingif(results.response){booking.time=results.response;}session.endDialog(‘Creating a booking from “%s” to “%s” at “%s” for you, please wait’,booking.from, booking.to, booking.time);}]).triggerAction({matches: ‘Book flight’ // match the intent name in your LUIS app})

If you’ve seen my last blog on the QnA Maker, you’ll know that this Azure Web App Bot can also be deployed onto channels like Skype/Microsoft Teams, do check that out, it’s pretty cool!

This is just the first step to making your chatbot truly intelligent. A good Microsoft chatbot is a combination of three key services: Microsoft’s bot framework, QnA Maker AND LUIS, so when you start building your bot, keep in mind to fully utilize all three! Results will not disappoint.

New blogs will be coming out soon, I’ll talk further into chatbot making, and go deeper into other Azure services as well. Also thinking about starting a YouTube channel to share about Azure, Engineering, and CS. Let me know what you think!

Hong Kong, May 19, 2019

Alison Xu

--

--