快速入门:使用 Node.js 获取意向Quickstart: Get intent using Node.js

在本快速入门中,你将向 LUIS 终结点传递话语并返回意向和实体。In this quickstart, pass utterances to a LUIS endpoint and get intent and entities back.

本快速入门使用可用的公共 LUIS 应用从会话文本中确定用户的意向。In this quickstart, use an available public LUIS app to determine a user's intention from conversational text. 将用户的意向作为文本发送到公共应用的 HTTP 预测终结点。Send the user's intention as text to the public app's HTTP prediction endpoint. 在终结点处,LUIS 应用公共应用的模型来分析自然语言文本的含义,确定总体意向并提取与应用的主题域相关的数据。At the endpoint, LUIS applies the public app's model to analyze the natural language text for meaning, determining overall intent and extracting data relevant to the app's subject domain.

本快速入门使用终结点 REST API。This quickstart uses the endpoint REST API. 有关详细信息,请参阅终结点 API 文档For more information, see the endpoint API documentation.

对于本文,需要一个免费的 LUIS 帐户。For this article, you need a free LUIS account.

先决条件Prerequisites

Note

Azure-Samples GitHub 存储库中提供了完整的 Node.js 解决方案。The complete Node.js solution is available from the Azure-Samples GitHub repository.

获取 LUIS 密钥Get LUIS key

通过终结点密钥提供对预测终结点的访问权限。Access to the prediction endpoint is provided with an endpoint key. 出于本快速入门的目的,请使用与 LUIS 帐户关联的免费初学者密钥。For the purposes of this quickstart, use the free starter key associated with your LUIS account.

  1. 使用 LUIS 帐户登录。Sign in using your LUIS account.

    语言理解 (LUIS) 应用列表的屏幕截图Screenshot of Language Understanding (LUIS) app list

  2. 在右上方菜单中选择你的名称,然后选择“设置”。Select your name in the top right menu, then select Settings.

    LUIS 用户设置菜单访问

  3. 复制创作密钥的值。Copy the value of the Authoring key. 稍后你将在此快速入门中使用它。You will use it later in the quickstart.

    语言理解 (LUIS) 用户设置的屏幕截图Screenshot of Language Understanding (LUIS) user settings

    创作密钥允许对创作 API 发出无限量的请求而无需付费,并允许每月为你的所有 LUIS 应用向预测终结点 API 发出最多 1000 个查询。The authoring key allows free unlimited requests to the authoring API and up to 1000 queries to the prediction endpoint API per month for all your LUIS apps.

使用浏览器获取意向Get intent with browser

若要了解 LUIS 预测终结点返回的内容,请在 Web 浏览器中查看预测结果。To understand what a LUIS prediction endpoint returns, view a prediction result in a web browser. 若要查询公共应用,需要自己密钥和应用 ID。In order to query a public app, you need your own key and the app ID. 公共 IoT 应用 ID (df67dcdb-c37d-46af-88e1-8b97951ca1c2) 在步骤 1 中作为 URL 的一部分提供。The public IoT app ID, df67dcdb-c37d-46af-88e1-8b97951ca1c2, is provided as part of the URL in step one.

GET 终结点请求的 URL 格式为:The format of the URL for a GET endpoint request is:

https://<region>.api.cognitive.azure.cn/luis/v2.0/apps/<appID>?subscription-key=<YOUR-KEY>&q=<user-utterance>
  1. 公共 IoT 应用的终结点采用以下格式:https://chinaeast2.api.cognitive.azure.cn/luis/v2.0/apps/df67dcdb-c37d-46af-88e1-8b97951ca1c2?subscription-key=<YOUR_KEY>&q=turn on the bedroom lightThe endpoint of the public IoT app is in this format: https://chinaeast2.api.cognitive.azure.cn/luis/v2.0/apps/df67dcdb-c37d-46af-88e1-8b97951ca1c2?subscription-key=<YOUR_KEY>&q=turn on the bedroom light

    复制 URL 并将 <YOUR_KEY> 的值替换为自己的密钥。Copy the URL and substitute your key for the value of <YOUR_KEY>.

  2. 将该 URL 粘贴到浏览器窗口中,然后按 Enter。Paste the URL into a browser window and press Enter. 浏览器显示的 JSON 结果指示 LUIS 将 HomeAutomation.TurnOn 意向检测为首要意向,并检测到值为 bedroomHomeAutomation.Room 实体。The browser displays a JSON result that indicates that LUIS detects the HomeAutomation.TurnOn intent as the top intent and the HomeAutomation.Room entity with the value bedroom.

    {
      "query": "turn on the bedroom light",
      "topScoringIntent": {
        "intent": "HomeAutomation.TurnOn",
        "score": 0.809439957
      },
      "entities": [
        {
          "entity": "bedroom",
          "type": "HomeAutomation.Room",
          "startIndex": 12,
          "endIndex": 18,
          "score": 0.8065475
        }
      ]
    }
    
  3. 将 URL 中的 q= 参数值更改为 turn off the living room light,然后按 Enter。Change the value of the q= parameter in the URL to turn off the living room light, and press Enter. 现在,结果指示 LUIS 将 HomeAutomation.TurnOff 意向检测为首要意向,并检测到值为 living roomHomeAutomation.Room 实体。The result now indicates that LUIS detected the HomeAutomation.TurnOff intent as the top intent and the HomeAutomation.Room entity with value living room.

    {
      "query": "turn off the living room light",
      "topScoringIntent": {
        "intent": "HomeAutomation.TurnOff",
        "score": 0.984057844
      },
      "entities": [
        {
          "entity": "living room",
          "type": "HomeAutomation.Room",
          "startIndex": 13,
          "endIndex": 23,
          "score": 0.9619945
        }
      ]
    }
    

以编程方式获取意向Get intent programmatically

可以使用 Node.js 来访问上一步骤中浏览器窗口显示的相同结果。You can use Node.js to access the same results you saw in the browser window in the previous step.

  1. 复制以下代码片段:Copy the following code snippet:
    require('dotenv').config();

    var request = require('request');
    var querystring = require('querystring');

    // Analyze text
    //
    // utterance = user's text
    //
    function getLuisIntent(utterance) {

        // endpoint URL
        var endpoint =
            "https://chinaeast2.api.cognitive.azure.cn/luis/v2.0/apps/";

        // Set the LUIS_APP_ID environment variable 
        // to df67dcdb-c37d-46af-88e1-8b97951ca1c2, which is the ID
        // of a public sample application.    
        var luisAppId = process.env.LUIS_APP_ID;

        // Read LUIS key from environment file ".env"
        // You can use the authoring key instead of the endpoint key. 
        // The authoring key allows 1000 endpoint queries a month.
        var endpointKey = process.env.LUIS_ENDPOINT_KEY;

        // Create query string 
        var queryParams = {
            "verbose":  true,
            "q": utterance,
            "subscription-key": endpointKey
        }

        // append query string to endpoint URL
        var luisRequest =
            endpoint + luisAppId +
            '?' + querystring.stringify(queryParams);

        // HTTP Request
        request(luisRequest,
            function (err,
                response, body) {

                // HTTP Response
                if (err)
                    console.log(err);
                else {
                    var data = JSON.parse(body);

                    console.log(`Query: ${data.query}`);
                    console.log(`Top Intent: ${data.topScoringIntent.intent}`);
                    console.log('Intents:');
                    console.log(JSON.stringify(data.intents));
                }
            });
    }

    // Pass an utterance to the sample LUIS app
    getLuisIntent('turn on the left light');
  1. 创建包含以下文本的 .env 文件,或者在系统环境中设置以下变量:Create .env file with the following text or set these variables in the system environment:

    LUIS_APP_ID=df67dcdb-c37d-46af-88e1-8b97951ca1c2
    LUIS_ENDPOINT_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    
  2. LUIS_ENDPOINT_KEY 环境变量设置为你的密钥。Set the LUIS_ENDPOINT_KEY environment variable to your key.

  3. 通过在命令行上运行以下命令来安装依赖项:npm installInstall dependencies by running the following command at the command-line: npm install.

  4. 通过 npm start 运行代码。Run the code with npm start. 其中会显示前面在浏览器窗口中显示的相同值。It displays the same values that you saw earlier in the browser window.

LUIS 密钥LUIS keys

为方便起见,本快速入门使用创作密钥。This quickstart uses the authoring key for convenience. 该密钥主要用于创作模型,但允许少量(1000 个)终结点请求。The key is primarily for authoring the model but does allow a small number (1000) of endpoint requests. 当你准备在测试环境、过渡环境或生产环境中处理更多终结点请求时,请在 Azure 门户中创建一个语言理解资源,并将其分配给 LUIS 门户中的 LUIS 应用。When you are ready for more endpoint requests in a test, stage or production environment, create a Language Understanding resource in the Azure portal and assign it to the LUIS app in the LUIS portal.

清理资源Clean up resources

删除 Node.js 文件。Delete the Node.js file.

后续步骤Next steps