/// <summary> /// Get Ask Question Results from QnA Maker Search Result /// </summary> public static AskQuestionResult[] GetAskQuestionResults(QnAMakerSearchResult searchResult, double confidenceScore, bool isAnswerJson = true) { AskQuestionResult[] results = searchResult.Answers .Where(a => a.Score >= confidenceScore) .Take(SettingsUtility.MaxResultsCount) .Select(a => isAnswerJson == true ? JsonConvert.DeserializeObject <AskQuestionResult>(WebUtility.HtmlDecode(a.Answer)) : new AskQuestionResult() { Answer = WebUtility.HtmlDecode(a.Answer) }) .ToArray(); return(results); }
public static async Task <AskQuestionResponse> AnswerQuestion(AskQuestionRequest request, ICollector <SessionTableEntity> sessionTable) { // Get unique identifier string id = Guid.NewGuid().ToString(); DateTime timestampUtc = DateTime.UtcNow; // Run keyphrases extraction request.Topics = await ServicesUtility.GetTopics(request.Question, request.Topics); // Run search services string queryWithTopics = request.Topics?.Count() > 0 ? string.Join(" ", request.Topics).Trim() : request.Question; // Process results AskQuestionResponse response = new AskQuestionResponse() { Id = id, Results = new AskQuestionResult[0] }; double confidenceScoreThreshold = 20; // Get results from QnA maker QnAMakerSearchResult qnaResult = await ServicesUtility.QnAMaker.GenerateAnswerAsync(queryWithTopics, SettingsUtility.QnAMakerWhoTaskKnowledgebaseId); if (qnaResult.Answers?.Where(a => a.Score >= confidenceScoreThreshold).Count() > 0) { response.Results = ServicesUtility.GetAskQuestionResults(qnaResult, confidenceScoreThreshold, true); } else { // Create Bing Search tasks BingWebSearchResult bingWebSearchResult = await ServicesUtility.BingSearch.SearchWebAsync(query : request.Question, count : SettingsUtility.MaxResultsCount); // Process Bing search results if (bingWebSearchResult.WebPagesResult?.Values?.Count() > 0) { response.Results = ServicesUtility.GetAskQuestionResults(bingWebSearchResult); } } // Write to session table sessionTable.Add(new SessionTableEntity(id, timestampUtc, "Who", request, response)); // Return response return(response); }
/// <summary> /// Generate answer from QnA Maker service /// </summary> public async Task <QnAMakerSearchResult> GenerateAnswerAsync(string query, string knowledgebaseId) { if (string.IsNullOrEmpty(query) == true) { return(default(QnAMakerSearchResult)); } // Get request uri string requestUrl = Path.Combine(this.BaseServiceUrl, "knowledgebases", knowledgebaseId, "generateAnswer"); Uri requestUri = new Uri(requestUrl); // Create content QnAMakerSearchRequest content = new QnAMakerSearchRequest() { Question = query }; // Post request QnAMakerSearchResult result = await HttpClientUtility.PostAsJsonAsync <QnAMakerSearchResult>(requestUri, this.RequestHeaders, content); return(result); }