Example #1
0
        public void GetInternalResourcesAsyncTestsShouldReturnProperResult()
        {
            //arrange
            PagedResources pagedResources = new PagedResources()
            {
                Results = resourcesData, ContinuationToken = "[]", TopicIds = topicIds
            };
            var topicResponse = topicsResourcesBusinessLogic.GetTopicsAsync(keyword, location);

            topicResponse.ReturnsForAnyArgs(topicsData);
            var resourceCount = topicsResourcesBusinessLogic.GetResourcesCountAsync(resourceFilter);

            resourceCount.ReturnsForAnyArgs <dynamic>(allResourcesCount);
            var paginationResult = topicsResourcesBusinessLogic.ApplyPaginationAsync(resourceFilter);

            paginationResult.ReturnsForAnyArgs <dynamic>(pagedResources);
            luisInput.Location = new Location {
                State = "AK"
            };

            //act
            var result = luisBusinessLogic.GetInternalResourcesAsync(keyword, luisInput, Arg.Any <IEnumerable <string> >()).Result;

            result = JsonConvert.SerializeObject(result);

            //assert
            Assert.Contains(expectedTopicId, result);
        }
Example #2
0
        public async Task <IActionResult> GetTopics([FromQuery] string state)
        {
            var topics = await topicsResourcesBusinessLogic.GetTopicsAsync(state);

            if (topics == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound));
            }

            return(Ok(topics));
        }
        public async Task <dynamic> GetInternalResourcesAsync(string keyword, LuisInput luisInput, IEnumerable <string> relevantIntents)
        {
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo    textInfo    = cultureInfo.TextInfo;
            Location    location    = luisInput.Location;

            var topics = await topicsResourcesBusinessLogic.GetTopicsAsync(textInfo.ToTitleCase(keyword), location);

            List <string> topicIds = new List <string>();

            foreach (var item in topics)
            {
                string topicId = item.id;
                topicIds.Add(topicId);
            }

            if (topicIds.Count == 0 || location == null)
            {
                return(new LuisViewModel
                {
                    TopIntent = keyword
                });
            }

            ResourceFilter resourceFilter = new ResourceFilter {
                TopicIds = topicIds, PageNumber = 0, ResourceType = Constants.All, Location = location
            };
            var            GetResourcesTask   = topicsResourcesBusinessLogic.GetResourcesCountAsync(resourceFilter);
            ResourceFilter sortResourceFilter = resourceFilter;

            sortResourceFilter.IsOrder      = true;
            sortResourceFilter.OrderByField = luisInput.OrderByField;
            sortResourceFilter.OrderBy      = luisInput.OrderBy;
            var ApplyPaginationTask = topicsResourcesBusinessLogic.ApplyPaginationAsync(sortResourceFilter);

            //To get guided assistant id
            resourceFilter.ResourceType = Constants.GuidedAssistant;
            var GetGuidedAssistantId = topicsResourcesBusinessLogic.ApplyPaginationAsync(resourceFilter);
            await Task.WhenAll(GetResourcesTask, ApplyPaginationTask, GetGuidedAssistantId);

            var            groupedResourceType     = GetResourcesTask.Result;
            PagedResources resources               = ApplyPaginationTask.Result;
            PagedResources guidedAssistantResponse = GetGuidedAssistantId.Result;
            var            guidedAssistantResult   = guidedAssistantResponse != null?JsonUtilities.DeserializeDynamicObject <GuidedAssistant>(guidedAssistantResponse.Results.FirstOrDefault()) : null;

            dynamic searchFilter = new JObject();

            searchFilter.OrderByField = resourceFilter.OrderByField;
            searchFilter.OrderBy      = resourceFilter.OrderBy;
            return(new LuisViewModel
            {
                TopIntent = keyword,
                RelevantIntents = relevantIntents != null?JsonUtilities.DeserializeDynamicObject <dynamic>(relevantIntents) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                      Topics = topics != null?JsonUtilities.DeserializeDynamicObject <dynamic>(topics) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                   Resources = resources != null?JsonUtilities.DeserializeDynamicObject <dynamic>(resources.Results) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                                   ContinuationToken = resources != null && resources.ContinuationToken != null?JsonConvert.DeserializeObject(resources.ContinuationToken) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                                                           TopicIds = topicIds != null?JsonUtilities.DeserializeDynamicObject <dynamic>(topicIds) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                                                                          ResourceTypeFilter = groupedResourceType != null?JsonUtilities.DeserializeDynamicObject <dynamic>(groupedResourceType) : JsonConvert.DeserializeObject(Constants.EmptyArray),
                                                                                                                                   GuidedAssistantId = guidedAssistantResult != null ? guidedAssistantResult.CuratedExperienceId : string.Empty,
                                                                                                                                   SearchFilter = searchFilter
            });
        }
        public async Task <dynamic> GetAnswersAsync(string inputQuestion, bool isLuisCallRequired = true)
        {
            //@TODO Need to make changes as per AI's are trained..
            dynamic answerObject = null;
            string  bestAnswer   = "Sorry, not able to get you. Please explain your problem in detail.";

            string[] input = inputQuestion.Split("|");
            if (!isLuisCallRequired)
            {
                //dynamic luisResponse = await luisProxy.GetIntents(question);
                //IntentWithScore luisTopIntents = luisBusinessLogic.ParseLuisIntent(luisResponse);
                //if (luisTopIntents != null && luisBusinessLogic.IsIntentAccurate(luisTopIntents))
                //{
                //    question = luisTopIntents.TopScoringIntent;
                //}
            }
            string question       = input[0];
            var    requestContent = JsonConvert.SerializeObject(new { question });

            using (var request = new HttpRequestMessage())
            {
                // set request data.
                request.Method     = HttpMethod.Post;
                request.RequestUri = new Uri(string.Format(CultureInfo.InvariantCulture, qnAMakerSettings.Endpoint.OriginalString, qnAMakerSettings.KnowledgeId));
                request.Content    = new StringContent(requestContent, Encoding.UTF8, "application/json");
                request.Headers.Add("Authorization", "EndpointKey " + qnAMakerSettings.AuthorizationKey);

                var response = await httpClientService.SendAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    string qnaResponse = response.Content.ReadAsStringAsync().Result;
                    var    qnAMakers   = JsonConvert.DeserializeObject <QnAMakerResult>(qnaResponse);
                    if (qnAMakers.Answers.LastOrDefault().Score > 0)
                    {
                        bestAnswer = qnAMakers.Answers.OrderBy(answer => answer.Score).LastOrDefault().Answer;
                    }
                }
                //if (bestAnswer.Contains("{") && bestAnswer.Contains("}"))
                if (bestAnswer.Contains("|"))
                {
                    string[]    keyword     = bestAnswer.Split("|");
                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo    textInfo    = cultureInfo.TextInfo;
                    Location    location    = new Location {
                        State = input[1]
                    };
                    var topics = await topicsResourcesBusinessLogic.GetTopicsAsync(textInfo.ToTitleCase(keyword[1]), location);

                    if (topics != null && topics.Count > 0)
                    {
                        string topicId = topics[0].id;
                        answerObject = new JObject {
                            { "description", bestAnswer }, { "topic", "topics/" + topicId }
                        };
                    }
                    else
                    {
                        bestAnswer   = "We couldn't find internal resources, please try our web search";
                        answerObject = new JObject {
                            { "description", bestAnswer }
                        };
                    }
                    //answerObject = JsonConvert.DeserializeObject(bestAnswer);
                }
                else
                {
                    answerObject = new JObject {
                        { "description", bestAnswer }
                    };
                }
                return(answerObject.ToString());
            }
        }