diff --git a/BotSharp.Core/Engines/BotSharp/BotSharpIntentClassifier.cs b/BotSharp.Core/Engines/BotSharp/BotSharpIntentClassifier.cs index 3b4caf5a..4306effa 100644 --- a/BotSharp.Core/Engines/BotSharp/BotSharpIntentClassifier.cs +++ b/BotSharp.Core/Engines/BotSharp/BotSharpIntentClassifier.cs @@ -1,16 +1,13 @@ using BotSharp.Core.Abstractions; using BotSharp.NLP; using BotSharp.NLP.Classify; -using BotSharp.NLP.Txt2Vec; using BotSharp.Platform.Models; +using BotSharp.Platform.Models.AiResponse; using BotSharp.Platform.Models.MachineLearning; using Microsoft.Extensions.Configuration; -using Newtonsoft.Json.Linq; using System; -using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; using System.Threading.Tasks; namespace BotSharp.Core.Engines.BotSharp diff --git a/BotSharp.Core/Engines/BotSharp/BotSharpTokenizer.cs b/BotSharp.Core/Engines/BotSharp/BotSharpTokenizer.cs index 835a4d59..70eebbe3 100644 --- a/BotSharp.Core/Engines/BotSharp/BotSharpTokenizer.cs +++ b/BotSharp.Core/Engines/BotSharp/BotSharpTokenizer.cs @@ -2,6 +2,7 @@ using BotSharp.NLP; using BotSharp.NLP.Tokenize; using BotSharp.Platform.Models; +using BotSharp.Platform.Models.AiResponse; using BotSharp.Platform.Models.MachineLearning; using Microsoft.Extensions.Configuration; using System; diff --git a/BotSharp.Core/Engines/NlpDoc.cs b/BotSharp.Core/Engines/NlpDoc.cs index e6155b09..17dafd33 100644 --- a/BotSharp.Core/Engines/NlpDoc.cs +++ b/BotSharp.Core/Engines/NlpDoc.cs @@ -1,5 +1,6 @@ using BotSharp.Core.Abstractions; using BotSharp.NLP.Tokenize; +using BotSharp.Platform.Models.AiResponse; using BotSharp.Platform.Models.Entities; using System; using System.Collections.Generic; diff --git a/BotSharp.Core/PlatformBuilderBase.cs b/BotSharp.Core/PlatformBuilderBase.cs index 789ef29a..9a4bf6f5 100644 --- a/BotSharp.Core/PlatformBuilderBase.cs +++ b/BotSharp.Core/PlatformBuilderBase.cs @@ -158,31 +158,15 @@ namespace BotSharp.Core if (predictedIntent.Confidence < Agent.MlConfig.MinConfidence) { - var data = new + predictedIntent = await FallbackResponse(request); + + predictedIntent.Confidence = Agent.MlConfig.MinConfidence; + predictedIntent.Label = "fallback"; + + Agent.Intents.Add(new Intent { - 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 + Name = predictedIntent.Label, + Responses = new List { new IntentResponse { @@ -191,14 +175,13 @@ namespace BotSharp.Core { new IntentResponseMessage { - Speech = "[\"" + result["text"] + "\"]", + Speech = "\"" + predictedIntent.Text + "\"", Type = AIResponseMessageType.Text } } } } - }); - } + }); } var aiResponse = new AiResponse @@ -215,6 +198,30 @@ namespace BotSharp.Core return await AssembleResult(aiResponse); } + public virtual async Task FallbackResponse(AiRequest request) + { + 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); + + return new TextClassificationResult + { + Classifier = "ownthink", + Text = result["text"].ToString() + }; + } + } + public virtual async Task AssembleResult(AiResponse response) { throw new NotImplementedException(); diff --git a/BotSharp.Platform.Abstraction/IPlatformBuilder.cs b/BotSharp.Platform.Abstraction/IPlatformBuilder.cs index 499c2e63..10fe73c0 100644 --- a/BotSharp.Platform.Abstraction/IPlatformBuilder.cs +++ b/BotSharp.Platform.Abstraction/IPlatformBuilder.cs @@ -52,5 +52,7 @@ namespace BotSharp.Platform.Abstraction Task TextRequest(AiRequest request); Task AssembleResult(AiResponse response); + + Task FallbackResponse(AiRequest request); } } diff --git a/BotSharp.Core/Engines/TextClassificationResult.cs b/BotSharp.Platform.Models/AiResponse/TextClassificationResult.cs similarity index 75% rename from BotSharp.Core/Engines/TextClassificationResult.cs rename to BotSharp.Platform.Models/AiResponse/TextClassificationResult.cs index a415ec83..dcf3fbfe 100644 --- a/BotSharp.Core/Engines/TextClassificationResult.cs +++ b/BotSharp.Platform.Models/AiResponse/TextClassificationResult.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace BotSharp.Core.Engines +namespace BotSharp.Platform.Models.AiResponse { public class TextClassificationResult { @@ -10,6 +10,8 @@ namespace BotSharp.Core.Engines public String Label { get; set; } + public string Text { get; set; } + public double Confidence { get; set; } } } diff --git a/BotSharp.WebHost/App_Data/AgentArchive/BotSharp_zh-cn.zip b/BotSharp.WebHost/App_Data/AgentArchive/BotSharp_zh-cn.zip index 972a28ef..f92eb764 100644 Binary files a/BotSharp.WebHost/App_Data/AgentArchive/BotSharp_zh-cn.zip 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 a833bf0d..cd448531 100644 --- a/BotSharp.WebHost/BotSharp.WebHost.csproj +++ b/BotSharp.WebHost/BotSharp.WebHost.csproj @@ -84,7 +84,7 @@ - + diff --git a/BotSharp.WebHost/Settings/app.json b/BotSharp.WebHost/Settings/app.json index c79a99a9..c05567cd 100644 --- a/BotSharp.WebHost/Settings/app.json +++ b/BotSharp.WebHost/Settings/app.json @@ -3,6 +3,7 @@ "assemblies": "BotSharp.Core", "platformModuleName": "DialogflowAi", + "overrideFallback": false, "modules": [ /*{ diff --git a/BotSharp.sln b/BotSharp.sln index 67bd1734..8e81b573 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Models", 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 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Channel.Weixin", "..\botsharp-channel-weixin\BotSharp.Channel.Weixin\BotSharp.Channel.Weixin.csproj", "{640AB037-2338-4550-A21A-0DB7C2A350F1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution ARTICULATE|Any CPU = ARTICULATE|Any CPU @@ -232,6 +234,30 @@ Global {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 + {640AB037-2338-4550-A21A-0DB7C2A350F1}.ARTICULATE|Any CPU.ActiveCfg = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.ARTICULATE|Any CPU.Build.0 = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.ARTICULATE|x64.ActiveCfg = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.ARTICULATE|x64.Build.0 = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Debug|x64.ActiveCfg = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Debug|x64.Build.0 = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.DIALOGFLOW|Any CPU.ActiveCfg = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.DIALOGFLOW|Any CPU.Build.0 = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.DIALOGFLOW|x64.ActiveCfg = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.DIALOGFLOW|x64.Build.0 = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.RASA|Any CPU.ActiveCfg = Release|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.RASA|Any CPU.Build.0 = Release|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.RASA|x64.ActiveCfg = Release|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.RASA|x64.Build.0 = Release|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Release|Any CPU.Build.0 = Release|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Release|x64.ActiveCfg = Release|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Release|x64.Build.0 = Release|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Test|Any CPU.ActiveCfg = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Test|Any CPU.Build.0 = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Test|x64.ActiveCfg = Debug|Any CPU + {640AB037-2338-4550-A21A-0DB7C2A350F1}.Test|x64.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE