Skip to content

Commit 371f4cc

Browse files
authored
Merge pull request #101524 from diberry/diberry/0117-luis-sdk-prediction
[Cogsvcs] LUIS - Prediction - SDK - pivot
2 parents a440e32 + c6ee7bc commit 371f4cc

9 files changed

+550
-169
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
---
2+
title: include file
3+
description: include file
4+
services: cognitive-services
5+
author: diberry
6+
manager: nitinme
7+
ms.service: cognitive-services
8+
ms.subservice: luis
9+
ms.topic: include
10+
ms.custom: include file
11+
ms.date: 01/14/2020
12+
ms.author: diberry
13+
---
14+
15+
16+
Use the Language Understanding (LUIS) prediction client library for .NET to:
17+
18+
* Get prediction by slot
19+
20+
[Reference documentation](https://docs.microsoft.com/dotnet/api/overview/azure/cognitiveservices/client/languageunderstanding?view=azure-dotnet) | [Library source code](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/cognitiveservices/Language.LUIS.Runtime) | [Prediction runtime Package (NuGet)](https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime/) | [C# Samples](https://github.com/Azure-Samples/cognitive-services-quickstart-code/tree/master/dotnet/LanguageUnderstanding/predict-with-sdk-3x)
21+
22+
## Prerequisites
23+
24+
* Language Understanding (LUIS) portal account - [Create one for free](https://www.luis.ai)
25+
* The current version of [.NET Core](https://dotnet.microsoft.com/download/dotnet-core).
26+
27+
Looking for more documentation?
28+
29+
* [SDK reference documentation](https://docs.microsoft.com/dotnet/api/overview/azure/cognitiveservices/client/languageunderstanding?view=azure-dotnet)
30+
31+
## Setting up
32+
33+
### Create an environment variable
34+
35+
Using your key, and the name of your resource, create two environment variables for authentication:
36+
37+
* `LUIS_PREDICTION_KEY` - The resource key for authenticating your requests.
38+
* `LUIS_ENDPOINT_NAME` - The resource name associated with your key.
39+
40+
Use the instructions for your operating system.
41+
42+
#### [Windows](#tab/windows)
43+
44+
```console
45+
setx LUIS_PREDICTION_KEY <replace-with-your-resource-key>
46+
setx LUIS_ENDPOINT_NAME <replace-with-your-resource-name>
47+
```
48+
49+
After you add the environment variable, restart the console window.
50+
51+
#### [Linux](#tab/linux)
52+
53+
```bash
54+
export LUIS_PREDICTION_KEY=<replace-with-your-resource-key>
55+
export LUIS_ENDPOINT_NAME=<replace-with-your-resource-name>
56+
```
57+
58+
After you add the environment variable, run `source ~/.bashrc` from your console window to make the changes effective.
59+
60+
#### [macOS](#tab/unix)
61+
62+
Edit your `.bash_profile`, and add the environment variable:
63+
64+
```bash
65+
export LUIS_PREDICTION_KEY=<replace-with-your-resource-key>
66+
export LUIS_ENDPOINT_NAME=<replace-with-your-resource-name>
67+
```
68+
69+
After you add the environment variable, run `source .bash_profile` from your console window to make the changes effective.
70+
71+
---
72+
73+
### Create a new C# application
74+
75+
Create a new .NET Core application in your preferred editor or IDE.
76+
77+
1. In a console window (such as cmd, PowerShell, or Bash), use the dotnet `new` command to create a new console app with the name `language-understanding-quickstart`. This command creates a simple "Hello World" C# project with a single source file: `Program.cs`.
78+
79+
```dotnetcli
80+
dotnet new console -n language-understanding-quickstart
81+
```
82+
83+
1. Change your directory to the newly created app folder.
84+
85+
1. You can build the application with:
86+
87+
```dotnetcli
88+
dotnet build
89+
```
90+
91+
The build output should contain no warnings or errors.
92+
93+
```console
94+
...
95+
Build succeeded.
96+
0 Warning(s)
97+
0 Error(s)
98+
...
99+
```
100+
101+
### Install the SDK
102+
103+
Within the application directory, install the Language Understanding (LUIS) prediction runtime client library for .NET with the following command:
104+
105+
```dotnetcli
106+
dotnet add package Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime --version 3.0.0
107+
```
108+
109+
If you're using the Visual Studio IDE, the client library is available as a downloadable NuGet package.
110+
111+
## Object model
112+
113+
The Language Understanding (LUIS) prediction runtime client is a [LUISRuntimeClient](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.luis.runtime.luisruntimeclient?view=azure-dotnet) object that authenticates to Azure, which contains your resource key.
114+
115+
Once the client is created, use this client to access functionality including:
116+
117+
* Prediction by [staging or product slot](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.luis.runtime.predictionoperationsextensions.getslotpredictionasync?view=azure-dotnet)
118+
* Prediction by [version](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.luis.runtime.predictionoperationsextensions.getversionpredictionasync?view=azure-dotnet)
119+
120+
121+
## Code examples
122+
123+
These code snippets show you how to do the following with the Language Understanding (LUIS) prediction runtime client library for .NET:
124+
125+
* [Prediction by slot](#get-prediction-from-runtime)
126+
127+
## Add the dependencies
128+
129+
From the project directory, open the *Program.cs* file in your preferred editor or IDE. Replace the existing `using` code with the following `using` directives:
130+
131+
[!code-csharp[Using statements](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_using)]
132+
133+
## Authenticate the client
134+
135+
1. Create variables for the key, name and app ID:
136+
137+
A variables to manage your prediction key pulled from an environment variable named `LUIS_PREDICTION_KEY`. If you created the environment variable after the application is launched, the editor, IDE, or shell running it will need to be closed and reloaded to access the variable. The methods will be created later.
138+
139+
Create a variable to hold your resource name `LUIS_ENDPOINT_NAME`.
140+
141+
Create a variable for the app ID as an environment variable named `LUIS_APP_ID`. Set the environment variable to the public IoT app:
142+
143+
**`df67dcdb-c37d-46af-88e1-8b97951ca1c2`**
144+
145+
[!code-csharp[Create variables](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_variables)]
146+
147+
1. Create an [ApiKeyServiceClientCredentials](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.luis.runtime.apikeyserviceclientcredentials?view=azure-dotnet) object with your key, and use it with your endpoint to create an [LUISRuntimeClient](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.luis.runtime.luisruntimeclient?view=azure-dotnet) object.
148+
149+
[!code-csharp[Create LUIS client object](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_create_client)]
150+
151+
## Get prediction from runtime
152+
153+
Add the following method to create the request to the prediction runtime.
154+
155+
The user utterance is part of the [PredictionRequest](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.luis.runtime.models.predictionrequest?view=azure-dotnet) object.
156+
157+
The **GetSlotPredictionAsync** method needs several parameters such as the app ID, the slot name, the prediction request object to fulfill the request. The other options such as verbose, show all intents, and log are optional.
158+
159+
[!code-csharp[Create method to get prediction runtime](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_maintask)]
160+
161+
## Main code for the prediction
162+
163+
Use the following main method to tie the variables and methods together to get the prediction.
164+
165+
[!code-csharp[Create method to get prediction runtime](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_main)]
166+
167+
## Run the application
168+
169+
Run the application with the `dotnet run` command from your application directory.
170+
171+
```dotnetcli
172+
dotnet run
173+
```
174+
175+
## Clean up resources
176+
177+
When you are done with your predictions, clean up the work from this quickstart by deleting the program.cs file and its subdirectories.

articles/cognitive-services/LUIS/includes/sdk-nodejs-authoring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.service: cognitive-services
88
ms.subservice: luis
99
ms.topic: include
1010
ms.custom: include file
11-
ms.date: 01/22/2020
11+
ms.date: 01/23/2020
1212
ms.author: diberry
1313
---
1414
Use the Language Understanding (LUIS) authoring client library for Node.js to:
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
title: include file
3+
description: include file
4+
services: cognitive-services
5+
author: diberry
6+
manager: nitinme
7+
ms.service: cognitive-services
8+
ms.subservice: luis
9+
ms.topic: include
10+
ms.custom: include file
11+
ms.date: 01/23/2020
12+
ms.author: diberry
13+
---
14+
Use the Language Understanding (LUIS) runtime client library for Node.js to:
15+
16+
* Prediction by slot
17+
* Prediction by Version
18+
19+
[Reference documentation](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-luis-runtime/?view=azure-node-latest) | [Library source code](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/cognitiveservices/cognitiveservices-luis-runtime) | [Runtime Package (NPM)](https://www.npmjs.com/package/@azure/cognitiveservices-luis-runtime) | [Samples](https://github.com/Azure-Samples/cognitive-services-quickstart-code/blob/master/javascript/LUIS/luis_prediction.js)
20+
21+
## Prerequisites
22+
23+
* Language Understanding runtime resource: [Create one in the Azure portal](https://ms.portal.azure.com/#create/Microsoft.CognitiveServicesLUISAllInOne)
24+
* [Node.js](https://nodejs.org)
25+
26+
## Setting up
27+
28+
### Get your Language Understanding (LUIS) runtime key
29+
30+
Get your [runtime key](../luis-how-to-azure-subscription.md) by creating a LUIS runtime resource. Keep your key, and the endpoint of the key for the next step.
31+
32+
[!INCLUDE [Set up environment variables for prediction quickstart](sdk-prediction-environment-variables.md)]
33+
34+
### Create a new javascript (Node.js) file
35+
36+
Create a new javascript file in your preferred editor or IDE, named `luis_prediction.js`.
37+
38+
### Install the NPM library for the LUIS runtime
39+
40+
Within the application directory, install the dependencies with the following command:
41+
42+
```console
43+
npm install @azure/cognitiveservices-luis-runtime @azure/ms-rest-js
44+
```
45+
46+
## Object model
47+
48+
The Language Understanding (LUIS) authoring client is a [LUISAuthoringClient](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-luis-runtime/luisruntimeclient?view=azure-node-latest) object that authenticates to Azure, which contains your authoring key.
49+
50+
Once the client is created, use this client to access functionality including:
51+
52+
* [Prediction](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-luis-runtime/predictionoperations?view=azure-node-latest#getslotprediction-string--string--predictionrequest--models-predictiongetslotpredictionoptionalparams-) by `staging` or `production` slot
53+
* [Prediction by version](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-luis-runtime/predictionoperations?view=azure-node-latest#getversionprediction-string--string--predictionrequest--models-predictiongetversionpredictionoptionalparams-)
54+
55+
## Code examples
56+
57+
These code snippets show you how to do the following with the Language Understanding (LUIS) prediction runtime client library:
58+
59+
* [Prediction by slot](#get-prediction-from-runtime)
60+
61+
## Add the dependencies
62+
63+
From the project directory, open the `luis_prediction.js` file in your preferred editor or IDE. Add the following dependencies:
64+
65+
[!code-javascript [Dependencies](~/cognitive-services-quickstart-code/javascript/LUIS/luis_prediction.js?name=Dependencies)]
66+
67+
## Authenticate the client
68+
69+
1. Create variables for your own required LUIS information:
70+
71+
Add variables to manage your prediction key pulled from an environment variable named `LUIS_RUNTIME_KEY`. If you created the environment variable after the application is launched, the editor, IDE, or shell running it will need to be closed and reloaded to access the variable. The methods will be created later.
72+
73+
Create a variable to hold your resource name `LUIS_RUNTIME_ENDPOINT`.
74+
75+
[!code-javascript [Azure resource variables](~/cognitive-services-quickstart-code/javascript/LUIS/luis_prediction.js?name=Variables)]
76+
77+
1. Create a variable for the app ID as an environment variable named `LUIS_APP_ID`. Set the environment variable to the public IoT app, **`df67dcdb-c37d-46af-88e1-8b97951ca1c2`** . Create a variable to set the `production` published slot.
78+
79+
[!code-javascript [LUIS app variables](~/cognitive-services-quickstart-code/javascript/LUIS/luis_prediction.js?name=OtherVariables)]
80+
81+
82+
1. Create an msRest.ApiKeyCredentials object with your key, and use it with your endpoint to create an [LUIS.LUISRuntimeClient](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-luis-runtime/luisruntimeclient?view=azure-node-latest) object.
83+
84+
[!code-javascript [LUIS Runtime client is required to access predictions for LUIS apps](~/cognitive-services-quickstart-code/javascript/LUIS/luis_prediction.js?name=AuthoringCreateClient)]
85+
86+
## Get prediction from runtime
87+
88+
Add the following method to create the request to the prediction runtime.
89+
90+
The user utterance is part of the [predictionRequest](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-luis-runtime/predictionrequest?view=azure-node-latest) object.
91+
92+
The **[luisRuntimeClient.prediction.getSlotPrediction](https://docs.microsoft.com/javascript/api/@azure/cognitiveservices-luis-runtime/predictionoperations?view=azure-node-latest#getslotprediction-string--string--predictionrequest--models-predictiongetslotpredictionoptionalparams-)** method needs several parameters such as the app ID, the slot name, and the prediction request object to fulfill the request. The other options such as verbose, show all intents, and log are optional.
93+
94+
[!code-javascript [LUIS prediction request and response in Node.js NPM SDK](~/cognitive-services-quickstart-code/javascript/LUIS/luis_prediction.js?name=predict)]
95+
96+
## Main code for the prediction
97+
98+
Use the following main method to tie the variables and methods together to get the prediction.
99+
100+
[!code-javascript [Main method and main call](~/cognitive-services-quickstart-code/javascript/LUIS/luis_prediction.js?name=Main)]
101+
102+
## Run the application
103+
104+
Run the application with the `node luis_prediction.js` command from your application directory.
105+
106+
```console
107+
node luis_prediction.js
108+
```
109+
110+
The prediction result returns a JSON object:
111+
112+
```console
113+
{
114+
"query":"turn on all lights",
115+
"prediction":{
116+
"topIntent":"HomeAutomation.TurnOn",
117+
"intents":{
118+
"HomeAutomation.TurnOn":{
119+
"score":0.5375382
120+
},
121+
"None":{
122+
"score":0.08687421
123+
},
124+
"HomeAutomation.TurnOff":{
125+
"score":0.0207554
126+
}
127+
},
128+
"entities":{
129+
"HomeAutomation.Operation":[
130+
"on"
131+
],
132+
"$instance":{
133+
"HomeAutomation.Operation":[
134+
{
135+
"type":"HomeAutomation.Operation",
136+
"text":"on",
137+
"startIndex":5,
138+
"length":2,
139+
"score":0.724984169,
140+
"modelTypeId":-1,
141+
"modelType":"Unknown",
142+
"recognitionSources":[
143+
"model"
144+
]
145+
}
146+
]
147+
}
148+
}
149+
}
150+
}
151+
```
152+
153+
154+
## Clean up resources
155+
156+
When you are done with your predictions, clean up the work from this quickstart by deleting the file and its subdirectories.

0 commit comments

Comments
 (0)