Support Default Fallback Intent

This commit is contained in:
Oceania2018 2018-06-11 14:48:25 -05:00
parent 4c91f6475f
commit 0de330d1d0
5 changed files with 67 additions and 25 deletions

View file

@ -6,6 +6,10 @@ namespace BotSharp.Core.Adapters.Dialogflow
{
public class DialogflowIntentResponseParameter
{
public DialogflowIntentResponseParameter()
{
PromptList = new List<DialogflowIntentResponseParameterPrompt>();
}
public string Id { get; set; }
public bool Required { get; set; }
public string DataType { get; set; }

View file

@ -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<DialogflowIntent>(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<List<DialogflowIntentExpression>>(expressionJson);
intent.UserSays = (intent.Responses[0].MessageList[0].Speech as JArray)
.Select(x => new DialogflowIntentExpression
{
Data = new List<DialogflowIntentExpressionPart>
{
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<List<DialogflowIntentExpression>>(expressionJson);
}
}
var newIntent = intent.ToObject<Intent>();
intent.Responses.ForEach(res =>

View file

@ -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<Intent>().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<Intent>().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)
/// <summary>
///
/// </summary>
/// <param name="agent"></param>
/// <param name="intentResponse"></param>
/// <param name="response"></param>
/// <param name="request"></param>
/// <returns>Required field is missed</returns>
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);

View file

@ -224,8 +224,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
</ItemGroup>
<ItemGroup>

View file

@ -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;