示例#1
0
        public async Task <IActionResult> UpdateKnowledgeBaseFromSrc()
        {
            var resultModel = new ActionResultModel(ActionStatus.Success, "Uplod completed");

            try
            {
                var fromFile = Request.Form.Files[0];

                if (fromFile == null || fromFile.Length == 0)
                {
                    resultModel.ResultStatus  = ActionStatus.Failed;
                    resultModel.ResultMessage = "Upload file without content";
                    return(Json(new { resultModel.ResultMessage, resultModel.ResultStatus }));
                }

                // Write file to server
                var fileName = Guid.NewGuid().ToString() + "-" + fromFile.FileName;
                var path     = Path.Combine($@"{_hostingEvn.WebRootPath}\kb-files", fileName);

                // Check file extension
                var extension = Path.GetExtension(path);
                if (string.Compare(extension.ToLower(), ".tsv") != 0)
                {
                    resultModel.ResultStatus  = ActionStatus.Failed;
                    resultModel.ResultMessage = "Upload file format not support";
                    return(Json(new { resultModel.ResultMessage, resultModel.ResultStatus }));
                }

#if DEBUG
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    await fromFile.CopyToAsync(stream);
                }
#endif

                // Upload file to azure storage
                resultModel = await _storageService.UploadToAzureStorage(_settings.AzureStorage.ConnectionString, _settings.AzureStorage.ContainerName, fileName, fromFile.OpenReadStream());

                if (resultModel.ResultStatus == ActionStatus.Failed)
                {
                    resultModel.ResultMessage = $"Upload file: {fromFile.FileName} to server failed";
                    return(Json(new { ResultMessge = resultModel.ResultMessage, resultModel.ResultStatus }));
                }

                // Import src file to knowledge base server
                resultModel = await _knowledgeBaseService.ImportKnowledgeBase(_settings.QnAMaker.SubscriptionKey, _settings.QnAMaker.KnowledgeBaseId, fromFile.FileName, resultModel.ResultMessage);

                if (resultModel.ResultStatus == ActionStatus.Failed)
                {
                    return(Json(new { ResultMessge = resultModel.ResultMessage, resultModel.ResultStatus }));
                }

                // Traning knowledge base and publish
                resultModel = await _knowledgeBaseService.PublishKnowledgeBase(_settings.QnAMaker.SubscriptionKey, _settings.QnAMaker.KnowledgeBaseId);

                // Inseret or update caches
                InsertOrUpdateCaches(fromFile.FileName, fromFile.OpenReadStream());

                return(Json(new { ResultMessge = resultModel.ResultMessage, resultModel.ResultStatus }));
            }
            catch (Exception ex)
            {
                resultModel.ResultStatus  = ActionStatus.Failed;
                resultModel.ResultMessage = ex.Message;
                return(Json(new { resultModel.ResultMessage, resultModel.ResultStatus }));
            }
        }