diff --git a/BotSharp.Core/BotSharp.Core.csproj b/BotSharp.Core/BotSharp.Core.csproj index 127c3644..1b75e69e 100644 --- a/BotSharp.Core/BotSharp.Core.csproj +++ b/BotSharp.Core/BotSharp.Core.csproj @@ -75,7 +75,7 @@ If you feel that this project is helpful to you, please Star on the project, we - + diff --git a/BotSharp.Core/Engines/BotPredictor.cs b/BotSharp.Core/Engines/BotPredictor.cs index 5c27007c..356852ff 100644 --- a/BotSharp.Core/Engines/BotPredictor.cs +++ b/BotSharp.Core/Engines/BotPredictor.cs @@ -58,7 +58,7 @@ namespace BotSharp.Core.Engines // pipe process - var pipelines = config.GetValue($"{meta.BotEngine}:pipe") + var pipelines = config.GetValue($"{meta.BotEngine}_{agent.Language}:pipe") .Split(',') .Select(x => x.Trim()) .ToList(); @@ -66,7 +66,7 @@ namespace BotSharp.Core.Engines for(int pipeIdx = 0; pipeIdx < pipelines.Count; pipeIdx++) { var pipe = TypeHelper.GetInstance(pipelines[pipeIdx], assemblies) as INlpPredict; - pipe.Configuration = config.GetSection(meta.BotEngine).GetSection(pipelines[pipeIdx]); + pipe.Configuration = config.GetSection($"{meta.BotEngine}_{agent.Language}").GetSection(pipelines[pipeIdx]); pipe.Settings = settings; var pipeModel = meta.Pipeline.FirstOrDefault(x => x.Name == pipelines[pipeIdx]); await pipe.Predict(agent, data, pipeModel); diff --git a/BotSharp.Core/Engines/BotTrainer.cs b/BotSharp.Core/Engines/BotTrainer.cs index 3833ff20..abc0b5de 100644 --- a/BotSharp.Core/Engines/BotTrainer.cs +++ b/BotSharp.Core/Engines/BotTrainer.cs @@ -45,9 +45,9 @@ namespace BotSharp.Core.Engines var assemblies = (string[])AppDomain.CurrentDomain.GetData("Assemblies"); var platform = config.GetSection($"platformModuleName").Value; var engine = this.settings.BotEngine; - string providerName = config.GetSection($"{engine}:Provider").Value; + string providerName = config.GetSection($"{engine}_{agent.Language}:Provider").Value; var provider = TypeHelper.GetInstance(providerName, assemblies) as INlpProvider; - provider.Configuration = config.GetSection(engine); + provider.Configuration = config.GetSection($"{engine}_{agent.Language}"); var pipeModel = new PipeModel { diff --git a/BotSharp.Core/Modules/ModulesStartup.cs b/BotSharp.Core/Modules/ModulesStartup.cs index 16ccc2e6..c6d462ee 100644 --- a/BotSharp.Core/Modules/ModulesStartup.cs +++ b/BotSharp.Core/Modules/ModulesStartup.cs @@ -34,7 +34,14 @@ namespace BotSharp.Core.Modules if (options.Modules.Count == 0) { + options.Modules.Add(new ModuleOptions + { + Name = "DialogflowAi", + Type = "BotSharp.Platform.Dialogflow" + }); + Console.WriteLine($"Platform emulator not found.", Color.Red); + Console.WriteLine($"Using default emulator: DialogflowAi, BotSharp.Platform.Dialogflow."); } this._modules = options.Modules diff --git a/BotSharp.Core/PlatformBuilderBase.cs b/BotSharp.Core/PlatformBuilderBase.cs index fbd30c42..789ef29a 100644 --- a/BotSharp.Core/PlatformBuilderBase.cs +++ b/BotSharp.Core/PlatformBuilderBase.cs @@ -4,13 +4,16 @@ using BotSharp.Platform.Models; using BotSharp.Platform.Models.AiRequest; using BotSharp.Platform.Models.AiResponse; using BotSharp.Platform.Models.Entities; +using BotSharp.Platform.Models.Intents; using BotSharp.Platform.Models.MachineLearning; using DotNetToolkit; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -151,13 +154,53 @@ namespace BotSharp.Core var preditor = new BotPredictor(); var doc = await preditor.Predict(Agent, request); - if (doc.Sentences[0].Entities == null) - { - doc.Sentences[0].Entities = new List(); - } - var predictedIntent = doc.Sentences[0].Intent; + if (predictedIntent.Confidence < Agent.MlConfig.MinConfidence) + { + var data = new + { + token = "openbot", + info = request.Text + }; + + using (var client = new HttpClient()) + { + var response = await client.PostAsync( + "https://api.ownthink.com/bot", + new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")); + var content = await response.Content.ReadAsStringAsync(); + var result = JsonConvert.DeserializeObject(content); + + predictedIntent = new TextClassificationResult + { + Confidence = Agent.MlConfig.MinConfidence, + Classifier = "ownthink", + Label = "fallback" + }; + + Agent.Intents.Add(new Intent + { + Name = predictedIntent.Label, + Responses = new List + { + new IntentResponse + { + IntentName = predictedIntent.Label, + Messages = new List + { + new IntentResponseMessage + { + Speech = "[\"" + result["text"] + "\"]", + Type = AIResponseMessageType.Text + } + } + } + } + }); + } + } + var aiResponse = new AiResponse { ResolvedQuery = request.Text, diff --git a/BotSharp.Platform.Models/AgentBase.cs b/BotSharp.Platform.Models/AgentBase.cs index 7da90b26..9aa424c9 100644 --- a/BotSharp.Platform.Models/AgentBase.cs +++ b/BotSharp.Platform.Models/AgentBase.cs @@ -1,4 +1,6 @@ -using System; +using BotSharp.Platform.Models.Intents; +using BotSharp.Platform.Models.MachineLearning; +using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text; @@ -10,6 +12,7 @@ namespace BotSharp.Platform.Models public AgentBase() { CreatedDate = DateTime.UtcNow; + Intents = new List(); } /// @@ -42,5 +45,33 @@ namespace BotSharp.Platform.Models public DateTime CreatedDate { get; set; } public TrainingCorpus Corpus { get; set; } + + public AgentMlConfig MlConfig { get; set; } + + public List Integrations { get; set; } + + public Boolean Published { get; set; } + + /// + /// Only access text/ audio rquest + /// + [StringLength(32)] + public String ClientAccessToken { get; set; } + + /// + /// Developer can access more APIs + /// + [StringLength(32)] + public String DeveloperAccessToken { get; set; } + + public List Intents { get; set; } + + public String Birthday + { + get + { + return CreatedDate.ToShortDateString(); + } + } } } diff --git a/BotSharp.Platform.Models/Intents/Intent.cs b/BotSharp.Platform.Models/Intents/Intent.cs index c033a902..ea5ae3f3 100644 --- a/BotSharp.Platform.Models/Intents/Intent.cs +++ b/BotSharp.Platform.Models/Intents/Intent.cs @@ -1,4 +1,5 @@ -using System; +using BotSharp.Platform.Models.MachineLearning; +using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -39,5 +40,9 @@ namespace BotSharp.Platform.Models.Intents [ForeignKey("IntentId")] public List Responses { get; set; } + + public AgentMlConfig MlConfig { get; set; } + + public List Integrations { get; set; } } } diff --git a/BotSharp.Platform.Models/Intents/IntentResponse.cs b/BotSharp.Platform.Models/Intents/IntentResponse.cs index 717ddf3b..95f4630f 100644 --- a/BotSharp.Platform.Models/Intents/IntentResponse.cs +++ b/BotSharp.Platform.Models/Intents/IntentResponse.cs @@ -8,6 +8,13 @@ namespace BotSharp.Platform.Models.Intents { public class IntentResponse { + public IntentResponse() + { + Parameters = new List(); + Contexts = new List(); + Messages = new List(); + } + /// /// Guid /// diff --git a/BotSharp.WebHost/App_Data/AgentArchive/BotSharp_zh-cn.zip b/BotSharp.WebHost/App_Data/AgentArchive/BotSharp_zh-cn.zip new file mode 100644 index 00000000..972a28ef Binary files /dev/null and b/BotSharp.WebHost/App_Data/AgentArchive/BotSharp_zh-cn.zip differ diff --git a/BotSharp.WebHost/BotSharp.WebHost.csproj b/BotSharp.WebHost/BotSharp.WebHost.csproj index a145174e..a833bf0d 100644 --- a/BotSharp.WebHost/BotSharp.WebHost.csproj +++ b/BotSharp.WebHost/BotSharp.WebHost.csproj @@ -84,6 +84,7 @@ + @@ -91,6 +92,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest @@ -100,7 +104,7 @@ PreserveNewest - + PreserveNewest diff --git a/BotSharp.WebHost/Settings/BotSharpNLU.json b/BotSharp.WebHost/Settings/BotSharpNLU_en.json similarity index 93% rename from BotSharp.WebHost/Settings/BotSharpNLU.json rename to BotSharp.WebHost/Settings/BotSharpNLU_en.json index 24d0c694..fbdb107d 100644 --- a/BotSharp.WebHost/Settings/BotSharpNLU.json +++ b/BotSharp.WebHost/Settings/BotSharpNLU_en.json @@ -1,7 +1,5 @@ { - "botSharpNLU": { - "lang": "en", - + "botSharpNLU_en": { "provider": "BotSharpProvider", "botSharpProvider": { diff --git a/BotSharp.WebHost/Settings/BotSharpNLU_zh-cn.json b/BotSharp.WebHost/Settings/BotSharpNLU_zh-cn.json new file mode 100644 index 00000000..537f0c5b --- /dev/null +++ b/BotSharp.WebHost/Settings/BotSharpNLU_zh-cn.json @@ -0,0 +1,27 @@ +{ + "botSharpNLU_zh-cn": { + "provider": "BotSharpProvider", + + "botSharpProvider": { + }, + + "pipe": "BotSharpTokenizer, BotSharpTagger, BotSharpNER, BotSharpIntentClassifier", + + "botSharpTokenizer": { + "tokenizer": "JiebaTokenizer" + }, + + "botSharpIntentClassifier": { + "classifer": "NaiveBayesClassifier", + "wordvecModel": "|App_Data|wordvec_enu.bin" + }, + + "botSharpTagger": { + "tagger": "JiebaTagger" + }, + + "botSharpNER": { + "template": "|App_Data|CRFLite/template.en" + } + } +} diff --git a/BotSharp.sln b/BotSharp.sln index 0dd12b6c..67bd1734 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -22,6 +22,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Abstracti EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Models", "BotSharp.Platform.Models\BotSharp.Platform.Models.csproj", "{C4F2EAE5-F2C7-4F52-9DB2-7E76D7080C72}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Dialogflow", "..\botsharp-dialogflow\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj", "{6A7E5DC1-3AE3-42C1-8F67-3131783646EB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution ARTICULATE|Any CPU = ARTICULATE|Any CPU @@ -206,6 +208,30 @@ Global {C4F2EAE5-F2C7-4F52-9DB2-7E76D7080C72}.Test|Any CPU.Build.0 = Release|Any CPU {C4F2EAE5-F2C7-4F52-9DB2-7E76D7080C72}.Test|x64.ActiveCfg = Release|Any CPU {C4F2EAE5-F2C7-4F52-9DB2-7E76D7080C72}.Test|x64.Build.0 = Release|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.ARTICULATE|Any CPU.ActiveCfg = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.ARTICULATE|Any CPU.Build.0 = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.ARTICULATE|x64.ActiveCfg = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.ARTICULATE|x64.Build.0 = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Debug|x64.ActiveCfg = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Debug|x64.Build.0 = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.DIALOGFLOW|Any CPU.ActiveCfg = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.DIALOGFLOW|Any CPU.Build.0 = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.DIALOGFLOW|x64.ActiveCfg = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.DIALOGFLOW|x64.Build.0 = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.RASA|Any CPU.ActiveCfg = Release|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.RASA|Any CPU.Build.0 = Release|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.RASA|x64.ActiveCfg = Release|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.RASA|x64.Build.0 = Release|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Release|Any CPU.Build.0 = Release|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Release|x64.ActiveCfg = Release|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Release|x64.Build.0 = Release|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Test|Any CPU.ActiveCfg = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Test|Any CPU.Build.0 = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Test|x64.ActiveCfg = Debug|Any CPU + {6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Test|x64.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE