Esempio n. 1
0
        public async Task <QnAAnswer> GetQnAAnswer(string query)
        {
            var qnaUrl             = _configuration.GetValue <string>("QnAPAth");
            var qnaAuthHeaderValue = _configuration.GetValue <string>("QnAAuthHeaderKeyValue");
            var body = new QnAQuery();

            body.question = query;

            var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

            //_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("EndpointKey", qnaAuthHeaderValue);
            HttpRequestMessage msg = new HttpRequestMessage();

            msg.Content               = content;
            msg.RequestUri            = new System.Uri(qnaUrl);
            msg.Headers.Authorization = new AuthenticationHeaderValue("EndpointKey", qnaAuthHeaderValue);
            msg.Method = HttpMethod.Post;

            try {
                HttpResponseMessage result = await _client.SendAsync(msg);

                QnAAnswer ret = JsonConvert.DeserializeObject <QnAAnswer>(await result.Content.ReadAsStringAsync());

                return(ret);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(null);
        }
Esempio n. 2
0
        public async void TrainService(QnAAnswer answer, string lastQuestion)
        {
            var client = new HttpClient();

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ServiceKeys.qnamakerSubscriptionKey);

            var uri = $"https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/{ServiceKeys.knowledgebaseId}/train";

            HttpResponseMessage response;

            // Request params
            string lastAnswerQ = answer.Questions[0];
            string lastAnswerA = answer.Answer;

            string body = "{\"feedbackRecords\": [{\"userId\": \"" + Guid.NewGuid() + "\",\"userQuestion\": \"" + lastQuestion + "\",\"kbQuestion\": \"" + lastAnswerQ + "\",\"kbAnswer\": \"" + lastAnswerA + "\"}]}";

            // Request body
            byte[] byteData = Encoding.UTF8.GetBytes(body);

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpRequestMessage req = new HttpRequestMessage(new HttpMethod("PATCH"), uri);
                req.Content = content;
                response    = await client.SendAsync(req);
            }
        }
        public async Task NoneIntent(IDialogContext context, LuisResult result)
        {
            QnAAnswer qnaAnswer = await cogSvc.AskWineChatbotFaqAsync(result.Query);

            string message =
                qnaAnswer.Score == 0 ?
                @"Sorry, I didn't get that. " + ExampleText :
                HttpUtility.HtmlDecode(qnaAnswer.Answer);

            message = await TranslateResponseAsync(context, message);

            await context.PostAsync(message);

            context.Wait(MessageReceived);
        }
Esempio n. 4
0
        private async Task HandleQuestionWorkflow(IDialogContext context, Microsoft.Bot.Connector.Activity activity, Data.Course course, QuestionModel questionModel)
        {
            var       predictiveQnAService = new PredictiveQnAService(course.Id);
            QnAAnswer response             = await predictiveQnAService.GetAnswer(questionModel.QuestionText);

            // if result, check confidence level
            if (response != null && response.Score > Convert.ToDouble(course.PredictiveQnAConfidenceThreshold))
            {
                await HandleBotAnswerWorkflow(context, activity, questionModel, response, questionModel.TeamId);
            }
            else
            {
                // a score of 0 or a score below threshold both result in regular workflow
                await HandleTagAdminWorkflow(activity, questionModel, questionModel.TeamId, course.Id);
            }
        }
Esempio n. 5
0
        private async Task HandleBotAnswerWorkflow(IDialogContext context, Microsoft.Bot.Connector.Activity activity, QuestionModel questionModel, QnAAnswer response, string teamId)
        {
            var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            // above threshold
            // post answer card with helpful/not-helpful

            // Set question state
            questionModel.QuestionStatus = Constants.QUESTION_STATUS_UNANSWERED;

            // Save question
            var questionId = SQLService.CreateOrUpdateQuestion(questionModel);

            var attachment = CreateBotAnswerCard(response.Id, response.Answer, response.Score, questionId, questionModel.OriginalPoster.Email);

            var reply = activity.CreateReply();

            reply.Attachments.Add(attachment);
            await context.PostAsync(reply);
        }