Get the Results from your LUIS endpoint

Spread the love

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;
            }
            
            }
        }

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!

Download the full sample from my Github here.

Leave a Reply

Your email address will not be published. Required fields are marked *