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.Drawing; using System.IO; using System.Linq; using System.Text; using Console = Colorful.Console; 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(RasaRequestModel request) { var config = new AIConfiguration("", SupportedLanguage.English); config.SessionId = "rasa nlu"; string body = ""; using (var reader = new StreamReader(Request.Body)) { body = reader.ReadToEnd(); } Console.WriteLine($"Got message from {Request.Host}: {body}", Color.Green); if(request.Project ==null && !String.IsNullOrEmpty(body)) { request = JsonConvert.DeserializeObject(body); } // Load agent var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.Project); if (String.IsNullOrEmpty(request.Model)) { request.Model = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last(); } var modelPath = Path.Combine(projectPath, request.Model); var agent = _platform.LoadAgentFromFile(modelPath); var aIResponse = _platform.TextRequest(new AIRequest { AgentDir = projectPath, 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 = agent.Name, IntentRanking = new List { new RasaResponseIntent { Name = aIResponse.Result.Metadata.IntentName, Confidence = aIResponse.Result.Score } }, Fullfillment = aIResponse.Result.Fulfillment }; return rasaResponse; } } #endif }