2018-08-09 21:06:40 +00:00
|
|
|
|
using BotSharp.Core.Abstractions;
|
|
|
|
|
|
using BotSharp.Core.Agents;
|
|
|
|
|
|
using BotSharp.Core.Models;
|
|
|
|
|
|
using DotNetToolkit;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Engines
|
|
|
|
|
|
{
|
|
|
|
|
|
public class BotPreditor
|
|
|
|
|
|
{
|
|
|
|
|
|
public async Task<string> Predict(Agent agent, AIRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
// load model
|
|
|
|
|
|
var dir = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "ModelFiles", agent.Id);
|
|
|
|
|
|
Console.WriteLine($"Load model from {dir}");
|
|
|
|
|
|
var metaJson = File.ReadAllText(Path.Join(dir, "metadata.json"));
|
|
|
|
|
|
var meta = JsonConvert.DeserializeObject<ModelMetaData>(metaJson);
|
|
|
|
|
|
|
|
|
|
|
|
// Get NLP Provider
|
|
|
|
|
|
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
|
|
|
|
|
|
var assemblies = (string[])AppDomain.CurrentDomain.GetData("Assemblies");
|
|
|
|
|
|
|
|
|
|
|
|
var providerPipe = meta.Pipeline.First();
|
|
|
|
|
|
var provider = TypeHelper.GetInstance(providerPipe.Name, assemblies) as INlpPipeline;
|
|
|
|
|
|
provider.Configuration = config.GetSection(meta.Platform);
|
|
|
|
|
|
|
|
|
|
|
|
var data = JObject.FromObject(new
|
|
|
|
|
|
{
|
2018-08-09 21:54:52 +00:00
|
|
|
|
Text = request.Query.FirstOrDefault()
|
2018-08-09 21:06:40 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await provider.Train(agent, data, providerPipe);
|
|
|
|
|
|
meta.Pipeline.RemoveAt(0);
|
|
|
|
|
|
|
2018-08-09 21:54:52 +00:00
|
|
|
|
var settings = new PipeSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
ModelDir = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "ModelFiles", agent.Id),
|
2018-08-10 15:43:05 +00:00
|
|
|
|
PredictDir = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "PredictFiles", agent.Id),
|
2018-08-09 21:54:52 +00:00
|
|
|
|
AlgorithmDir = Path.Join(AppDomain.CurrentDomain.GetData("ContentRootPath").ToString(), "Algorithms")
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-08-10 14:52:31 +00:00
|
|
|
|
if (!Directory.Exists(settings.PredictDir))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(settings.PredictDir);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-09 21:06:40 +00:00
|
|
|
|
// pipe process
|
|
|
|
|
|
meta.Pipeline.ForEach(async pipeMeta =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var pipe = TypeHelper.GetInstance(pipeMeta.Name, assemblies) as INlpPipeline;
|
|
|
|
|
|
pipe.Configuration = provider.Configuration;
|
2018-08-09 21:54:52 +00:00
|
|
|
|
pipe.Settings = settings;
|
2018-08-09 21:06:40 +00:00
|
|
|
|
await pipe.Predict(agent, data, pipeMeta);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|