如何使用 REST API 获取意图How to get an intent using the REST APIs
在本文中,你将使用 LUIS 应用从会话文本中确定用户的意向。In this article, you will use a LUIS app to determine a user's intention from conversational text. 将用户的意向作为文本发送到 Pizza 应用的 HTTP 预测终结点。Send the user's intention as text to the Pizza app's HTTP prediction endpoint. 在终结点处,LUIS 应用 Pizza 应用的模型来分析自然语言文本的含义,确定总体意向并提取与应用的主题域相关的数据。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.
对于本文,需要一个免费的 LUIS 帐户。For this article, you need a free LUIS account.
参考文档 | 示例Reference documentation | Sample
先决条件Prerequisites
创建 Pizza 应用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.
在 LUIS 网站的右上方,选择“训练”按钮。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.
若要在聊天机器人或其他客户端应用程序中接收 LUIS 预测,需要将应用发布到预测终结点。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.
在通知中选择“访问终结点 URL”,以转到“Azure 资源”页 。Select Access your endpoint URLs in the notification to go to the Azure Resources page. 只有你拥有与应用关联的预测资源时,才能看到 URL。You will only be able to see the URLs if you have a prediction resource associated with the app. 还可以单击“管理”来找到“Azure 资源”页 。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
使用 C# (.NET Core) 查询预测终结点并获取预测结果。Use C# (.NET Core) to query the prediction endpoint and get a prediction result.
使用项目和名为
csharp-predict-with-rest
的文件夹创建一个面向 C# 语言的新控制台应用程序。Create a new console application targeting the C# language, with a project and folder name ofcsharp-predict-with-rest
.dotnet new console -lang C# -n csharp-predict-with-rest
更改为创建的
csharp-predict-with-rest
目录,并使用以下命令安装所需的依赖项:Change to thecsharp-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
在喜好的 IDE 或编辑器中打开
Program.cs
。OpenProgram.cs
in your favorite IDE or editor. 然后使用以下代码覆盖Program.cs
: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 luis.azure.cn 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://api.cognitive.azure.cn/"
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());
}
}
}
将以
YOUR-
开头的值替换为你自己的值。Replace the values starting withYOUR-
with your own values.信息Information 目的Purpose YOUR-APP-ID
你的应用程序 ID。Your app ID. 位于 LUIS 门户中,你的应用的“应用程序设置”页。Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
32 字符预测密钥。Your 32 character prediction key. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
预测 URL 终结点。Your prediction URL endpoint. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app.
例如,https://api.cognitive.azure.cn/
。For example,https://api.cognitive.azure.cn/
.使用以下命令生成控制台应用程序:Build the console application with this command:
dotnet build
运行控制台应用程序。Run the console application. 控制台输出会显示前面在浏览器窗口中显示的相同 JSON。The console output displays the same JSON that you saw earlier in the browser window.
dotnet run
查看以 JSON 形式返回的预测响应: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 响应: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 编程语言Go programming language
- Visual Studio CodeVisual Studio Code
创建 Pizza 应用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.
在 LUIS 网站的右上方,选择“训练”按钮。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.
若要在聊天机器人或其他客户端应用程序中接收 LUIS 预测,需要将应用发布到预测终结点。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.
在通知中选择“访问终结点 URL”,以转到“Azure 资源”页 。Select Access your endpoint URLs in the notification to go to the Azure Resources page. 只有你拥有与应用关联的预测资源时,才能看到 URL。You will only be able to see the URLs if you have a prediction resource associated with the app. 还可以单击“管理”来找到“Azure 资源”页 。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
使用 Go 查询预测终结点并获取预测结果。Use Go to query the prediction endpoint and get a prediction result.
- 创建名为
predict.go
的新文件。Create a new file namedpredict.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 luis.azure.cn 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://api.cognitive.azure.cn/"
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))
}
将以
YOUR-
开头的值替换为你自己的值。Replace the values starting withYOUR-
with your own values.信息Information 目的Purpose YOUR-APP-ID
你的应用程序 ID。Your app ID. 位于 LUIS 门户中,你的应用的“应用程序设置”页。Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
32 字符预测密钥。Your 32 character prediction key. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
预测 URL 终结点。Your prediction URL endpoint. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app.
例如,https://api.cognitive.azure.cn/
。For example,https://api.cognitive.azure.cn/
.在创建该文件的同一目录中,在命令提示符下输入以下命令来编译 Go 文件: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
通过在命令提示符下输入以下文本从命令行运行 Go 应用程序:Run the Go application from the command line by entering the following text in the command prompt:
go run predict.go
查看以 JSON 形式返回的预测响应: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 响应: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 开发工具包,标准版)JDK SE (Java Development Kit, Standard Edition)
- Visual Studio Code 或你喜欢用的 IDEVisual Studio Code or your favorite IDE
创建 Pizza 应用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.
在 LUIS 网站的右上方,选择“训练”按钮。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.
若要在聊天机器人或其他客户端应用程序中接收 LUIS 预测,需要将应用发布到预测终结点。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.
在通知中选择“访问终结点 URL”,以转到“Azure 资源”页 。Select Access your endpoint URLs in the notification to go to the Azure Resources page. 只有你拥有与应用关联的预测资源时,才能看到 URL。You will only be able to see the URLs if you have a prediction resource associated with the app. 还可以单击“管理”来找到“Azure 资源”页 。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
使用 Java 查询预测终结点并获取预测结果。Use Java to query the prediction endpoint and get a prediction result.
创建一个新文件夹以保存 Java 项目,例如
java-predict-with-rest
。Create a new folder to hold your Java project, such asjava-predict-with-rest
.创建一个名为
lib
的子目录,并将以下 Java 库中的内容复制到lib
子目录:Make a subdirectory namedlib
and copy in the following java libs into thelib
subdirectory:复制以下代码以在名为
Predict.java
的文件中创建一个类:Copy the following code to create a class in a file namedPredict.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 luis.azure.cn 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://api.cognitive.azure.cn/"
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());
}
}
}
将以
YOUR-
开头的值替换为你自己的值。Replace the values starting withYOUR-
with your own values.信息Information 目的Purpose YOUR-APP-ID
你的应用程序 ID。Your app ID. 位于 LUIS 门户中,你的应用的“应用程序设置”页。Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
32 字符预测密钥。Your 32 character prediction key. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
预测 URL 终结点。Your prediction URL endpoint. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app.
例如,https://api.cognitive.azure.cn/
。For example,https://api.cognitive.azure.cn/
.通过命令行编译 Java 程序。Compile the java program from the command line.
- 如果使用的是 Windows,请使用此命令:
javac -cp ";lib/*" Predict.java
If you are using Windows, use this command:javac -cp ";lib/*" Predict.java
- 如果使用的是 macOS 或 Linux,请使用此命令:
javac -cp ":lib/*" Predict.java
If you are using macOS or Linux, use this command:javac -cp ":lib/*" Predict.java
- 如果使用的是 Windows,请使用此命令:
通过命令行运行 Java 程序:Run the java program from the command line:
- 如果使用的是 Windows,请使用此命令:
java -cp ";lib/*" Predict
If you are using Windows, use this command:java -cp ";lib/*" Predict
- 如果使用的是 macOS 或 Linux,请使用此命令:
java -cp ":lib/*" Predict
If you are using macOS or Linux, use this command:java -cp ":lib/*" Predict
- 如果使用的是 Windows,请使用此命令:
查看以 JSON 形式返回的预测响应: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 响应: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 编程语言Node.js programming language
- Visual Studio CodeVisual Studio Code
创建 Pizza 应用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.
在 LUIS 网站的右上方,选择“训练”按钮。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.
若要在聊天机器人或其他客户端应用程序中接收 LUIS 预测,需要将应用发布到预测终结点。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.
在通知中选择“访问终结点 URL”,以转到“Azure 资源”页 。Select Access your endpoint URLs in the notification to go to the Azure Resources page. 只有你拥有与应用关联的预测资源时,才能看到 URL。You will only be able to see the URLs if you have a prediction resource associated with the app. 还可以单击“管理”来找到“Azure 资源”页 。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.
创建 Node.js 项目Create the Node.js project
创建一个新文件夹以保存 Node.js 项目,例如
node-predict-with-rest
。Create a new folder to hold your Node.js project, such asnode-predict-with-rest
.打开新的命令提示符,导航到你创建的文件夹,并执行以下命令:Open a new Command Prompt, navigate to the folder you created and execute the following command:
npm init
在每个提示符下按 Enter 以接受默认设置。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
使用 Node.js 查询预测终结点并获取预测结果。Use Node.js to query the prediction endpoint and get a prediction result.
- 将以下代码片段复制到名为
predict.js
的文件中:Copy the following code snippet to a file namedpredict.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 luis.azure.cn 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://api.cognitive.azure.cn/"
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));
将以
YOUR-
开头的值替换为你自己的值。Replace the values starting withYOUR-
with your own values.信息Information 目的Purpose YOUR-APP-ID
你的应用程序 ID。Your app ID. 位于 LUIS 门户中,你的应用的“应用程序设置”页。Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
32 字符预测密钥。Your 32 character prediction key. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
预测 URL 终结点。Your prediction URL endpoint. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app.
例如,https://api.cognitive.azure.cn/
。For example,https://api.cognitive.azure.cn/
.查看以 JSON 形式返回的预测响应: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 或更高版本。Python 3.6 or later.
- Visual Studio CodeVisual Studio Code
创建 Pizza 应用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.
在 LUIS 网站的右上方,选择“训练”按钮。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.
若要在聊天机器人或其他客户端应用程序中接收 LUIS 预测,需要将应用发布到预测终结点。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.
在通知中选择“访问终结点 URL”,以转到“Azure 资源”页 。Select Access your endpoint URLs in the notification to go to the Azure Resources page. 只有你拥有与应用关联的预测资源时,才能看到 URL。You will only be able to see the URLs if you have a prediction resource associated with the app. 还可以单击“管理”来找到“Azure 资源”页 。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
使用 Python 查询预测终结点并获取预测结果。Use Python to query the prediction endpoint and get a prediction result.
- 将以下代码片段复制到名为
predict.py
的文件中:Copy this code snippet into a file calledpredict.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 luis.azure.cn 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://api.cognitive.azure.cn/"
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}')
将以
YOUR-
开头的值替换为你自己的值。Replace the values starting withYOUR-
with your own values.信息Information 目的Purpose YOUR-APP-ID
你的应用程序 ID。Your app ID. 位于 LUIS 门户中,你的应用的“应用程序设置”页。Located on the LUIS portal, Application Settings page for your app. YOUR-PREDICTION-KEY
32 字符预测密钥。Your 32 character prediction key. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app. YOUR-PREDICTION-ENDPOINT
预测 URL 终结点。Your prediction URL endpoint. 位于 LUIS 门户中,你的应用的“Azure 资源”页。Located on the LUIS portal, Azure Resources page for your app.
例如,https://api.cognitive.azure.cn/
。For example,https://api.cognitive.azure.cn/
.安装
requests
依赖项。Install therequests
dependency.requests
库用于发出 HTTP 请求:Therequests
library is used to make HTTP requests:pip install requests
使用以下控制台命令运行脚本:Run your script with this console command:
python predict.py
查看以 JSON 形式返回的预测响应: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 响应: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.