How to get an intent using the REST APIs
In this article, you will use a LUIS app to determine a user's intention from conversational text. Send the user's intention as text to the Pizza app's HTTP prediction endpoint. At the endpoint, LUIS applies the Pizza app's model to analyze the natural language text for meaning, determining overall intent and extracting data relevant to the app's subject domain.
For this article, you need a free LUIS account.
Reference documentation | Sample
Prerequisites
Create Pizza app
- Select pizza-app-for-luis-v6.json to bring up the GitHub page for the
pizza-app-for-luis.json
file. - Right-click or long tap the Raw button and select Save link as to save the
pizza-app-for-luis.json
to your computer. - Sign into the LUIS portal.
- Select My Apps.
- On the My Apps page, select + New app for conversation.
- Select Import as JSON.
- In the Import new app dialog, select the Choose File button.
- Select the
pizza-app-for-luis.json
file you downloaded, then select Open. - In the Import new app dialog Name field, enter a name for your Pizza app, then select the Done button.
The app will be imported.
If you see the dialog How to create an effective LUIS app, close the dialog.
Train and publish the Pizza app
You should see the Intents page with a list of the intents in the Pizza app.
In the top-right side of the LUIS website, select the Train button.
Training is complete when status indicator on the Train button is green.
In order to receive a LUIS prediction in a chat bot or other client application, you need to publish the app to the prediction endpoint.
Select Publish in the top-right navigation.
Select the Production slot, then select Done.
Select Access your endpoint URLs in the notification to go to the Azure Resources page. You will only be able to see the URLs if you have a prediction resource associated with the app. You can also find the Azure Resources page by clicking Manage.
Your Pizza app is now ready to use.
Record the app ID, prediction key, and prediction endpoint of your Pizza app
To use your new Pizza app, you will need the app ID, prediction key, and prediction endpoint of your Pizza app.
To find these values:
- From the Intents page, select MANAGE.
- From the Application Settings page, record the App ID.
- Select Azure Resources.
- From the Azure Resources page, record the Primary Key. This value is your prediction key.
- Record the Endpoint URL. This value is your prediction endpoint.
Get intent programmatically
Use C# (.NET Core) to query the prediction endpoint and get a prediction result.
Create a new console application targeting the C# language, with a project and folder name of
csharp-predict-with-rest
.dotnet new console -lang C# -n csharp-predict-with-rest
Change to the
csharp-predict-with-rest
directory you created, and install the required dependency with this command:cd csharp-predict-with-rest dotnet add package System.Net.Http
Open
Program.cs
in your favorite IDE or editor. Then overwriteProgram.cs
with the following code:// // This quickstart shows how to predict the intent of an utterance by using the LUIS REST APIs. // using System; using System.Net.Http; using System.Web; namespace predict_with_rest { class Program { static void Main(string[] args) { ////////// // Values to modify. // YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page. var appId = "YOUR-APP-ID"; // YOUR-PREDICTION-KEY: 32 character key. var predictionKey = "YOUR-PREDICTION-KEY"; // YOUR-PREDICTION-ENDPOINT: Example is "https://westus.api.cognitive.microsoft.com/" var predictionEndpoint = "https://YOUR-PREDICTION-ENDPOINT/"; // An utterance to test the pizza app. var utterance = "I want two large pepperoni pizzas on thin crust please"; ////////// MakeRequest(predictionKey, predictionEndpoint, appId, utterance); Console.WriteLine("Press ENTER to exit..."); Console.ReadLine(); } static async void MakeRequest(string predictionKey, string predictionEndpoint, string appId, string utterance) { var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // The request header contains your subscription key client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", predictionKey); // The "q" parameter contains the utterance to send to LUIS queryString["query"] = utterance; // These optional request parameters are set to their default values queryString["verbose"] = "true"; queryString["show-all-intents"] = "true"; queryString["staging"] = "false"; queryString["timezoneOffset"] = "0"; var predictionEndpointUri = String.Format("{0}luis/prediction/v3.0/apps/{1}/slots/production/predict?{2}", predictionEndpoint, appId, queryString); // Remove these before updating the article. Console.WriteLine("endpoint: " + predictionEndpoint); Console.WriteLine("appId: " + appId); Console.WriteLine("queryString: " + queryString); Console.WriteLine("endpointUri: " + predictionEndpointUri); var response = await client.GetAsync(predictionEndpointUri); var strResponseContent = await response.Content.ReadAsStringAsync(); // Display the JSON result from LUIS. Console.WriteLine(strResponseContent.ToString()); } } }
Replace the values starting with
YOUR-
with your own values.Information Purpose YOUR-APP-ID
Your app ID. Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
Your 32 character prediction key. Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
Your prediction URL endpoint. Located on the LUIS portal, Azure Resources page for your app.
For example,https://westus.api.cognitive.microsoft.com/
.Build the console application with this command:
dotnet build
Run the console application. The console output displays the same JSON that you saw earlier in the browser window.
dotnet run
Review the prediction response, which is returned as JSON:
{"query":"I want two large pepperoni pizzas on thin crust please","prediction":{"topIntent":"ModifyOrder","intents":{"ModifyOrder":{"score":1.0},"None":{"score":8.55E-09},"Greetings":{"score":1.82222226E-09},"CancelOrder":{"score":1.47272727E-09},"Confirmation":{"score":9.8125E-10}},"entities":{"Order":[{"FullPizzaWithModifiers":[{"PizzaType":["pepperoni pizzas"],"Size":[["Large"]],"Quantity":[2],"Crust":[["Thin"]],"$instance":{"PizzaType":[{"type":"PizzaType","text":"pepperoni pizzas","startIndex":17,"length":16,"score":0.9978157,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Size":[{"type":"SizeList","text":"large","startIndex":11,"length":5,"score":0.9984481,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Quantity":[{"type":"builtin.number","text":"two","startIndex":7,"length":3,"score":0.999770939,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Crust":[{"type":"CrustList","text":"thin crust","startIndex":37,"length":10,"score":0.933985531,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}]}}],"$instance":{"FullPizzaWithModifiers":[{"type":"FullPizzaWithModifiers","text":"two large pepperoni pizzas on thin crust","startIndex":7,"length":40,"score":0.90681237,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}]}}],"ToppingList":[["Pepperoni"]],"$instance":{"Order":[{"type":"Order","text":"two large pepperoni pizzas on thin crust","startIndex":7,"length":40,"score":0.9047088,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"ToppingList":[{"type":"ToppingList","text":"pepperoni","startIndex":17,"length":9,"modelTypeId":5,"modelType":"List Entity Extractor","recognitionSources":["model"]}]}}}}
The JSON response formatted for readability:
{ "query": "I want two large pepperoni pizzas on thin crust please", "prediction": { "topIntent": "ModifyOrder", "intents": { "ModifyOrder": { "score": 1 }, "None": { "score": 8.55e-9 }, "Greetings": { "score": 1.82222226e-9 }, "CancelOrder": { "score": 1.47272727e-9 }, "Confirmation": { "score": 9.8125e-10 } }, "entities": { "Order": [ { "FullPizzaWithModifiers": [ { "PizzaType": [ "pepperoni pizzas" ], "Size": [ [ "Large" ] ], "Quantity": [ 2 ], "Crust": [ [ "Thin" ] ], "$instance": { "PizzaType": [ { "type": "PizzaType", "text": "pepperoni pizzas", "startIndex": 17, "length": 16, "score": 0.9978157, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Size": [ { "type": "SizeList", "text": "large", "startIndex": 11, "length": 5, "score": 0.9984481, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Quantity": [ { "type": "builtin.number", "text": "two", "startIndex": 7, "length": 3, "score": 0.999770939, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Crust": [ { "type": "CrustList", "text": "thin crust", "startIndex": 37, "length": 10, "score": 0.933985531, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ] } } ], "$instance": { "FullPizzaWithModifiers": [ { "type": "FullPizzaWithModifiers", "text": "two large pepperoni pizzas on thin crust", "startIndex": 7, "length": 40, "score": 0.90681237, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ] } } ], "ToppingList": [ [ "Pepperoni" ] ], "$instance": { "Order": [ { "type": "Order", "text": "two large pepperoni pizzas on thin crust", "startIndex": 7, "length": 40, "score": 0.9047088, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "ToppingList": [ { "type": "ToppingList", "text": "pepperoni", "startIndex": 17, "length": 9, "modelTypeId": 5, "modelType": "List Entity Extractor", "recognitionSources": [ "model" ] } ] } } } }
Clean up resources
When you are finished with this quickstart, delete the project folder from the file system.
Next steps
Reference documentation | Sample
Prerequisites
- Go programming language
- Visual Studio Code
Create Pizza app
- Select pizza-app-for-luis-v6.json to bring up the GitHub page for the
pizza-app-for-luis.json
file. - Right-click or long tap the Raw button and select Save link as to save the
pizza-app-for-luis.json
to your computer. - Sign into the LUIS portal.
- Select My Apps.
- On the My Apps page, select + New app for conversation.
- Select Import as JSON.
- In the Import new app dialog, select the Choose File button.
- Select the
pizza-app-for-luis.json
file you downloaded, then select Open. - In the Import new app dialog Name field, enter a name for your Pizza app, then select the Done button.
The app will be imported.
If you see the dialog How to create an effective LUIS app, close the dialog.
Train and publish the Pizza app
You should see the Intents page with a list of the intents in the Pizza app.
In the top-right side of the LUIS website, select the Train button.
Training is complete when status indicator on the Train button is green.
In order to receive a LUIS prediction in a chat bot or other client application, you need to publish the app to the prediction endpoint.
Select Publish in the top-right navigation.
Select the Production slot, then select Done.
Select Access your endpoint URLs in the notification to go to the Azure Resources page. You will only be able to see the URLs if you have a prediction resource associated with the app. You can also find the Azure Resources page by clicking Manage.
Your Pizza app is now ready to use.
Record the app ID, prediction key, and prediction endpoint of your Pizza app
To use your new Pizza app, you will need the app ID, prediction key, and prediction endpoint of your Pizza app.
To find these values:
- From the Intents page, select MANAGE.
- From the Application Settings page, record the App ID.
- Select Azure Resources.
- From the Azure Resources page, record the Primary Key. This value is your prediction key.
- Record the Endpoint URL. This value is your prediction endpoint.
Get intent programmatically
Use Go to query the prediction endpoint and get a prediction result.
Create a new file named
predict.go
. Add the following code:// // This quickstart shows how to predict the intent of an utterance by using the LUIS REST APIs. // package main // Import dependencies. import ( "fmt" "net/http" "net/url" "io/ioutil" "log" ) func main() { ////////// // Values to modify. // YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page. var appID = "YOUR-APP-ID" // YOUR-PREDICTION-KEY: Your LUIS authoring key, 32 character value. var predictionKey = "YOUR-PREDICTION-KEY" // YOUR-PREDICTION-ENDPOINT: Replace with your authoring key endpoint. // For example, "https://westus.api.cognitive.microsoft.com/" var predictionEndpoint = "https://YOUR-PREDICTION-ENDPOINT/" // utterance for public app var utterance = "I want two large pepperoni pizzas on thin crust please" ////////// // Call the prediction endpoint. endpointPrediction(appID, predictionKey, predictionEndpoint, utterance) } // Calls the prediction endpoint and displays the prediction results on the console. func endpointPrediction(appID string, predictionKey string, predictionEndpoint string, utterance string) { var endpointUrl = fmt.Sprintf("%sluis/prediction/v3.0/apps/%s/slots/production/predict?subscription-key=%s&verbose=true&show-all-intents=true&query=%s", predictionEndpoint, appID, predictionKey, url.QueryEscape(utterance)) response, err := http.Get(endpointUrl) if err != nil { // handle error fmt.Println("error from Get") log.Fatal(err) } response2, err2 := ioutil.ReadAll(response.Body) if err2 != nil { // handle error fmt.Println("error from ReadAll") log.Fatal(err2) } fmt.Println("response") fmt.Println(string(response2)) }
Replace the values starting with
YOUR-
with your own values.Information Purpose YOUR-APP-ID
Your app ID. Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
Your 32 character prediction key. Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
Your prediction URL endpoint. Located on the LUIS portal, Azure Resources page for your app.
For example,https://westus.api.cognitive.microsoft.com/
.With a command prompt in the same directory as where you created the file, enter the following command to compile the Go file:
go build predict.go
Run the Go application from the command line by entering the following text in the command prompt:
go run predict.go
Review the prediction response, which is returned as JSON:
response {"query":"I want two large pepperoni pizzas on thin crust please","prediction":{"topIntent":"ModifyOrder","intents":{"ModifyOrder":{"score":1.0},"None":{"score":8.55E-09},"Greetings":{"score":1.82222226E-09},"CancelOrder":{"score":1.47272727E-09},"Confirmation":{"score":9.8125E-10}},"entities":{"Order":[{"FullPizzaWithModifiers":[{"PizzaType":["pepperoni pizzas"],"Size":[["Large"]],"Quantity":[2],"Crust":[["Thin"]],"$instance":{"PizzaType":[{"type":"PizzaType","text":"pepperoni pizzas","startIndex":17,"length":16,"score":0.9978157,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Size":[{"type":"SizeList","text":"large","startIndex":11,"length":5,"score":0.9984481,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Quantity":[{"type":"builtin.number","text":"two","startIndex":7,"length":3,"score":0.999770939,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Crust":[{"type":"CrustList","text":"thin crust","startIndex":37,"length":10,"score":0.933985531,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}]}}],"$instance":{"FullPizzaWithModifiers":[{"type":"FullPizzaWithModifiers","text":"two large pepperoni pizzas on thin crust","startIndex":7,"length":40,"score":0.90681237,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}]}}],"ToppingList":[["Pepperoni"]],"$instance":{"Order":[{"type":"Order","text":"two large pepperoni pizzas on thin crust","startIndex":7,"length":40,"score":0.9047088,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"ToppingList":[{"type":"ToppingList","text":"pepperoni","startIndex":17,"length":9,"modelTypeId":5,"modelType":"List Entity Extractor","recognitionSources":["model"]}]}}}}
JSON response formatted for readability:
response { "query": "I want two large pepperoni pizzas on thin crust please", "prediction": { "topIntent": "ModifyOrder", "intents": { "ModifyOrder": { "score": 1 }, "None": { "score": 8.55e-9 }, "Greetings": { "score": 1.82222226e-9 }, "CancelOrder": { "score": 1.47272727e-9 }, "Confirmation": { "score": 9.8125e-10 } }, "entities": { "Order": [ { "FullPizzaWithModifiers": [ { "PizzaType": [ "pepperoni pizzas" ], "Size": [ [ "Large" ] ], "Quantity": [ 2 ], "Crust": [ [ "Thin" ] ], "$instance": { "PizzaType": [ { "type": "PizzaType", "text": "pepperoni pizzas", "startIndex": 17, "length": 16, "score": 0.9978157, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Size": [ { "type": "SizeList", "text": "large", "startIndex": 11, "length": 5, "score": 0.9984481, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Quantity": [ { "type": "builtin.number", "text": "two", "startIndex": 7, "length": 3, "score": 0.999770939, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Crust": [ { "type": "CrustList", "text": "thin crust", "startIndex": 37, "length": 10, "score": 0.933985531, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ] } } ], "$instance": { "FullPizzaWithModifiers": [ { "type": "FullPizzaWithModifiers", "text": "two large pepperoni pizzas on thin crust", "startIndex": 7, "length": 40, "score": 0.90681237, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ] } } ], "ToppingList": [ [ "Pepperoni" ] ], "$instance": { "Order": [ { "type": "Order", "text": "two large pepperoni pizzas on thin crust", "startIndex": 7, "length": 40, "score": 0.9047088, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "ToppingList": [ { "type": "ToppingList", "text": "pepperoni", "startIndex": 17, "length": 9, "modelTypeId": 5, "modelType": "List Entity Extractor", "recognitionSources": [ "model" ] } ] } } } }
Clean up resources
When you are finished with this quickstart, delete the file from the file system.
Next steps
Reference documentation | Sample
Prerequisites
- JDK SE (Java Development Kit, Standard Edition)
- Visual Studio Code or your favorite IDE
Create Pizza app
- Select pizza-app-for-luis-v6.json to bring up the GitHub page for the
pizza-app-for-luis.json
file. - Right-click or long tap the Raw button and select Save link as to save the
pizza-app-for-luis.json
to your computer. - Sign into the LUIS portal.
- Select My Apps.
- On the My Apps page, select + New app for conversation.
- Select Import as JSON.
- In the Import new app dialog, select the Choose File button.
- Select the
pizza-app-for-luis.json
file you downloaded, then select Open. - In the Import new app dialog Name field, enter a name for your Pizza app, then select the Done button.
The app will be imported.
If you see the dialog How to create an effective LUIS app, close the dialog.
Train and publish the Pizza app
You should see the Intents page with a list of the intents in the Pizza app.
In the top-right side of the LUIS website, select the Train button.
Training is complete when status indicator on the Train button is green.
In order to receive a LUIS prediction in a chat bot or other client application, you need to publish the app to the prediction endpoint.
Select Publish in the top-right navigation.
Select the Production slot, then select Done.
Select Access your endpoint URLs in the notification to go to the Azure Resources page. You will only be able to see the URLs if you have a prediction resource associated with the app. You can also find the Azure Resources page by clicking Manage.
Your Pizza app is now ready to use.
Record the app ID, prediction key, and prediction endpoint of your Pizza app
To use your new Pizza app, you will need the app ID, prediction key, and prediction endpoint of your Pizza app.
To find these values:
- From the Intents page, select MANAGE.
- From the Application Settings page, record the App ID.
- Select Azure Resources.
- From the Azure Resources page, record the Primary Key. This value is your prediction key.
- Record the Endpoint URL. This value is your prediction endpoint.
Get intent programmatically
Use Java to query the prediction endpoint and get a prediction result.
Create a new folder to hold your Java project, such as
java-predict-with-rest
.Make a subdirectory named
lib
and copy in the following java libs into thelib
subdirectory:Copy the following code to create a class in a file named
Predict.java
:// // This quickstart shows how to predict the intent of an utterance by using the LUIS REST APIs. // import java.io.*; import java.net.URI; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; // To compile, execute this command at the console: // Windows: javac -cp ";lib/*" Predict.java // macOs: javac -cp ":lib/*" Predict.java // Linux: javac -cp ":lib/*" Predict.java // To run, execute this command at the console: // Windows: java -cp ";lib/*" Predict // macOs: java -cp ":lib/*" Predict // Linux: java -cp ":lib/*" Predict public class Predict { public static void main(String[] args) { HttpClient httpclient = HttpClients.createDefault(); try { ////////// // Values to modify. // YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page. String AppId = "YOUR-APP-ID"; // YOUR-PREDICTION-KEY: Your LUIS authoring key, 32 character value. String Key = "YOUR-PREDICTION-KEY"; // YOUR-PREDICTION-ENDPOINT: Replace this with your authoring key endpoint. // For example, "https://westus.api.cognitive.microsoft.com/" String Endpoint = "https://YOUR-PREDICTION-ENDPOINT/"; // The utterance you want to use. String Utterance = "I want two large pepperoni pizzas on thin crust please"; ////////// // Begin building the endpoint URL. URIBuilder endpointURLbuilder = new URIBuilder(Endpoint + "luis/prediction/v3.0/apps/" + AppId + "/slots/production/predict?"); // Create the query string params. endpointURLbuilder.setParameter("query", Utterance); endpointURLbuilder.setParameter("subscription-key", Key); endpointURLbuilder.setParameter("show-all-intents", "true"); endpointURLbuilder.setParameter("verbose", "true"); // Create the prediction endpoint URL. URI endpointURL = endpointURLbuilder.build(); // Create the HTTP object from the URL. HttpGet request = new HttpGet(endpointURL); // Access the LUIS endpoint to analyze the text utterance. HttpResponse response = httpclient.execute(request); // Get the response. HttpEntity entity = response.getEntity(); // Print the response on the console. if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } // Display errors if they occur. catch (Exception e) { System.out.println(e.getMessage()); } } }
Replace the values starting with
YOUR-
with your own values.Information Purpose YOUR-APP-ID
Your app ID. Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
Your 32 character prediction key. Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
Your prediction URL endpoint. Located on the LUIS portal, Azure Resources page for your app.
For example,https://westus.api.cognitive.microsoft.com/
.Compile the java program from the command line.
- If you are using Windows, use this command:
javac -cp ";lib/*" Predict.java
- If you are using macOS or Linux, use this command:
javac -cp ":lib/*" Predict.java
- If you are using Windows, use this command:
Run the java program from the command line:
- If you are using Windows, use this command:
java -cp ";lib/*" Predict
- If you are using macOS or Linux, use this command:
java -cp ":lib/*" Predict
- If you are using Windows, use this command:
Review the prediction response, which is returned as JSON:
{"query":"I want two large pepperoni pizzas on thin crust please","prediction":{"topIntent":"ModifyOrder","intents":{"ModifyOrder":{"score":1.0},"None":{"score":8.55E-09},"Greetings":{"score":1.82222226E-09},"CancelOrder":{"score":1.47272727E-09},"Confirmation":{"score":9.8125E-10}},"entities":{"Order":[{"FullPizzaWithModifiers":[{"PizzaType":["pepperoni pizzas"],"Size":[["Large"]],"Quantity":[2],"Crust":[["Thin"]],"$instance":{"PizzaType":[{"type":"PizzaType","text":"pepperoni pizzas","startIndex":17,"length":16,"score":0.9978157,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Size":[{"type":"SizeList","text":"large","startIndex":11,"length":5,"score":0.9984481,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Quantity":[{"type":"builtin.number","text":"two","startIndex":7,"length":3,"score":0.999770939,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Crust":[{"type":"CrustList","text":"thin crust","startIndex":37,"length":10,"score":0.933985531,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}]}}],"$instance":{"FullPizzaWithModifiers":[{"type":"FullPizzaWithModifiers","text":"two large pepperoni pizzas on thin crust","startIndex":7,"length":40,"score":0.90681237,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}]}}],"ToppingList":[["Pepperoni"]],"$instance":{"Order":[{"type":"Order","text":"two large pepperoni pizzas on thin crust","startIndex":7,"length":40,"score":0.9047088,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"ToppingList":[{"type":"ToppingList","text":"pepperoni","startIndex":17,"length":9,"modelTypeId":5,"modelType":"List Entity Extractor","recognitionSources":["model"]}]}}}}
The JSON response formatted for readability:
{ "query": "I want two large pepperoni pizzas on thin crust please", "prediction": { "topIntent": "ModifyOrder", "intents": { "ModifyOrder": { "score": 1 }, "None": { "score": 8.55e-9 }, "Greetings": { "score": 1.82222226e-9 }, "CancelOrder": { "score": 1.47272727e-9 }, "Confirmation": { "score": 9.8125e-10 } }, "entities": { "Order": [ { "FullPizzaWithModifiers": [ { "PizzaType": [ "pepperoni pizzas" ], "Size": [ [ "Large" ] ], "Quantity": [ 2 ], "Crust": [ [ "Thin" ] ], "$instance": { "PizzaType": [ { "type": "PizzaType", "text": "pepperoni pizzas", "startIndex": 17, "length": 16, "score": 0.9978157, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Size": [ { "type": "SizeList", "text": "large", "startIndex": 11, "length": 5, "score": 0.9984481, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Quantity": [ { "type": "builtin.number", "text": "two", "startIndex": 7, "length": 3, "score": 0.999770939, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Crust": [ { "type": "CrustList", "text": "thin crust", "startIndex": 37, "length": 10, "score": 0.933985531, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ] } } ], "$instance": { "FullPizzaWithModifiers": [ { "type": "FullPizzaWithModifiers", "text": "two large pepperoni pizzas on thin crust", "startIndex": 7, "length": 40, "score": 0.90681237, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ] } } ], "ToppingList": [ [ "Pepperoni" ] ], "$instance": { "Order": [ { "type": "Order", "text": "two large pepperoni pizzas on thin crust", "startIndex": 7, "length": 40, "score": 0.9047088, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "ToppingList": [ { "type": "ToppingList", "text": "pepperoni", "startIndex": 17, "length": 9, "modelTypeId": 5, "modelType": "List Entity Extractor", "recognitionSources": [ "model" ] } ] } } } }
Clean up resources
When you are finished with this quickstart, delete the project folder from the file system.
Next steps
Reference documentation | Sample
Prerequisites
- Node.js programming language
- Visual Studio Code
Create Pizza app
- Select pizza-app-for-luis-v6.json to bring up the GitHub page for the
pizza-app-for-luis.json
file. - Right-click or long tap the Raw button and select Save link as to save the
pizza-app-for-luis.json
to your computer. - Sign into the LUIS portal.
- Select My Apps.
- On the My Apps page, select + New app for conversation.
- Select Import as JSON.
- In the Import new app dialog, select the Choose File button.
- Select the
pizza-app-for-luis.json
file you downloaded, then select Open. - In the Import new app dialog Name field, enter a name for your Pizza app, then select the Done button.
The app will be imported.
If you see the dialog How to create an effective LUIS app, close the dialog.
Train and publish the Pizza app
You should see the Intents page with a list of the intents in the Pizza app.
In the top-right side of the LUIS website, select the Train button.
Training is complete when status indicator on the Train button is green.
In order to receive a LUIS prediction in a chat bot or other client application, you need to publish the app to the prediction endpoint.
Select Publish in the top-right navigation.
Select the Production slot, then select Done.
Select Access your endpoint URLs in the notification to go to the Azure Resources page. You will only be able to see the URLs if you have a prediction resource associated with the app. You can also find the Azure Resources page by clicking Manage.
Your Pizza app is now ready to use.
Record the app ID, prediction key, and prediction endpoint of your Pizza app
To use your new Pizza app, you will need the app ID, prediction key, and prediction endpoint of your Pizza app.
To find these values:
- From the Intents page, select MANAGE.
- From the Application Settings page, record the App ID.
- Select Azure Resources.
- From the Azure Resources page, record the Primary Key. This value is your prediction key.
- Record the Endpoint URL. This value is your prediction endpoint.
Create the Node.js project
Create a new folder to hold your Node.js project, such as
node-predict-with-rest
.Open a new Command Prompt, navigate to the folder you created and execute the following command:
npm init
Press Enter at each prompt to accept the default settings.
Install the dependencies by entering the following commands:
npm install --save request npm install --save request-promise npm install --save querystring
Get intent programmatically
Use Node.js to query the prediction endpoint and get a prediction result.
Copy the following code snippet to a file named
predict.js
:// // This quickstart shows how to predict the intent of an utterance by using the LUIS REST APIs. // var requestPromise = require('request-promise'); var queryString = require('querystring'); // Analyze a string utterance. getPrediction = async () => { ////////// // Values to modify. // YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page. const LUIS_appId = "YOUR-APP-ID"; // YOUR-PREDICTION-KEY: Your LUIS authoring key, 32 character value. const LUIS_predictionKey = "YOUR-PREDICTION-KEY"; // YOUR-PREDICTION-ENDPOINT: Replace this with your authoring key endpoint. // For example, "https://westus.api.cognitive.microsoft.com/" const LUIS_endpoint = "https://YOUR-PREDICTION-ENDPOINT/"; // The utterance you want to use. const utterance = "I want two large pepperoni pizzas on thin crust please"; ////////// // Create query string const queryParams = { "show-all-intents": true, "verbose": true, "query": utterance, "subscription-key": LUIS_predictionKey } // Create the URI for the REST call. const URI = `${LUIS_endpoint}luis/prediction/v3.0/apps/${LUIS_appId}/slots/production/predict?${queryString.stringify(queryParams)}` // Send the REST call. const response = await requestPromise(URI); // Display the response from the REST call. console.log(response); } // Pass an utterance to the sample LUIS app getPrediction().then(()=>console.log("done")).catch((err)=>console.log(err));
Replace the values starting with
YOUR-
with your own values.Information Purpose YOUR-APP-ID
Your app ID. Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
Your 32 character prediction key. Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
Your prediction URL endpoint. Located on the LUIS portal, Azure Resources page for your app.
For example,https://westus.api.cognitive.microsoft.com/
.Review the prediction response, which is returned as JSON:
{"query":"I want two large pepperoni pizzas on thin crust please","prediction":{"topIntent":"ModifyOrder","intents":{"ModifyOrder":{"score":1.0},"None":{"score":8.55E-09},"Greetings":{"score":1.82222226E-09},"CancelOrder":{"score":1.47272727E-09},"Confirmation":{"score":9.8125E-10}},"entities":{"Order":[{"FullPizzaWithModifiers":[{"PizzaType":["pepperoni pizzas"],"Size":[["Large"]],"Quantity":[2],"Crust":[["Thin"]],"$instance":{"PizzaType":[{"type":"PizzaType","text":"pepperoni pizzas","startIndex":17,"length":16,"score":0.9978157,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Size":[{"type":"SizeList","text":"large","startIndex":11,"length":5,"score":0.9984481,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Quantity":[{"type":"builtin.number","text":"two","startIndex":7,"length":3,"score":0.999770939,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"Crust":[{"type":"CrustList","text":"thin crust","startIndex":37,"length":10,"score":0.933985531,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}]}}],"$instance":{"FullPizzaWithModifiers":[{"type":"FullPizzaWithModifiers","text":"two large pepperoni pizzas on thin crust","startIndex":7,"length":40,"score":0.90681237,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}]}}],"ToppingList":[["Pepperoni"]],"$instance":{"Order":[{"type":"Order","text":"two large pepperoni pizzas on thin crust","startIndex":7,"length":40,"score":0.9047088,"modelTypeId":1,"modelType":"Entity Extractor","recognitionSources":["model"]}],"ToppingList":[{"type":"ToppingList","text":"pepperoni","startIndex":17,"length":9,"modelTypeId":5,"modelType":"List Entity Extractor","recognitionSources":["model"]}]}}}} ``` The JSON response formatted for readability: ```JSON { "query": "I want two large pepperoni pizzas on thin crust please", "prediction": { "topIntent": "ModifyOrder", "intents": { "ModifyOrder": { "score": 1 }, "None": { "score": 8.55e-9 }, "Greetings": { "score": 1.82222226e-9 }, "CancelOrder": { "score": 1.47272727e-9 }, "Confirmation": { "score": 9.8125e-10 } }, "entities": { "Order": [ { "FullPizzaWithModifiers": [ { "PizzaType": [ "pepperoni pizzas" ], "Size": [ [ "Large" ] ], "Quantity": [ 2 ], "Crust": [ [ "Thin" ] ], "$instance": { "PizzaType": [ { "type": "PizzaType", "text": "pepperoni pizzas", "startIndex": 17, "length": 16, "score": 0.9978157, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Size": [ { "type": "SizeList", "text": "large", "startIndex": 11, "length": 5, "score": 0.9984481, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Quantity": [ { "type": "builtin.number", "text": "two", "startIndex": 7, "length": 3, "score": 0.999770939, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "Crust": [ { "type": "CrustList", "text": "thin crust", "startIndex": 37, "length": 10, "score": 0.933985531, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ] } } ], "$instance": { "FullPizzaWithModifiers": [ { "type": "FullPizzaWithModifiers", "text": "two large pepperoni pizzas on thin crust", "startIndex": 7, "length": 40, "score": 0.90681237, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ] } } ], "ToppingList": [ [ "Pepperoni" ] ], "$instance": { "Order": [ { "type": "Order", "text": "two large pepperoni pizzas on thin crust", "startIndex": 7, "length": 40, "score": 0.9047088, "modelTypeId": 1, "modelType": "Entity Extractor", "recognitionSources": [ "model" ] } ], "ToppingList": [ { "type": "ToppingList", "text": "pepperoni", "startIndex": 17, "length": 9, "modelTypeId": 5, "modelType": "List Entity Extractor", "recognitionSources": [ "model" ] } ] } } } }
Clean up resources
When you are finished with this quickstart, delete the project folder from the file system.
Next steps
Reference documentation | Sample
Prerequisites
- Python 3.6 or later.
- Visual Studio Code
Create Pizza app
- Select pizza-app-for-luis-v6.json to bring up the GitHub page for the
pizza-app-for-luis.json
file. - Right-click or long tap the Raw button and select Save link as to save the
pizza-app-for-luis.json
to your computer. - Sign into the LUIS portal.
- Select My Apps.
- On the My Apps page, select + New app for conversation.
- Select Import as JSON.
- In the Import new app dialog, select the Choose File button.
- Select the
pizza-app-for-luis.json
file you downloaded, then select Open. - In the Import new app dialog Name field, enter a name for your Pizza app, then select the Done button.
The app will be imported.
If you see the dialog How to create an effective LUIS app, close the dialog.
Train and publish the Pizza app
You should see the Intents page with a list of the intents in the Pizza app.
In the top-right side of the LUIS website, select the Train button.
Training is complete when status indicator on the Train button is green.
In order to receive a LUIS prediction in a chat bot or other client application, you need to publish the app to the prediction endpoint.
Select Publish in the top-right navigation.
Select the Production slot, then select Done.
Select Access your endpoint URLs in the notification to go to the Azure Resources page. You will only be able to see the URLs if you have a prediction resource associated with the app. You can also find the Azure Resources page by clicking Manage.
Your Pizza app is now ready to use.
Record the app ID, prediction key, and prediction endpoint of your Pizza app
To use your new Pizza app, you will need the app ID, prediction key, and prediction endpoint of your Pizza app.
To find these values:
- From the Intents page, select MANAGE.
- From the Application Settings page, record the App ID.
- Select Azure Resources.
- From the Azure Resources page, record the Primary Key. This value is your prediction key.
- Record the Endpoint URL. This value is your prediction endpoint.
Get intent from the prediction endpoint
Use Python to query the prediction endpoint and get a prediction result.
Copy this code snippet into a file called
predict.py
:########### Python 3.6 ############# # # This quickstart shows how to predict the intent of an utterance by using the LUIS REST APIs. # import requests try: ########## # Values to modify. # YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page. appId = 'YOUR-APP-ID' # YOUR-PREDICTION-KEY: Your LUIS authoring key, 32 character value. prediction_key = 'YOUR-PREDICTION-KEY' # YOUR-PREDICTION-ENDPOINT: Replace with your authoring key endpoint. # For example, "https://westus.api.cognitive.microsoft.com/" prediction_endpoint = 'https://YOUR-PREDICTION-ENDPOINT/' # The utterance you want to use. utterance = 'I want two large pepperoni pizzas on thin crust please' ########## # The headers to use in this REST call. headers = { } # The URL parameters to use in this REST call. params ={ 'query': utterance, 'timezoneOffset': '0', 'verbose': 'true', 'show-all-intents': 'true', 'spellCheck': 'false', 'staging': 'false', 'subscription-key': prediction_key } # Make the REST call. response = requests.get(f'{prediction_endpoint}luis/prediction/v3.0/apps/{appId}/slots/production/predict', headers=headers, params=params) # Display the results on the console. print(response.json()) except Exception as e: # Display the error string. print(f'{e}')
Replace the values starting with
YOUR-
with your own values.Information Purpose YOUR-APP-ID
Your app ID. Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
Your 32 character prediction key. Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
Your prediction URL endpoint. Located on the LUIS portal, Azure Resources page for your app.
For example,https://westus.api.cognitive.microsoft.com/
.Install the
requests
dependency. Therequests
library is used to make HTTP requests:pip install requests
Run your script with this console command:
python predict.py
Review the prediction response, which is returned as JSON:
{'query': 'I want two large pepperoni pizzas on thin crust please', 'prediction': {'topIntent': 'ModifyOrder', 'intents': {'ModifyOrder': {'score': 1.0}, 'None': {'score': 8.55e-09}, 'Greetings': {'score': 1.82222226e-09}, 'CancelOrder': {'score': 1.47272727e-09}, 'Confirmation': {'score': 9.8125e-10}}, 'entities': {'Order': [{'FullPizzaWithModifiers': [{'PizzaType': ['pepperoni pizzas'], 'Size': [['Large']], 'Quantity': [2], 'Crust': [['Thin']], '$instance': {'PizzaType': [{'type': 'PizzaType', 'text': 'pepperoni pizzas', 'startIndex': 17, 'length': 16, 'score': 0.9978157, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': ['model']}], 'Size': [{'type': 'SizeList', 'text': 'large', 'startIndex': 11, 'length': 5, 'score': 0.9984481, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': ['model']}], 'Quantity': [{'type': 'builtin.number', 'text': 'two', 'startIndex': 7, 'length': 3, 'score': 0.999770939, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': ['model']}], 'Crust': [{'type': 'CrustList', 'text': 'thin crust', 'startIndex': 37, 'length': 10, 'score': 0.933985531, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': ['model']}]}}], '$instance': {'FullPizzaWithModifiers': [{'type': 'FullPizzaWithModifiers', 'text': 'two large pepperoni pizzas on thin crust', 'startIndex': 7, 'length': 40, 'score': 0.90681237, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': ['model']}]}}], 'ToppingList': [['Pepperoni']], '$instance': {'Order': [{'type': 'Order', 'text': 'two large pepperoni pizzas on thin crust', 'startIndex': 7, 'length': 40, 'score': 0.9047088, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': ['model']}], 'ToppingList': [{'type': 'ToppingList', 'text': 'pepperoni', 'startIndex': 17, 'length': 9, 'modelTypeId': 5, 'modelType': 'List Entity Extractor', 'recognitionSources': ['model']}]}}}}
JSON response formatted for readability:
{ 'query': 'I want two large pepperoni pizzas on thin crust please', 'prediction': { 'topIntent': 'ModifyOrder', 'intents': { 'ModifyOrder': { 'score': 1.0 }, 'None': { 'score': 8.55e-9 }, 'Greetings': { 'score': 1.82222226e-9 }, 'CancelOrder': { 'score': 1.47272727e-9 }, 'Confirmation': { 'score': 9.8125e-10 } }, 'entities': { 'Order': [ { 'FullPizzaWithModifiers': [ { 'PizzaType': [ 'pepperoni pizzas' ], 'Size': [ [ 'Large' ] ], 'Quantity': [ 2 ], 'Crust': [ [ 'Thin' ] ], '$instance': { 'PizzaType': [ { 'type': 'PizzaType', 'text': 'pepperoni pizzas', 'startIndex': 17, 'length': 16, 'score': 0.9978157, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': [ 'model' ] } ], 'Size': [ { 'type': 'SizeList', 'text': 'large', 'startIndex': 11, 'length': 5, 'score': 0.9984481, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': [ 'model' ] } ], 'Quantity': [ { 'type': 'builtin.number', 'text': 'two', 'startIndex': 7, 'length': 3, 'score': 0.999770939, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': [ 'model' ] } ], 'Crust': [ { 'type': 'CrustList', 'text': 'thin crust', 'startIndex': 37, 'length': 10, 'score': 0.933985531, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': [ 'model' ] } ] } } ], '$instance': { 'FullPizzaWithModifiers': [ { 'type': 'FullPizzaWithModifiers', 'text': 'two large pepperoni pizzas on thin crust', 'startIndex': 7, 'length': 40, 'score': 0.90681237, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': [ 'model' ] } ] } } ], 'ToppingList': [ [ 'Pepperoni' ] ], '$instance': { 'Order': [ { 'type': 'Order', 'text': 'two large pepperoni pizzas on thin crust', 'startIndex': 7, 'length': 40, 'score': 0.9047088, 'modelTypeId': 1, 'modelType': 'Entity Extractor', 'recognitionSources': [ 'model' ] } ], 'ToppingList': [ { 'type': 'ToppingList', 'text': 'pepperoni', 'startIndex': 17, 'length': 9, 'modelTypeId': 5, 'modelType': 'List Entity Extractor', 'recognitionSources': [ 'model' ] } ] } } } }
Clean up resources
When you are finished with this quickstart, delete the file from the file system.