In this tutorial we are going to see how we can get the results from your LUIS (Language Understanding Intelligent Service) Endpoint. For those of you who do not know Language Understanding Intelligent Service (LUIS) is machine learning-based service to build natural language into apps, bots, and IoT devices.
Create a LUIS Application
To follow this tutorial first of all you need to create a LUIS application. Download this json file.
Login to https://www.luis.ai to import the Luis Application using this guide: Getting started with Luis.ai. In this guide you can see the explanation on the model included and find the endpoint and keys you need to call the Luis.ai EndPoint and get results from your application.
Get results from your Luis application
After you have completed the previous steps, create a Console Application in Visual Studio 2017.
Add the following code in Program.cs file.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello this is an automating system. How can I help you?");
MakeRequest(Console.ReadLine());
Console.WriteLine("Calculating...");
Console.ReadLine();
}
static async void MakeRequest(string Input)
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// This app ID is for a public sample app that recognizes requests to turn on and turn off lights
var luisAppId = "AddyourID";
var endpointKey = "AddyourID";
// The request header contains your subscription key
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", endpointKey);
// The "q" parameter contains the utterance to send to LUIS
queryString["q"] = Input;
// These optional request parameters are set to their default values
queryString["timezoneOffset"] = "0";
queryString["verbose"] = "false";
queryString["spellCheck"] = "false";
queryString["staging"] = "false";
var endpointUri = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString;
var response = await client.GetAsync(endpointUri);
var strResponseContent = await response.Content.ReadAsStringAsync();
Result result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result>(strResponseContent);
switch (result.topScoringIntent.intent)
{
case "ShowHotelsReviews":
Console.WriteLine("I see you are looking for hotel reviews? Visit hotelreviews.com or call at +00123457989 for more");
break;
case "SearchHotels":
Console.WriteLine("For bookings a call center employee will contact you. Leave your details here.");
break;
case "Help":
Console.WriteLine("Type what you need for example \"I am looking for hotels near Athens\"");
break;
default:
Console.WriteLine("Oups! Something went wrong. Cannt find what you are looking for? Type what you need for example \"I am looking for hotels near Athens\"");
break;
}
}
}
class Program
{
staticvoidMain(string[] args)
{
Console.WriteLine("Hello this is an automating system. How can I help you?");
MakeRequest(Console.ReadLine());
Console.WriteLine("Calculating...");
Console.ReadLine();
}
staticasyncvoidMakeRequest(string Input)
{
var client = newHttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// This app ID is for a public sample app that recognizes requests to turn on and turn off lights
var luisAppId = "AddyourID";
var endpointKey = "AddyourID";
// The request header contains your subscription key
// The "q" parameter contains the utterance to send to LUIS
queryString["q"] = Input;
// These optional request parameters are set to their default values
queryString["timezoneOffset"] = "0";
queryString["verbose"] = "false";
queryString["spellCheck"] = "false";
queryString["staging"] = "false";
var endpointUri = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString;
var response = await client.GetAsync(endpointUri);
var strResponseContent = await response.Content.ReadAsStringAsync();
Result result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result>(strResponseContent);
switch(result.topScoringIntent.intent)
{
case"ShowHotelsReviews":
Console.WriteLine("I see you are looking for hotel reviews? Visit hotelreviews.com or call at +00123457989 for more");
break;
case"SearchHotels":
Console.WriteLine("For bookings a call center employee will contact you. Leave your details here.");
break;
case"Help":
Console.WriteLine("Type what you need for example \"I am looking for hotels near Athens\"");
break;
default:
Console.WriteLine("Oups! Something went wrong. Cannt find what you are looking for? Type what you need for example \"I am looking for hotels near Athens\"");
break;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello this is an automating system. How can I help you?");
MakeRequest(Console.ReadLine());
Console.WriteLine("Calculating...");
Console.ReadLine();
}
static async void MakeRequest(string Input)
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// This app ID is for a public sample app that recognizes requests to turn on and turn off lights
var luisAppId = "AddyourID";
var endpointKey = "AddyourID";
// The request header contains your subscription key
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", endpointKey);
// The "q" parameter contains the utterance to send to LUIS
queryString["q"] = Input;
// These optional request parameters are set to their default values
queryString["timezoneOffset"] = "0";
queryString["verbose"] = "false";
queryString["spellCheck"] = "false";
queryString["staging"] = "false";
var endpointUri = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString;
var response = await client.GetAsync(endpointUri);
var strResponseContent = await response.Content.ReadAsStringAsync();
Result result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result>(strResponseContent);
switch (result.topScoringIntent.intent)
{
case "ShowHotelsReviews":
Console.WriteLine("I see you are looking for hotel reviews? Visit hotelreviews.com or call at +00123457989 for more");
break;
case "SearchHotels":
Console.WriteLine("For bookings a call center employee will contact you. Leave your details here.");
break;
case "Help":
Console.WriteLine("Type what you need for example \"I am looking for hotels near Athens\"");
break;
default:
Console.WriteLine("Oups! Something went wrong. Cannt find what you are looking for? Type what you need for example \"I am looking for hotels near Athens\"");
break;
}
}
}
Replace the luisAppId variable with your App ID and the endpointKey with your End Point Key.
Run the program! The MakeRequest function, depending on the Luis result, responds to the user accordingly!
Georgia Kalyva is a Microsoft AI MVP with years of experience in software engineering and is currently working for ITT as Web Applications Developer. She is passionate about AI and Azure and has represented Greece in global competitions in Technology and Entrepreneurship. She is also a member of the Microsoft Student Partners team and has taken over the role of mentor in several Microsoft competitions and trainings.