快速入门:使用 cURL 和 REST 获取意向Quickstart: Use cURL and REST to get intent

本快速入门介绍如何使用 LUIS 应用从会话文本中确定用户的意向。This quickstart will show you how to use a LUIS app to determine a user's intention from conversational text. 你将使用 cURL 将用户的意向作为文本发送到 Pizza 应用的 HTTP 预测终结点。You will use cURL to 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.

参考文档Reference documentation

先决条件Prerequisites

  • 免费的 LUIS 帐户。A free LUIS account.
  • 文本编辑器,如 Visual Studio CodeA text editor such as Visual Studio Code.
  • 命令行程序 cURL。The command-line program cURL. 在 macOS、大多数 Linux 分发版和 Windows 10 版本 1803 及更高版本上已安装该 cURL 程序。The cURL program is already installed on macOS, most Linux distributions, and Windows 10 build 1803 and later. 若需要安装 cURL,可在 cURL 下载页中下载 cURL。If you need to install cURL, you can download cURL from the cURL download page.

创建 Pizza 应用Create Pizza app

Create the pizza app.

  1. Select pizza-app-for-luis-v6.json to bring up the GitHub page for the pizza-app-for-luis.json file.
  2. Right-click or long tap the Raw button and select Save link as to save the pizza-app-for-luis.json to your computer.
  3. Sign into the LUIS portal.
  4. Select My Apps.
  5. On the My Apps page, select + New app for conversation.
  6. Select Import as JSON.
  7. In the Import new app dialog, select the Choose File button.
  8. Select the pizza-app-for-luis.json file you downloaded, then select Open.
  9. 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 a 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.

  1. 在 LUIS 网站的右上方,选择“训练”按钮。In the top-right side of the LUIS website, select the Train button.

    “训练”按钮

  2. 当“训练”按钮上的状态指示器为绿色时,即表示训练完成。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.

  1. 在右上方的导航栏中选择“发布”。Select Publish in the top-right navigation.

    右上方菜单中的“LUIS 发布到终结点”按钮的屏幕截图

  2. 选择“生产”槽,然后选择“完成” 。Select the Production slot, then select Done.

    “LUIS 发布到终结点”的屏幕截图Screenshot of LUIS publish to endpoint

  3. 在通知中选择“访问终结点 URL”链接,转到“Azure 资源”页。 Select the Access your endpoint URLs link in the notification to go to the Azure Resources page. 终结点 URL 作为“示例查询”列出。The endpoint URLs are listed as the Example Query.

Add an authoring resource to the Pizza app

  1. Select MANAGE.
  2. Select Azure Resources.
  3. Select Authoring Resource.
  4. Select Change authoring resource.

If you have an authoring resource, enter the Tenant Name, Subscription Name, and LUIS resource name of your authoring resource.

If you do not have an authoring resource:

  1. Select Create new resource.
  2. Enter a Tenant Name, Resource Name, Subscription Name, and Azure Resource Group Name.

Your Pizza app is now ready to use.

Record the access values for your Pizza app

To use your new Pizza app, you will need the app ID, authoring key, and authoring endpoint of your Pizza app.

To find these values:

  1. From the Intents page, select MANAGE.
  2. From the Application Settings page, record the App ID.
  3. Select Azure Resources.
  4. Select Authoring Resource.
  5. From the Authoring Resource tab, record the Primary Key. This value is your authoring key.
  6. Record the Endpoint URL. This value is your authoring endpoint.

从预测终结点获取意向Get intent from the prediction endpoint

使用 cURL 查询预测终结点并获取预测结果。Use cURL to query the prediction endpoint and get a prediction result.

  1. 请将以下命令复制到自己的文本编辑器中:Copy this command into your text editor:

    curl "https://***YOUR-PREDICTION-ENDPOINT**_/luis/prediction/v3.0/apps/_*_YOUR-APP-ID_*_/slots/production/predict" ^
          --request GET ^
          --get ^
          --data "subscription-key=_*_YOUR-PREDICTION-KEY_*_" ^
          --data "verbose=true" ^
          --data "show-all-intents=true" ^
          --data-urlencode "query=I want two large pepperoni pizzas on thin crust please"
    
  2. 将以 _*_YOUR- 开头的值替换为你自己的值。Replace the values starting with _*_YOUR- with your own values.

    信息Information 目的Purpose
    _*_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/.
    _*_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.
  3. 将文本复制到控制台窗口并按 Enter 执行该命令:Copy the text to a console window and press Enter to execute the command:

  4. 使用以下控制台命令运行脚本:Run your script with this console command:

    python predict.py
    
  5. 查看以 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"
                ]
              }
            ]
          }
        }
      }
    }
    

后续步骤Next steps