add entity and synonym api
This commit is contained in:
parent
c235108d29
commit
083f2640ad
|
|
@ -14,7 +14,7 @@ namespace BotSharp.Core.Adapters.Dialogflow
|
|||
|
||||
public List<String> RawSynonyms { get; set; }
|
||||
|
||||
public List<EntityEntrySynonym> Synonyms { get; set; }
|
||||
public List<EntrySynonym> Synonyms { get; set; }
|
||||
|
||||
public DialogflowEntityEntry()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ namespace BotSharp.Core.Agents
|
|||
|
||||
[ForeignKey("AgentId")]
|
||||
[JsonProperty("entity_types")]
|
||||
public List<Entity> Entities { get; set; }
|
||||
public List<EntityType> Entities { get; set; }
|
||||
|
||||
public String Birthday
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using BotSharp.Core.Adapters.Rasa;
|
||||
using BotSharp.Core.Engines;
|
||||
using BotSharp.Core.Entities;
|
||||
using BotSharp.Core.Expressions;
|
||||
using BotSharp.Core.Intents;
|
||||
|
|
@ -12,22 +13,49 @@ using System.Text;
|
|||
|
||||
namespace BotSharp.Core.Agents
|
||||
{
|
||||
public static class AgentExtension
|
||||
public static class AgentDriver
|
||||
{
|
||||
/// <summary>
|
||||
/// Get agent header row from Agent table
|
||||
/// </summary>
|
||||
/// <param name="dc"></param>
|
||||
/// <param name="agentId"></param>
|
||||
/// <returns></returns>
|
||||
public static Agent Agent(this Database dc, string agentId)
|
||||
public static Agent LoadAgent(this IBotEngine engine, Database dc, AIConfiguration aiConfig)
|
||||
{
|
||||
return dc.Table<Agent>().Find(agentId);
|
||||
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);
|
||||
}
|
||||
|
||||
public static String CreateEntity(this Agent agent, Entity entity, Database dc)
|
||||
/// <summary>
|
||||
/// Restore a agent instance from backup json files
|
||||
/// </summary>
|
||||
/// <param name="importor"></param>
|
||||
/// <param name="agentId"></param>
|
||||
/// <returns></returns>
|
||||
public static Agent RestoreAgent(this IBotEngine engine, IAgentImporter importer, String agentId, string dataDir)
|
||||
{
|
||||
return entity.Id;
|
||||
// Load agent summary
|
||||
var agent = importer.LoadAgent(agentId, dataDir);
|
||||
|
||||
// Load agent entities
|
||||
importer.LoadEntities(agent, dataDir);
|
||||
|
||||
// Load agent intents
|
||||
importer.LoadIntents(agent, dataDir);
|
||||
|
||||
return agent;
|
||||
}
|
||||
|
||||
public static String SaveAgent(this Agent agent, Database dc)
|
||||
{
|
||||
var existedAgent = dc.Table<Agent>().FirstOrDefault(x => x.Id == agent.Id || x.Name == agent.Name);
|
||||
if (existedAgent == null)
|
||||
{
|
||||
dc.Table<Agent>().Add(agent);
|
||||
return agent.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
agent.Id = existedAgent.Id;
|
||||
return existedAgent.Id;
|
||||
}
|
||||
}
|
||||
|
||||
public static RasaTrainingData GrabCorpus(this Agent agent, Database dc)
|
||||
|
|
@ -77,9 +105,9 @@ namespace BotSharp.Core.Agents
|
|||
// assemble entity synonmus
|
||||
if (!trainingData.Entities.Any(y => y.EntityType == x.Alias && y.EntityValue == x.Text))
|
||||
{
|
||||
var allSynonyms = (from e in dc.Table<Entity>()
|
||||
var allSynonyms = (from e in dc.Table<EntityType>()
|
||||
join ee in dc.Table<EntityEntry>() on e.Id equals ee.EntityId
|
||||
join ees in dc.Table<EntityEntrySynonym>() on ee.Id equals ees.EntityEntryId
|
||||
join ees in dc.Table<EntrySynonym>() on ee.Id equals ees.EntityEntryId
|
||||
where e.Name == x.Alias && ee.Value == x.Text & ees.Synonym != x.Text
|
||||
select ees.Synonym).ToList();
|
||||
|
||||
|
|
@ -163,9 +191,9 @@ namespace BotSharp.Core.Agents
|
|||
// assemble entity synonmus
|
||||
if (!trainingData.Entities.Any(y => y.EntityType == x.Alias && y.EntityValue == x.Text))
|
||||
{
|
||||
var allSynonyms = (from e in dc.Table<Entity>()
|
||||
var allSynonyms = (from e in dc.Table<EntityType>()
|
||||
join ee in dc.Table<EntityEntry>() on e.Id equals ee.EntityId
|
||||
join ees in dc.Table<EntityEntrySynonym>() on ee.Id equals ees.EntityEntryId
|
||||
join ees in dc.Table<EntrySynonym>() on ee.Id equals ees.EntityEntryId
|
||||
where e.Name == x.Alias && ee.Value == x.Text & ees.Synonym != x.Text
|
||||
select ees.Synonym ).ToList();
|
||||
|
||||
|
|
@ -29,7 +29,7 @@ namespace BotSharp.Core.Engines
|
|||
|
||||
public void LoadEntities(Agent agent, string agentDir)
|
||||
{
|
||||
agent.Entities = new List<Entity>();
|
||||
agent.Entities = new List<EntityType>();
|
||||
string entityDir = $"{agentDir}{Path.DirectorySeparatorChar}Dialogflow{Path.DirectorySeparatorChar}{agent.Name}{Path.DirectorySeparatorChar}entities";
|
||||
if (!Directory.Exists(entityDir)) return;
|
||||
|
||||
|
|
@ -50,13 +50,13 @@ namespace BotSharp.Core.Engines
|
|||
string entriesJson = File.ReadAllText($"{entriesFileName}");
|
||||
entriesJson = entriesJson.Replace("\"synonyms\":", "\"rawSynonyms\":");
|
||||
entity.Entries = JsonConvert.DeserializeObject<List<DialogflowEntityEntry>>(entriesJson);
|
||||
entity.Entries.ForEach(x => x.Synonyms = x.RawSynonyms.Select(s => new EntityEntrySynonym
|
||||
entity.Entries.ForEach(x => x.Synonyms = x.RawSynonyms.Select(s => new EntrySynonym
|
||||
{
|
||||
Synonym = s
|
||||
}).ToList());
|
||||
}
|
||||
|
||||
agent.Entities.Add(entity.ToObject<Entity>());
|
||||
agent.Entities.Add(entity.ToObject<EntityType>());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
10
BotSharp.Core/Engines/IBotEngine.cs
Normal file
10
BotSharp.Core/Engines/IBotEngine.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.Core.Engines
|
||||
{
|
||||
public interface IBotEngine
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ namespace BotSharp.Core.Engines
|
|||
/// <summary>
|
||||
/// Rasa nlu 0.11.x
|
||||
/// </summary>
|
||||
public class RasaAi
|
||||
public class RasaAi : IBotEngine
|
||||
{
|
||||
public Database dc { get; set; }
|
||||
public AIConfiguration AiConfig { get; set; }
|
||||
|
|
@ -35,61 +35,8 @@ namespace BotSharp.Core.Engines
|
|||
this.dc = dc;
|
||||
|
||||
AiConfig = aiConfig;
|
||||
agent = LoadAgent();
|
||||
agent = this.LoadAgent(dc, aiConfig);
|
||||
aiConfig.DevMode = agent.DeveloperAccessToken == aiConfig.ClientAccessToken;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restore a agent instance from backup json files
|
||||
/// </summary>
|
||||
/// <param name="importor"></param>
|
||||
/// <param name="agentId"></param>
|
||||
/// <returns></returns>
|
||||
public Agent RestoreAgent(IAgentImporter importer, String agentId, string dataDir)
|
||||
{
|
||||
// Load agent summary
|
||||
agent = importer.LoadAgent(agentId, dataDir);
|
||||
|
||||
// Load agent entities
|
||||
importer.LoadEntities(agent, dataDir);
|
||||
|
||||
// Load agent intents
|
||||
importer.LoadIntents(agent, dataDir);
|
||||
|
||||
return agent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dump agent train data to json file
|
||||
/// </summary>
|
||||
/// <param name="agentId"></param>
|
||||
/// <returns></returns>
|
||||
public bool DumpAgent(String agentId)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public Agent LoadAgent()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
public String SaveAgent(Agent agent)
|
||||
{
|
||||
var existedAgent = dc.Table<Agent>().FirstOrDefault(x => x.Id == agent.Id || x.Name == agent.Name);
|
||||
if (existedAgent == null)
|
||||
{
|
||||
dc.Table<Agent>().Add(agent);
|
||||
return agent.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
agent.Id = existedAgent.Id;
|
||||
return existedAgent.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,6 @@ namespace BotSharp.Core.Entities
|
|||
public String Value { get; set; }
|
||||
|
||||
[ForeignKey("EntityEntryId")]
|
||||
public List<EntityEntrySynonym> Synonyms { get; set; }
|
||||
public List<EntrySynonym> Synonyms { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
28
BotSharp.Core/Entities/EntityEntryDriver.cs
Normal file
28
BotSharp.Core/Entities/EntityEntryDriver.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using BotSharp.Core.Agents;
|
||||
using EntityFrameworkCore.BootKit;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.Core.Entities
|
||||
{
|
||||
public static class EntityEntryDriver
|
||||
{
|
||||
public static void CreateEntity(this Agent agent, Database dc, EntityType entity)
|
||||
{
|
||||
dc.Table<EntityType>().Add(entity);
|
||||
}
|
||||
|
||||
public static void DeleteEntity(this Agent agent, Database dc, string entityId)
|
||||
{
|
||||
var entity = dc.Table<EntityType>().Find(entityId);
|
||||
dc.Table<EntityType>().Remove(entity);
|
||||
}
|
||||
|
||||
public static void UpdateEntity(this Agent agent, Database dc, EntityType entity)
|
||||
{
|
||||
var oldEntity = dc.Table<EntityType>().Find(entity.Id);
|
||||
oldEntity.Name = entity.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,8 +7,8 @@ using System.Text;
|
|||
|
||||
namespace BotSharp.Core.Entities
|
||||
{
|
||||
[Table("Bot_Entity")]
|
||||
public class Entity : DbRecord, IDbRecord
|
||||
[Table("Bot_EntityType")]
|
||||
public class EntityType : DbRecord, IDbRecord
|
||||
{
|
||||
[Required]
|
||||
[StringLength(36)]
|
||||
41
BotSharp.Core/Entities/EntityTypeDriver.cs
Normal file
41
BotSharp.Core/Entities/EntityTypeDriver.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
using BotSharp.Core.Agents;
|
||||
using EntityFrameworkCore.BootKit;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.Core.Entities
|
||||
{
|
||||
public static class EntityTypeDriver
|
||||
{
|
||||
public static string CreateEntityType(this Agent agent, Database dc, EntityType entityType)
|
||||
{
|
||||
if (dc.Table<EntityType>().Any(x => x.Name == entityType.Name && x.AgentId == agent.Id)) return agent.Id;
|
||||
|
||||
dc.Table<EntityType>().Add(entityType);
|
||||
|
||||
return entityType.Id;
|
||||
}
|
||||
|
||||
public static void UpdateEntityType(this Agent agent, Database dc, String entityTypeId, EntityType entityType)
|
||||
{
|
||||
var existedEntityType = dc.Table<EntityType>().Find(entityTypeId);
|
||||
if (entityType == null) return;
|
||||
|
||||
existedEntityType.Name = entityType.Name;
|
||||
existedEntityType.Description = entityType.Description;
|
||||
existedEntityType.Color = entityType.Color;
|
||||
existedEntityType.IsEnum = entityType.IsEnum;
|
||||
existedEntityType.UpdatedTime = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public static void DeleteEntityType(this Agent agent, Database dc, String entityTypeId)
|
||||
{
|
||||
var entityType = dc.Table<EntityType>().FirstOrDefault(x => x.Id == entityTypeId);
|
||||
if (entityType == null) return;
|
||||
|
||||
dc.Table<EntityType>().Remove(entityType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
using BotSharp.Core.Agents;
|
||||
using EntityFrameworkCore.BootKit;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.Core.Entities
|
||||
{
|
||||
public static class EntityTypeExtension
|
||||
{
|
||||
public static string CreateEntityType(this Agent agent, Database dc, Entity entityType)
|
||||
{
|
||||
if (dc.Table<Entity>().Any(x => x.Name == entityType.Name && x.AgentId == agent.Id)) return agent.Id;
|
||||
|
||||
dc.Table<Entity>().Add(entityType);
|
||||
|
||||
return entityType.Id;
|
||||
}
|
||||
|
||||
public static void DeleteEntityType(this Agent agent, Database dc, String entityTypeId)
|
||||
{
|
||||
var entityType = dc.Table<Entity>().FirstOrDefault(x => x.Id == entityTypeId);
|
||||
if (entityType == null) return;
|
||||
|
||||
dc.Table<Entity>().Remove(entityType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ using System.Text;
|
|||
namespace BotSharp.Core.Entities
|
||||
{
|
||||
[Table("Bot_EntityEntrySynonym")]
|
||||
public class EntityEntrySynonym : DbRecord, IDbRecord
|
||||
public class EntrySynonym : DbRecord, IDbRecord
|
||||
{
|
||||
[Required]
|
||||
[StringLength(36)]
|
||||
17
BotSharp.Core/Entities/EntrySynonymDriver.cs
Normal file
17
BotSharp.Core/Entities/EntrySynonymDriver.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using EntityFrameworkCore.BootKit;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.Core.Entities
|
||||
{
|
||||
public static class EntrySynonymDriver
|
||||
{
|
||||
public static void CreateEntitySynonym(this EntityType agent, Database dc, EntrySynonym entity)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,6 @@ namespace BotSharp.Core.Models
|
|||
internal string SessionId { get; set; }
|
||||
|
||||
[JsonProperty("entities")]
|
||||
public List<Entity> Entities { get; set; }
|
||||
public List<EntityType> Entities { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ namespace BotSharp.Core.Models
|
|||
{
|
||||
public List<AIContext> Contexts { get; set; }
|
||||
|
||||
public List<Entity> Entities { get; set; }
|
||||
public List<EntityType> Entities { get; set; }
|
||||
|
||||
public bool HasContexts
|
||||
{
|
||||
|
|
@ -40,7 +40,7 @@ namespace BotSharp.Core.Models
|
|||
{
|
||||
}
|
||||
|
||||
public RequestExtras(List<AIContext> contexts, List<Entity> entities)
|
||||
public RequestExtras(List<AIContext> contexts, List<EntityType> entities)
|
||||
{
|
||||
this.Contexts = contexts;
|
||||
this.Entities = entities;
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ namespace BotSharp.UnitTest
|
|||
};
|
||||
var rasa = new RasaAi(dc);
|
||||
|
||||
int row = dc.DbTran(() => rasa.SaveAgent(agent));
|
||||
int row = dc.DbTran(() => rasa.agent.SaveAgent(dc));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -39,7 +39,7 @@ namespace BotSharp.UnitTest
|
|||
};
|
||||
var rasa = new RasaAi(dc);
|
||||
|
||||
int row = dc.DbTran(() => rasa.SaveAgent(agent));
|
||||
int row = dc.DbTran(() => rasa.agent.SaveAgent(dc));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -54,8 +54,9 @@ namespace BotSharp.UnitTest
|
|||
agent.ClientAccessToken = BOT_CLIENT_TOKEN;
|
||||
agent.DeveloperAccessToken = BOT_DEVELOPER_TOKEN;
|
||||
agent.UserId = Guid.NewGuid().ToString();
|
||||
rasa.agent = agent;
|
||||
|
||||
int row = dc.DbTran(() => rasa.SaveAgent(agent));
|
||||
int row = dc.DbTran(() => rasa.agent.SaveAgent(dc));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -65,7 +66,7 @@ namespace BotSharp.UnitTest
|
|||
config.SessionId = Guid.NewGuid().ToString();
|
||||
|
||||
var rasa = new RasaAi(dc, config);
|
||||
rasa.agent = rasa.LoadAgent();
|
||||
rasa.agent = rasa.LoadAgent(dc, config);
|
||||
string msg = rasa.Train(dc);
|
||||
|
||||
Assert.IsTrue(!String.IsNullOrEmpty(msg));
|
||||
|
|
|
|||
|
|
@ -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 = "Voicebot";
|
||||
public static String BOT_NAME = "VirtualAssistant";
|
||||
|
||||
protected Database dc { get; set; }
|
||||
protected string contentRoot;
|
||||
|
|
|
|||
Loading…
Reference in a new issue