Fix traing issue using Rasa UI.
This commit is contained in:
parent
f42a42d34e
commit
16ab106ce7
|
|
@ -12,7 +12,7 @@ namespace BotSharp.Core.Engines.BotSharp
|
|||
{
|
||||
agent.Corpus = GetIntentExpressions();
|
||||
var trainer = new BotTrainer(agent.Id, dc);
|
||||
await trainer.Train(agent);
|
||||
await trainer.Train(agent, new BotTrainOptions { });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
14
BotSharp.Core/Engines/BotTrainOptions.cs
Normal file
14
BotSharp.Core/Engines/BotTrainOptions.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.Core.Engines
|
||||
{
|
||||
public class BotTrainOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Model Name
|
||||
/// </summary>
|
||||
public string Model { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ namespace BotSharp.Core.Engines
|
|||
this.agentId = agentId;
|
||||
}
|
||||
|
||||
public async Task<ModelMetaData> Train(Agent agent)
|
||||
public async Task<ModelMetaData> Train(Agent agent, BotTrainOptions options)
|
||||
{
|
||||
/*agent.Intents = dc.Table<Intent>()
|
||||
.Include(x => x.Contexts)
|
||||
|
|
@ -70,7 +70,7 @@ namespace BotSharp.Core.Engines
|
|||
AlgorithmDir = Path.Combine(AppDomain.CurrentDomain.GetData("ContentRootPath").ToString(), "Algorithms")
|
||||
};
|
||||
|
||||
settings.ModelDir = Path.Combine(settings.ProjectDir, "model" + DateTime.UtcNow.ToString("MMddyyyyHHmm"));
|
||||
settings.ModelDir = Path.Combine(settings.ProjectDir, String.IsNullOrEmpty(options.Model) ? "model" + DateTime.UtcNow.ToString("MMddyyyyHHmm") : options.Model);
|
||||
|
||||
if (!Directory.Exists(settings.ProjectDir))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,13 +2,18 @@
|
|||
using BotSharp.Core.Models;
|
||||
using BotSharp.NLP;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Rasa
|
||||
{
|
||||
#if RASA_UI
|
||||
/// <summary>
|
||||
/// send a text request
|
||||
/// </summary>
|
||||
[Route("[controller]")]
|
||||
public class ParseController : ControllerBase
|
||||
{
|
||||
|
|
@ -23,14 +28,26 @@ namespace BotSharp.RestApi.Rasa
|
|||
_platform = platform;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult<RasaResponse> Parse(RasaRequestModel request)
|
||||
/// <summary>
|
||||
/// parse request
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost, HttpGet]
|
||||
public ActionResult<RasaResponse> Parse()
|
||||
{
|
||||
String clientAccessToken = Request.Headers["ClientAccessToken"];
|
||||
var config = new AIConfiguration(clientAccessToken, SupportedLanguage.English);
|
||||
var config = new AIConfiguration("", SupportedLanguage.English);
|
||||
config.SessionId = "rasa nlu";
|
||||
|
||||
_platform.LoadAgent(clientAccessToken);
|
||||
string body = "";
|
||||
using (var reader = new StreamReader(Request.Body))
|
||||
{
|
||||
body = reader.ReadToEnd();
|
||||
}
|
||||
var request = JsonConvert.DeserializeObject<RasaRequestModel>(body);
|
||||
|
||||
//_platform.LoadAgent(clientAccessToken);
|
||||
|
||||
var aIResponse = _platform.TextRequest(new AIRequest
|
||||
{
|
||||
|
|
@ -48,7 +65,7 @@ namespace BotSharp.RestApi.Rasa
|
|||
{
|
||||
|
||||
},
|
||||
Text = request.Text
|
||||
Text = ""
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ namespace BotSharp.RestApi.Rasa
|
|||
{
|
||||
public string Project { get; set; }
|
||||
|
||||
public string Model { get; set; }
|
||||
|
||||
[JsonProperty("rasa_nlu_data")]
|
||||
public RasaTrainingData Corpus { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Rasa
|
||||
{
|
||||
public class RasaUiMiddlewareRequestDataModel
|
||||
{
|
||||
public string Project { get; set; }
|
||||
public string Agent { get; set; }
|
||||
public RasaTrainRequestModel Data { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Rasa
|
||||
{
|
||||
public class RasaUiMiddlewareRequestModel
|
||||
{
|
||||
[JsonProperty("ip_address")]
|
||||
public string IP { get; set; }
|
||||
|
||||
public string Query { get; set; }
|
||||
|
||||
[JsonProperty("event_type")]
|
||||
public string EventType { get; set; }
|
||||
|
||||
/*[JsonProperty("event_data")]
|
||||
public T EventData { get; set; }*/
|
||||
}
|
||||
}
|
||||
|
|
@ -1,24 +1,67 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using BotSharp.Core.Engines;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Rasa
|
||||
{
|
||||
#if RASA_UI
|
||||
/// <summary>
|
||||
/// This returns all the currently available projects.
|
||||
/// </summary>
|
||||
[Route("[controller]")]
|
||||
public class StatusController : ControllerBase
|
||||
{
|
||||
private readonly IBotPlatform _platform;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize status controller and get a platform instance
|
||||
/// </summary>
|
||||
/// <param name="platform"></param>
|
||||
public StatusController(IBotPlatform platform)
|
||||
{
|
||||
_platform = platform;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of available projects the server can use to fulfill /parse requests.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public ActionResult<RasaVersionModel> Get()
|
||||
{
|
||||
var status = new RasaStatusModel();
|
||||
status.AvailableProjects = JObject.FromObject(new RasaProjectModel
|
||||
status.AvailableProjects = JObject.FromObject(new { });
|
||||
|
||||
// scan dir, get all models
|
||||
var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects");
|
||||
var projectDirs = Directory.GetDirectories(projectPath);
|
||||
for(int idx = 0; idx < projectDirs.Length; idx++)
|
||||
{
|
||||
Status = "ready",
|
||||
AvailableModels = new List<string> { "<model_XXXXXX>" }
|
||||
});
|
||||
string project = projectDirs[idx].Split('\\').Last();
|
||||
var modelDirs = Directory.GetDirectories(projectDirs[idx]);
|
||||
|
||||
List<string> availableModels = new List<string>();
|
||||
|
||||
for (int mIdx = 0; mIdx < modelDirs.Length; mIdx++)
|
||||
{
|
||||
string model = modelDirs[mIdx].Split('\\').Last();
|
||||
if (model.StartsWith(project + "_"))
|
||||
{
|
||||
availableModels.Add(model);
|
||||
}
|
||||
}
|
||||
|
||||
status.AvailableProjects.Add(project, JObject.FromObject(new RasaProjectModel
|
||||
{
|
||||
Status = "ready",
|
||||
AvailableModels = availableModels
|
||||
}));
|
||||
}
|
||||
|
||||
return Ok(status);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,10 @@ using System.Threading.Tasks;
|
|||
namespace BotSharp.RestApi.Rasa
|
||||
{
|
||||
#if RASA_UI
|
||||
/// <summary>
|
||||
/// You can post your training data to this endpoint to train a new model for a project.
|
||||
/// This request will wait for the server answer: either the model was trained successfully or the training exited with an error.
|
||||
/// </summary>
|
||||
[Route("[controller]")]
|
||||
public class TrainController : ControllerBase
|
||||
{
|
||||
|
|
@ -29,12 +33,29 @@ namespace BotSharp.RestApi.Rasa
|
|||
_platform = platform;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Using the HTTP server, you must specify the project you want to train a new model for to be able to use it during parse requests later on : /train?project=my_project.
|
||||
/// </summary>
|
||||
/// <param name="agent">Model name</param>
|
||||
/// <param name="project"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<String>> Train(RasaUiMiddlewareRequestModel request)
|
||||
public async Task<ActionResult<String>> Train([FromQuery] string agent, [FromQuery] string project)
|
||||
{
|
||||
return Ok();
|
||||
string body = "";
|
||||
using (var reader = new StreamReader(Request.Body))
|
||||
{
|
||||
body = reader.ReadToEnd();
|
||||
}
|
||||
|
||||
var rasa_nlu_data = JsonConvert.DeserializeObject<RasaTrainRequestModel>(body);
|
||||
rasa_nlu_data.Model = agent;
|
||||
var trainResult = await Train(rasa_nlu_data, project);
|
||||
|
||||
return trainResult;
|
||||
}
|
||||
/*public async Task<ActionResult<String>> Train([FromBody] RasaTrainRequestModel request, [FromQuery] string project)
|
||||
|
||||
private async Task<ActionResult<String>> Train([FromBody] RasaTrainRequestModel request, [FromQuery] string project)
|
||||
{
|
||||
var trainer = new BotTrainer();
|
||||
if (String.IsNullOrEmpty(request.Project))
|
||||
|
|
@ -52,6 +73,8 @@ namespace BotSharp.RestApi.Rasa
|
|||
Directory.CreateDirectory(agentPath);
|
||||
}
|
||||
|
||||
// Save raw data to file, then parse it to Agent instance.
|
||||
// in order to unify the process.
|
||||
var fileName = Path.Combine(agentPath, "corpus.json");
|
||||
|
||||
System.IO.File.WriteAllText(fileName, JsonConvert.SerializeObject(request.Corpus, new JsonSerializerSettings
|
||||
|
|
@ -69,10 +92,10 @@ namespace BotSharp.RestApi.Rasa
|
|||
Name = project
|
||||
});
|
||||
|
||||
var info = await trainer.Train(agent);
|
||||
var info = await trainer.Train(agent, new BotTrainOptions { Model = request.Model });
|
||||
|
||||
return Ok(new { info = info.Model });
|
||||
}*/
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue