components

This commit is contained in:
Oceania2018 2018-06-13 07:23:03 -05:00
parent 0de330d1d0
commit f933f8d183
13 changed files with 192 additions and 56 deletions

View file

@ -14,7 +14,7 @@ namespace BotSharp.Core.Agents
{
public static class AgentDriver
{
public static Agent LoadAgentById(this IBotEngine engine, Database dc, string agentId)
public static Agent LoadAgentById(this IBotPlatform engine, Database dc, string agentId)
{
var clientAccessToken = dc.Table<Agent>().Find(agentId).ClientAccessToken;
@ -26,7 +26,7 @@ namespace BotSharp.Core.Agents
return rasa.agent;
}
public static Agent LoadAgent(this IBotEngine engine, Database dc, AIConfiguration aiConfig)
public static Agent LoadAgent(this IBotPlatform engine, Database dc, AIConfiguration aiConfig)
{
return dc.Table<Agent>()
.Include(x => x.Intents).ThenInclude(x => x.Contexts)
@ -40,7 +40,7 @@ namespace BotSharp.Core.Agents
/// <param name="importor"></param>
/// <param name="agentId"></param>
/// <returns></returns>
public static Agent RestoreAgent(this IBotEngine engine, IAgentImporter importer, String agentId, string dataDir)
public static Agent RestoreAgent(this IBotPlatform engine, IAgentImporter importer, String agentId, string dataDir)
{
// Load agent summary
var agent = importer.LoadAgent(agentId, dataDir);

View file

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Text;
using DotNetToolkit;
using EntityFrameworkCore.BootKit;
namespace BotSharp.Core.Engines
{
public class BotTrainer
{
private Database dc;
private string agentId;
private string config;
public BotTrainer(string agentId, Database dc, string config = "BotSharpAi")
{
this.dc = dc;
this.agentId = agentId;
this.config = config;
}
public string Train()
{
// Get NLP Provider
string providerName = Database.Configuration.GetSection($"{config}:Provider").Value;
var provider = TypeHelper.GetInstance(providerName, Database.Assemblies) as INlpProvider;
// tokenize
ITokenizer tokenizer;
INlpProvider nlpProvider;
return "";
}
}
}

View file

@ -4,7 +4,7 @@ using System.Text;
namespace BotSharp.Core.Engines
{
public interface IBotEngine
public interface IBotPlatform
{
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.Engines
{
public interface INlpProvider
{
void LoadModel();
Object GetDoc();
}
}

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.Engines
{
/// <summary>
///
/// </summary>
public interface ITokenizer
{
}
}

View file

@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
@ -15,13 +17,12 @@ using System.Text;
namespace BotSharp.Core.Engines
{
/// <summary>
/// Rasa nlu 0.11.x
/// Rasa nlu 0.12.x
/// </summary>
public class RasaAi : IBotEngine
public class RasaAi : IBotPlatform
{
public Database dc { get; set; }
public AIConfiguration AiConfig { get; set; }
public static IConfiguration Configuration { get; set; }
public Agent agent { get; set; }
@ -38,5 +39,52 @@ namespace BotSharp.Core.Engines
agent = this.LoadAgent(dc, aiConfig);
aiConfig.DevMode = agent.DeveloperAccessToken == aiConfig.ClientAccessToken;
}
public string Train()
{
var client = new RestClient($"{Database.Configuration.GetSection("Rasa:Nlu").Value}");
var rest = new RestRequest("train", Method.POST);
rest.AddQueryParameter("project", agent.Id);
var corpus = 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
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});
#if RASA_NLU_0_11
rest.AddParameter("application/json", json, ParameterType.RequestBody);
#else
string trainingConfig = 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);
#endif
var response = client.Execute(rest);
if (response.IsSuccessful)
{
var result = JObject.Parse(response.Content);
string modelName = result["info"].Value<String>().Split(": ")[1];
return modelName;
}
else
{
var result = JObject.Parse(response.Content);
Console.WriteLine(result["error"]);
return String.Empty;
}
}
}
}

View file

@ -471,53 +471,6 @@ namespace BotSharp.Core.Engines
return aiResponse;
}
public static string Train(this RasaAi console, Database dc)
{
var client = new RestClient($"{Database.Configuration.GetSection("Rasa:Nlu").Value}");
var rest = new RestRequest("train", Method.POST);
rest.AddQueryParameter("project", console.agent.Id);
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
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});
#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_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);
#endif
var response = client.Execute(rest);
if (response.IsSuccessful)
{
var result = JObject.Parse(response.Content);
string modelName = result["info"].Value<String>().Split(": ")[1];
return modelName;
}
else
{
var result = JObject.Parse(response.Content);
Console.WriteLine(result["error"]);
return String.Empty;
}
}
/// <summary>
/// Need two categories at least
/// </summary>

View file

@ -0,0 +1,24 @@
using BotSharp.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
namespace BotSharp.Core.Engines.SpaCy
{
public class SpaCyProvider : INlpProvider
{
public async void LoadModel()
{
using (var client = new HttpClient())
{
var response = await client.PostAsync("", new JsonContent(new { }));
}
}
public object GetDoc()
{
throw new NotImplementedException();
}
}
}

View file

@ -11,7 +11,7 @@ namespace BotSharp.Core.Intents
{
public static class IntentDriver
{
public static Intent GetIntent(this IBotEngine bot, Database dc, string intentId)
public static Intent GetIntent(this IBotPlatform bot, Database dc, string intentId)
{
var intent = dc.Table<Intent>()
.Include(x => x.Contexts)

View file

@ -0,0 +1,16 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
namespace BotSharp.Core.Utilities
{
public class JsonContent : StringContent
{
public JsonContent(object obj) :
base(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")
{
}
}
}

View file

@ -66,7 +66,7 @@ namespace BotSharp.UnitTest
config.SessionId = Guid.NewGuid().ToString();
var rasa = new RasaAi(dc, config);
string msg = rasa.Train(dc);
string msg = rasa.Train();
Assert.IsTrue(!String.IsNullOrEmpty(msg));
}

View file

@ -0,0 +1,19 @@
using BotSharp.Core.Engines;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.UnitTest
{
[TestClass]
public class BotTrainerTest : TestEssential
{
[TestMethod]
public void TrainingTest()
{
var trainer = new BotTrainer(BOT_ID, dc);
trainer.Train();
}
}
}

View file

@ -1,5 +1,16 @@
{
"Rasa": {
"Nlu": "http://localhost:5000"
},
"BotSharpAi": {
"Lang": "en",
"Provider": "SpaCyProvider",
"SpaCyProvider": {
"Url": "http://localhost:5005"
},
"Pipe": [
]
}
}