2018-06-13 12:23:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2018-08-08 21:39:00 +00:00
|
|
|
|
using System.IO;
|
2018-06-14 15:03:01 +00:00
|
|
|
|
using System.Linq;
|
2018-06-13 12:23:03 +00:00
|
|
|
|
using System.Text;
|
2018-08-08 21:39:00 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2018-06-14 15:03:01 +00:00
|
|
|
|
using BotSharp.Core.Abstractions;
|
2018-06-15 17:39:24 +00:00
|
|
|
|
using BotSharp.Core.Agents;
|
|
|
|
|
|
using BotSharp.Core.Intents;
|
2018-06-13 12:23:03 +00:00
|
|
|
|
using DotNetToolkit;
|
|
|
|
|
|
using EntityFrameworkCore.BootKit;
|
2018-06-15 17:39:24 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-08-02 21:14:46 +00:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2018-08-08 21:39:00 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2018-06-14 15:03:01 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2018-08-08 21:39:00 +00:00
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2018-06-13 12:23:03 +00:00
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Engines
|
|
|
|
|
|
{
|
|
|
|
|
|
public class BotTrainer
|
|
|
|
|
|
{
|
|
|
|
|
|
private Database dc;
|
|
|
|
|
|
|
|
|
|
|
|
private string agentId;
|
|
|
|
|
|
|
2018-08-06 15:51:49 +00:00
|
|
|
|
public BotTrainer(string agentId, Database dc)
|
2018-06-13 12:23:03 +00:00
|
|
|
|
{
|
|
|
|
|
|
this.dc = dc;
|
|
|
|
|
|
this.agentId = agentId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 21:39:00 +00:00
|
|
|
|
public async Task<string> Train(Agent agent)
|
2018-06-13 12:23:03 +00:00
|
|
|
|
{
|
2018-06-15 17:39:24 +00:00
|
|
|
|
agent.Intents = dc.Table<Intent>()
|
|
|
|
|
|
.Include(x => x.Contexts)
|
|
|
|
|
|
.Include(x => x.Responses).ThenInclude(x => x.Contexts)
|
|
|
|
|
|
.Include(x => x.Responses).ThenInclude(x => x.Parameters).ThenInclude(x => x.Prompts)
|
|
|
|
|
|
.Include(x => x.Responses).ThenInclude(x => x.Messages)
|
|
|
|
|
|
.Include(x => x.UserSays).ThenInclude(x => x.Data)
|
|
|
|
|
|
.Where(x => x.AgentId == agentId)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
var data = JObject.FromObject(new
|
|
|
|
|
|
{
|
|
|
|
|
|
});
|
2018-06-14 15:03:01 +00:00
|
|
|
|
|
2018-06-13 12:23:03 +00:00
|
|
|
|
// Get NLP Provider
|
2018-08-02 21:14:46 +00:00
|
|
|
|
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
|
|
|
|
|
|
var assemblies = (string[])AppDomain.CurrentDomain.GetData("Assemblies");
|
2018-08-06 15:29:14 +00:00
|
|
|
|
var platform = config.GetSection($"BotPlatform").Value;
|
|
|
|
|
|
string providerName = config.GetSection($"{platform}:Provider").Value;
|
2018-08-02 21:14:46 +00:00
|
|
|
|
var provider = TypeHelper.GetInstance(providerName, assemblies) as INlpPipeline;
|
2018-08-06 15:29:14 +00:00
|
|
|
|
provider.Configuration = config.GetSection(platform);
|
2018-06-13 12:23:03 +00:00
|
|
|
|
|
2018-08-08 21:39:00 +00:00
|
|
|
|
var pipeModel = new PipeModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = providerName,
|
|
|
|
|
|
Class = provider.ToString(),
|
|
|
|
|
|
Meta = new JObject(),
|
|
|
|
|
|
Time = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await provider.Train(agent, data, pipeModel);
|
|
|
|
|
|
|
|
|
|
|
|
var meta = new ModelMetaData
|
|
|
|
|
|
{
|
|
|
|
|
|
Platform = platform,
|
|
|
|
|
|
Language = agent.Language,
|
|
|
|
|
|
TrainingDate = DateTime.UtcNow,
|
|
|
|
|
|
Version = config.GetValue<String>($"Version"),
|
|
|
|
|
|
Pipeline = new List<PipeModel>() { pipeModel }
|
|
|
|
|
|
};
|
2018-06-13 12:23:03 +00:00
|
|
|
|
|
2018-08-09 20:22:53 +00:00
|
|
|
|
var dirTrain = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "TrainingFiles", agent.Id);
|
|
|
|
|
|
if (!Directory.Exists(dirTrain))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(dirTrain);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var dirModel = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "ModelFiles", agent.Id);
|
|
|
|
|
|
if (!Directory.Exists(dirModel))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(dirModel);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-13 22:17:35 +00:00
|
|
|
|
// pipe process
|
2018-08-06 15:29:14 +00:00
|
|
|
|
var pipelines = provider.Configuration.GetSection($"Pipe").Value
|
2018-06-14 15:03:01 +00:00
|
|
|
|
.Split(',')
|
|
|
|
|
|
.Select(x => x.Trim())
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
2018-08-08 21:39:00 +00:00
|
|
|
|
pipelines.ForEach(async pipeName =>
|
2018-06-14 15:03:01 +00:00
|
|
|
|
{
|
2018-08-02 21:14:46 +00:00
|
|
|
|
var pipe = TypeHelper.GetInstance(pipeName, assemblies) as INlpPipeline;
|
2018-06-14 15:03:01 +00:00
|
|
|
|
pipe.Configuration = provider.Configuration;
|
2018-08-08 21:39:00 +00:00
|
|
|
|
pipeModel = new PipeModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = pipeName,
|
|
|
|
|
|
Class = pipe.ToString(),
|
|
|
|
|
|
Time = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
meta.Pipeline.Add(pipeModel);
|
|
|
|
|
|
|
|
|
|
|
|
await pipe.Train(agent, data, pipeModel);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// save model meta data
|
|
|
|
|
|
var dir = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "ModelFiles", agent.Id);
|
|
|
|
|
|
var metaJson = JsonConvert.SerializeObject(meta, new JsonSerializerSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
2018-06-14 15:03:01 +00:00
|
|
|
|
});
|
2018-08-08 21:39:00 +00:00
|
|
|
|
File.WriteAllText(Path.Join(dir, "metadata.json"), metaJson);
|
2018-06-13 12:23:03 +00:00
|
|
|
|
|
2018-08-08 21:39:00 +00:00
|
|
|
|
Console.WriteLine(metaJson);
|
2018-06-13 12:23:03 +00:00
|
|
|
|
|
2018-08-08 21:39:00 +00:00
|
|
|
|
return metaJson;
|
2018-06-13 12:23:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|