Repository navigation
Expand file tree
/
Copy pathPlatformBuilderBase.cs
More file actions
119 lines (93 loc) · 3.3 KB
/
Copy pathPlatformBuilderBase.cs
File metadata and controls
119 lines (93 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using BotSharp.Core.Engines;
using BotSharp.Platform.Abstraction;
using BotSharp.Platform.Models;
using BotSharp.Platform.Models.MachineLearning;
using DotNetToolkit;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Core
{
public abstract class PlatformBuilderBase<TAgent> where TAgent : AgentBase
{
public IAgentStorage<TAgent> Storage { get; set; }
private readonly IAgentStorageFactory agentStorageFactory;
public PlatformBuilderBase(IAgentStorageFactory agentStorageFactory)
{
this.agentStorageFactory = agentStorageFactory;
}
public async Task<List<TAgent>> GetAllAgents()
{
await GetStorage();
return await Storage.Query();
}
public async Task<TAgent> LoadAgentFromFile<TImporter>(string dataDir) where TImporter : IAgentImporter<TAgent>, new()
{
var meta = LoadMeta(dataDir);
var importer = new TImporter
{
AgentDir = dataDir
};
// Load agent summary
var agent = await importer.LoadAgent(meta);
// Load user custom entities
await importer.LoadCustomEntities(agent);
// Load agent intents
await importer.LoadIntents(agent);
// Load system buildin entities
await importer.LoadBuildinEntities(agent);
return agent;
}
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)
{
GetStorage();
return await Storage.FetchById(agentId);
}
public async Task<TAgent> GetAgentByName(string agentName)
{
await GetStorage();
return await Storage.FetchByName(agentName);
}
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();
// default save agent in FileStorage
await Storage.Persist(agent);
return true;
}
protected async Task<IAgentStorage<TAgent>> GetStorage()
{
if (Storage == null)
{
Storage = await agentStorageFactory.Get<TAgent>();
}
return Storage;
}
}
}