BotSharp/BotSharp.Core/PlatformBuilderBase.cs

120 lines
3.3 KiB
C#
Raw Normal View History

2018-10-03 01:44:11 +00:00
using BotSharp.Core.Engines;
using BotSharp.Platform.Abstraction;
using BotSharp.Platform.Models;
2018-10-04 21:18:46 +00:00
using BotSharp.Platform.Models.MachineLearning;
2018-10-01 17:15:17 +00:00
using DotNetToolkit;
using Microsoft.Extensions.Configuration;
2018-10-03 01:44:11 +00:00
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
2018-10-03 01:44:11 +00:00
using System.IO;
2018-10-01 17:15:17 +00:00
using System.Linq;
using System.Text;
2018-10-04 21:18:46 +00:00
using System.Threading.Tasks;
namespace BotSharp.Core
{
2018-10-01 17:15:17 +00:00
public abstract class PlatformBuilderBase<TAgent> where TAgent : AgentBase
{
2018-10-01 17:15:17 +00:00
public IAgentStorage<TAgent> Storage { get; set; }
2018-10-11 13:24:50 +00:00
private readonly IAgentStorageFactory<TAgent> agentStorageFactory;
2018-10-11 13:24:50 +00:00
public PlatformBuilderBase(IAgentStorageFactory<TAgent> agentStorageFactory)
{
this.agentStorageFactory = agentStorageFactory;
}
public async Task<List<TAgent>> GetAllAgents()
{
await GetStorage();
2018-10-01 17:15:17 +00:00
return await Storage.Query();
}
public async Task<TAgent> LoadAgentFromFile<TImporter>(string dataDir) where TImporter : IAgentImporter<TAgent>, new()
2018-10-03 01:44:11 +00:00
{
var meta = LoadMeta(dataDir);
var importer = new TImporter
{
AgentDir = dataDir
};
2018-10-03 01:44:11 +00:00
// Load agent summary
var agent = await importer.LoadAgent(meta);
2018-10-03 01:44:11 +00:00
// Load user custom entities
await importer.LoadCustomEntities(agent);
2018-10-03 01:44:11 +00:00
// Load agent intents
await importer.LoadIntents(agent);
2018-10-03 01:44:11 +00:00
// Load system buildin entities
await importer.LoadBuildinEntities(agent);
2018-10-03 01:44:11 +00:00
2018-10-03 06:00:07 +00:00
return agent;
2018-10-03 01:44:11 +00:00
}
private AgentImportHeader LoadMeta(string dataDir)
{
// load meta
string metaJson = File.ReadAllText(Path.Combine(dataDir, "meta.json"));
return JsonConvert.DeserializeObject<AgentImportHeader>(metaJson);
}
public async Task<TAgent> GetAgentById(string agentId)
{
2018-10-01 17:15:17 +00:00
GetStorage();
return await Storage.FetchById(agentId);
}
public async Task<TAgent> GetAgentByName(string agentName)
{
await GetStorage();
2018-10-01 17:15:17 +00:00
return await Storage.FetchByName(agentName);
}
2018-10-04 21:18:46 +00:00
public virtual async Task<ModelMetaData> Train(TAgent agent, TrainingCorpus corpus, BotTrainOptions options)
{
if (String.IsNullOrEmpty(options.AgentDir))
{
options.AgentDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", agent.Id);
}
if (String.IsNullOrEmpty(options.Model))
{
options.Model = "model_" + DateTime.UtcNow.ToString("yyyyMMdd");
}
var trainer = new BotTrainer();
agent.Corpus = corpus;
var info = await trainer.Train(agent, options);
return info;
}
public virtual async Task<bool> SaveAgent(TAgent agent)
{
await GetStorage();
2018-10-01 17:15:17 +00:00
// default save agent in FileStorage
await Storage.Persist(agent);
return true;
}
2018-10-01 17:15:17 +00:00
protected async Task<IAgentStorage<TAgent>> GetStorage()
2018-10-01 17:15:17 +00:00
{
if (Storage == null)
{
2018-10-11 13:24:50 +00:00
Storage = await agentStorageFactory.Get();
2018-10-01 17:15:17 +00:00
}
return Storage;
}
}
}