public async Task TestPredictionAsync(string customTextKey, string endpointUrl, string appId, IHttpHandler httpHandler, string inputText, CliException expectedException)
        {
            /* TEST NOTES
             * *************
             * we only care about prediction result
             * i.e. prediction results maps to our object correctly
             *
             * we don't care about the actual values in the object
             * because the service provider (in this case CustomText team)
             * may optimize their engine
             * rendering the values in our "ExpectedResult" object in correct
             * */
            // act
            if (expectedException == null)
            {
                var predictionService = new CustomTextPredictionService(httpHandler, customTextKey, endpointUrl, appId);
                var actualResult      = await predictionService.GetPredictionAsync(inputText);

                // validate object values aren't null
                Assert.NotNull(actualResult.Prediction.PositiveClassifiers);
                Assert.NotNull(actualResult.Prediction.Classifiers);
                Assert.NotNull(actualResult.Prediction.Extractors);
            }
            else
            {
                await Assert.ThrowsAsync(expectedException.GetType(), async() =>
                {
                    var predictionService = new CustomTextPredictionService(httpHandler, customTextKey, endpointUrl, appId);
                    await predictionService.GetPredictionAsync(inputText);
                });
            }
        }
Ejemplo n.º 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest request,
            ILogger log,
            ExecutionContext executionContext)
        {
            // some meta data
            log.LogInformation("LUIS-D Extractor and Classifier function");
            var functionName = executionContext.FunctionName;

            // input mapping
            var requestRecords = ViewModelMapper.GetRequestRecords(request);

            if (requestRecords == null)
            {
                return(new BadRequestObjectResult($"Invalid request body!"));
            }

            // extract credentials
            var credentials = CredentialsHelper.GetProjectCredentials(request.Headers);

            if (string.IsNullOrEmpty(credentials.ResourceCredentials.EndpointUrl) ||
                string.IsNullOrEmpty(credentials.ResourceCredentials.Key))
            {
                return(new BadRequestObjectResult("please provide a valid Custom Text resource endpoint and key!"));
            }

            try
            {
                // create input documents
                var documents = new Dictionary <string, string>();
                requestRecords.ToList().ForEach(record => documents.Add(record.RecordId, record.Data["text"] as string));

                // analyze custom text
                var customTextPredictionService = new CustomTextPredictionService(credentials.ResourceCredentials);
                var targetTasks = CustomTextTaskHelper.InitializeTargetTasks(credentials.Projects);
                if (targetTasks.Count == 0)
                {
                    return(new BadRequestObjectResult("please provide one or more Custom Text projects to use for document analysis!"));
                }
                var responseRecords = await customTextPredictionService.AnalyzeDocuments(documents, targetTasks);

                // create custom skillset response
                var customSkillsetResponse = ViewModelMapper.CreateResponseData(responseRecords);
                return(new OkObjectResult(customSkillsetResponse));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }
        }
Ejemplo n.º 3
0
        public async Task TestPredictionAsync(string customTextKey, string endpointUrl, string appId, string inputText, CliException expectedException)
        {
            // arrange
            var mockHttpHandler = new Mock <IHttpHandler>();

            // mock post submit prediction request
            mockHttpHandler.Setup(handler => handler.SendJsonPostRequestAsync(
                                      It.IsAny <string>(),
                                      It.IsAny <Object>(),
                                      It.IsAny <Dictionary <string, string> >(),
                                      It.IsAny <Dictionary <string, string> >()
                                      )
                                  ).Returns(Task.FromResult(GetPredictionRequestHttpResponseMessage(expectedException)));
            // mock get operation status
            mockHttpHandler.Setup(handler => handler.SendGetRequestAsync(
                                      It.Is <string>(s => s.Contains("status")),
                                      It.IsAny <Dictionary <string, string> >(),
                                      It.IsAny <Dictionary <string, string> >()
                                      )
                                  ).Returns(Task.FromResult(GetStatusHttpResponseMessage(expectedException)));
            // mock get prediction result
            mockHttpHandler.Setup(handler => handler.SendGetRequestAsync(
                                      It.Is <string>(s => !s.Contains("status")),
                                      It.IsAny <Dictionary <string, string> >(),
                                      It.IsAny <Dictionary <string, string> >()
                                      )
                                  ).Returns(Task.FromResult(GetResultHttpResponseMessage(expectedException)));

            // act
            if (expectedException == null)
            {
                var predictionService = new CustomTextPredictionService(mockHttpHandler.Object, customTextKey, endpointUrl, appId);
                var actualResult      = await predictionService.GetPredictionAsync(inputText);

                // validate object values aren't null
                Assert.NotNull(actualResult.Prediction.PositiveClassifiers);
                Assert.NotNull(actualResult.Prediction.Classifiers);
                Assert.NotNull(actualResult.Prediction.Extractors);
            }
            else
            {
                await Assert.ThrowsAsync(expectedException.GetType(), async() =>
                {
                    var predictionService = new CustomTextPredictionService(mockHttpHandler.Object, customTextKey, endpointUrl, appId);
                    await predictionService.GetPredictionAsync(inputText);
                });
            }
        }