public async Task <QNAResponse> GetAnswerAsync(string question) { string responseString = string.Empty; //Build the URI Uri qnamakerUriBase = new Uri("https://westus.api.cognitive.microsoft.com/qnamaker/v1.0"); var builder = new UriBuilder($"{qnamakerUriBase}/knowledgebases/{knowledgebaseId}/generateAnswer"); //Add the question as part of the body var postBody = $"{{\"question\": \"{question}\"}}"; //Send the POST request using (WebClient client = new WebClient()) { //Add the subscription key header client.Headers.Add("Ocp-Apim-Subscription-Key", qnamakerSubscriptionKey); client.Headers.Add("Content-Type", "application/json"); responseString = await client.UploadStringTaskAsync(builder.Uri, postBody); } QNAResponse response = await JSONHelper.DeSerializeJSON <QNAResponse>(responseString); return(response); }
// Pass question and get top answer public async static Task <string> GetAnswer(string question) { var uri = endpoint_host + endpointService + baseRoute + "/" + kbid + "/generateAnswer"; //Console.WriteLine("Get answers " + uri + "."); using (var client = new HttpClient()) using (var request = new HttpRequestMessage()) { request.Method = HttpMethod.Post; request.RequestUri = new Uri(uri); request.Content = new StringContent("{question:'" + question + "'}", Encoding.UTF8, "application/json"); // NOTE: The value of the header contains the string/text 'EndpointKey ' with the trailing space request.Headers.Add("Authorization", "EndpointKey " + endpoint_key); var response = await client.SendAsync(request); var responseBody = await response.Content.ReadAsStringAsync(); //Console.WriteLine(PrettyPrint(responseBody)); QNAResponse qnaResponse = JsonConvert.DeserializeObject <QNAResponse>(responseBody); return(qnaResponse.answers[0].answer); //return responseBody. } }
public async Task FAQS(IDialogContext context, LuisResult result) { QNAService qNa = new QNAService(); QNAResponse reponse = await qNa.GetAnswerAsync(result.Query); await context.PostAsync(reponse.Answer); context.Wait(this.MessageReceived); }