load entity and synonym for agent.

This commit is contained in:
haiping008@gmail.com 2018-04-30 17:06:52 -05:00
parent d511d84226
commit 3e0fb9b647
4 changed files with 73 additions and 45 deletions

View file

@ -75,6 +75,7 @@ namespace BotSharp.Core.Engines
{
return dc.Table<Agent>()
.Include(x => x.Intents).ThenInclude(x => x.Contexts)
.Include(x => x.Entities).ThenInclude(x => x.Entries).ThenInclude(x => x.Synonyms)
.FirstOrDefault(x => x.ClientAccessToken == AiConfig.ClientAccessToken || x.DeveloperAccessToken == AiConfig.ClientAccessToken);
}

View file

@ -78,43 +78,10 @@ namespace BotSharp.Core.Engines
aiResponse.Status = new AIResponseStatus { };
aiResponse.SessionId = rasa.AiConfig.SessionId;
aiResponse.Timestamp = DateTime.UtcNow;
intentResponse.Parameters.ForEach(p => {
string query = request.Query.First();
var entity = response.Entities.FirstOrDefault(x => x.Entity == p.Name);
if(entity != null)
{
p.Value = query.Substring(entity.Start, entity.End - entity.Start);
}
// fixed entity per request
if(request.Entities != null)
{
var fixedEntity = request.Entities.FirstOrDefault(x => x.Name == p.Name);
if (fixedEntity != null)
{
if (query.ToLower().Contains(fixedEntity.Entries.First().Value.ToLower()))
{
p.Value = fixedEntity.Entries.First().Value;
}
}
}
});
intentResponse.Messages = intentResponse.Messages.OrderBy(x => x.UpdatedTime).ToList();
intentResponse.Messages.ToList()
.ForEach(msg =>
{
if (msg.Type == AIResponseMessageType.Custom)
{
}
else
{
msg.Speech = msg.Speech.StartsWith("[") ?
ArrayHelper.GetRandom(msg.Speech.Substring(2, msg.Speech.Length - 4).Split("\",\"").ToList()) :
msg.Speech;
}
});
HandleParameter(rasa.agent, intentResponse, response, request);
HandleMessage(intentResponse);
aiResponse.Result = new AIResponseResult
{
@ -144,6 +111,66 @@ namespace BotSharp.Core.Engines
}
};
HandleContext(dc, rasa, intentResponse, aiResponse);
return aiResponse;
}
private static void HandleParameter(Agent agent, IntentResponse intentResponse, RasaResponse response, AIRequest request)
{
intentResponse.Parameters.ForEach(p => {
string query = request.Query.First();
var entity = response.Entities.FirstOrDefault(x => x.Entity == p.Name);
if (entity != null)
{
p.Value = query.Substring(entity.Start, entity.End - entity.Start);
}
// fixed entity per request
if (request.Entities != null)
{
var fixedEntity = request.Entities.FirstOrDefault(x => x.Name == p.Name);
if (fixedEntity != null)
{
if (query.ToLower().Contains(fixedEntity.Entries.First().Value.ToLower()))
{
p.Value = fixedEntity.Entries.First().Value;
}
}
}
// convert to Standard entity value
if (!String.IsNullOrEmpty(p.Value) && !p.DataType.StartsWith("@sys."))
{
p.Value = agent.Entities.FirstOrDefault(x => x.Name == p.Name).Entries.FirstOrDefault((entry) => {
return entry.Value.ToLower() == p.Value.ToLower() ||
entry.Synonyms.Select(synonym => synonym.Synonym.ToLower()).Contains(p.Value.ToLower());
})?.Value;
}
});
}
private static void HandleMessage(IntentResponse intentResponse)
{
intentResponse.Messages = intentResponse.Messages.OrderBy(x => x.UpdatedTime).ToList();
intentResponse.Messages.ToList()
.ForEach(msg =>
{
if (msg.Type == AIResponseMessageType.Custom)
{
}
else
{
msg.Speech = msg.Speech.StartsWith("[") ?
ArrayHelper.GetRandom(msg.Speech.Substring(2, msg.Speech.Length - 4).Split("\",\"").ToList()) :
msg.Speech;
}
});
}
private static void HandleContext(Database dc, RasaAi rasa, IntentResponse intentResponse, AIResponse aiResponse)
{
// Merge context lifespan
// override if exists, otherwise add, delete if lifespan is zero
dc.DbTran(() =>
@ -186,8 +213,6 @@ namespace BotSharp.Core.Engines
.Where(x => x.SessionId == rasa.AiConfig.SessionId)
.Select(x => new AIContext { Name = x.Context.ToLower(), Lifespan = x.Lifespan })
.ToArray();
return aiResponse;
}
private static IRestResponse<RasaResponse> CallRasa(string projectId, string text, string model)

View file

@ -13,10 +13,18 @@ namespace BotSharp.Core.Intents
[Required]
[StringLength(36)]
public String IntentResponseId { get; set; }
public bool Required { get; set; }
[MaxLength(32)]
public string DataType { get; set; }
[MaxLength(64)]
public string Name { get; set; }
[MaxLength(128)]
public string Value { get; set; }
public bool IsList { get; set; }
}
}

View file

@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.UnitTest", "BotSha
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Core", "BotSharp.Core\BotSharp.Core.csproj", "{95780673-2A1A-4953-962F-C46CBFDD07FF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetToolkit", "..\DotNetToolkit\DotNetToolkit\DotNetToolkit.csproj", "{AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -23,10 +21,6 @@ Global
{95780673-2A1A-4953-962F-C46CBFDD07FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95780673-2A1A-4953-962F-C46CBFDD07FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95780673-2A1A-4953-962F-C46CBFDD07FF}.Release|Any CPU.Build.0 = Release|Any CPU
{AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE