|
| 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. |
0 commit comments