예제 #1
0
        public async Task <IHttpActionResult> TrainNewModel(CancellationToken cancellationToken, [FromBody] ModelParameters modelParameters)
        {
            // validate input
            if (modelParameters == null)
            {
                var message = $"Invalid format. Expected a valid '{nameof(ModelParameters)}' JSON";
                Trace.TraceVerbose(message);
                return(BadRequest(message));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ModelsRegistry modelsRegistry = WebAppContext.ModelsRegistry;

            Trace.TraceVerbose("Converting the model parameters to trainer settings, using default values where needed");
            var @default = ModelTrainingParameters.Default;
            var settings = new ModelTrainingParameters
            {
                BlobContainerName                     = modelParameters.BlobContainerName,
                CatalogFileRelativeLocation           = modelParameters.CatalogFileRelativeLocation?.Replace('\\', '/'),
                UsageFolderRelativeLocation           = modelParameters.UsageFolderRelativeLocation?.Replace('\\', '/'),
                EvaluationUsageFolderRelativeLocation = modelParameters.EvaluationUsageFolderRelativeLocation?.Replace('\\', '/'),
                SupportThreshold                = modelParameters.SupportThreshold ?? @default.SupportThreshold,
                CooccurrenceUnit                = modelParameters.CooccurrenceUnit ?? @default.CooccurrenceUnit,
                SimilarityFunction              = modelParameters.SimilarityFunction ?? @default.SimilarityFunction,
                EnableColdItemPlacement         = modelParameters.EnableColdItemPlacement ?? @default.EnableColdItemPlacement,
                EnableColdToColdRecommendations =
                    modelParameters.EnableColdToColdRecommendations ?? @default.EnableColdToColdRecommendations,
                EnableUserAffinity = modelParameters.EnableUserAffinity ?? @default.EnableUserAffinity,
                EnableUserToItemRecommendations =
                    modelParameters.EnableUserToItemRecommendations ?? @default.EnableUserToItemRecommendations,
                AllowSeedItemsInRecommendations =
                    modelParameters.AllowSeedItemsInRecommendations ?? @default.AllowSeedItemsInRecommendations,
                EnableBackfilling = modelParameters.EnableBackfilling ?? @default.EnableBackfilling,
                DecayPeriodInDays = modelParameters.DecayPeriodInDays ?? @default.DecayPeriodInDays
            };

            Trace.TraceInformation("Creating new model in registry");
            Model model = await modelsRegistry.CreateModelAsync(settings, modelParameters.Description, cancellationToken);

            Trace.TraceInformation($"Queueing a new train model message to the queue for model id {model.Id}");
            ModelQueueMessage modelQueueMessage = new ModelQueueMessage {
                ModelId = model.Id
            };
            await WebAppContext.TrainModelQueue.AddMessageAsync(modelQueueMessage, cancellationToken);

            // return the URL to the created model
            return(CreatedAtRoute(nameof(GetModel), new { modelId = model.Id }, model));
        }
예제 #2
0
        public async Task CreateModelAsyncTest()
        {
            ITable table          = Substitute.For <ITable>();
            var    modelsRegistry = new ModelsRegistry(table);

            table.InsertOrReplaceEntityAsync(Arg.Any <ModelIdTableEntity>(), CancellationToken.None).Returns(Task.FromResult(true));
            table.InsertEntityAsync(Arg.Any <ModelTableEntity>(), CancellationToken.None).Returns(Task.FromResult(true));

            Model model = await modelsRegistry.CreateModelAsync(ModelTrainingParameters.Default, null, CancellationToken.None);

            await table.Received(1).InsertEntityAsync(
                Arg.Is <ModelTableEntity>(me => Guid.Parse(me.RowKey) == model.Id && me.ModelStatus == ModelStatus.Created.ToString()),
                CancellationToken.None);
        }