This commit is contained in:
Bo Peng 2018-06-12 16:14:46 -05:00
commit 30424f7b6d
13 changed files with 128 additions and 34 deletions

View file

@ -6,11 +6,18 @@ 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; }
public string DefaultValue { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public bool IsList { get; set; }
public List<DialogflowIntentResponseParameterPrompt> PromptList { get; set; }
}
}

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.Adapters.Dialogflow
{
public class DialogflowIntentResponseParameterPrompt
{
public string Lang { get; set; }
public string Value { get; set; }
}
}

View file

@ -1,7 +1,6 @@
using BotSharp.Core.Adapters.Rasa;
using BotSharp.Core.Engines;
using BotSharp.Core.Entities;
using BotSharp.Core.Expressions;
using BotSharp.Core.Intents;
using BotSharp.Core.Models;
using EntityFrameworkCore.BootKit;
@ -83,7 +82,7 @@ namespace BotSharp.Core.Agents
var intents = dc.Table<Intent>()
.Include(x => x.Contexts)
.Include(x => x.UserSays).ThenInclude(say => say.Data)
.Where(x => x.UserSays.Count > 0)
.Where(x => x.AgentId == agent.Id && x.UserSays.Count > 0)
.ToList();
intents.ForEach(intent =>

View file

@ -72,23 +72,43 @@ 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}");
// avoid confict data structure
intentJson = intentJson.Replace("\"contexts\":", "\"contextList\":");
intentJson = intentJson.Replace("\"messages\":", "\"messageList\":");
intentJson = intentJson.Replace("\"prompts\":", "\"promptList\":");
var intent = JsonConvert.DeserializeObject<DialogflowIntent>(intentJson);
intent.Name = intent.Name.Replace("/","_");
// 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 =>
@ -124,6 +144,13 @@ namespace BotSharp.Core.Engines
}
}).ToList();
newResponse.Parameters = res.Parameters.Select(p =>
{
var rp = p.ToObject<IntentResponseParameter>();
rp.Prompts = p.PromptList.Select(pl => new ResponseParameterPrompt { Prompt = pl.Value }).ToList();
return rp;
}).ToList();
});
newIntent.Contexts = intent.ContextList.Select(x => new IntentInputContext { Name = x }).ToList();

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

@ -13,13 +13,18 @@ namespace BotSharp.Core.Intents
{
public static Intent GetIntent(this IBotEngine bot, Database dc, string intentId)
{
return dc.Table<Intent>()
var intent = dc.Table<Intent>()
.Include(x => x.Contexts)
.Include(x => x.Responses).ThenInclude(x => x.Contexts)
.Include(x => x.Responses).ThenInclude(x => x.Parameters)
.Include(x => x.Responses).ThenInclude(x => x.Parameters).ThenInclude(x => x.Prompts)
.Include(x => x.Responses).ThenInclude(x => x.Messages)
.Include(x => x.UserSays).ThenInclude(x => x.Data)
.FirstOrDefault(x => x.Id == intentId);
// order parts by time
intent.UserSays.ForEach(x => x.Data = x.Data.OrderBy(d => d.UpdatedTime).ToList());
return intent;
}
public static String CreateIntent(this Agent agent, Database dc, Intent intent)

View file

@ -1,5 +1,4 @@
using BotSharp.Core.Expressions;
using EntityFrameworkCore.BootKit;
using EntityFrameworkCore.BootKit;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

View file

@ -5,7 +5,7 @@ using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace BotSharp.Core.Expressions
namespace BotSharp.Core.Intents
{
[Table("Bot_IntentExpressionPart")]
public class IntentExpressionPart : DbRecord, IDbRecord

View file

@ -19,6 +19,9 @@ namespace BotSharp.Core.Intents
[MaxLength(32)]
public string DataType { get; set; }
[MaxLength(128)]
public string DefaultValue { get; set; }
[MaxLength(64)]
public string Name { get; set; }
@ -26,5 +29,8 @@ namespace BotSharp.Core.Intents
public string Value { get; set; }
public bool IsList { get; set; }
[ForeignKey("ResponseParameterId")]
public List<ResponseParameterPrompt> Prompts { get; set; }
}
}

View file

@ -0,0 +1,20 @@
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace BotSharp.Core.Intents
{
[Table("Bot_ResponseParameterPrompt")]
public class ResponseParameterPrompt : DbRecord, IDbRecord
{
[Required]
[StringLength(36)]
public String ResponseParameterId { get; set; }
[MaxLength(256)]
public string Prompt { get; set; }
}
}

View file

@ -1,5 +1,4 @@
using BotSharp.Core.Expressions;
using System;
using System;
using System.Collections.Generic;
using System.Text;

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;