Quickstart: Get intent using Ruby

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.

Prerequisites

Note

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

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 Ruby 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 a file named endpoint-call.rb:

    require 'net/https'
    require 'uri'
    require 'json'
    
    # **********************************************
    # *** Update or verify the following values. ***
    # **********************************************
    
    # The ID of a public sample LUIS app that recognizes intents for turning on and off lights
    appId = "df67dcdb-c37d-46af-88e1-8b97951ca1c2"
    
    # Replace the string value with your endpoint key.
    # You can use the authoring key instead of the endpoint key. 
    # The authoring key allows 1000 endpoint queries a month.
    endpointKey = "YOUR-KEY"
    
    # The endpoint URI below is for the West US region.
    # If you use a different region, update accordingly.
    host = "https://westus.api.cognitive.microsoft.com"
    path = "/luis/v2.0/apps/"
    
    # Analyze this text
    term = "turn on the left light"
    
    if endpointKey.length != 32 then
        puts "Invalid LUIS endpoint key!"
        puts "Please paste yours into the source code."
        abort
    end
    
    qs = URI.encode_www_form(
        "q" => term, 
        "timezoneOffset" => 0, 
        "verbose" => false, 
        "spellCheck" => false, 
        "staging" => false
    )
    uri = URI(host + path + appId + "?" + qs)
    
    puts
    puts "LUIS query: " + term
    puts
    puts "Request URI: " + uri.to_s
    
    request = Net::HTTP::Get.new(uri)
    request["Ocp-Apim-Subscription-Key"] = endpointKey
    
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
        http.request(request)
    end
    
    puts "\nJSON Response:\n\n"
    puts JSON::pretty_generate(JSON(response.body))
    
  2. Replace "YOUR-KEY" with your endpoint key.

  3. Run the Ruby application at the command-line with ruby endpoint-call.rb. It displays the same JSON that you saw earlier in the browser window.

    LUIS query: turn on the left light
    
    Request URI: https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/df67dcdb-c37d-46af-88e1-8b97951ca1c2?q=turn+on+the+left+light&timezoneOffset=0&verbose=false&spellCheck=false&staging=false
    
    JSON Response:
    
    {
      "query": "turn on the left light",
      "topScoringIntent": {
        "intent": "HomeAutomation.TurnOn",
        "score": 0.933549
      },
      "entities": [
        {
          "entity": "left",
          "type": "HomeAutomation.Room",
          "startIndex": 12,
          "endIndex": 15,
          "score": 0.540835142
        }
      ]
    }
    

Clean up resources

Delete Ruby file.

Next steps