BotSharp/BotSharp.RestApi/Rasa/ParseController.cs

84 lines
2.4 KiB
C#
Raw Normal View History

2018-08-14 14:23:15 +00:00
using BotSharp.Core.Engines;
2018-08-23 12:25:55 +00:00
using BotSharp.Core.Engines.Rasa;
2018-08-14 14:23:15 +00:00
using BotSharp.Core.Models;
using BotSharp.NLP;
2018-08-14 14:23:15 +00:00
using Microsoft.AspNetCore.Mvc;
2018-08-23 04:24:13 +00:00
using Newtonsoft.Json;
2018-08-14 14:23:15 +00:00
using System;
using System.Collections.Generic;
2018-08-23 04:24:13 +00:00
using System.IO;
2018-08-14 14:23:15 +00:00
using System.Text;
namespace BotSharp.RestApi.Rasa
{
#if RASA_UI
2018-08-23 04:24:13 +00:00
/// <summary>
/// send a text request
/// </summary>
2018-08-14 14:23:15 +00:00
[Route("[controller]")]
public class ParseController : ControllerBase
{
private readonly IBotPlatform _platform;
/// <summary>
/// Initialize dialog controller and get a platform instance
/// </summary>
/// <param name="platform"></param>
public ParseController(IBotPlatform platform)
{
_platform = platform;
}
2018-08-23 04:24:13 +00:00
/// <summary>
/// parse request
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost, HttpGet]
public ActionResult<RasaResponse> Parse()
2018-08-14 14:23:15 +00:00
{
2018-08-23 04:24:13 +00:00
var config = new AIConfiguration("", SupportedLanguage.English);
2018-08-14 14:23:15 +00:00
config.SessionId = "rasa nlu";
2018-08-23 04:24:13 +00:00
string body = "";
using (var reader = new StreamReader(Request.Body))
{
body = reader.ReadToEnd();
}
var request = JsonConvert.DeserializeObject<RasaRequestModel>(body);
2018-08-23 12:25:55 +00:00
// Load agent
var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.Project);
var modelPath = Path.Combine(projectPath, request.Model);
_platform.LoadAgentFromFile<AgentImporterInRasa>(modelPath,
new AgentImportHeader
{
Id = request.Project,
Name = request.Project
});
2018-08-14 14:23:15 +00:00
var aIResponse = _platform.TextRequest(new AIRequest
{
2018-08-23 12:25:55 +00:00
Model = request.Model,
2018-08-14 14:23:15 +00:00
Query = new String[] { request.Text }
});
return new RasaResponse
{
Intent = new RasaResponseIntent
{
Name = aIResponse.Result.Metadata.IntentName,
Confidence = aIResponse.Result.Score
},
Entities = new List<RasaResponseEntity>
{
},
2018-08-23 04:24:13 +00:00
Text = ""
2018-08-14 14:23:15 +00:00
};
}
}
#endif
}