Skip to content

Commit 74e2b26

Browse files
authored
Merge pull request #103108 from diberry/diberry/0203-fresh-2
[Cogsvcs] LUIS - fresh - tutorial
2 parents 26ddaec + 62cb98c commit 74e2b26

File tree

8 files changed

+253
-600
lines changed

8 files changed

+253
-600
lines changed

.openpublishing.redirection.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,16 @@
820820
"redirect_url": "/azure/cognitive-services//QnAMaker/Quickstarts/get-answer-from-knowledge-base-using-url-tool",
821821
"redirect_document_id": false
822822
},
823+
{
824+
"source_path": "articles/cognitive-services/LUIS/luis-tutorial-bot-csharp-appinsights.md",
825+
"redirect_url": "/azure/cognitive-services/LUIS/luis-csharp-tutorial-bf-v4",
826+
"redirect_document_id": false
827+
},
828+
{
829+
"source_path": "articles/cognitive-services/LUIS/luis-tutorial-bot-nodejs-appinsights.md",
830+
"redirect_url": "/azure/cognitive-services/LUIS/luis-nodejs-tutorial-bf-v4",
831+
"redirect_document_id": false
832+
},
823833
{
824834
"source_path": "articles/cognitive-services/LUIS/luis-tutorial-pattern-roles.md",
825835
"redirect_url": "/azure/cognitive-services/LUIS/luis-tutorial-pattern",

articles/cognitive-services/LUIS/luis-csharp-tutorial-bf-v4.md

Lines changed: 165 additions & 84 deletions
Large diffs are not rendered by default.
Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
---
2-
title: "Tutorial: Language Understanding Bot Node.js v4"
3-
titleSuffix: Azure Cognitive Services
2+
title: "Tutorial: Language Understanding Bot Node.js v4"
43
description: Using Node.js, build a chat bot integrated with language understanding (LUIS) in this tutorial. This chat bot uses the Human Resources app to quickly implement a bot solution. The bot is built with the Bot Framework version 4 and the Azure Web app bot.
5-
services: cognitive-services
6-
author: diberry
7-
ms.custom: seodec18
8-
manager: nitinme
9-
ms.service: cognitive-services
10-
ms.subservice: language-understanding
114
ms.topic: tutorial
12-
ms.date: 10/14/2019
13-
ms.author: diberry
5+
ms.date: 02/03/2020
146
---
157

16-
# Tutorial: Use a Web App Bot enabled with Language Understanding in Node.js
8+
# Tutorial: Use a Web App Bot enabled with Language Understanding in Node.js
179

1810
Use Node.js to build a chat bot integrated with language understanding (LUIS). The bot is built with the Azure [Web app bot](https://docs.microsoft.com/azure/bot-service/) resource and [Bot Framework version](https://github.com/Microsoft/botbuilder-dotnet) V4.
1911

20-
[!INCLUDE [Waiting for Bot refresh](./includes/wait-bot-upgrade.md)]
21-
2212
**In this tutorial, you learn how to:**
2313

2414
> [!div class="checklist"]
@@ -62,16 +52,17 @@ Use Node.js to build a chat bot integrated with language understanding (LUIS). T
6252
|SDK version|Bot framework version|**SDK v4**|
6353
|SDK language|Programming language of bot|**Node.js**|
6454
|Bot|Type of bot|**Basic bot**|
65-
55+
6656
1. Select **Create**. This creates and deploys the bot service to Azure. Part of this process creates a LUIS app named `luis-nodejs-bot-XXXX`. This name is based on the /Azure Bot Service app name.
6757

68-
[![Create web app bot](./media/bfv4-nodejs/create-web-app-service.png)](./media/bfv4-nodejs/create-web-app-service.png#lightbox)
58+
> [!div class="mx-imgBorder"]
59+
> [![Create web app bot](./media/bfv4-nodejs/create-web-app-service.png)](./media/bfv4-nodejs/create-web-app-service.png#lightbox)
6960
7061
Wait until the bot service is created before continuing.
7162

7263
## The bot has a Language Understanding model
7364

74-
The bot service creation process also creates a new LUIS app with intents and example utterances. The bot provides intent mapping to the new LUIS app for the following intents:
65+
The bot service creation process also creates a new LUIS app with intents and example utterances. The bot provides intent mapping to the new LUIS app for the following intents:
7566

7667
|Basic bot LUIS intents|example utterance|
7768
|--|--|
@@ -82,31 +73,31 @@ The bot service creation process also creates a new LUIS app with intents and ex
8273

8374
## Test the bot in Web Chat
8475

85-
1. While still in the Azure portal for the new bot, select **Test in Web Chat**.
86-
1. In the **Type your message** textbox, enter the text `Book a flight from Seattle to Berlin tomorrow`. The bot responds with verification that you want to book a flight.
76+
1. While still in the Azure portal for the new bot, select **Test in Web Chat**.
77+
1. In the **Type your message** textbox, enter the text `Book a flight from Seattle to Berlin tomorrow`. The bot responds with verification that you want to book a flight.
8778

8879
![Screenshot of Azure portal, enter the text `hello`.](./media/bfv4-nodejs/ask-bot-question-in-portal-test-in-web-chat.png)
8980

90-
You can use the test functionality to quickly testing your bot. For more complete testing, including debugging, download the bot code and use Visual Studio.
81+
You can use the test functionality to quickly testing your bot. For more complete testing, including debugging, download the bot code and use Visual Studio.
9182

9283
## Download the web app bot source code
93-
In order to develop the web app bot code, download the code and use on your local computer.
84+
In order to develop the web app bot code, download the code and use on your local computer.
9485

95-
1. In the Azure portal, select **Build** from the **Bot management** section.
86+
1. In the Azure portal, select **Build** from the **Bot management** section.
9687

97-
1. Select **Download Bot source code**.
88+
1. Select **Download Bot source code**.
9889

9990
[![Download web app bot source code for basic bot](../../../includes/media/cognitive-services-luis/bfv4/download-code.png)](../../../includes/media/cognitive-services-luis/bfv4/download-code.png#lightbox)
10091

101-
1. When the pop-up dialog asks **Include app settings in the downloaded zip file?**, select **Yes**. This provides the LUIS settings.
92+
1. When the pop-up dialog asks **Include app settings in the downloaded zip file?**, select **Yes**. This provides the LUIS settings.
10293

103-
1. When the source code is zipped, a message will provide a link to download the code. Select the link.
94+
1. When the source code is zipped, a message will provide a link to download the code. Select the link.
10495

105-
1. Save the zip file to your local computer and extract the files. Open the project with Visual Studio.
96+
1. Save the zip file to your local computer and extract the files. Open the project with Visual Studio.
10697

10798
## Review code to send utterance to LUIS and get response
10899

109-
1. To send the user utterance to the LUIS prediction endpoint, open the **dialogs -> flightBookingRecognizer.js** file. This is where the user utterance entered into the bot is sent to LUIS. The response from LUIS is returned from the **executeLuisQuery** method.
100+
1. To send the user utterance to the LUIS prediction endpoint, open the **dialogs -> flightBookingRecognizer.js** file. This is where the user utterance entered into the bot is sent to LUIS. The response from LUIS is returned from the **executeLuisQuery** method.
110101

111102
````javascript
112103
class FlightBookingRecognizer {
@@ -130,64 +121,72 @@ In order to develop the web app bot code, download the code and use on your loca
130121

131122

132123
````javascript
133-
class MainDialog extends ComponentDialog {
124+
// Copyright (c) Microsoft Corporation. All rights reserved.
125+
// Licensed under the MIT License.
134126

135-
constructor(luisRecognizer, bookingDialog) {
136-
...
137-
this.luisRecognizer = luisRecognizer;
138-
...
139-
}
127+
const { LuisRecognizer } = require('botbuilder-ai');
140128

129+
class FlightBookingRecognizer {
130+
constructor(config) {
131+
const luisIsConfigured = config && config.applicationId && config.endpointKey && config.endpoint;
132+
if (luisIsConfigured) {
133+
this.recognizer = new LuisRecognizer(config, {}, true);
134+
}
135+
}
141136

142-
...
137+
get isConfigured() {
138+
return (this.recognizer !== undefined);
139+
}
143140

144141
/**
145-
* Second step in the waterfall. This will use LUIS to attempt to extract the origin, destination and travel dates.
146-
* Then, it hands off to the bookingDialog child dialog to collect any remaining details.
142+
* Returns an object with preformatted LUIS results for the bot's dialogs to consume.
143+
* @param {TurnContext} context
147144
*/
148-
async actStep(stepContext) {
149-
150-
...
151-
152-
const luisResult = await this.luisRecognizer.executeLuisQuery(stepContext.context);
153-
154-
switch (LuisRecognizer.topIntent(luisResult)) {
155-
case 'BookFlight':
156-
// Extract the values for the composite entities from the LUIS result.
157-
const fromEntities = this.luisRecognizer.getFromEntities(luisResult);
158-
const toEntities = this.luisRecognizer.getToEntities(luisResult);
159-
160-
// Show a warning for Origin and Destination if we can't resolve them.
161-
await this.showWarningForUnsupportedCities(stepContext.context, fromEntities, toEntities);
162-
163-
// Initialize BookingDetails with any entities we may have found in the response.
164-
bookingDetails.destination = toEntities.airport;
165-
bookingDetails.origin = fromEntities.airport;
166-
bookingDetails.travelDate = this.luisRecognizer.getTravelDate(luisResult);
167-
console.log('LUIS extracted these booking details:', JSON.stringify(bookingDetails));
168-
169-
// Run the BookingDialog passing in whatever details we have from the LUIS call, it will fill out the remainder.
170-
return await stepContext.beginDialog('bookingDialog', bookingDetails);
171-
172-
case 'GetWeather':
173-
// We haven't implemented the GetWeatherDialog so we just display a TODO message.
174-
const getWeatherMessageText = 'TODO: get weather flow here';
175-
await stepContext.context.sendActivity(getWeatherMessageText, getWeatherMessageText, InputHints.IgnoringInput);
176-
break;
177-
178-
default:
179-
// Catch all for unhandled intents
180-
const didntUnderstandMessageText = `Sorry, I didn't get that. Please try asking in a different way (intent was ${ LuisRecognizer.topIntent(luisResult) })`;
181-
await stepContext.context.sendActivity(didntUnderstandMessageText, didntUnderstandMessageText, InputHints.IgnoringInput);
182-
}
183-
184-
return await stepContext.next();
145+
async executeLuisQuery(context) {
146+
return await this.recognizer.recognize(context);
147+
}
148+
149+
getFromEntities(result) {
150+
let fromValue, fromAirportValue;
151+
if (result.entities.$instance.From) {
152+
fromValue = result.entities.$instance.From[0].text;
153+
}
154+
if (fromValue && result.entities.From[0].Airport) {
155+
fromAirportValue = result.entities.From[0].Airport[0][0];
156+
}
185157

158+
return { from: fromValue, airport: fromAirportValue };
186159
}
187160

188-
...
161+
getToEntities(result) {
162+
let toValue, toAirportValue;
163+
if (result.entities.$instance.To) {
164+
toValue = result.entities.$instance.To[0].text;
165+
}
166+
if (toValue && result.entities.To[0].Airport) {
167+
toAirportValue = result.entities.To[0].Airport[0][0];
168+
}
189169

170+
return { to: toValue, airport: toAirportValue };
171+
}
172+
173+
/**
174+
* This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part.
175+
* TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year.
176+
*/
177+
getTravelDate(result) {
178+
const datetimeEntity = result.entities.datetime;
179+
if (!datetimeEntity || !datetimeEntity[0]) return undefined;
180+
181+
const timex = datetimeEntity[0].timex;
182+
if (!timex || !timex[0]) return undefined;
183+
184+
const datetime = timex[0].split('T')[0];
185+
return datetime;
186+
}
190187
}
188+
189+
module.exports.FlightBookingRecognizer = FlightBookingRecognizer;
191190
````
192191
<a name="ask-bot-a-question-for-the-book-flight-intent"></a>
193192

@@ -203,7 +202,7 @@ Ask bot a question for the Book Flight intent.
203202

204203
[![Basic bot response in emulator](./media/bfv4-nodejs/ask-bot-emulator-a-question-and-get-response.png)](./media/bfv4-nodejs/ask-bot-emulator-a-question-and-get-response.png#lightbox)
205204

206-
1. Select **Yes**. The bot responds with a summary of its actions.
205+
1. Select **Yes**. The bot responds with a summary of its actions.
207206
1. From the log of the bot emulator, select the line that includes `Luis Trace`. This displays the JSON response from LUIS for the intent and entities of the utterance.
208207

209208
[![Basic bot response in emulator](./media/bfv4-nodejs/ask-luis-book-flight-question-get-json-response-in-bot-emulator.png)](./media/bfv4-nodejs/ask-luis-book-flight-question-get-json-response-in-bot-emulator.png#lightbox)
@@ -213,7 +212,7 @@ Ask bot a question for the Book Flight intent.
213212

214213
## Next steps
215214

216-
See more [samples](https://github.com/microsoft/botframework-solutions) with conversational bots.
215+
See more [samples](https://github.com/microsoft/botframework-solutions) with conversational bots.
217216

218217
> [!div class="nextstepaction"]
219218
> [Build a Language Understanding app with a custom subject domain](luis-quickstart-intents-only.md)

0 commit comments

Comments
 (0)