diff --git a/BotSharp.Core.UnitTest/AgentTest.cs b/BotSharp.Core.UnitTest/AgentTest.cs index 000057c8..71f1602c 100644 --- a/BotSharp.Core.UnitTest/AgentTest.cs +++ b/BotSharp.Core.UnitTest/AgentTest.cs @@ -51,7 +51,7 @@ namespace BotSharp.Core.UnitTest agents.ForEach(agentHeader => { var bot = new BotSharpAi(); - bot.RestoreAgent(agentHeader); + bot.RestoreAgent(); }); } diff --git a/BotSharp.Core/Abstractions/IAgentImporter.cs b/BotSharp.Core/Abstractions/IAgentImporter.cs index afa9f0f7..42540d54 100644 --- a/BotSharp.Core/Abstractions/IAgentImporter.cs +++ b/BotSharp.Core/Abstractions/IAgentImporter.cs @@ -15,9 +15,8 @@ namespace BotSharp.Core.Engines /// /// Load agent summary /// - /// /// - Agent LoadAgent(AgentImportHeader agentHeader); + Agent LoadAgent(); /// /// Load user customized entity type which defined in dictionary diff --git a/BotSharp.Core/Abstractions/IBotPlatform.cs b/BotSharp.Core/Abstractions/IBotPlatform.cs index 10eff57b..cc2a4b45 100644 --- a/BotSharp.Core/Abstractions/IBotPlatform.cs +++ b/BotSharp.Core/Abstractions/IBotPlatform.cs @@ -17,7 +17,7 @@ namespace BotSharp.Core.Engines /// Agent LoadAgent(string id); - Agent LoadAgentFromFile(string dataDir, AgentImportHeader agentHeader) where TAgentImporter : IAgentImporter, new(); + Agent LoadAgentFromFile(string dataDir) where TAgentImporter : IAgentImporter, new(); AIResponse TextRequest(AIRequest request); diff --git a/BotSharp.Core/BotSharp.Core.csproj b/BotSharp.Core/BotSharp.Core.csproj index 1c2d4850..b83e0ac7 100644 --- a/BotSharp.Core/BotSharp.Core.csproj +++ b/BotSharp.Core/BotSharp.Core.csproj @@ -43,6 +43,12 @@ TRACE;MODEL_PER_CONTEXTS + + + + + + diff --git a/BotSharp.Core/Engines/BotEngineBase.cs b/BotSharp.Core/Engines/BotEngineBase.cs index f582624d..bd0f387c 100644 --- a/BotSharp.Core/Engines/BotEngineBase.cs +++ b/BotSharp.Core/Engines/BotEngineBase.cs @@ -86,28 +86,27 @@ namespace BotSharp.Core.Engines /// Restore a agent instance from backup json files /// /// - /// /// /// - public bool RestoreAgent(AgentImportHeader agentHeader) where TAgentImporter : IAgentImporter, new() + public bool RestoreAgent() where TAgentImporter : IAgentImporter, new() { string dataDir = Path.Combine(DbInitializerPath, "Agents"); int row = dc.DbTran(() => { - LoadAgentFromFile(dataDir, agentHeader); + LoadAgentFromFile(dataDir); SaveAgent(); }); return row > 0; } - public Agent LoadAgentFromFile(string dataDir, AgentImportHeader agentHeader) where TAgentImporter : IAgentImporter, new() + public Agent LoadAgentFromFile(string dataDir) where TAgentImporter : IAgentImporter, new() { var importer = new TAgentImporter(); importer.AgentDir = dataDir; // Load agent summary - agent = importer.LoadAgent(agentHeader); + agent = importer.LoadAgent(); // Load user custom entities importer.LoadCustomEntities(agent); diff --git a/BotSharp.Core/Engines/Dialogflow/AgentImporterInDialogflow.cs b/BotSharp.Core/Engines/Dialogflow/AgentImporterInDialogflow.cs index 3b643b4e..d70eca76 100644 --- a/BotSharp.Core/Engines/Dialogflow/AgentImporterInDialogflow.cs +++ b/BotSharp.Core/Engines/Dialogflow/AgentImporterInDialogflow.cs @@ -27,10 +27,15 @@ namespace BotSharp.Core.Engines /// /// /// - public Agent LoadAgent(AgentImportHeader agentHeader) + public Agent LoadAgent() { + // load meta + string metaJson = File.ReadAllText(Path.Combine(AgentDir, "meta.json")); + + AgentImportHeader agentHeader = JsonConvert.DeserializeObject(metaJson); + // load agent profile - string data = File.ReadAllText(Path.Combine(AgentDir, "Dialogflow", $"{agentHeader.Name}{Path.DirectorySeparatorChar}agent.json")); + string data = File.ReadAllText(Path.Combine(AgentDir, "agent.json")); var agent = JsonConvert.DeserializeObject(data); agent.Name = agentHeader.Name; agent.Id = agentHeader.Id; @@ -54,7 +59,7 @@ namespace BotSharp.Core.Engines public void LoadCustomEntities(Agent agent) { agent.Entities = new List(); - string entityDir = Path.Combine(AgentDir, "Dialogflow", $"{agent.Name}{Path.DirectorySeparatorChar}entities"); + string entityDir = Path.Combine(AgentDir, "entities"); if (!Directory.Exists(entityDir)) return; Directory.EnumerateFiles(entityDir) @@ -89,7 +94,7 @@ namespace BotSharp.Core.Engines public void LoadIntents(Agent agent) { agent.Intents = new List(); - string intentDir = Path.Combine(AgentDir, "Dialogflow", $"{agent.Name}{Path.DirectorySeparatorChar}intents"); + string intentDir = Path.Combine(AgentDir, "intents"); if (!Directory.Exists(intentDir)) return; Directory.EnumerateFiles(intentDir) diff --git a/BotSharp.Core/Engines/Rasa/AgentImporterInRasa.cs b/BotSharp.Core/Engines/Rasa/AgentImporterInRasa.cs index f3811c40..6ee9d4c8 100644 --- a/BotSharp.Core/Engines/Rasa/AgentImporterInRasa.cs +++ b/BotSharp.Core/Engines/Rasa/AgentImporterInRasa.cs @@ -16,8 +16,10 @@ namespace BotSharp.Core.Engines.Rasa { public string AgentDir { get; set; } - public Agent LoadAgent(AgentImportHeader agentHeader) + public Agent LoadAgent() { + AgentImportHeader agentHeader = null; + var agent = new Agent(); agent.ClientAccessToken = Guid.NewGuid().ToString("N"); agent.DeveloperAccessToken = Guid.NewGuid().ToString("N"); diff --git a/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs b/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs index a0c40150..1338361c 100644 --- a/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs +++ b/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs @@ -25,11 +25,12 @@ namespace BotSharp.Core.Engines /// /// Load agent meta /// - /// /// /// - public Agent LoadAgent(AgentImportHeader agentHeader) + public Agent LoadAgent() { + AgentImportHeader agentHeader = null; + // load agent profile string data = File.ReadAllText(Path.Combine(AgentDir, "Sebis", $"{agentHeader.Name}{Path.DirectorySeparatorChar}agent.json")); var agent = JsonConvert.DeserializeObject(data); diff --git a/BotSharp.RestApi/AgentController.cs b/BotSharp.RestApi/AgentController.cs index 4257ddb8..41f0e6b5 100644 --- a/BotSharp.RestApi/AgentController.cs +++ b/BotSharp.RestApi/AgentController.cs @@ -46,13 +46,12 @@ namespace BotSharp.RestApi /// /// Restore a agent from a uploaded zip file /// - /// /// /// [HttpPost] - public async Task Restore(IFormFile file) + public async Task Restore(IFormFile uploadedFile) { - if (file == null || file.Length == 0) + if (uploadedFile == null || uploadedFile.Length == 0) { return BadRequest(); } @@ -61,15 +60,17 @@ namespace BotSharp.RestApi using (var stream = new FileStream(filePath, FileMode.Create)) { - await file.CopyToAsync(stream); + await uploadedFile.CopyToAsync(stream); } - string dest = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", "", DateTime.UtcNow.ToString("MMddyyyyHHmm")); + string dest = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", uploadedFile.FileName.Split('.').First(), "model_" + DateTime.UtcNow.ToString("MMddyyyyHHmm")); ZipFile.ExtractToDirectory(filePath, dest); - _platform.LoadAgentFromFile("", new AgentImportHeader { }); + System.IO.File.Delete(filePath); - return Ok(); + var agent = _platform.LoadAgentFromFile(dest); + + return Ok(agent.Id); } /// diff --git a/BotSharp.RestApi/Authentication/AuthenticationController.cs b/BotSharp.RestApi/Authentication/AuthenticationController.cs index 72df1704..8d0ee028 100644 --- a/BotSharp.RestApi/Authentication/AuthenticationController.cs +++ b/BotSharp.RestApi/Authentication/AuthenticationController.cs @@ -1,5 +1,4 @@ -using BotSharp.Core.Accounts; -using DotNetToolkit; +using DotNetToolkit; using DotNetToolkit.JwtHelper; using EntityFrameworkCore.BootKit; using Microsoft.AspNetCore.Authorization; diff --git a/BotSharp.RestApi/BotSharp.RestApi.csproj b/BotSharp.RestApi/BotSharp.RestApi.csproj index 0b253e5e..5c04d379 100644 --- a/BotSharp.RestApi/BotSharp.RestApi.csproj +++ b/BotSharp.RestApi/BotSharp.RestApi.csproj @@ -36,6 +36,12 @@ TRACE;MODE_DIALOGFLOW;RELEASE;NETCOREAPP;NETCOREAPP2_1;RELEASE;NETCOREAPP;NETCOREAPP2_1 + + + + + + diff --git a/BotSharp.RestApi/Rasa/ParseController.cs b/BotSharp.RestApi/Rasa/ParseController.cs index 8be0d301..e073a1e6 100644 --- a/BotSharp.RestApi/Rasa/ParseController.cs +++ b/BotSharp.RestApi/Rasa/ParseController.cs @@ -52,12 +52,7 @@ namespace BotSharp.RestApi.Rasa var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.Project); var modelPath = Path.Combine(projectPath, request.Model); - _platform.LoadAgentFromFile(modelPath, - new AgentImportHeader - { - Id = request.Project, - Name = request.Project - }); + _platform.LoadAgentFromFile(modelPath); var aIResponse = _platform.TextRequest(new AIRequest { diff --git a/BotSharp.RestApi/Rasa/TrainController.cs b/BotSharp.RestApi/Rasa/TrainController.cs index 861ea9db..68dbb44d 100644 --- a/BotSharp.RestApi/Rasa/TrainController.cs +++ b/BotSharp.RestApi/Rasa/TrainController.cs @@ -83,12 +83,7 @@ namespace BotSharp.RestApi.Rasa ContractResolver = new CamelCasePropertyNamesContractResolver() })); - var agent = _platform.LoadAgentFromFile(modelPath, - new AgentImportHeader - { - Id = request.Project, - Name = project - }); + var agent = _platform.LoadAgentFromFile(modelPath); var info = await trainer.Train(agent, new BotTrainOptions { Model = request.Model }); diff --git a/BotSharp.WebHost/BotSharp.WebHost.csproj b/BotSharp.WebHost/BotSharp.WebHost.csproj index 70f74239..bd2cd349 100644 --- a/BotSharp.WebHost/BotSharp.WebHost.csproj +++ b/BotSharp.WebHost/BotSharp.WebHost.csproj @@ -15,21 +15,33 @@ + + + + + + + + + + + + @@ -37,44 +49,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -88,44 +62,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -136,4 +72,8 @@ + + + + diff --git a/BotSharp.WebHost/Settings/config_jieba_mitie_sklearn.yml b/BotSharp.WebHost/Settings/config_jieba_mitie_sklearn.yml deleted file mode 100644 index 9368c2fa..00000000 --- a/BotSharp.WebHost/Settings/config_jieba_mitie_sklearn.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: "zh" - -pipeline: -- name: "nlp_mitie" - model: "data/total_word_feature_extractor_zh.dat" -- name: "tokenizer_jieba" -- name: "ner_mitie" -- name: "ner_synonyms" -- name: "intent_entity_featurizer_regex" -- name: "intent_featurizer_mitie" -- name: "intent_classifier_sklearn" diff --git a/BotSharp.WebHost/Settings/config_mitie_sklearn.yml b/BotSharp.WebHost/Settings/config_mitie_sklearn.yml deleted file mode 100644 index 5cab1429..00000000 --- a/BotSharp.WebHost/Settings/config_mitie_sklearn.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: "en" - -pipeline: -- name: "nlp_mitie" - model: "data/total_word_feature_extractor.dat" -- name: "tokenizer_mitie" -- name: "ner_mitie" -- name: "ner_synonyms" -- name: "intent_entity_featurizer_regex" -- name: "intent_featurizer_mitie" -- name: "intent_classifier_sklearn" \ No newline at end of file diff --git a/BotSharp.WebHost/Settings/config_spacy.yml b/BotSharp.WebHost/Settings/config_spacy.yml deleted file mode 100644 index bc12d3f5..00000000 --- a/BotSharp.WebHost/Settings/config_spacy.yml +++ /dev/null @@ -1,3 +0,0 @@ -language: "en" - -pipeline: "spacy_sklearn" \ No newline at end of file diff --git a/BotSharp.WebHost/SwaggerFileUploadOperation.cs b/BotSharp.WebHost/SwaggerFileUploadOperation.cs index 28349e74..2e270330 100644 --- a/BotSharp.WebHost/SwaggerFileUploadOperation.cs +++ b/BotSharp.WebHost/SwaggerFileUploadOperation.cs @@ -11,7 +11,7 @@ namespace BotSharp.WebHost { public void Apply(Operation operation, OperationFilterContext context) { - if (operation.OperationId == "V1AgentRestoreByNamePost") + if (operation.OperationId == "V1AgentRestorePost") { operation.Parameters.Clear(); @@ -19,7 +19,7 @@ namespace BotSharp.WebHost { Name = "uploadedFile", In = "formData", - Description = "Upload Zip File", + Description = "Upload Zip File, meta.json is the extra data for customized function.", Required = true, Type = "file" });