Uri IQnAService.BuildRequest(string queryText, out QnAMakerRequestBody postBody, out string authKey) { var knowledgebaseId = this.qnaInfo.KnowledgebaseId; Uri UriBase; // Check if the hostname was passed if (string.IsNullOrEmpty(this.qnaInfo.EndpointHostName)) { // No hostname passed, add the V2 URI UriBase = UriBaseV2; // V2 subacription Key authKey = this.qnaInfo.AuthKey.Trim(); } else { // Hostname was passed, build the V4 endpoint URI string hostName = this.qnaInfo.EndpointHostName.ToLower(); // Remove https if (hostName.Contains("https://")) { hostName = hostName.Split('/')[2]; } // Remove qnamaker if (hostName.Contains("qnamaker")) { hostName = hostName.Split('/')[0]; } // Trim any trailing / hostName = hostName.TrimEnd('/'); // Create the V4 Uri based on the hostname UriBase = new Uri("https://" + hostName + "/qnamaker/knowledgebases"); // Check if key has endpoint in it if (this.qnaInfo.AuthKey.ToLower().Contains("endpointkey")) { authKey = this.qnaInfo.AuthKey.Trim(' '); } else { authKey = "EndpointKey " + this.qnaInfo.AuthKey.Trim(' '); } } var builder = new UriBuilder($"{UriBase}/{knowledgebaseId}/generateanswer"); postBody = new QnAMakerRequestBody { question = queryText, top = this.qnaInfo.Top }; return(builder.Uri); }
async Task <QnAMakerResults> IQnAService.QueryServiceAsync(Uri uri, QnAMakerRequestBody postBody, string authKey) { string json = string.Empty; using (HttpClient client = new HttpClient()) { //Add the key header according to V2 or V4 format if (authKey.ToLower().Contains("endpointkey")) { client.DefaultRequestHeaders.Add("Authorization", authKey); } else { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", authKey); } var response = await client.PostAsync(uri, new StringContent(JsonConvert.SerializeObject(postBody), Encoding.UTF8, "application/json")); if (response != null && response.Content != null) { json = await response.Content.ReadAsStringAsync(); } } 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); } }