Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. public string InitialMessage;
  2. private string knowledgeBaseId;
  3. private string subscriptionKey;
  4.  
  5. public override async Task StartAsync(IDialogContext context)
  6. {
  7. var type = this.GetType();
  8. var qNaServiceAttribute = type.GetCustomAttributes<QnAMakerServiceAttribute>().FirstOrDefault();
  9. subscriptionKey = qNaServiceAttribute.SubscriptionKey;
  10. knowledgeBaseId = qNaServiceAttribute.KnowledgeBaseId;
  11.  
  12.  
  13. if (!string.IsNullOrEmpty(InitialMessage))
  14. {
  15. await HandleMessage(context, InitialMessage);
  16. }
  17. else
  18. {
  19. context.Wait(MessageReceived);
  20. }
  21. }
  22.  
  23. private async Task HandleMessage(IDialogContext context, string queryText)
  24. {
  25. var response = await GetQnAMakerResponse(queryText, knowledgeBaseId, subscriptionKey);
  26.  
  27. if (HandlerByMaximumScore == null)
  28. {
  29. HandlerByMaximumScore =
  30. new Dictionary<QnAMakerResponseHandlerAttribute, QnAMakerResponseHandler>(GetHandlersByMaximumScore());
  31. }
  32.  
  33. if (response.Score == 0)
  34. {
  35. await NoMatchHandler(context, queryText);
  36. }
  37. else
  38. {
  39. var applicableHandlers = HandlerByMaximumScore.OrderBy(h => h.Key.MaximumScore).Where(h => h.Key.MaximumScore > response.Score);
  40. var handler = applicableHandlers.Any() ? applicableHandlers.First().Value : null;
  41.  
  42. if (handler != null)
  43. {
  44. await handler.Invoke(context, queryText, response);
  45. }
  46. else
  47. {
  48. await DefaultMatchHandler(context, queryText, response);
  49. }
  50. }
  51. }
  52.  
  53. private async Task<QnAMakerResult> GetQnAMakerResponse(string query, string knowledgeBaseId, string subscriptionKey)
  54. {
  55. string responseString = string.Empty;
  56.  
  57. var knowledgebaseId = knowledgeBaseId; // Use knowledge base id created.
  58. var qnamakerSubscriptionKey = subscriptionKey; //Use subscription key assigned to you.
  59.  
  60. //Build the URI
  61. Uri qnamakerUriBase = new Uri("https://westus.api.cognitive.microsoft.com/qnamaker/v1.0");
  62. var builder = new UriBuilder($"{qnamakerUriBase}/knowledgebases/{knowledgebaseId}/generateAnswer");
  63.  
  64. //Add the question as part of the body
  65. var postBody = $"{{\"question\": \"{query}\"}}";
  66.  
  67. //Send the POST request
  68. using (WebClient client = new WebClient())
  69. {
  70. //Set the encoding to UTF8
  71. client.Encoding = System.Text.Encoding.UTF8;
  72.  
  73. //Add the subscription key header
  74. client.Headers.Add("Ocp-Apim-Subscription-Key", qnamakerSubscriptionKey);
  75. client.Headers.Add("Content-Type", "application/json");
  76. responseString = client.UploadString(builder.Uri, postBody);
  77. }
  78.  
  79. //De-serialize the response
  80. QnAMakerResult response;
  81. try
  82. {
  83. response = JsonConvert.DeserializeObject<QnAMakerResult>(responseString);
  84. return response;
  85. }
  86. catch
  87. {
  88. throw new Exception("Unable to deserialize QnA Maker response string.");
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement