Introduction
In my previous two articles I have explained how can we create a bot application. In this article I will explain you how can we create our bot application more interactive for our user.s In this article I will use one of the
Microsoft Cognitive Services that is
LUIS.
My Previous Articles on Bot Framework
What is LUIS ?
According to
luis.ai, LUIS is a Language Understanding Intelligent Service, which offers a fast and effective way of adding language understanding to applications. With LUIS, you can use pre-existing, world-class, pre-built models from Bing and Cortana whenever they suit your purposes and when you need specialized models, LUIS guides you through the process of quickly building them. LUIS is a part of
Microsoft Cognitive Service.
Let's Understand LUIS with an example:
Example 1:
Suppose I search in my Windows 10 PC or Windows phone through Cortana like
"Where am I," then I will get a map and my current location. However I am not saying to Cortana to search for location, I am just saying
"Where am I", And cortana is understanding what am I looking for and this is happening because of LUIS.
Example 2: Suppose I search like
Email to [email protected] then cortana recognizes my command using LUIS and give me result like as follows:
So in above examples you can see there is more interaction.
Let's look at my previous example
In my previous article I created a
Real Time Bot Application where I was searching for stock price by stock symbol like as following figure.
In the above figure you can see that message is not more conversational I am just sending input as msft and getting result but for our users it will not be more interactive.
In this article I will explain to you how can we make our Bot more interactive. Like my input will be
"What about msft" or "What is the price of orcl stock", Like as following figure:
The above conversation is more interactive, and this can be done by using LUIS.
Let's create application step by step
Step 1: Firstly open visual studio, and create a bot application with any name. Follow following steps to create an Bot application:
- File > New > Project or just Press CTRL + SHIFT + N.
- Now for Visual C# template select Bot Application then give the name of the application and then click OK button to create the application.
Step 2: Now go to
LUIS Website, then login in to your account or register there. Then After create new application.
Step 3: Now one dialog box will be open; provide Application Name, Description, and Application Culture and then click on Add App Button.
I have created my app by name StockApp.
Step 4: Now edit your application by clicking on Edit button.
Step 5: On the left-hand panel, you will see an option to add entities. So quickly add an Entity with Name
StockSymbol. Follow following figure to add an entity.
What is Entity: An entity is a keyword for which we will search like in our example entity is
msft that is nothing but an Stock Symbol so I have given my entity name
StockSymbol.
Step 6: On the left-hand panel, you will see an option to Add Intents. So le'ts Quickly add Some intents. Give the name of the intents and example of utterance and Save.
I have added two intents StockPrice and StockPrice2.
What is Intents? :
Intent means just what we desire and what is our intention so my intention is I would like to ask to my Bot "what about msft" or "what is the price of msft stock".
What are utterances:
How will I search will be my statements. So I have added 4-5 statements. But after 4-5 LUIS will automatically detect what we want.
Step 7: Now add some utterances like as following.
Here you have to define
StockSymbol, After doing this for 4-5 LUIS will automatically detect what is our entity.
Note: Don't forget to add entity by clicking on entity and submiting it after defining entity.
Step 8: Publish your application by clicking on publish button.
Step 9: Now one dialog box will open search there for your query.
Step 10: Hit Enter and you will get JSON format result.
Step 11: Now again flip to visual studio and create class file for this JSON result. I am addinga few of the classes for this JOSN result. Class code is given below in figure.
Step 12:
Now Create a class in your project with any name. I am giving Name as YahooBot, Which class Yahoo Finance API to get Stock Information, I am writing the following code inside it.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Threading.Tasks;
- using System.Web;
-
- namespace StockBot2
- {
- public class YahooBot
- {
- public static async Task<double?> GetStockRateAsync(string StockSymbol)
- {
- try
- {
- string ServiceURL = $"http://finance.yahoo.com/d/quotes.csv?s={StockSymbol}&f=sl1d1nd";
- string ResultInCSV;
- using (WebClient client = new WebClient())
- {
- ResultInCSV = await client.DownloadStringTaskAsync(ServiceURL).ConfigureAwait(false);
- }
- var FirstLine = ResultInCSV.Split('\n')[0];
- var Price = FirstLine.Split(',')[1];
- if (Price != null && Price.Length >= 0)
- {
- double result;
- if (double.TryParse(Price, out result))
- {
- return result;
- }
- }
- return null;
- }
- catch (WebException ex)
- {
-
- throw ex;
- }
- }
- }
- }
In the above code you can see a function that is GetStockRateAsync, which takes a StockSymbol as a parameter. I am calling Yahoo Finance API, where I will pass that symbol and that will return a CSV file of stock market rate, and I am splitting CSV File by commas because CSV File means Comma Separated Value. So, I am splitting by comma(,) and getting current price of that stock. Then I am converting it into double because of course our stock rate will be in double and returning current price.
Step 13: Now in MessagesController create a class that will call this above function., so I am creating a GetStock function in My MessageController class.
- private async Task<string> GetStock(string StockSymbol)
- {
- double? dblStockValue = await YahooBot.GetStockRateAsync(StockSymbol);
- if(dblStockValue==null)
- {
- return string.Format("This \"{0}\" is not an valid stock symbol",StockSymbol);
- }
- else
- {
- return string.Format("Stock Price of {0} is {1}",StockSymbol,dblStockValue);
- }
- }
In above code you can see I am calling my GetStockRateAsync function that will return a nullable double. I am checking whether that stock value is null or not. According to stock value I am returning appropriate string.
Step 14: As we know we like to interact via LUIS so I am creating one more method in MessagesController with name GetEntityFromLUIS.
- private static async Task<StockLUIS> GetEntityFromLUIS(string Query)
- {
- Query = Uri.EscapeDataString(Query);
- StockLUIS Data = new StockLUIS();
- using (HttpClient client=new HttpClient())
- {
- string RequestURI = "https://api.projectoxford.ai/luis/v1/application?id=7f626790-38d6-4143-9d46-fe85c56a9016&subscription-key=09f80de609fa4698ab4fe5249321d165&q=" + Query;
- HttpResponseMessage msg = await client.GetAsync(RequestURI);
-
- if (msg.IsSuccessStatusCode)
- {
- var JsonDataResponse = await msg.Content.ReadAsStringAsync();
- Data = JsonConvert.DeserializeObject<StockLUIS>(JsonDataResponse);
- }
- }
- return Data;
- }
Above code will take query from us and process query using LUIS Service and return StockLUIS class object, That is nothing but JSON data as a class format.
Step 15: Now write following code in Post Action of Message Controller.
- public async Task<Message> Post([FromBody]Message message)
- {
- if (message.Type == "Message")
- {
- string StockRateString;
- StockLUIS StLUIS = await GetEntityFromLUIS(message.Text);
- if(StLUIS.intents.Count()>0)
- {
- switch(StLUIS.intents[0].intent)
- {
- case "StockPrice":
- StockRateString = await GetStock(StLUIS.entities[0].entity);
- break;
- case "StockPrice2":
- StockRateString = await GetStock(StLUIS.entities[0].entity);
- break;
- default:
- StockRateString = "Sorry, I am not getting you...";
- break;
- }
- }
- else
- {
- StockRateString = "Sorry, I am not getting you...";
- }
-
-
- return message.CreateReplyMessage(StockRateString);
- }
- else
- {
- return HandleSystemMessage(message);
- }
- }
Above code takes message from Bot User and sends message to LUIS now LUIS will process the message if the message will be identify then send StockLUIS class Object. Now if data will be identified then it will check intents and whatever entity will return LUIS service that will be pass to Yahoo Web Service and we will get string as a result that will be pass to client. If no Intent will be there then we will tell to our client that "Sorry, I am Not Getting You...".
Step 16: Now run this project in Emulator.
Output: If we send wrong query to LUIS then Output will be as follows,
If we query with invalid stock symbol then output will be as follows,
Query with valid query and valid stock symbol:
Suppose in above figure I Forget to provide stock there then also LUIS will give me the accurate result.
Conclusion:
- In this article we have created an interactive Bot Application with LUIS using Microsoft Bot Framework.
- We learn about LUIS and its power.
- We have seen how LUIS helps in powerful interactive Conversation.
Read more articles on Machine Learning: