|
1 | 1 | ---
|
2 | 2 | title: "Quickstart: C# SDK query prediction endpoint - LUIS"
|
3 |
| -titleSuffix: Azure Cognitive Services |
4 |
| -description: This article will show you how to use the C# SDK to send a user utterance to the Azure Cognitive Services LUIS application and receive a prediction. |
| 3 | +titleSuffix: Azure Cognitive Services |
| 4 | +description: This article will show you how to use the C# SDK to send a user utterance to the Azure Cognitive Services LUIS application and receive a prediction. |
5 | 5 | author: diberry
|
6 | 6 | manager: nitinme
|
7 | 7 | ms.service: cognitive-services
|
8 | 8 | services: cognitive-services
|
9 | 9 | ms.subservice: language-understanding
|
10 | 10 | ms.topic: quickstart
|
11 |
| -ms.date: 09/27/2019 |
| 11 | +ms.date: 12/09/2019 |
12 | 12 | ms.author: diberry
|
13 | 13 | ---
|
14 | 14 |
|
15 |
| -# Quickstart: Query V2 prediction endpoint with C# .NET SDK |
| 15 | +# Quickstart: Query V3 prediction endpoint with C# .NET SDK |
16 | 16 |
|
17 |
| -Use the .NET SDK, found on [NuGet](https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime/), to send a user utterance to Language Understanding (LUIS) and receive a prediction of the user's intention. |
| 17 | +Use the .NET SDK, found on [NuGet](https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime/), to send a user utterance to Language Understanding (LUIS) and receive a prediction of the user's intention. |
18 | 18 |
|
19 |
| -This quickstart sends a user utterance, such as `turn on the bedroom light`, to a public Language Understanding application, then receives the prediction and displays the top-scoring intent `HomeAutomation.TurnOn` and the entity `HomeAutomation.Room` found within the utterance. |
| 19 | +Use the Language Understanding (LUIS) prediction client library for .NET to: |
20 | 20 |
|
21 |
| -## Prerequisites |
| 21 | +* Get prediction by slot |
| 22 | + |
| 23 | +[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) |
22 | 24 |
|
23 |
| -* [Visual Studio Community 2017 edition](https://visualstudio.microsoft.com/vs/community/) |
24 |
| -* C# programming language (included with VS Community 2017) |
25 |
| -* Public app ID: df67dcdb-c37d-46af-88e1-8b97951ca1c2 |
| 25 | +## Prerequisites |
26 | 26 |
|
27 |
| -> [!Note] |
28 |
| -> The complete solution is available from the [cognitive-services-language-understanding](https://github.com/Azure-Samples/cognitive-services-language-understanding/tree/master/documentation-samples/sdk-quickstarts/c%23/UsePredictionRuntime) GitHub repository. |
| 27 | +* Language Understanding (LUIS) portal account - [Create one for free](https://www.luis.ai) |
| 28 | +* The current version of [.NET Core](https://dotnet.microsoft.com/download/dotnet-core). |
29 | 29 |
|
30 | 30 | Looking for more documentation?
|
31 | 31 |
|
32 | 32 | * [SDK reference documentation](https://docs.microsoft.com/dotnet/api/overview/azure/cognitiveservices/client/languageunderstanding?view=azure-dotnet)
|
33 | 33 |
|
| 34 | +## Setting up |
| 35 | + |
| 36 | +### Create an environment variable |
34 | 37 |
|
35 |
| -## Get Cognitive Services or Language Understanding key |
| 38 | +Using your key, and the name of your resource, create two environment variables for authentication: |
36 | 39 |
|
37 |
| -In order to use the public app for home automation, you need a valid key for endpoint predictions. You can use either a Cognitive Services key (created below with the Azure CLI), which is valid for many cognitive services, or a `Language Understanding` key. |
| 40 | +* `LUIS_PREDICTION_KEY` - The resource key for authenticating your requests. |
| 41 | +* `LUIS_ENDPOINT_NAME` - The resource name associated with your key. |
38 | 42 |
|
39 |
| -Use the following [Azure CLI command to create a Cognitive Service key](https://docs.microsoft.com/cli/azure/cognitiveservices/account?view=azure-cli-latest#az-cognitiveservices-account-create): |
| 43 | +Use the instructions for your operating system. |
40 | 44 |
|
41 |
| -```azurecli-interactive |
42 |
| -az cognitiveservices account create \ |
43 |
| - -n my-cog-serv-resource \ |
44 |
| - -g my-cog-serv-resource-group \ |
45 |
| - --kind CognitiveServices \ |
46 |
| - --sku S0 \ |
47 |
| - -l WestEurope \ |
48 |
| - --yes |
| 45 | +#### [Windows](#tab/windows) |
| 46 | + |
| 47 | +```console |
| 48 | +setx LUIS_PREDICTION_KEY <replace-with-your-resource-key> |
| 49 | +setx LUIS_ENDPOINT_NAME <replace-with-your-resource-name> |
49 | 50 | ```
|
50 | 51 |
|
51 |
| -## Create .NET Core project |
| 52 | +After you add the environment variable, restart the console window. |
52 | 53 |
|
53 |
| -Create a .NET Core console project in Visual Studio Community 2017. |
| 54 | +#### [Linux](#tab/linux) |
| 55 | + |
| 56 | +```bash |
| 57 | +export LUIS_PREDICTION_KEY=<replace-with-your-resource-key> |
| 58 | +export LUIS_ENDPOINT_NAME=<replace-with-your-resource-name> |
| 59 | +``` |
54 | 60 |
|
55 |
| -1. Open Visual Studio Community 2017. |
56 |
| -1. Create a new project, from the **Visual C#** section, choose **Console App (.NET Core)**. |
57 |
| -1. Enter the project name `QueryPrediction`, leave the remaining default values, and select **OK**. |
58 |
| - This creates a simple project with the primary code file named **Program.cs**. |
| 61 | +After you add the environment variable, run `source ~/.bashrc` from your console window to make the changes effective. |
59 | 62 |
|
60 |
| -## Add SDK with NuGet |
| 63 | +#### [macOS](#tab/unix) |
61 | 64 |
|
62 |
| -1. In the **Solution Explorer**, select the Project in the tree view named **QueryPrediction**, then right-click. From the menu, select **Manage NuGet Packages...**. |
63 |
| -1. Select **Browse** then enter `Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime`. When the package information displays, select **Install** to install the package into the project. |
64 |
| -1. Add the following _using_ statements to the top of **Program.cs**. Do not remove the existing _using_ statement for `System`. |
| 65 | +Edit your `.bash_profile`, and add the environment variable: |
65 | 66 |
|
66 |
| -```csharp |
67 |
| -using System.Threading; |
68 |
| -using System.Threading.Tasks; |
69 |
| -using Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime; |
70 |
| -using Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime.Models; |
| 67 | +```bash |
| 68 | +export LUIS_PREDICTION_KEY=<replace-with-your-resource-key> |
| 69 | +export LUIS_ENDPOINT_NAME=<replace-with-your-resource-name> |
71 | 70 | ```
|
72 | 71 |
|
73 |
| -## Create a new method for the prediction |
| 72 | +After you add the environment variable, run `source .bash_profile` from your console window to make the changes effective. |
| 73 | +*** |
74 | 74 |
|
75 |
| -Create a new method, `GetPrediction` to send the query to the query prediction endpoint. The method will create and configure all necessary objects then return a `Task` with the [`LuisResult`](/python/api/azure-cognitiveservices-language-luis/azure.cognitiveservices.language.luis.runtime.models.luisresult) prediction results. |
| 75 | +### Create a new C# application |
76 | 76 |
|
77 |
| -```csharp |
78 |
| -static async Task<LuisResult> GetPrediction() { |
79 |
| -} |
80 |
| -``` |
| 77 | +Create a new .NET Core application in your preferred editor or IDE. |
81 | 78 |
|
82 |
| -## Create credentials object |
| 79 | +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`. |
83 | 80 |
|
84 |
| -Add the following code to the `GetPrediction` method to create the client credentials with your Cognitive Service key. |
| 81 | + ```dotnetcli |
| 82 | + dotnet new console -n language-understanding-quickstart |
| 83 | + ``` |
85 | 84 |
|
86 |
| -Replace `<REPLACE-WITH-YOUR-KEY>` with your Cognitive Service key's region. The key is in the [Azure portal](https://portal.azure.com) on the Keys page for that resource. |
| 85 | +1. Change your directory to the newly created app folder. |
87 | 86 |
|
88 |
| -```csharp |
89 |
| -// Use Language Understanding or Cognitive Services key |
90 |
| -// to create authentication credentials |
91 |
| -var endpointPredictionkey = "<REPLACE-WITH-YOUR-KEY>"; |
92 |
| -var credentials = new ApiKeyServiceClientCredentials(endpointPredictionkey); |
93 |
| -``` |
| 87 | +1. You can build the application with: |
| 88 | +
|
| 89 | + ```dotnetcli |
| 90 | + dotnet build |
| 91 | + ``` |
| 92 | +
|
| 93 | + The build output should contain no warnings or errors. |
94 | 94 |
|
95 |
| -## Create Language Understanding client |
| 95 | + ```console |
| 96 | + ... |
| 97 | + Build succeeded. |
| 98 | + 0 Warning(s) |
| 99 | + 0 Error(s) |
| 100 | + ... |
| 101 | + ``` |
96 | 102 |
|
97 |
| -In the `GetPrediction` method, after the preceding code, add the following code to use the new credentials, creating a [`LUISRuntimeClient`](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.luis.runtime.luisruntimeclient.-ctor?view=azure-dotnet#Microsoft_Azure_CognitiveServices_Language_LUIS_Runtime_LUISRuntimeClient__ctor_Microsoft_Rest_ServiceClientCredentials_System_Net_Http_DelegatingHandler___) client object. |
| 103 | +### Install the SDK |
98 | 104 |
|
99 |
| -Replace `<REPLACE-WITH-YOUR-KEY-REGION>` with your key's region, such as `westus`. The key region is in the [Azure portal](https://portal.azure.com) on the Overview page for that resource. |
| 105 | +Within the application directory, install the Language Understanding (LUIS) prediction runtime client library for .NET with the following command: |
100 | 106 |
|
101 |
| -```csharp |
102 |
| -// Create Luis client and set endpoint |
103 |
| -// region of endpoint must match key's region, for example `westus` |
104 |
| -var luisClient = new LUISRuntimeClient(credentials, new System.Net.Http.DelegatingHandler[] { }); |
105 |
| -luisClient.Endpoint = "https://<REPLACE-WITH-YOUR-KEY-REGION>.api.cognitive.microsoft.com"; |
| 107 | +```dotnetcli |
| 108 | +dotnet add package Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime --version 3.0.0 |
106 | 109 | ```
|
107 | 110 |
|
108 |
| -## Set query parameters |
| 111 | +If you're using the Visual Studio IDE, the client library is available as a downloadable NuGet package. |
109 | 112 |
|
110 |
| -In the `GetPrediction` method, after the preceding code, add the following code to set the query parameters. |
| 113 | +## Object model |
111 | 114 |
|
112 |
| -```csharp |
113 |
| -// public Language Understanding Home Automation app |
114 |
| -var appId = "df67dcdb-c37d-46af-88e1-8b97951ca1c2"; |
| 115 | +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. |
115 | 116 |
|
116 |
| -// query specific to home automation app |
117 |
| -var query = "turn on the bedroom light"; |
| 117 | +Once the client is created, use this client to access functionality including: |
118 | 118 |
|
119 |
| -// common settings for remaining parameters |
120 |
| -Double? timezoneOffset = null; |
121 |
| -var verbose = true; |
122 |
| -var staging = false; |
123 |
| -var spellCheck = false; |
124 |
| -String bingSpellCheckKey = null; |
125 |
| -var log = false; |
126 |
| -``` |
| 119 | +* Prediction by [staging or product slot]((https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.luis.runtime.predictionoperationsextensions.getslotpredictionasync?view=azure-dotnet)) |
| 120 | +* Prediction by [version](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.luis.runtime.predictionoperationsextensions.getversionpredictionasync?view=azure-dotnet) |
127 | 121 |
|
128 |
| -## Query prediction endpoint |
129 | 122 |
|
130 |
| -In the `GetPrediction` method, after the preceding code, add the following code to set the query parameters: |
| 123 | +## Code examples |
131 | 124 |
|
132 |
| -```csharp |
133 |
| -// Create prediction client |
134 |
| -var prediction = new Prediction(luisClient); |
| 125 | +These code snippets show you how to do the following with the Language Understanding (LUIS) prediction runtime client library for .NET: |
135 | 126 |
|
136 |
| -// get prediction |
137 |
| -return await prediction.ResolveAsync(appId, query, timezoneOffset, verbose, staging, spellCheck, bingSpellCheckKey, log, CancellationToken.None); |
138 |
| -``` |
| 127 | +* [Prediction by slot](#get-prediction-from-runtime) |
139 | 128 |
|
140 |
| -## Display prediction results |
| 129 | +## Add the dependencies |
141 | 130 |
|
142 |
| -Change the **Main** method to call the new `GetPrediction` method and return the prediction results: |
| 131 | +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: |
143 | 132 |
|
144 |
| -```csharp |
145 |
| -static void Main(string[] args) |
146 |
| -{ |
| 133 | +[!code-csharp[Using statements](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_using)] |
147 | 134 |
|
148 |
| - var luisResult = GetPrediction().Result; |
| 135 | +## Authenticate the client |
149 | 136 |
|
150 |
| - // Display query |
151 |
| - Console.WriteLine("Query:'{0}'", luisResult.Query); |
| 137 | +1. Create variables for the key, name and app ID: |
152 | 138 |
|
153 |
| - // Display most common properties of query result |
154 |
| - Console.WriteLine("Top intent is '{0}' with score {1}", luisResult.TopScoringIntent.Intent,luisResult.TopScoringIntent.Score); |
| 139 | + 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. |
155 | 140 |
|
156 |
| - // Display all entities detected in query utterance |
157 |
| - foreach (var entity in luisResult.Entities) |
158 |
| - { |
159 |
| - Console.WriteLine("{0}:'{1}' begins at position {2} and ends at position {3}", entity.Type, entity.Entity, entity.StartIndex, entity.EndIndex); |
160 |
| - } |
| 141 | + Create a variable to hold your resource name `LUIS_ENDPOINT_NAME`. |
161 | 142 |
|
162 |
| - Console.Write("done"); |
| 143 | + Create a variable for the app ID as an environment variable named `LUIS_APP_ID`. Set the environment variable to the public IoT app: |
163 | 144 |
|
164 |
| -} |
165 |
| -``` |
| 145 | + **`df67dcdb-c37d-46af-88e1-8b97951ca1c2`** |
166 | 146 |
|
167 |
| -## Run the project |
| 147 | + [!code-csharp[Create variables](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_variables)] |
168 | 148 |
|
169 |
| -Build the project in Studio and run the project to the see the output of the query: |
| 149 | +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. |
170 | 150 |
|
171 |
| -```console |
172 |
| -Query:'turn on the bedroom light' |
173 |
| -Top intent is 'HomeAutomation.TurnOn' with score 0.809439957 |
174 |
| -HomeAutomation.Room:'bedroom' begins at position 12 and ends at position 18 |
| 151 | + [!code-csharp[Create LUIS client object](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_create_client)] |
| 152 | + |
| 153 | +## Get prediction from runtime |
| 154 | + |
| 155 | +Add the following method to create the request to the prediction runtime. |
| 156 | + |
| 157 | +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. |
| 158 | + |
| 159 | +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. |
| 160 | + |
| 161 | +[!code-csharp[Create method to get prediction runtime](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_maintask)] |
| 162 | + |
| 163 | +## Main code for the prediction |
| 164 | + |
| 165 | +Use the following main method to tie the variables and methods together to get the prediction. |
| 166 | + |
| 167 | +[!code-csharp[Create method to get prediction runtime](~/cognitive-services-quickstart-code/dotnet/LanguageUnderstanding/predict-with-sdk-3x/Program.cs?name=snippet_main)] |
| 168 | + |
| 169 | +## Run the application |
| 170 | + |
| 171 | +Run the application with the `dotnet run` command from your application directory. |
| 172 | + |
| 173 | +```dotnetcli |
| 174 | +dotnet run |
175 | 175 | ```
|
176 | 176 |
|
| 177 | +## Clean up resources |
| 178 | + |
| 179 | +When you are done with your predictions, clean up the work from this quickstart by deleting the program.cs file and its subdirectories. |
| 180 | + |
177 | 181 | ## Next steps
|
178 | 182 |
|
179 |
| -Learn more about the [.NET SDK](https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime/) and the [.NET reference documentation](https://docs.microsoft.com/dotnet/api/overview/azure/cognitiveservices/client/languageunderstanding?view=azure-dotnet). |
| 183 | +Learn more about the [.NET SDK](https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime/) and the [.NET reference documentation](https://docs.microsoft.com/dotnet/api/overview/azure/cognitiveservices/client/languageunderstanding?view=azure-dotnet). |
180 | 184 |
|
181 |
| -> [!div class="nextstepaction"] |
182 |
| -> [Tutorial: Build LUIS app to determine user intentions](luis-quickstart-intents-only.md) |
| 185 | +> [!div class="nextstepaction"] |
| 186 | +> [Tutorial: Build LUIS app to determine user intentions](luis-quickstart-intents-only.md) |
0 commit comments