Add entity type api

This commit is contained in:
haiping008 2018-01-02 12:05:35 -06:00
parent d286d0dd83
commit 1773a4afc6
16 changed files with 172 additions and 40 deletions

View file

@ -1,8 +1,10 @@
using Bot.Rasa.Agents; using Bot.Rasa.Agents;
using Bot.Rasa.Console; using Bot.Rasa.Console;
using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
namespace Bot.Rasa.RestApi namespace Bot.Rasa.RestApi
@ -19,10 +21,10 @@ namespace Bot.Rasa.RestApi
[HttpPost] [HttpPost]
public String Create([FromBody] Agent agent) public String Create([FromBody] Agent agent)
{ {
var console = new RasaConsole(dc);
dc.DbTran(() => { dc.DbTran(() => {
console.CreateAgent(agent);
}); });
return agent.Id; return agent.Id;

View file

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@ -9,6 +10,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="DotNetToolkit" Version="1.0.0" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="1.3.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
</ItemGroup> </ItemGroup>

View file

@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.RestApi
{
public class EntityItemController : EssentialController
{
[HttpPost]
public IActionResult CreateEntity()
{
return Ok();
}
}
}

View file

@ -0,0 +1,31 @@
using Bot.Rasa.Agents;
using Bot.Rasa.Entities;
using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.RestApi
{
public class EntityTypeController : EssentialController
{
[HttpPost]
public string CreateType([FromBody] EntityType entityType)
{
var agent = dc.Table<Agent>().Find(entityType.AgentId);
dc.DbTran(() => agent.CreateEntityType(dc, entityType));
return entityType.Id;
}
[HttpDelete("{agentId}/{entityTypeId}")]
public IActionResult DeleteType([FromRoute] String agentId, [FromRoute] String entityTypeId)
{
var agent = dc.Table<Agent>().Find(agentId);
dc.DbTran(() => agent.DeleteEntityType(dc, entityTypeId));
return Ok();
}
}
}

View file

@ -1,8 +1,10 @@
using Bot.Rasa.Console; using Bot.Rasa.Console;
using DotNetToolkit;
using EntityFrameworkCore.BootKit; using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SqlClient; using System.Data.SqlClient;
@ -63,5 +65,19 @@ namespace Bot.Rasa.RestApi
} }
} }
[HttpPatch("{table}/{id}")]
public IActionResult Patch([FromRoute] String table, [FromRoute] String id, [FromBody] JObject jObject)
{
var patch = new DbPatchModel
{
Table = table,
Id = id,
Values = jObject.ToDictionary()
};
dc.Patch<IDbRecord>(patch);
return Ok();
}
} }
} }

View file

@ -0,0 +1,26 @@
using Bot.Rasa.Agents;
using Bot.Rasa.Intents;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.RestApi
{
public class IntentController : EssentialController
{
/// <summary>
/// User intent, a intent is connected to a dialog flow.
/// </summary>
/// <param name="agentId"></param>
/// <param name="intent"></param>
/// <returns></returns>
[HttpPost("{agentId}")]
public string CreateIntent([FromRoute] String agentId, [FromBody] Intent intent)
{
var agent = dc.Agent(agentId);
return intent.Id;
}
}
}

View file

@ -1,4 +1,5 @@
using Bot.Rasa.Entities; using Bot.Rasa.Entities;
using Bot.Rasa.Intents;
using Bot.Rasa.Models; using Bot.Rasa.Models;
using EntityFrameworkCore.BootKit; using EntityFrameworkCore.BootKit;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -11,6 +12,17 @@ namespace Bot.Rasa.Agents
{ {
public static class AgentExtension public static class AgentExtension
{ {
/// <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)
{
return dc.Table<Agent>().Find(agentId);
}
public static String CreateEntity(this Agent agent, EntityType entity, Database dc) public static String CreateEntity(this Agent agent, EntityType entity, Database dc)
{ {
return entity.Id; return entity.Id;
@ -23,7 +35,7 @@ namespace Bot.Rasa.Agents
UserSays = new List<UserSay>() UserSays = new List<UserSay>()
}; };
var intents = dc.Intent().Include(x => x.Expressions).ToList(); var intents = dc.Table<Intent>().Include(x => x.Expressions).ToList();
intents.ForEach(intent => { intents.ForEach(intent => {

View file

@ -2,10 +2,11 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="EntityFrameworkCore.BootKit" Version="1.2.0" /> <PackageReference Include="EntityFrameworkCore.BootKit" Version="1.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="RestSharp" Version="106.1.0" /> <PackageReference Include="RestSharp" Version="106.1.0" />
</ItemGroup> </ItemGroup>

View file

@ -66,14 +66,14 @@ namespace Bot.Rasa.Console
public Agent LoadAgent(String agentId) public Agent LoadAgent(String agentId)
{ {
return dc.Agent().Include(x => x.Intents).FirstOrDefault(x => x.Id == agentId); return dc.Table<Agent>().Include(x => x.Intents).FirstOrDefault(x => x.Id == agentId);
} }
public String CreateAgent(Agent agent) public String CreateAgent(Agent agent)
{ {
if (dc.Agent().Any(x => x.Name == agent.Name)) return String.Empty; if (dc.Table<Agent>().Any(x => x.Id == agent.Id)) return String.Empty;
dc.Agent().Add(agent); dc.Table<Agent>().Add(agent);
return agent.Id; return agent.Id;
} }

View file

@ -39,7 +39,7 @@ namespace Bot.Rasa.Console
/// <returns></returns> /// <returns></returns>
public static bool Train(this RasaConsole console, Database dc, String agentId) public static bool Train(this RasaConsole console, Database dc, String agentId)
{ {
var agent = dc.Agent().Find(agentId); var agent = dc.Table<Agent>().Find(agentId);
var corpus = agent.GrabCorpus(dc); var corpus = agent.GrabCorpus(dc);
string json = JsonConvert.SerializeObject(new { rasa_nlu_data = corpus }, string json = JsonConvert.SerializeObject(new { rasa_nlu_data = corpus },

View file

@ -14,6 +14,7 @@ namespace Bot.Rasa.Entities
[StringLength(36)] [StringLength(36)]
public String AgentId { get; set; } public String AgentId { get; set; }
[Required]
[MaxLength(64)] [MaxLength(64)]
public String Name { get; set; } public String Name { get; set; }

View file

@ -0,0 +1,29 @@
using Bot.Rasa.Agents;
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bot.Rasa.Entities
{
public static class EntityTypeExtension
{
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 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);
}
}
}

View file

@ -1,29 +0,0 @@
using Bot.Rasa.Agents;
using Bot.Rasa.Intents;
using EntityFrameworkCore.BootKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bot.Rasa
{
public static class EntityDbContextExtension
{
public static DbSet<Agent> Agent(this Database dc)
{
return dc.Table<Agent>();
}
public static DbSet<Intent> Intent(this Database dc)
{
return dc.Table<Intent>();
}
public static DbSet<IntentExpression> IntentExpression(this Database dc)
{
return dc.Table<IntentExpression>();
}
}
}

View file

@ -7,6 +7,9 @@ using System.Text;
namespace Bot.Rasa.Intents namespace Bot.Rasa.Intents
{ {
/// <summary>
/// Intent Table
/// </summary>
[Table("Bot_Intent")] [Table("Bot_Intent")]
public class Intent : DbRecord, IDbRecord public class Intent : DbRecord, IDbRecord
{ {

View file

@ -0,0 +1,21 @@
using Bot.Rasa.Agents;
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bot.Rasa.Intents
{
public static class IntentExtension
{
public static string CreateIntent(this Agent agent, Database dc, Intent intent)
{
if (dc.Table<Intent>().Any(x => x.Id == intent.Id)) return intent.Id;
dc.Table<Intent>().Add(intent);
return intent.Id;
}
}
}

View file

@ -26,7 +26,7 @@ namespace Bot.UnitTest
if(row > 0) if(row > 0)
{ {
//var result = rasa.Train(dc, agent.Id); var result = rasa.Train(dc, agent.Id);
} }
var loadedAgent = rasa.LoadAgent(agent.Id); var loadedAgent = rasa.LoadAgent(agent.Id);
@ -41,7 +41,7 @@ namespace Bot.UnitTest
{ {
var rasa = new RasaConsole(dc); var rasa = new RasaConsole(dc);
var response = rasa.TextRequest(PIZZA_BOT_ID, "how old are you"); var response = rasa.TextRequest(PIZZA_BOT_ID, "how old are you");
response = rasa.TextRequest(PIZZA_BOT_ID, "where do you come from"); response = rasa.TextRequest(PIZZA_BOT_ID, "where are you from");
response = rasa.TextRequest(PIZZA_BOT_ID, "would you like some cookie"); response = rasa.TextRequest(PIZZA_BOT_ID, "would you like some cookie");
} }