Exemplo n.º 1
0
        private QnAQuery Ask(string question)
        {
            QnAQuery query = new QnAQuery();

            query.Question = question;

            string requestUri = string.Format("{0}{1}{2}{3}",
                                              "qnamaker/v2.0/",
                                              "knowledgebases/",
                                              _knowledgeBase,
                                              "/generateAnswer");

            RestRequest request = new RestRequest(requestUri, Method.POST);

            request.AddJsonBody(query);
            request.AddHeader("Ocp-Apim-Subscription-Key", _ocpApimSubscriptionKey);

            RestClient client = new RestClient(_baseURL);
            IRestResponse <QnAResponse> response = client.Execute <QnAResponse>(request);

            if (response.IsSuccessful)
            {
                query.Answer = response.Data.Answers[0].Answer;
            }

            return(query);
        }
Exemplo n.º 2
0
        private static async Task <QnAQuery> QueryQnABot(string Query)
        {
            QnAQuery QnAQueryResult = new QnAQuery();

            using (System.Net.Http.HttpClient client =
                       new System.Net.Http.HttpClient())
            {
                string RequestURI = String.Format("{0}{1}{2}{3}{4}",
                                                  @"https://westus.api.cognitive.microsoft.com/",
                                                  @"qnamaker/v1.0/",
                                                  @"knowledgebases/",
                                                  _KnowledgeBase,
                                                  @"/generateAnswer");
                var httpContent =
                    new StringContent($"{{\"question\": \"{Query}\"}}",
                                      Encoding.UTF8, "application/json");
                httpContent.Headers.Add(
                    "Ocp-Apim-Subscription-Key", _OcpApimSubscriptionKey);
                System.Net.Http.HttpResponseMessage msg =
                    await client.PostAsync(RequestURI, httpContent);

                if (msg.IsSuccessStatusCode)
                {
                    var JsonDataResponse =
                        await msg.Content.ReadAsStringAsync();

                    QnAQueryResult =
                        JsonConvert.DeserializeObject <QnAQuery>(JsonDataResponse);
                }
            }
            return(QnAQueryResult);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public async Task <ActionResult> About(string NewQuestion, string NewAnswer)
        {
            QnAQuery objQnAResult = new QnAQuery();

            objQnAResult.Message = "";
            try
            {
                if (this.Request.ContentLength > 0)
                {
                    if (Request.Form["AddEntry"].Count() > 0)
                    {
                        // El agregar entrada se ejecuta
                        if ((NewQuestion != null) && (NewAnswer != null))
                        {
                            // Llamamos a los servicios del qna
                            objQnAResult.Message =
                                await UpdateQueryQnABot(NewQuestion, NewAnswer, Mode.Add);

                            objQnAResult.Message =
                                await TrainAndPublish();

                            var strFAQ = await GetFAQ();
                        }
                    }
                    else if (Request.Form["DeleteEntry"].Count() > 0)
                    {
                        // Eliminar entrada se ejecuta
                        if ((NewQuestion != null) && (NewAnswer != null))
                        {
                            // Llamamos a los servicios del qna
                            objQnAResult.Message =
                                await UpdateQueryQnABot(NewQuestion, NewAnswer, Mode.Delete);

                            objQnAResult.Message =
                                await TrainAndPublish();

                            var strFAQ = await GetFAQ();
                        }
                    }
                }
                return(View(objQnAResult));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex);
                return(View(objQnAResult));
            }
        }
Exemplo n.º 5
0
        public async Task <ActionResult> Index(string searchString)
        {
            QnAQuery objQnAResult = new QnAQuery();

            try
            {
                if (searchString != null)
                {
                    objQnAResult = await QueryQnABot(searchString);
                }
                return(View(objQnAResult));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex);
                return(View(objQnAResult));
            }
        }
Exemplo n.º 6
0
        public async Task <bool> UpdateKbAsync(string question, string answer)
        {
            QnAQuery objQnAResult = new QnAQuery();

            objQnAResult.Message = "";
            try
            {
                string _newQ = question;
                string _newA = string.Empty;
                _newA = answer;
                objQnAResult.Message = await UpdateQueryQnABot(_newQ, _newA, Mode.Add);

                objQnAResult.Message = await TrainAndPublish();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Exemplo n.º 7
0
        public async Task <ActionResult> Index(string searchString, string NewQuestion, string NewAnswer)
        {
            QnAQuery objQnAResult = new QnAQuery();

            objQnAResult.Message = "";
            try
            {
                if (searchString != null)
                {
                    objQnAResult = await QueryQnABot(searchString);

                    Console.WriteLine("Pregunta: " + searchString);
                    String answer = objQnAResult.Answer;
                    Console.WriteLine("Respuesta: " + answer);
                    if (answer.Equals("No good match found in the KB"))
                    {
                        objQnAResult.Answer = "No tengo esa información a la mano, pero te contactaré con un ingeniero especializado en eso :)";
                        answer = "No tengo esa información a la mano, pero te contactaré con un ingeniero especializado en eso :)";
                    }
                    String link  = "";
                    String link2 = "";
                    int    index = answer.IndexOf("&lt;");
                    if (index > 0)
                    {
                        // Tiene imagen
                        link   = answer.Substring(index + 4);
                        answer = answer.Remove(index);
                    }
                    else
                    {
                        index = answer.IndexOf("&gt;");
                        if (index > 0)
                        {
                            // Tiene video
                            link2  = answer.Substring(index + 4);
                            answer = answer.Remove(index);
                        }
                    }
                    Program.questions.Add(searchString);
                    Program.answers.Add(answer);
                    Program.images.Add(link);
                    Program.videos.Add(link2);
                }
                if (this.Request.ContentLength > 0)
                {
                    if (Request.Form["AddEntry"].Count() > 0)
                    {
                        // El boton de agregar entrada fue presionado
                        if ((NewQuestion != null) && (NewAnswer != null))
                        {
                            // LLamamos al servicio de qna
                            objQnAResult.Message =
                                await UpdateQueryQnABot(NewQuestion, NewAnswer, Mode.Add);

                            objQnAResult.Message =
                                await TrainAndPublish();

                            var strFAQ = await GetFAQ();
                        }
                    }
                    else if (Request.Form["DeleteEntry"].Count() > 0)
                    {
                        // El boton de eliminar entrada fue presionado
                        if ((NewQuestion != null) && (NewAnswer != null))
                        {
                            // LLamamos al servicio de qna
                            objQnAResult.Message =
                                await UpdateQueryQnABot(NewQuestion, NewAnswer, Mode.Delete);

                            objQnAResult.Message =
                                await TrainAndPublish();

                            var strFAQ = await GetFAQ();
                        }
                    }
                }
                return(View(objQnAResult));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex);
                return(View(objQnAResult));
            }
        }
Exemplo n.º 8
0
        public IActionResult Index(QnAQuery query)
        {
            QnAQuery result = Ask(query.Question);

            return(View(result));
        }
Exemplo n.º 9
0
        public IActionResult Index()
        {
            QnAQuery model = new QnAQuery();

            return(View(model));
        }