Add bunch of APIs

This commit is contained in:
haiping008 2017-12-30 14:26:35 -06:00
parent 7b05297c30
commit d286d0dd83
29 changed files with 594 additions and 126 deletions

View file

@ -0,0 +1,31 @@
using Bot.Rasa.Agents;
using Bot.Rasa.Console;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.RestApi
{
public class AgentController : EssentialController
{
[HttpGet("id")]
public Agent Get([FromRoute] String id)
{
var console = new RasaConsole(dc);
return console.LoadAgent(id);
}
[HttpPost]
public String Create([FromBody] Agent agent)
{
dc.DbTran(() => {
});
return agent.Id;
}
}
}

View file

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>bin\Debug\netcoreapp2.0\Bot.Rasa.RestApi.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bot.Rasa\Bot.Rasa.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,17 @@
using Bot.Rasa.Entities;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.RestApi
{
public class EntityController : EssentialController
{
[HttpPost]
public string CreateEntity([FromBody] EntityType entity)
{
return entity.Id;
}
}
}

View file

@ -0,0 +1,67 @@
using Bot.Rasa.Console;
using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
namespace Bot.Rasa.RestApi
{
#if !DEBUG
[Authorize]
#endif
[Produces("application/json")]
[Route("bot/[controller]")]
public class EssentialController : ControllerBase
{
protected Database dc { get; set; }
public EssentialController()
{
dc = new Database();
string db = RasaConsole.Options.DbName;
string connectionString = RasaConsole.Options.DbConnectionString;
if (db.Equals("SqlServer"))
{
dc.BindDbContext<IDbRecord, DbContext4SqlServer>(new DatabaseBind
{
MasterConnection = new SqlConnection(connectionString),
CreateDbIfNotExist = true,
AssemblyNames = RasaConsole.Options.Assembles
});
}
else if (db.Equals("Sqlite"))
{
connectionString = connectionString.Replace("|DataDirectory|\\", RasaConsole.Options.ContentRootPath + "\\App_Data\\");
dc.BindDbContext<IDbRecord, DbContext4Sqlite>(new DatabaseBind
{
MasterConnection = new SqliteConnection(connectionString),
CreateDbIfNotExist = true,
AssemblyNames = RasaConsole.Options.Assembles
});
}
else if (db.Equals("MySql"))
{
dc.BindDbContext<IDbRecord, DbContext4MySql>(new DatabaseBind
{
MasterConnection = new MySqlConnection(connectionString),
CreateDbIfNotExist = true,
AssemblyNames = RasaConsole.Options.Assembles
});
}
else if (db.Equals("InMemory"))
{
dc.BindDbContext<IDbRecord, DbContext4Memory>(new DatabaseBind
{
AssemblyNames = RasaConsole.Options.Assembles
});
}
}
}
}

View file

@ -1,11 +1,15 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
VisualStudioVersion = 15.0.27130.2003 VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot.Rasa", "Bot.Rasa\Bot.Rasa.csproj", "{8E57A9A5-EB37-4F83-93FE-3324A069B568}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot.Rasa", "Bot.Rasa\Bot.Rasa.csproj", "{8E57A9A5-EB37-4F83-93FE-3324A069B568}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot.UnitTest", "Bot.UnitTest\Bot.UnitTest.csproj", "{90705625-1342-4ED8-A05B-46C720D20EE4}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot.UnitTest", "Bot.UnitTest\Bot.UnitTest.csproj", "{90705625-1342-4ED8-A05B-46C720D20EE4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot.Rasa.RestApi", "Bot.Rasa.RestApi\Bot.Rasa.RestApi.csproj", "{310EECBB-8B77-4F4A-A09C-6B5EED29155E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot.WebStarter", "Bot.WebStarter\Bot.WebStarter.csproj", "{ACBEB546-00AB-4EB7-8DBF-4BFF722FC9EA}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -21,6 +25,14 @@ Global
{90705625-1342-4ED8-A05B-46C720D20EE4}.Debug|Any CPU.Build.0 = Debug|Any CPU {90705625-1342-4ED8-A05B-46C720D20EE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90705625-1342-4ED8-A05B-46C720D20EE4}.Release|Any CPU.ActiveCfg = Release|Any CPU {90705625-1342-4ED8-A05B-46C720D20EE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90705625-1342-4ED8-A05B-46C720D20EE4}.Release|Any CPU.Build.0 = Release|Any CPU {90705625-1342-4ED8-A05B-46C720D20EE4}.Release|Any CPU.Build.0 = Release|Any CPU
{310EECBB-8B77-4F4A-A09C-6B5EED29155E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{310EECBB-8B77-4F4A-A09C-6B5EED29155E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{310EECBB-8B77-4F4A-A09C-6B5EED29155E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{310EECBB-8B77-4F4A-A09C-6B5EED29155E}.Release|Any CPU.Build.0 = Release|Any CPU
{ACBEB546-00AB-4EB7-8DBF-4BFF722FC9EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACBEB546-00AB-4EB7-8DBF-4BFF722FC9EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACBEB546-00AB-4EB7-8DBF-4BFF722FC9EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACBEB546-00AB-4EB7-8DBF-4BFF722FC9EA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View file

@ -1,6 +1,7 @@
using Bot.Rasa.Intents; using Bot.Rasa.Entities;
using CustomEntityFoundation.Entities; using Bot.Rasa.Intents;
using EntityFrameworkCore.BootKit; using EntityFrameworkCore.BootKit;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
@ -9,12 +10,17 @@ using System.Text;
namespace Bot.Rasa.Agents namespace Bot.Rasa.Agents
{ {
public class RasaAgent : Entity, IDbRecord [Table("Bot_Agent")]
public class Agent : DbRecord, IDbRecord
{ {
[MaxLength(64)] [MaxLength(64)]
public String Name { get; set; } public String Name { get; set; }
[ForeignKey("AgentId")] [ForeignKey("AgentId")]
public List<RasaIntent> Intents { get; set; } public List<Intent> Intents { get; set; }
[ForeignKey("AgentId")]
[JsonProperty("entity_types")]
public List<EntityType> EntityTypes { get; set; }
} }
} }

View file

@ -1,5 +1,6 @@
using Bot.Rasa.Models; using Bot.Rasa.Entities;
using CustomEntityFoundation; using Bot.Rasa.Models;
using EntityFrameworkCore.BootKit;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -10,7 +11,12 @@ namespace Bot.Rasa.Agents
{ {
public static class AgentExtension public static class AgentExtension
{ {
public static RasaTrainingData GrabCorpus(this RasaAgent agent, EntityDbContext dc) public static String CreateEntity(this Agent agent, EntityType entity, Database dc)
{
return entity.Id;
}
public static RasaTrainingData GrabCorpus(this Agent agent, Database dc)
{ {
var trainingData = new RasaTrainingData var trainingData = new RasaTrainingData
{ {

View file

@ -1,5 +1,4 @@
using Bot.Rasa.Intents; using Bot.Rasa.Intents;
using CustomEntityFoundation.Entities;
using EntityFrameworkCore.BootKit; using EntityFrameworkCore.BootKit;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

View file

@ -5,11 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Entities\" /> <PackageReference Include="EntityFrameworkCore.BootKit" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CustomEntityFoundation" Version="1.3.3" />
<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

@ -1,7 +1,13 @@
using Bot.Rasa.Agents; using Bot.Rasa.Agents;
using CustomEntityFoundation; using Bot.Rasa.Entities;
using EntityFrameworkCore.BootKit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -9,21 +15,61 @@ namespace Bot.Rasa.Console
{ {
public class RasaConsole public class RasaConsole
{ {
private EntityDbContext dc { get; set; } private Database dc { get; set; }
public RasaOptions options { get; set; } public static RasaOptions Options { get; set; }
public static IConfiguration Configuration { get; set; }
public RasaConsole(EntityDbContext dc, RasaOptions options) public RasaConsole(Database dc)
{ {
this.dc = dc; this.dc = dc;
this.options = options;
} }
public RasaAgent LoadAgent(String agentId) /// <summary>
/// Restore a agent instance from json file
/// </summary>
/// <param name="agentId"></param>
/// <returns></returns>
public Agent RestoreAgent(String agentId)
{ {
return dc.Agent().Find(agentId); string json = File.ReadAllText($"{Options.ContentRootPath}\\App_Data\\DbInitializer\\Agents\\{agentId}.json");
var agent = JsonConvert.DeserializeObject<Agent>(json);
agent.Id = agentId;
agent.EntityTypes.ForEach(entityType =>
{
entityType.Items = entityType.Values.Select(x => new EntityItem
{
Value = x
}).ToList();
});
agent.Intents.ForEach(intent => {
intent.Expressions.ForEach(expression =>
{
});
});
return agent;
} }
public String CreateAgent(RasaAgent 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(String agentId)
{
return dc.Agent().Include(x => x.Intents).FirstOrDefault(x => x.Id == agentId);
}
public String CreateAgent(Agent agent)
{ {
if (dc.Agent().Any(x => x.Name == agent.Name)) return String.Empty; if (dc.Agent().Any(x => x.Name == agent.Name)) return String.Empty;

View file

@ -7,5 +7,9 @@ namespace Bot.Rasa.Console
public class RasaOptions public class RasaOptions
{ {
public string HostUrl { get; set; } public string HostUrl { get; set; }
public String[] Assembles { get; set; }
public string ContentRootPath { get; set; }
public String DbName { get; set; }
public String DbConnectionString { get; set; }
} }
} }

View file

@ -1,6 +1,6 @@
using Bot.Rasa.Agents; using Bot.Rasa.Agents;
using Bot.Rasa.Models; using Bot.Rasa.Models;
using CustomEntityFoundation; using EntityFrameworkCore.BootKit;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using RestSharp; using RestSharp;
@ -15,18 +15,29 @@ namespace Bot.Rasa.Console
{ {
public static AgentResponse TextRequest(this RasaConsole console, String agentId, String text) public static AgentResponse TextRequest(this RasaConsole console, String agentId, String text)
{ {
var client = new RestClient($"{console.options.HostUrl}"); var client = new RestClient($"{RasaConsole.Options.HostUrl}");
var request = new RestRequest("parse?project={project}&q={text}", Method.GET); var request = new RestRequest("parse", Method.POST);
request.AddUrlSegment("project", agentId); string json = JsonConvert.SerializeObject(new { Project = agentId, Q = text },
request.AddUrlSegment("text", text); new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = client.Execute<AgentResponse>(request); var response = client.Execute<AgentResponse>(request);
return response.Data; return response.Data;
} }
public static bool Train(this RasaConsole console, EntityDbContext dc, String agentId) /// <summary>
/// Need two categories at least
/// </summary>
/// <param name="console"></param>
/// <param name="dc"></param>
/// <param name="agentId"></param>
/// <returns></returns>
public static bool Train(this RasaConsole console, Database dc, String agentId)
{ {
var agent = dc.Agent().Find(agentId); var agent = dc.Agent().Find(agentId);
var corpus = agent.GrabCorpus(dc); var corpus = agent.GrabCorpus(dc);
@ -37,14 +48,14 @@ namespace Bot.Rasa.Console
ContractResolver = new CamelCasePropertyNamesContractResolver() ContractResolver = new CamelCasePropertyNamesContractResolver()
}); });
var client = new RestClient($"{console.options.HostUrl}"); var client = new RestClient($"{RasaConsole.Options.HostUrl}");
var request = new RestRequest("train", Method.POST); var request = new RestRequest("train", Method.POST);
request.AddQueryParameter("project", agentId); request.AddQueryParameter("project", agentId);
request.AddParameter("application/json", json, ParameterType.RequestBody); request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = client.Execute(request); var response = client.Execute(request);
return true; return response.IsSuccessful;
} }
} }
} }

View file

@ -0,0 +1,23 @@
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace Bot.Rasa.Entities
{
[Table("Bot_EntityItem")]
public class EntityItem : DbRecord, IDbRecord
{
[Required]
[StringLength(36)]
public String EntityTypeId { get; set; }
[MaxLength(128)]
public String Value { get; set; }
[NotMapped]
public List<String> Synonyms { get; set; }
}
}

View file

@ -0,0 +1,26 @@
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace Bot.Rasa.Entities
{
[Table("Bot_EntityType")]
public class EntityType : DbRecord, IDbRecord
{
[Required]
[StringLength(36)]
public String AgentId { get; set; }
[MaxLength(64)]
public String Name { get; set; }
[NotMapped]
public List<String> Values { get; set; }
[ForeignKey("EntityTypeId")]
public List<EntityItem> Items { get; set; }
}
}

View file

@ -1,6 +1,5 @@
using Bot.Rasa.Agents; using Bot.Rasa.Agents;
using Bot.Rasa.Intents; using Bot.Rasa.Intents;
using CustomEntityFoundation;
using EntityFrameworkCore.BootKit; using EntityFrameworkCore.BootKit;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
@ -12,19 +11,19 @@ namespace Bot.Rasa
{ {
public static class EntityDbContextExtension public static class EntityDbContextExtension
{ {
public static DbSet<RasaAgent> Agent(this EntityDbContext dc) public static DbSet<Agent> Agent(this Database dc)
{ {
return dc.Table<RasaAgent>(); return dc.Table<Agent>();
} }
public static DbSet<RasaIntent> Intent(this EntityDbContext dc) public static DbSet<Intent> Intent(this Database dc)
{ {
return dc.Table<RasaIntent>(); return dc.Table<Intent>();
} }
public static DbSet<RasaIntentExpression> IntentExpression(this EntityDbContext dc) public static DbSet<IntentExpression> IntentExpression(this Database dc)
{ {
return dc.Table<RasaIntentExpression>(); return dc.Table<IntentExpression>();
} }
} }
} }

View file

@ -0,0 +1,27 @@
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace Bot.Rasa.Expressions
{
[Table("Bot_EntityOfSpeech")]
public class EntitiyOfSpeech : DbRecord, IDbRecord
{
[Required]
[StringLength(36)]
public String ExpressionId { get; set; }
public int Start { get; set; }
[Required]
[MaxLength(128)]
public String Value { get; set; }
[Required]
[MaxLength(64)]
public String Entity { get; set; }
}
}

View file

@ -1,5 +1,4 @@
using CustomEntityFoundation.Entities; using EntityFrameworkCore.BootKit;
using EntityFrameworkCore.BootKit;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
@ -8,7 +7,8 @@ using System.Text;
namespace Bot.Rasa.Intents namespace Bot.Rasa.Intents
{ {
public class RasaIntent : Entity, IDbRecord [Table("Bot_Intent")]
public class Intent : DbRecord, IDbRecord
{ {
[Required] [Required]
[StringLength(36)] [StringLength(36)]
@ -21,6 +21,6 @@ namespace Bot.Rasa.Intents
public String Description { get; set; } public String Description { get; set; }
[ForeignKey("IntentId")] [ForeignKey("IntentId")]
public List<RasaIntentExpression> Expressions { get; set; } public List<IntentExpression> Expressions { get; set; }
} }
} }

View file

@ -1,5 +1,4 @@
using CustomEntityFoundation; using Bot.Rasa.Expressions;
using CustomEntityFoundation.Entities;
using EntityFrameworkCore.BootKit; using EntityFrameworkCore.BootKit;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
@ -11,8 +10,14 @@ using System.Text;
namespace Bot.Rasa.Intents namespace Bot.Rasa.Intents
{ {
public class RasaIntentExpression : Entity, IDbRecord [Table("Bot_IntentExpression")]
public class IntentExpression : DbRecord, IDbRecord
{ {
public IntentExpression()
{
Entities = new List<EntitiyOfSpeech>();
}
[Required] [Required]
[StringLength(36)] [StringLength(36)]
public String IntentId { get; set; } public String IntentId { get; set; }
@ -21,9 +26,12 @@ namespace Bot.Rasa.Intents
[MaxLength(128)] [MaxLength(128)]
public String Text { get; set; } public String Text { get; set; }
public override bool IsExist<T>(EntityDbContext dc) [ForeignKey("ExpressionId")]
public List<EntitiyOfSpeech> Entities { get; set; }
public bool IsExist(Database dc)
{ {
return dc.Table<RasaIntentExpression>().Any(x => x.IntentId == IntentId && x.Text == Text); return dc.Table<IntentExpression>().Any(x => x.IntentId == IntentId && x.Text == Text);
} }
} }
} }

View file

@ -1,4 +1,5 @@
using System; using Bot.Rasa.Expressions;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@ -6,7 +7,13 @@ namespace Bot.Rasa.Models
{ {
public class UserSay public class UserSay
{ {
public UserSay()
{
Entities = new List<EntitiyOfSpeech>();
}
public String Text { get; set; } public String Text { get; set; }
public String Intent { get; set; } public String Intent { get; set; }
public List<EntitiyOfSpeech> Entities { get; set; }
} }
} }

View file

@ -11,34 +11,35 @@ using System.Text;
namespace Bot.UnitTest namespace Bot.UnitTest
{ {
[TestClass] [TestClass]
public class AgentTest : Database public class AgentTest : TestEssential
{ {
public static String PIZZA_BOT_ID = "2b6a288e-d891-40c6-96ce-6a0cf324545c"; public static String PIZZA_BOT_ID = "2b6a288e-d891-40c6-96ce-6a0cf324545c";
public static RasaOptions Options = new RasaOptions { HostUrl = "http://192.168.56.101:5000" };
[TestMethod] [TestMethod]
public void CreateAgent() public void CreateAgent()
{ {
var rasa = new RasaConsole(dc, Options); var rasa = new RasaConsole(dc);
var agent = new RasaAgent var agent = rasa.RestoreAgent(PIZZA_BOT_ID);
{
Id = PIZZA_BOT_ID,
Name = "Pizza Bot"
};
int row = dc.DbTran(() => rasa.CreateAgent(agent)); int row = dc.DbTran(() => rasa.CreateAgent(agent));
if(row > 0) if(row > 0)
{ {
var generator = new GenerateTestData(); //var result = rasa.Train(dc, agent.Id);
dc.DbTran(() => generator.LoadData(dc, agent));
} }
var loadedAgent = rasa.LoadAgent(agent.Id);
Assert.IsTrue(loadedAgent.Intents.Count == agent.Intents.Count);
var response = rasa.TextRequest(agent.Id, "weather in Chicago tomorrow");
Assert.IsTrue(response.Intent.Name == "weather");
} }
[TestMethod] [TestMethod]
public void TextRequest() public void TextRequest()
{ {
var rasa = new RasaConsole(dc, Options); 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 do you come from");
response = rasa.TextRequest(PIZZA_BOT_ID, "would you like some cookie"); response = rasa.TextRequest(PIZZA_BOT_ID, "would you like some cookie");
@ -47,7 +48,7 @@ namespace Bot.UnitTest
[TestMethod] [TestMethod]
public void Train() public void Train()
{ {
var rasa = new RasaConsole(dc, Options); var rasa = new RasaConsole(dc);
rasa.Train(dc, PIZZA_BOT_ID); rasa.Train(dc, PIZZA_BOT_ID);
} }
} }

View file

@ -1,30 +0,0 @@
using CustomEntityFoundation;
using System;
using System.IO;
namespace Bot.UnitTest
{
public abstract class Database
{
protected EntityDbContext dc { get; set; }
public Database()
{
EntityDbContext.Assembles = new String[] { "Bot.Rasa" };
var options = new DatabaseOptions
{
ContentRootPath = Directory.GetCurrentDirectory() + "\\..\\..\\..\\..",
};
// Sqlite
options.Database = "Sqlite";
options.ConnectionString = "Data Source=|DataDirectory|\\bot.db";
EntityDbContext.Options = options;
dc = new EntityDbContext();
dc.InitDb();
}
}
}

View file

@ -1,34 +0,0 @@
using Bot.Rasa;
using Bot.Rasa.Agents;
using Bot.Rasa.Intents;
using CustomEntityFoundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bot.UnitTest
{
public class GenerateTestData
{
public void LoadData(EntityDbContext dc, RasaAgent agent)
{
var intent = new RasaIntent
{
AgentId = agent.Id,
Name = "Weather",
Expressions = new List<RasaIntentExpression>
{
new RasaIntentExpression { Text ="What is the weather like today in Chicago?" },
new RasaIntentExpression { Text ="Is it will be rain?" },
new RasaIntentExpression { Text ="It's windy outside?" },
new RasaIntentExpression { Text ="It's very code there?" }
}
};
if (dc.Intent().Any(x => x.Name == intent.Name)) return;
dc.Intent().Add(intent);
}
}
}

View file

@ -0,0 +1,34 @@
using Bot.Rasa.Console;
using EntityFrameworkCore.BootKit;
using Microsoft.Data.Sqlite;
using System;
using System.IO;
namespace Bot.UnitTest
{
public abstract class TestEssential
{
protected Database dc { get; set; }
public TestEssential()
{
RasaConsole.Options = new RasaOptions
{
HostUrl = "http://192.168.56.101:5000",
ContentRootPath = $"{Directory.GetCurrentDirectory()}\\..\\..\\..\\..\\Bot.WebStarter",
Assembles = new String[] { "Bot.Rasa" }
};
dc = new Database();
dc.BindDbContext<IDbRecord, DbContext4Sqlite>(new DatabaseBind
{
MasterConnection = new SqliteConnection($"Data Source={RasaConsole.Options.ContentRootPath}\\App_Data\\bot-rasa.db"),
CreateDbIfNotExist = true,
AssemblyNames = new string[] { "Bot.Rasa" }
});
}
}
}

View file

@ -0,0 +1,61 @@
{
"name": "Weather Bot",
"entity_types": [
{
"name": "date",
"values": [ "today", "tomorrow", "yesterday", "now" ]
}
],
"entity_synonyms": [
],
"intents": [
{
"name": "greet",
"expressions": [
{ "text": "Hi" },
{ "text": "Hey" },
{ "text": "Hello" },
{ "text": "How are you?" },
{ "text": "What's up?" },
{ "text": "How is going?" }
]
},
{
"name": "weather",
"expressions": [
{
"text": "What is the weather like today in Chicago?",
"entities": [
{
"start": 25,
"value": "today",
"entity": "date"
}
]
},
{
"text": "Will it be rain tomorrow in Beijing?",
"entities": [
{
"start": 16,
"value": "tomorrow",
"entity": "date"
},
{
"start": 28,
"value": "Beijing",
"entity": "location"
}
]
},
{ "text": "It's windy outside?" },
{ "text": "It's very code there?" },
{ "text": "It's gonna be snow?" }
]
}
]
}

View file

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Remove="App_Data\bot-rasa.db" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bot.Rasa.RestApi\Bot.Rasa.RestApi.csproj" />
</ItemGroup>
</Project>

25
Bot.WebStarter/Program.cs Normal file
View file

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace Bot.WebStarter
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}

View file

@ -0,0 +1,19 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"Rasa": {
"Host": "http://192.168.56.101:5000"
}
}

View file

@ -0,0 +1,10 @@
{
"Database": {
"Default": "Sqlite",
"ConnectionStrings": {
"InMemory": "DataSource=:memory:",
"Sqlite": "Data Source=|DataDirectory|\\bot-rasa.db;",
"SqlServer": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=bot-rasa;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
}
}

51
Bot.WebStarter/Startup.cs Normal file
View file

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bot.Rasa.Console;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Bot.WebStarter
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
string db = RasaConsole.Configuration.GetSection("Database:Default").Value;
RasaConsole.Options = new RasaOptions
{
HostUrl = Configuration.GetSection("Rasa:Host").Value,
Assembles = new String[] { "Bot.Rasa" },
ContentRootPath = env.ContentRootPath,
DbName = db,
DbConnectionString = Configuration.GetSection("Database:ConnectionStrings")[db]
};
}
}
}