Uri IQnAService.BuildRequest(string queryText, out QnAMakerRequestBody postBody, out string subscriptionKey) { var knowledgebaseId = this.qnaInfo.KnowledgebaseId; var builder = new UriBuilder($"{UriBase}/{knowledgebaseId}/generateanswer"); postBody = new QnAMakerRequestBody { question = queryText, top = this.qnaInfo.Top }; subscriptionKey = this.qnaInfo.SubscriptionKey; return(builder.Uri); }
async Task <QnAMakerResults> IQnAService.QueryServiceAsync(Uri uri, QnAMakerRequestBody postBody, string subscriptionKey) { string json; using (WebClient client = new WebClient()) { //Set the encoding to UTF8 client.Encoding = Encoding.UTF8; //Add the subscription key header client.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey); client.Headers.Add("Content-Type", "application/json"); json = client.UploadString(uri, JsonConvert.SerializeObject(postBody)); } try { var qnaMakerResults = JsonConvert.DeserializeObject <QnAMakerResults>(json); //Adding internal service cfg reference [used when checking configured threshold to provide an answer] qnaMakerResults.ServiceCfg = this.qnaInfo; var filteredQnAResults = new List <QnAMakerResult>(); foreach (var qnaMakerResult in qnaMakerResults.Answers) { qnaMakerResult.Score /= 100; if (qnaMakerResult.Score >= this.qnaInfo.ScoreThreshold) { qnaMakerResult.Answer = HttpUtility.HtmlDecode(qnaMakerResult.Answer); qnaMakerResult.Questions = qnaMakerResult.Questions.Select(x => HttpUtility.HtmlDecode(x)).ToList(); filteredQnAResults.Add(qnaMakerResult); } } qnaMakerResults.Answers = filteredQnAResults; return(qnaMakerResults); } catch (JsonException ex) { throw new ArgumentException("Unable to deserialize the QnA service response.", ex); } }