Tutorial: Call a LUIS endpoint using Ruby
Pass utterances to a LUIS endpoint and get intent and entities back.
- Create LUIS subscription and copy key value for later use
- View LUIS endpoint results from browser to public sample IoT app
- Create Visual Studio C# console app to make HTTPS call to LUIS endpoint
For this article, you need a free LUIS account in order to author your LUIS application.
Create LUIS subscription key
You need a Cognitive Services API key to make calls to the sample LUIS app used in this walkthrough.
To get an API key, follow these steps:
Log in to the Azure portal at https://portal.azure.com.
Follow the steps in Creating Subscription Keys using Azure to get a key.
Go back to the LUIS website and log in using your Azure account.
Understand what LUIS returns
To understand what a LUIS app returns, you can paste the URL of a sample LUIS app into a browser window. The sample app is an IoT app that detects whether the user wants to turn on or turn off lights.
- The endpoint of the sample app is in this format:
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/df67dcdb-c37d-46af-88e1-8b97951ca1c2?subscription-key=<YOUR_API_KEY>&verbose=false&q=turn%20on%20the%20bedroom%20lightCopy the URL and substitute your subscription key for the value of the
Paste the URL into a browser window and press Enter. The browser displays a JSON result that indicates that LUIS detects the
HomeAutomation.TurnOnintent and the
HomeAutomation.Roomentity with the value
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 the LUIS detected the
HomeAutomation.TurnOffintent and the
HomeAutomation.Roomentity with value
Consume a LUIS result using the Endpoint API with Ruby
You can use Ruby to access the same results you saw in the browser window in the previous step.
Copy the code that follows and save it into an HTML file:
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 subscriptionKey string value with your valid Azure Subscription key. subscriptionKey = "YOUR-SUBSCRIPTION-KEY" # The endpoint URI below is for the West US region. # If your subscription is in a different region, update accordingly. host = "https://westus.api.cognitive.microsoft.com" path = "/luis/v2.0/apps/" # The LUIS query term term = "turn on the left light" if subscriptionKey.length != 32 then puts "Invalid LUIS API subscription 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"] = subscriptionKey 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))
"YOUR-SUBSCRIPTION-KEY"with your subscription key in this line of code:
subscriptionKey = "YOUR-SUBSCRIPTION-KEY"
Run the Ruby application. It displays the same JSON that you saw earlier in the browser window.
Clean up resources
The two resources created in this tutorial are the LUIS subscription key and the C# project. Delete the LUIS subscription key from the Azure portal. Close the Visual Studio project and remove the directory from the file system.