using BotSharp.Core.Engines;
using BotSharp.Core.Engines.Rasa;
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.Linq;
using System.Text;
namespace BotSharp.RestApi.Rasa
{
#if RASA_UI
///
/// send a text request
///
[Route("[controller]")]
public class ParseController : ControllerBase
{
private readonly IBotPlatform _platform;
///
/// Initialize dialog controller and get a platform instance
///
///
public ParseController(IBotPlatform platform)
{
_platform = platform;
}
///
/// parse request
///
///
///
[HttpPost, HttpGet]
public ActionResult Parse()
{
var config = new AIConfiguration("", SupportedLanguage.English);
config.SessionId = "rasa nlu";
string body = "";
using (var reader = new StreamReader(Request.Body))
{
body = reader.ReadToEnd();
}
var request = JsonConvert.DeserializeObject(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,
new AgentImportHeader
{
Id = request.Project,
Name = request.Project
});
var aIResponse = _platform.TextRequest(new AIRequest
{
Model = request.Model,
Query = new String[] { request.Text }
});
var rasaResponse = new RasaResponse
{
Intent = new RasaResponseIntent
{
Name = aIResponse.Result.Metadata.IntentName,
Confidence = aIResponse.Result.Score
},
Entities = aIResponse.Result.Entities.Select(x => new RasaResponseEntity
{
Extractor = x.Extrator,
Start = x.Start,
Entity = x.Entity,
Value = x.Value
}).ToList(),
Text = request.Text,
Model = request.Model,
Project = request.Project,
IntentRanking = new List { }
};
return rasaResponse;
}
}
#endif
}