add agentDir.

This commit is contained in:
Oceania2018 2018-08-28 16:45:56 -05:00
parent 8a12e2e825
commit 0f6a101de2
12 changed files with 1709 additions and 1676 deletions

View file

@ -51,7 +51,7 @@ namespace BotSharp.Core.UnitTest
agents.ForEach(agentHeader => {
var bot = new BotSharpAi();
bot.RestoreAgent<AgentImporterInDialogflow>();
bot.RestoreAgent<AgentImporterInDialogflow>(dataPath);
});
}

View file

@ -26,13 +26,9 @@ namespace BotSharp.Core.Engines
protected Agent agent { get; set; }
public String DbInitializerPath { get; private set; }
public BotEngineBase()
{
dc = new DefaultDataContextLoader().GetDefaultDc();
string dataPath = AppDomain.CurrentDomain.GetData("DataPath").ToString();
DbInitializerPath = Path.Combine(dataPath, $"DbInitializer");
}
public AIResponse TextRequest(AIRequest request)
@ -89,10 +85,8 @@ namespace BotSharp.Core.Engines
/// <param name="importer"></param>
/// <param name="dataDir"></param>
/// <returns></returns>
public bool RestoreAgent<TAgentImporter>() where TAgentImporter : IAgentImporter, new()
public bool RestoreAgent<TAgentImporter>(string dataDir) where TAgentImporter : IAgentImporter, new()
{
string dataDir = Path.Combine(DbInitializerPath, "Agents");
int row = dc.DbTran(() => {
LoadAgentFromFile(dataDir);
SaveAgent();

View file

@ -20,9 +20,9 @@ namespace BotSharp.Core.Engines
public async Task<NlpDoc> Predict(Agent agent, AIRequest request)
{
// load model
var dir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", agent.Id, request.Model);
var dir = Path.Combine(request.AgentDir, request.Model);
Console.WriteLine($"Load model from {dir}");
var metaJson = File.ReadAllText(Path.Combine(dir, "metadata.json"));
var metaJson = File.ReadAllText(Path.Combine(dir, "model-meta.json"));
var meta = JsonConvert.DeserializeObject<ModelMetaData>(metaJson);
// Get NLP Provider
@ -50,7 +50,7 @@ namespace BotSharp.Core.Engines
var settings = new PipeSettings
{
ModelDir = dir,
ProjectDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", agent.Id),
ProjectDir = request.AgentDir,
AlgorithmDir = Path.Combine(AppDomain.CurrentDomain.GetData("ContentRootPath").ToString(), "Algorithms")
};

View file

@ -57,11 +57,11 @@ namespace BotSharp.Core.Engines
var settings = new PipeSettings
{
ProjectDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", agent.Id),
ProjectDir = Path.Combine(options.AgentDir),
AlgorithmDir = Path.Combine(AppDomain.CurrentDomain.GetData("ContentRootPath").ToString(), "Algorithms")
};
settings.ModelDir = Path.Combine(settings.ProjectDir, String.IsNullOrEmpty(options.Model) ? "model" + DateTime.UtcNow.ToString("MMddyyyyHHmm") : options.Model);
settings.ModelDir = Path.Combine(options.AgentDir, options.Model);
if (!Directory.Exists(settings.ProjectDir))
{
@ -117,7 +117,7 @@ namespace BotSharp.Core.Engines
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
File.WriteAllText(Path.Combine(settings.ModelDir, "metadata.json"), metaJson);
File.WriteAllText(Path.Combine(settings.ModelDir, "model-meta.json"), metaJson);
Console.WriteLine(metaJson);

View file

@ -31,6 +31,7 @@ namespace BotSharp.Core.Engines.Classifiers
doc.Sentences[0].Intent = new TextClassificationResult
{
Classifier = "FasttextClassifier",
Label = output.Split(' ')[0].Split(new string[] { "__label__" }, StringSplitOptions.None)[1],
Confidence = decimal.Parse(output.Split(' ')[1])
};

View file

@ -17,6 +17,11 @@ namespace BotSharp.Core.Models
public OriginalRequest OriginalRequest { get; set; }
/// <summary>
/// Agent directory
/// </summary>
public string AgentDir { get; set; }
/// <summary>
/// What model is used to predict.
/// </summary>

View file

@ -6,6 +6,8 @@ namespace BotSharp.Core.Engines
{
public class TextClassificationResult
{
public String Classifier { get; set; }
public String Label { get; set; }
public Decimal Confidence { get; set; }

View file

@ -82,7 +82,7 @@ namespace BotSharp.RestApi
public string Train([FromRoute] String agentId)
{
string agentDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", agentId);
string dest = Directory.GetDirectories(agentDir).Last();
string dest = Directory.GetDirectories(agentDir).Where(x => x.Contains("model_")).Last();
var agent = _platform.LoadAgentFromFile(dest);
_platform.Train(new BotTrainOptions { AgentDir = agentDir, Model = dest.Split(Path.DirectorySeparatorChar).Last() });

View file

@ -36,7 +36,7 @@ namespace BotSharp.RestApi.Rasa
/// <param name="request"></param>
/// <returns></returns>
[HttpPost, HttpGet]
public ActionResult<RasaResponse> Parse()
public ActionResult<RasaResponse> Parse(RasaRequestModel request)
{
var config = new AIConfiguration("", SupportedLanguage.English);
config.SessionId = "rasa nlu";
@ -46,16 +46,21 @@ namespace BotSharp.RestApi.Rasa
{
body = reader.ReadToEnd();
}
var request = JsonConvert.DeserializeObject<RasaRequestModel>(body);
if(request ==null && !String.IsNullOrEmpty(body))
{
request = JsonConvert.DeserializeObject<RasaRequestModel>(body);
}
// Load agent
var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.Project);
var modelPath = Path.Combine(projectPath, request.Model);
_platform.LoadAgentFromFile(modelPath);
var agent = _platform.LoadAgentFromFile(modelPath);
var aIResponse = _platform.TextRequest(new AIRequest
{
AgentDir = projectPath,
Model = request.Model,
Query = new String[] { request.Text }
});
@ -76,7 +81,7 @@ namespace BotSharp.RestApi.Rasa
}).ToList(),
Text = request.Text,
Model = request.Model,
Project = request.Project,
Project = agent.Name,
IntentRanking = new List<RasaResponseIntent> { }
};

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Linq;
namespace BotSharp.WebHost
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
Microsoft.AspNetCore.WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
var settings = Directory.GetFiles(Path.Combine(env.ContentRootPath, "Settings"), "*.json");
settings.ToList().ForEach(setting =>
{
config.AddJsonFile(setting, optional: false, reloadOnChange: true);
});
})
#if RASA_UI
.UseUrls("http://0.0.0.0:5000")
#else
.UseUrls("http://0.0.0.0:3112")
#endif
.UseStartup<Startup>()
.Build();
}
}

View file

@ -15,15 +15,6 @@ Make sure you've got `Docker`_ installed.
Point your web browser at http://localhost:5001 and enjoy Rasa-UI with BotSharp.
Integrate with `Articulate UI`_, you can use docker compose to run.
Make sure you've got `Docker`_ installed.
::
PS D:\BotSharp\> docker-compose -f docker-compose-articulateui.yml up
Point your web browser at http://localhost:3000 and enjoy Rasa-UI with BotSharp.
Building BotSharp
^^^^^^^^^^^^^^^^^
Make sure the `Microsoft .NET Core`_ build environment is installed.
@ -77,4 +68,4 @@ Use BotSharp.NLP as a natural language processing toolkit alone.
.. _Rasa UI: https://github.com/paschmann/rasa-ui
.. _Articulate UI: https://spg.ai/projects/articulate
.. _Microsoft .NET Core: https://www.microsoft.com/net/download
.. _Docker: https://www.docker.com
.. _Docker: https://www.docker.com