Пример #1
0
        /// <summary>
        /// Add a single QnA pair to the database, and call EFQnARepository to
        /// add the pair to knowledgebase on QnAMaker.ai
        /// </summary>
        /// <param name="qna">the QnA to be added</param>
        /// <returns>true if added, false if not</returns>
        public async Task <bool> AddSingleQnAPairToBaseAsync(QnATrainBase qna)
        {
            var r = await qnaRepository.AddSingleQnAPairAsync(qna);

            if (r)
            {
                QnAPairs pair = new QnAPairs
                {
                    Query           = qna.Query,
                    Answer          = qna.Answer,
                    Dep             = qna.Dep,
                    KnowledgeBaseId = qna.KnowledgeBaseId,
                    Trained         = true,
                    TrainedDate     = DateTime.Now,
                    Published       = false,
                    PublishingType  = "add"
                };

                await db.AddAsync(pair);

                await db.SaveChangesAsync();

                return(true);
            }
            else
            {
                return(false);
            }
        }
        public async Task <IActionResult> EditAndStoreMessagePairToBot([FromForm][Bind("Question", "Answer", "Dep", "ConversationId")] QnAEditAndStoreMessagePairToBot qna)
        {
            var c = await chatbotRepository.GetConversationByIdAsync(qna.ConversationId);

            var knowledgebase = await chatbotRepository.GetQnAKnowledgeBaseAsync(c.KnowledgebaseId);

            var qnabot = await chatbotRepository.GetQnABaseClassById(knowledgebase.QnABotId);

            var QnA = new QnATrainBase
            {
                Query             = qna.Question,
                Answer            = qna.Answer,
                Dep               = qna.Type,
                QnABotName        = qnabot.chatbotName,
                KnowledgeBaseId   = knowledgebase.QnAKnowledgeBaseId,
                KnowledgeBaseName = knowledgebase.QnAKnowledgeName,
                SubscriptionKey   = qnabot.subscriptionKey
            };
            var message = await chatbotRepository.AddSingleQnAPairToBaseAsync(QnA);

            if (message)
            {
                TempData["success"] = String.Format("QnA paret er lagt til kunnskapsbase {0}", knowledgebase.QnAKnowledgeName);
                return(RedirectToAction("ViewConversationDetails", new { id = qna.ConversationId }));
            }
            else
            {
                TempData["error"] = String.Format("Klarte ikke å legge QnA til kunnskapsbase {0}", knowledgebase.QnAKnowledgeName);
                return(RedirectToAction("ViewConversationDetails", new { id = qna.ConversationId }));
            }
        }
        /// <summary>
        /// Add a single QnA pair to the knowledgebase given to the QnATrainBase
        /// </summary>
        /// <param name="qna">QnATrainBase qna</param>
        /// <returns>true if added, false if not</returns>
        public async Task <bool> AddSingleQnAPairAsync(QnATrainBase qna)
        {
            var c = await Task.Run(() => db.QnABaseClass.FirstOrDefault(X => X.subscriptionKey == qna.SubscriptionKey));

            var b = await Task.Run(() => db.QnAKnowledgeBase.FirstOrDefault(X => X.QnAKnowledgeBaseId == qna.KnowledgeBaseId));

            var client = new HttpClient();

            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", c.subscriptionKey);

            string pair = "{'add': {'qnaPairs': [{'answer': '" + qna.Answer + "','question': '" + qna.Query + "'}]},}";

            HttpResponseMessage response;

            var uri = b.AddQnAPairUrl;

            var         method  = new HttpMethod("PATCH");
            HttpContent content = new StringContent(pair, Encoding.UTF8, "application/json");
            var         request = new HttpRequestMessage(method, uri)
            {
                Content = content
            };

            response = await client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public async Task <IActionResult> AddQnAPairsToBaseAsync([FromForm][Bind("Query", "Answer", "Dep", "SubscriptionKey", "KnowledgeBaseId", "QnABotName", "KnowledgeBaseName")] QnATrainBase qna)
        {
            var r = await chatbotRepository.AddSingleQnAPairToBaseAsync(qna);

            if (r)
            {
                TempData["success"] = String.Format("Kunnskapsbase med navn {0} er oppdatert med nytt QnA par!", qna.QnABotName);
                return(RedirectToAction("AddQnAPairsToBaseAsync"));
            }
            else
            {
                TempData["error"] = String.Format("Kunnskapsbase med navn {0} er ikke oppdatert med nytt QnA par!", qna.QnABotName);
                return(RedirectToAction("AddQnAPairsToBaseAsync"));
            }
        }
        public async Task <IActionResult> AddQnAPairsToBaseAsync()
        {
            var q = await chatbotRepository.GetActiveQnABaseClassAsync();

            var b = await chatbotRepository.GetActiveQnAKnowledgeBaseAsync();

            var t = new QnATrainBase
            {
                KnowledgeBaseId   = b.QnAKnowledgeBaseId,
                KnowledgeBaseName = b.QnAKnowledgeName,
                SubscriptionKey   = q.subscriptionKey,
                QnABotName        = q.chatbotName
            };

            return(View(t));
        }