forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotPredictor.cs
More file actions
86 lines (75 loc) · 3.04 KB
/
BotPredictor.cs
File metadata and controls
86 lines (75 loc) · 3.04 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
using BotSharp.Core.Abstractions;
using BotSharp.Platform.Models;
using BotSharp.Platform.Models.AiRequest;
using BotSharp.Platform.Models.MachineLearning;
using DotNetToolkit;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Console = Colorful.Console;
namespace BotSharp.Core.Engines
{
public class BotPredictor
{
public async Task<NlpDoc> Predict(AgentBase agent, AiRequest request)
{
// load model per context
var dir = Path.Combine(request.AgentDir, request.Model);
Console.WriteLine($"Load model from {dir}");
var metaJson = File.ReadAllText(Path.Combine(dir, "model-meta.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 INlpProvider;
provider.Configuration = config.GetSection(meta.Platform);
var data = new NlpDoc
{
Sentences = new List<NlpDocSentence>
{
new NlpDocSentence
{
Text = request.Text
}
}
};
await provider.Load(agent, providerPipe);
meta.Pipeline.RemoveAt(0);
var settings = new PipeSettings
{
ModelDir = dir,
ProjectDir = request.AgentDir
};
// pipe process
var pipelines = config.GetValue<String>($"{meta.BotEngine}_{agent.Language}:pipe")
.Split(',')
.Select(x => x.Trim())
.ToList();
for(int pipeIdx = 0; pipeIdx < pipelines.Count; pipeIdx++)
{
var pipe = TypeHelper.GetInstance(pipelines[pipeIdx], assemblies) as INlpPredict;
pipe.Configuration = config.GetSection($"{meta.BotEngine}_{agent.Language}").GetSection(pipelines[pipeIdx]);
pipe.Settings = settings;
var pipeModel = meta.Pipeline.FirstOrDefault(x => x.Name == pipelines[pipeIdx]);
await pipe.Predict(agent, data, pipeModel);
}
/*Console.WriteLine($"Prediction result:", Color.Green);
Console.WriteLine(JsonConvert.SerializeObject(data, new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
}));*/
return data;
}
}
}