BotSharp/BotSharp.RestApi/Rasa/ParseController.cs

110 lines
3.5 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;
using System.Drawing;
2018-08-23 04:24:13 +00:00
using System.IO;
2018-08-24 01:05:20 +00:00
using System.Linq;
2018-08-14 14:23:15 +00:00
using System.Text;
using Console = Colorful.Console;
2018-08-14 14:23:15 +00:00
namespace BotSharp.RestApi.Rasa
{
#if RASA
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]
2018-08-28 21:45:56 +00:00
public ActionResult<RasaResponse> Parse(RasaRequestModel request)
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();
}
2018-08-28 21:45:56 +00:00
Console.WriteLine($"Got message from {Request.Host}: {body}", Color.Green);
2018-08-31 02:59:36 +00:00
if(request.Project ==null && !String.IsNullOrEmpty(body))
2018-08-28 21:45:56 +00:00
{
request = JsonConvert.DeserializeObject<RasaRequestModel>(body);
}
2018-08-23 04:24:13 +00:00
2018-08-23 12:25:55 +00:00
// Load agent
var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.Project);
2018-08-29 22:06:27 +00:00
if (String.IsNullOrEmpty(request.Model))
{
2018-08-29 22:24:12 +00:00
request.Model = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last();
2018-08-29 22:06:27 +00:00
}
2018-08-23 12:25:55 +00:00
var modelPath = Path.Combine(projectPath, request.Model);
2018-08-28 21:45:56 +00:00
var agent = _platform.LoadAgentFromFile(modelPath);
2018-08-14 14:23:15 +00:00
var aIResponse = _platform.TextRequest(new AIRequest
{
2018-08-28 21:45:56 +00:00
AgentDir = projectPath,
2018-08-23 12:25:55 +00:00
Model = request.Model,
2018-08-14 14:23:15 +00:00
Query = new String[] { request.Text }
});
2018-08-24 01:05:20 +00:00
var rasaResponse = new RasaResponse
2018-08-14 14:23:15 +00:00
{
Intent = new RasaResponseIntent
{
Name = aIResponse.Result.Metadata.IntentName,
Confidence = aIResponse.Result.Score
},
Entities = aIResponse.Result.Entities.Select(x => new RasaResponseEntity
2018-08-14 14:23:15 +00:00
{
Extractor = x.Extrator,
Start = x.Start,
Entity = x.Entity,
Value = x.Value
2018-08-24 01:05:20 +00:00
}).ToList(),
Text = request.Text,
Model = request.Model,
2018-08-28 21:45:56 +00:00
Project = agent.Name,
2018-08-29 22:24:12 +00:00
IntentRanking = new List<RasaResponseIntent>
{
new RasaResponseIntent
{
Name = aIResponse.Result.Metadata.IntentName,
Confidence = aIResponse.Result.Score
}
},
Fullfillment = aIResponse.Result.Fulfillment
2018-08-14 14:23:15 +00:00
};
2018-08-24 01:05:20 +00:00
2018-08-24 04:10:30 +00:00
return rasaResponse;
2018-08-14 14:23:15 +00:00
}
}
#endif
}