2018-08-14 22:21:09 +00:00
|
|
|
|
using BotSharp.Core.Agents;
|
|
|
|
|
|
using BotSharp.Core.Engines;
|
|
|
|
|
|
using BotSharp.Core.Engines.Rasa;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Newtonsoft.Json;
|
2018-08-22 15:45:50 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2018-08-14 22:21:09 +00:00
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.RestApi.Rasa
|
|
|
|
|
|
{
|
2018-08-21 22:37:09 +00:00
|
|
|
|
#if RASA_UI
|
2018-08-14 22:21:09 +00:00
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
|
public class TrainController : ControllerBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IBotPlatform _platform;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize dialog controller and get a platform instance
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="platform"></param>
|
|
|
|
|
|
public TrainController(IBotPlatform platform)
|
|
|
|
|
|
{
|
|
|
|
|
|
_platform = platform;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2018-08-22 15:45:50 +00:00
|
|
|
|
public async Task<ActionResult<String>> Train(RasaUiMiddlewareRequestModel request)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
/*public async Task<ActionResult<String>> Train([FromBody] RasaTrainRequestModel request, [FromQuery] string project)
|
2018-08-14 22:21:09 +00:00
|
|
|
|
{
|
|
|
|
|
|
var trainer = new BotTrainer();
|
|
|
|
|
|
if (String.IsNullOrEmpty(request.Project))
|
|
|
|
|
|
{
|
|
|
|
|
|
request.Project = project;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// save corpus to agent dir
|
2018-08-15 22:37:23 +00:00
|
|
|
|
var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects");
|
|
|
|
|
|
var dataPath = Path.Combine(projectPath, project);
|
|
|
|
|
|
var agentPath = Path.Combine(dataPath, "Temp");
|
2018-08-14 22:21:09 +00:00
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(agentPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(agentPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-15 22:37:23 +00:00
|
|
|
|
var fileName = Path.Combine(agentPath, "corpus.json");
|
2018-08-14 22:21:09 +00:00
|
|
|
|
|
|
|
|
|
|
System.IO.File.WriteAllText(fileName, JsonConvert.SerializeObject(request.Corpus, new JsonSerializerSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
var bot = new RasaAi();
|
|
|
|
|
|
var agent = bot.LoadAgentFromFile<AgentImporterInRasa>(agentPath,
|
|
|
|
|
|
new AgentImportHeader
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = request.Project,
|
|
|
|
|
|
Name = project
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2018-08-15 10:46:53 +00:00
|
|
|
|
var info = await trainer.Train(agent);
|
2018-08-14 22:21:09 +00:00
|
|
|
|
|
2018-08-15 10:46:53 +00:00
|
|
|
|
return Ok(new { info = info.Model });
|
2018-08-22 15:45:50 +00:00
|
|
|
|
}*/
|
2018-08-14 22:21:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|