Quickstart: Get intent using Javascript

In this quickstart, use an available public LUIS app to determine a user's intention from conversational text. Send the user's intention as text to the public app's HTTP prediction endpoint. 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.

This quickstart uses the endpoint REST API. For more information, see the endpoint API documentation.

For this article, you need a free LUIS account.

Note

The complete solution is available from the LUIS-Samples Github repository.

Prerequisites

Get LUIS key

Access to the prediction endpoint is provided with an endpoint key. For the purposes of this quickstart, use the free starter key associated with your LUIS account.

  1. Sign in using your LUIS account.

    Screenshot of Language Understanding (LUIS) app list

  2. Select your name in the top right menu, then select Settings.

    LUIS user settings menu access

  3. Copy the value of the Authoring key. You will use it later in the quickstart.

    Screenshot of Language Understanding (LUIS) user settings

    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

To understand what a LUIS prediction endpoint returns, view a prediction result in a web browser. In order to query a public app, you need your own key and the app ID. The public IoT app ID, df67dcdb-c37d-46af-88e1-8b97951ca1c2, is provided as part of the URL in step one.

The format of the URL for a GET endpoint request is:

https://<region>.api.cognitive.microsoft.com/luis/v2.0/apps/<appID>?subscription-key=<YOUR-KEY>&q=<user-utterance>
  1. The endpoint of the public IoT app is in this format: https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/df67dcdb-c37d-46af-88e1-8b97951ca1c2?subscription-key=<YOUR_KEY>&q=turn on the bedroom light

    Copy the URL and substitute your key for the value of <YOUR_KEY>.

  2. Paste the URL into a browser window and press Enter. 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. Change the value of the q= parameter in the URL to turn off the living room light, and press Enter. 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

You can use Javascript to access the same results you saw in the browser window in the previous step.

  1. Copy the code that follows and save it into an HTML file:

    <!DOCTYPE html>
    <html>
    
    <head>
        <title>Javascript LUIS endpoint query</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    </head>
    
    <body>
        <form ajax="true">
    
            <p>This uses the IoT HomeAutomation app ID unless you change the LUIS App ID.</p>
    
            <div>
                <div>
                    <label>LUIS App ID:<br>(default: public IoT app )</label>
                    <input id="appid" type="text" name="html" value="df67dcdb-c37d-46af-88e1-8b97951ca1c2" size="40" />
                </div>
    
                <div>
                    <label>LUIS Endpoint Key:<br>(You can use the authoring key instead of the endpoint key.<br>The authoring key allows 1000 endpoint queries a month.)</label>
                    <input id="endpointkey" type="text" name="html" value="" size="40" />
                </div>
    
                <div>
                    <label>LUIS utterance:<br>(User text to analyze)</label>
                    <input id="utterance" type="text" name="html" value="Turn on the lights" size="40" />
                </div>
    
                <div>
                    <input type="submit" value="Submit" />
                </div>
    
        </form>
        <hr>
    
        <div id="prediction">
        </div>
    
    
        <script type="text/javascript">
            $(document).ready(function (e) {
    
                // Function called when the form is submitted
                $("form[ajax=true]").submit(function (e) {
    
                    e.preventDefault();
    
                    // Set or get variables needed for HTTP call
                    var region = "westus";
                    var appid = $("#appid").val();
                    var endpointkey = $("#endpointkey").val();
                    var utterance = encodeURIComponent($("#utterance").val());
    
                    var params = $.param({
                        // These are optional request parameters. 
                        "verbose": "false",
                    });
    
                    // Endpoint URL
                    var url = `https://${region}.api.cognitive.microsoft.com/luis/v2.0/apps/${appid}?subscription-key=${endpointkey}&q=${utterance}&${params}`;
    
                    $.ajax({
                        url: url,
                        type: "GET",
                        cache: false,
                        beforeSend: function (xhrObj) {
                            // Request headers
                            xhrObj.setRequestHeader("Content-Type", "application/json");
                            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", endpointkey);
                        },
                        success: function (returnhtml) {
                            // show response
                            $("#prediction").append("<p>" + JSON.stringify(returnhtml) + "<p>");
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            // show error
                            console.log(xhr.status);
                            console.log(thrownError);
                        }
                    });
    
                });
    
            });
        </script>
    </body>
    
    </html>
    
  2. Open the file in a browser. Enter your LUIS endpoint key in the form and select Submit.

    Html sample displayed in browser with LUIS results for Home Automation app

    The result display under the form.

LUIS keys

This quickstart uses the authoring key for convenience. The key is primarily for authoring the model but does allow a small number (1000) of endpoint requests. 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

Delete the Javascript file.

Next steps