diff --git a/BotSharp.Core/Engines/Rasa/RasaResponse.cs b/BotSharp.Core/Engines/Rasa/RasaResponse.cs index af3694e6..b9f5e374 100644 --- a/BotSharp.Core/Engines/Rasa/RasaResponse.cs +++ b/BotSharp.Core/Engines/Rasa/RasaResponse.cs @@ -15,6 +15,10 @@ namespace BotSharp.Core.Models public List Entities { get; set; } public String Text { get; set; } + + public String Project { get; set; } + + public String Model { get; set; } } public class RasaResponseIntent diff --git a/BotSharp.RestApi/BotSharp.RestApi.csproj b/BotSharp.RestApi/BotSharp.RestApi.csproj index 7cbd306a..4f92570f 100644 --- a/BotSharp.RestApi/BotSharp.RestApi.csproj +++ b/BotSharp.RestApi/BotSharp.RestApi.csproj @@ -19,11 +19,11 @@ bin\Debug\netcoreapp2.1\BotSharp.RestApi.xml - TRACE;DEBUG;MODE_DIALOGFLOW + TRACE;DEBUG;MODE_RASA - TRACE;MODE_DIALOGFLOW + TRACE;MODE_DIALOGFLOW;RELEASE;NETCOREAPP;NETCOREAPP2_1;RELEASE;NETCOREAPP;NETCOREAPP2_1 diff --git a/BotSharp.RestApi/Dialogflow/QueryController.cs b/BotSharp.RestApi/Dialogflow/QueryController.cs index 15c546e3..da8ec268 100644 --- a/BotSharp.RestApi/Dialogflow/QueryController.cs +++ b/BotSharp.RestApi/Dialogflow/QueryController.cs @@ -13,7 +13,6 @@ namespace BotSharp.RestApi.Dialogflow /// /// Dialogflow mode query controller /// - [Authorize] [Route("v1/[controller]")] public class QueryController : ControllerBase { diff --git a/BotSharp.RestApi/Rasa/ConfigController.cs b/BotSharp.RestApi/Rasa/ConfigController.cs new file mode 100644 index 00000000..f86be9f4 --- /dev/null +++ b/BotSharp.RestApi/Rasa/ConfigController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.RestApi.Rasa +{ +#if MODE_RASA + [Route("[controller]")] + public class ConfigController : ControllerBase + { + [HttpGet] + public ActionResult Get() + { + var status = new RasaStatusModel(); + status.AvailableProjects = JObject.FromObject(new RasaProjectModel + { + Status = "ready", + AvailableModels = new List { "model_XXXXXX" }, + LoadedModels = new List { "model_XXXXXX" } + }); + + return Ok(status); + } + } +#endif +} diff --git a/BotSharp.RestApi/Rasa/ParseController.cs b/BotSharp.RestApi/Rasa/ParseController.cs new file mode 100644 index 00000000..5deb8e76 --- /dev/null +++ b/BotSharp.RestApi/Rasa/ParseController.cs @@ -0,0 +1,55 @@ +using BotSharp.Core.Engines; +using BotSharp.Core.Models; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.RestApi.Rasa +{ +#if MODE_RASA + [Route("[controller]")] + public class ParseController : ControllerBase + { + private readonly IBotPlatform _platform; + + /// + /// Initialize dialog controller and get a platform instance + /// + /// + public ParseController(IBotPlatform platform) + { + _platform = platform; + } + + [HttpPost] + public ActionResult Parse(RasaRequestModel request) + { + String clientAccessToken = Request.Headers["ClientAccessToken"]; + var config = new AIConfiguration(clientAccessToken, SupportedLanguage.English); + config.SessionId = "rasa nlu"; + + _platform.LoadAgent(clientAccessToken); + + var aIResponse = _platform.TextRequest(new AIRequest + { + Query = new String[] { request.Text } + }); + + return new RasaResponse + { + Intent = new RasaResponseIntent + { + Name = aIResponse.Result.Metadata.IntentName, + Confidence = aIResponse.Result.Score + }, + Entities = new List + { + + }, + Text = request.Text + }; + } + } +#endif +} diff --git a/BotSharp.RestApi/Rasa/RasaConfigModel.cs b/BotSharp.RestApi/Rasa/RasaConfigModel.cs new file mode 100644 index 00000000..382b5ee6 --- /dev/null +++ b/BotSharp.RestApi/Rasa/RasaConfigModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.RestApi.Rasa +{ + public class RasaConfigModel + { + public string Config { get; set; } + + public string Data { get; set; } + } +} diff --git a/BotSharp.RestApi/Rasa/RasaRequestModel.cs b/BotSharp.RestApi/Rasa/RasaRequestModel.cs new file mode 100644 index 00000000..902e5607 --- /dev/null +++ b/BotSharp.RestApi/Rasa/RasaRequestModel.cs @@ -0,0 +1,17 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.RestApi.Rasa +{ + public class RasaRequestModel + { + [JsonProperty("q")] + public string Text { get; set; } + + public string Project { get; set; } + + public string Model { get; set; } + } +} diff --git a/BotSharp.RestApi/Rasa/RasaStatusModel.cs b/BotSharp.RestApi/Rasa/RasaStatusModel.cs new file mode 100644 index 00000000..39154e36 --- /dev/null +++ b/BotSharp.RestApi/Rasa/RasaStatusModel.cs @@ -0,0 +1,26 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.RestApi.Rasa +{ + public class RasaStatusModel + { + [JsonProperty("available_projects")] + public JObject AvailableProjects { get; set; } + } + + public class RasaProjectModel + { + [JsonProperty("status")] + public string Status { get; set; } + + [JsonProperty("available_models")] + public List AvailableModels { get; set; } + + [JsonProperty("loaded_models")] + public List LoadedModels { get; set; } + } +} diff --git a/BotSharp.RestApi/Rasa/RasaVersionModel.cs b/BotSharp.RestApi/Rasa/RasaVersionModel.cs new file mode 100644 index 00000000..7fd69128 --- /dev/null +++ b/BotSharp.RestApi/Rasa/RasaVersionModel.cs @@ -0,0 +1,15 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.RestApi.Rasa +{ + public class RasaVersionModel + { + public string Version { get; set; } + + [JsonProperty("minimum_compatible_version")] + public string MinimumCompatibleVersion { get; set; } + } +} diff --git a/BotSharp.RestApi/Rasa/StatusController.cs b/BotSharp.RestApi/Rasa/StatusController.cs new file mode 100644 index 00000000..f6b2617f --- /dev/null +++ b/BotSharp.RestApi/Rasa/StatusController.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.RestApi.Rasa +{ +#if MODE_RASA + [Route("[controller]")] + public class StatusController : ControllerBase + { + [HttpGet] + public ActionResult Get() + { + var status = new RasaStatusModel(); + status.AvailableProjects = JObject.FromObject(new RasaProjectModel + { + Status = "ready", + AvailableModels = new List { "" } + }); + + return Ok(status); + } + } +#endif +} diff --git a/BotSharp.RestApi/Rasa/VersionController.cs b/BotSharp.RestApi/Rasa/VersionController.cs new file mode 100644 index 00000000..b3f86a0a --- /dev/null +++ b/BotSharp.RestApi/Rasa/VersionController.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.RestApi.Rasa +{ +#if MODE_RASA + [Route("[controller]")] + public class VersionController : ControllerBase + { + [HttpGet] + public ActionResult Get() + { + return Ok(new RasaVersionModel + { + Version = "0.13.0", + MinimumCompatibleVersion = "0.13.0" + }); + } + } +#endif +} diff --git a/BotSharp.WebHost/BotSharp.WebHost.csproj b/BotSharp.WebHost/BotSharp.WebHost.csproj index 146dd9b0..38607d2b 100644 --- a/BotSharp.WebHost/BotSharp.WebHost.csproj +++ b/BotSharp.WebHost/BotSharp.WebHost.csproj @@ -5,6 +5,10 @@ Portable;win10-x64;centos.7-x64 + + TRACE;DEBUG;MODE_RASA + + diff --git a/BotSharp.WebHost/Program.cs b/BotSharp.WebHost/Program.cs index 19cc1605..4bedf365 100644 --- a/BotSharp.WebHost/Program.cs +++ b/BotSharp.WebHost/Program.cs @@ -27,7 +27,11 @@ namespace BotSharp.WebHost config.AddJsonFile(setting, optional: false, reloadOnChange: true); }); }) +#if MODE_RASA + .UseUrls("http://0.0.0.0:5000") +#else .UseUrls("http://0.0.0.0:3112") +#endif .UseStartup() .Build(); }