async Task <QnAMakerResult> 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 result = JsonConvert.DeserializeObject <QnAMakerResult>(json); //Adding internal service cfg reference [used when checking configured threshold to provide an answer] result.ServiceCfg = this.qnaInfo; return(result); } catch (JsonException ex) { throw new ArgumentException("Unable to deserialize the QnA service response.", ex); } }
async Task <QnAMakerResult> 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 result = JsonConvert.DeserializeObject <QnAMakerResult>(json); result.Score /= 100; if (result.Score >= qnaInfo.ScoreThreshold) { result.Answer = HttpUtility.HtmlDecode(result.Answer); return(result); } return(new QnAMakerResult { Answer = qnaInfo.DefaultMessage, Score = 0.0 }); } catch (JsonException ex) { throw new ArgumentException("Unable to deserialize the QnA service response.", ex); } }
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); }
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 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); } }
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); } }
Uri IQnAService.BuildRequest(string queryText, List <Metadata> strictFilters, out QnAMakerRequestBody postBody, out string authKey) { return(BuildRequestInternal(queryText, strictFilters, out postBody, out authKey)); }
Uri IQnAService.BuildRequest(string queryText, out QnAMakerRequestBody postBody, out string authKey) { return(BuildRequestInternal(queryText, null, out postBody, out authKey)); }