From 0de330d1d0b36cd67e870b8d342f8fdc31b383ac Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Mon, 11 Jun 2018 14:48:25 -0500 Subject: [PATCH] Support Default Fallback Intent --- .../DialogflowIntentResponseParameter.cs | 4 ++ .../Engines/AgentImporterInDialogflow.cs | 29 ++++++++-- BotSharp.Core/Engines/RequestExtension.cs | 53 +++++++++++++------ BotSharp.UnitTest/BotSharp.UnitTest.csproj | 4 +- BotSharp.UnitTest/TestEssential.cs | 2 +- 5 files changed, 67 insertions(+), 25 deletions(-) diff --git a/BotSharp.Core/Adapters/Dialogflow/DialogflowIntentResponseParameter.cs b/BotSharp.Core/Adapters/Dialogflow/DialogflowIntentResponseParameter.cs index b0e96c4b..6b3d8103 100644 --- a/BotSharp.Core/Adapters/Dialogflow/DialogflowIntentResponseParameter.cs +++ b/BotSharp.Core/Adapters/Dialogflow/DialogflowIntentResponseParameter.cs @@ -6,6 +6,10 @@ namespace BotSharp.Core.Adapters.Dialogflow { public class DialogflowIntentResponseParameter { + public DialogflowIntentResponseParameter() + { + PromptList = new List(); + } public string Id { get; set; } public bool Required { get; set; } public string DataType { get; set; } diff --git a/BotSharp.Core/Engines/AgentImporterInDialogflow.cs b/BotSharp.Core/Engines/AgentImporterInDialogflow.cs index d0da5418..437541ef 100644 --- a/BotSharp.Core/Engines/AgentImporterInDialogflow.cs +++ b/BotSharp.Core/Engines/AgentImporterInDialogflow.cs @@ -72,7 +72,8 @@ namespace BotSharp.Core.Engines .ToList() .ForEach(fileName => { - if (!fileName.Contains("_usersays_" + agent.Language)) + if (!fileName.Contains("_usersays_" + agent.Language) + || fileName.Contains("Default Fallback Intent")) { string intentJson = File.ReadAllText($"{fileName}"); @@ -84,12 +85,30 @@ namespace BotSharp.Core.Engines var intent = JsonConvert.DeserializeObject(intentJson); // load user expressions - string expressionFileName = fileName.Replace(intent.Name, $"{intent.Name}_usersays_{agent.Language}"); - if (File.Exists(expressionFileName)) + if (fileName.Contains("Default Fallback Intent")) { - string expressionJson = File.ReadAllText($"{expressionFileName}"); - intent.UserSays = JsonConvert.DeserializeObject>(expressionJson); + intent.UserSays = (intent.Responses[0].MessageList[0].Speech as JArray) + .Select(x => new DialogflowIntentExpression + { + Data = new List + { + new DialogflowIntentExpressionPart + { + Text = x.ToString() + } + } + }).ToList(); } + else + { + string expressionFileName = fileName.Replace(intent.Name, $"{intent.Name}_usersays_{agent.Language}"); + if (File.Exists(expressionFileName)) + { + string expressionJson = File.ReadAllText($"{expressionFileName}"); + intent.UserSays = JsonConvert.DeserializeObject>(expressionJson); + } + } + var newIntent = intent.ToObject(); intent.Responses.ForEach(res => diff --git a/BotSharp.Core/Engines/RequestExtension.cs b/BotSharp.Core/Engines/RequestExtension.cs index c21c657e..7c11981d 100644 --- a/BotSharp.Core/Engines/RequestExtension.cs +++ b/BotSharp.Core/Engines/RequestExtension.cs @@ -39,7 +39,7 @@ namespace BotSharp.Core.Engines aiResponse.Timestamp = DateTime.UtcNow; var intentResponse = HandleIntentPerContextIn(rasa, request, result.Data); - HandleParameter(rasa.agent, intentResponse, response, request); + bool missedRequiredField = HandleParameter(rasa.agent, intentResponse, response, request); HandleMessage(intentResponse); @@ -113,31 +113,45 @@ namespace BotSharp.Core.Engines response.Intent }; } - response.IntentRanking = response.IntentRanking.Where(x => intents.Select(i => i.Name).Contains(x.Name)).ToList(); + response.IntentRanking = response.IntentRanking + .Where(x => x.Confidence > decimal.Parse("0.2") && intents.Select(i => i.Name).Contains(x.Name)).ToList(); + // add Default Fallback Intent if (response.IntentRanking.Count == 0) { - return null; + var defaultFallbackIntent = rasa.agent.Intents.FirstOrDefault(x => x.Name == "Default Fallback Intent"); + response.IntentRanking.Add(new RasaResponseIntent + { + Name = defaultFallbackIntent.Name, + Confidence = decimal.Parse("0.8") + }); } - else - { - response.Intent = response.IntentRanking.First(); - var intent = (dc.Table().Where(x => x.Name == response.Intent.Name) - .Include(x => x.Responses).ThenInclude(x => x.Contexts) - .Include(x => x.Responses).ThenInclude(x => x.Parameters) - .Include(x => x.Responses).ThenInclude(x => x.Messages)).First(); + response.Intent = response.IntentRanking.First(); - var intentResponse = ArrayHelper.GetRandom(intent.Responses); - intentResponse.IntentName = intent.Name; + var intent = (dc.Table().Where(x => x.AgentId == rasa.agent.Id && x.Name == response.Intent.Name) + .Include(x => x.Responses).ThenInclude(x => x.Contexts) + .Include(x => x.Responses).ThenInclude(x => x.Parameters) + .Include(x => x.Responses).ThenInclude(x => x.Messages)).First(); + + var intentResponse = ArrayHelper.GetRandom(intent.Responses); + intentResponse.IntentName = intent.Name; + + return intentResponse; - return intentResponse; - } } - private static void HandleParameter(Agent agent, IntentResponse intentResponse, RasaResponse response, AIRequest request) + /// + /// + /// + /// + /// + /// + /// + /// Required field is missed + private static bool HandleParameter(Agent agent, IntentResponse intentResponse, RasaResponse response, AIRequest request) { - if (intentResponse == null) return; + if (intentResponse == null) return false; intentResponse.Parameters.ForEach(p => { string query = request.Query.First(); @@ -169,6 +183,8 @@ namespace BotSharp.Core.Engines } } }); + + return intentResponse.Parameters.Any(x => x.Required && String.IsNullOrEmpty(x.Value)); } private static void HandleMessage(IntentResponse intentResponse) @@ -463,6 +479,9 @@ namespace BotSharp.Core.Engines var corpus = console.agent.GrabCorpus(dc); + // remove Default Fallback Intent + corpus.UserSays = corpus.UserSays.Where(x => x.Intent != "Default Fallback Intent").ToList(); + string json = JsonConvert.SerializeObject(new { rasa_nlu_data = corpus }, new JsonSerializerSettings { @@ -473,7 +492,7 @@ namespace BotSharp.Core.Engines #if RASA_NLU_0_11 rest.AddParameter("application/json", json, ParameterType.RequestBody); #else - string trainingConfig = console.agent.Language == "zh" ? "config_jieba_mitie_sklearn.yml" : "config_spacy.yml"; + string trainingConfig = console.agent.Language == "zh" ? "config_jieba_mitie_sklearn.yml" : "config_mitie_sklearn.yml"; string body = File.ReadAllText($"{Database.ContentRootPath}{Path.DirectorySeparatorChar}Settings{Path.DirectorySeparatorChar}{trainingConfig}"); body = $"{body}\r\ndata: {json}"; rest.AddParameter("application/x-yml", body, ParameterType.RequestBody); diff --git a/BotSharp.UnitTest/BotSharp.UnitTest.csproj b/BotSharp.UnitTest/BotSharp.UnitTest.csproj index 4b239e00..44a9776a 100644 --- a/BotSharp.UnitTest/BotSharp.UnitTest.csproj +++ b/BotSharp.UnitTest/BotSharp.UnitTest.csproj @@ -224,8 +224,8 @@ - - + + diff --git a/BotSharp.UnitTest/TestEssential.cs b/BotSharp.UnitTest/TestEssential.cs index 6a48021a..eff7a16e 100644 --- a/BotSharp.UnitTest/TestEssential.cs +++ b/BotSharp.UnitTest/TestEssential.cs @@ -13,7 +13,7 @@ namespace BotSharp.UnitTest public static String BOT_ID = "fd9f1b29-fed8-4c68-8fda-69ab463da126"; public static String BOT_CLIENT_TOKEN = "23a53c46d6244840bbb10c89c171d299"; public static String BOT_DEVELOPER_TOKEN = "d86103f446d049ff8d5f506e8dfe5f3f"; - public static String BOT_NAME = "VirtualAssistant"; + public static String BOT_NAME = "Voicebot"; protected Database dc { get; set; } protected string contentRoot;