In previous article, I have explained how to educate QnA maker for questions and answers. In this article we will discuss about how to consume QnA maker service in Bot application created using Microsoft Bot Framework.
I am going to build a bot which will answer FAQs related to LUIS with the help of QnA maker service we have created in previous article.
Prerequisite
- Download Visual Studio 2015/2017 and update all extensions.
- Download and install bot application template from here.
- Download Bot Framework Emulator. The emulator is a desktop application that lets you test a bot application on localhost or running remotely.
Create bot application
Open Visual Studio and create a new C# project. Choose the Bot Application template. If required, also update NuGet Packages installed in project.
Template contains all components required to build a bot as well as initial code setup.
Code
In solution explorer, you will see WebApiConfig.cs file, Controllers and Dialogs folders. 'MessageController' is inherited from 'System.Web.Http.ApiController'. Bot application is nothing but webapis which will be called from chat window to get response.
When user is going to enter query in chat window, 'post( )' method within controller will get called. Then controller will invoke specified dialog, which will process the query and return the result.
For our chat bot, I have added AnswerDialog.cs (see image above) to process questions from user. You can pull code from GitHub repository.
MessageController.cs
Updated code for invoking AnswerDialog instead of default RootDialog.
- public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
- {
- if (activity.Type == ActivityTypes.Message)
- {
- await Conversation.SendAsync(activity, () => new Dialogs.AnswerDialog());
- }
- else
- {
- HandleSystemMessage(activity);
- }
- var response = Request.CreateResponse(HttpStatusCode.OK);
- return response;
- }
AnswerDialog.cs
- public class QnAMakerResult
- {
- [JsonProperty(PropertyName = "answers")]
- public List<Result> Answers { get; set; }
- }
-
- public class Result
- {
- [JsonProperty(PropertyName = "answer")]
- public string Answer { get; set; }
-
- [JsonProperty(PropertyName = "questions")]
- public List<string> Questions { get; set; }
-
- [JsonProperty(PropertyName = "score")]
- public double Score { get; set; }
- }
-
-
- public Task StartAsync(IDialogContext context)
- {
- context.Wait(QuestionReceivedAsync);
-
- return Task.CompletedTask;
- }
-
- private async Task QuestionReceivedAsync(IDialogContext context, IAwaitable<object> result)
- {
- var activity = await result as Activity;
-
- await context.PostAsync(GetAnswer(activity.Text));
- }
-
- private string GetAnswer(string query)
- {
- string responseString = string.Empty;
-
- var knowledgebaseId = Convert.ToString(ConfigurationManager.AppSettings["KNOWLEDGE_BASE_ID"], CultureInfo.InvariantCulture);
-
-
- var builder = new UriBuilder(string.Format(Convert.ToString(ConfigurationManager.AppSettings["QNA_SERVICE_URL"], CultureInfo.InvariantCulture), knowledgebaseId));
-
-
- var postBody = string.Format("{{\"question\": \"{0}\"}}", query);
-
-
- using (WebClient client = new WebClient())
- {
-
- client.Encoding = System.Text.Encoding.UTF8;
-
-
- var qnamakerSubscriptionKey = Convert.ToString(ConfigurationManager.AppSettings["SUBSCRIPTION_KEY"], CultureInfo.InvariantCulture);
- client.Headers.Add("Ocp-Apim-Subscription-Key", qnamakerSubscriptionKey);
- client.Headers.Add("Content-Type", "application/json");
- responseString = client.UploadString(builder.Uri, postBody);
- }
- QnAMakerResult result = JsonConvert.DeserializeObject<QnAMakerResult>(responseString);
- return result.Answers[0].Answer;
- }
When controller invokes AnswerDialog, control will come to 'StartAsync( )' method of dialog. It will invoke 'QuestionReceivedAsync( )' method which in sequence calls 'GetAnswer( )' method with user query as parameter and waits for result.
In GetAnswer( ) method, QnA service URL will be consumed to request an answer. We can copy service URL from sample HTTP request given by QnA maker while publishing the service.
- POST /knowledgebases/{{knowledge base id}}/generateAnswer
- Host: https:
- Ocp-Apim-Subscription-Key: {{your subscription key}}
- Content-Type: application/json
- {"question":"hi"}
I have kept QnA service URL and knowledge based id as well as subscription key which is required to make post call to QnA service, in web.config
- <add key="QNA_SERVICE_URL" value="https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/{0}/generateAnswer"/>
- <add key="KNOWLEDGE_BASE_ID" value="{{your knowledge base id}}"/>
- <add key="SUBSCRIPTION_KEY" value="{{your subscription key}}"/>
Service will return result in responseString variable. We will serialize json response to get object of type QnAMakerResult and will return answer property to user.
Test
Hit F5 to test a bot application. It will host application with IIS Express and open browser. To understand the control flow, insert breakpoints in Post( ) method of MessageController and StartAsync( ) method of AnswerDialog.
Now launch bot emulator app. Put URL as 'http://localhost:{{port no. from browser url}}/api/{{controller name}}' to connect emulator with bot application. Keep App ID and App Password blank, click on connect.
Start asking questions to bot and bot will answer you with the help of QnA maker service. Happy chatting! :)
In next post, we will look into how to publish this bot application to different channels like Skype, Web Chat, Bing etc.
Read more on Microsoft Bot Framework