add rasa rest api.

This commit is contained in:
Oceania2018 2018-08-14 09:23:15 -05:00
parent 122b5bad71
commit 027b9fdbe4
13 changed files with 218 additions and 3 deletions

View file

@ -15,6 +15,10 @@ namespace BotSharp.Core.Models
public List<RasaResponseEntity> Entities { get; set; }
public String Text { get; set; }
public String Project { get; set; }
public String Model { get; set; }
}
public class RasaResponseIntent

View file

@ -19,11 +19,11 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>bin\Debug\netcoreapp2.1\BotSharp.RestApi.xml</DocumentationFile>
<DefineConstants>TRACE;DEBUG;MODE_DIALOGFLOW</DefineConstants>
<DefineConstants>TRACE;DEBUG;MODE_RASA</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE;MODE_DIALOGFLOW</DefineConstants>
<DefineConstants>TRACE;MODE_DIALOGFLOW;RELEASE;NETCOREAPP;NETCOREAPP2_1;RELEASE;NETCOREAPP;NETCOREAPP2_1</DefineConstants>
</PropertyGroup>
<ItemGroup>

View file

@ -13,7 +13,6 @@ namespace BotSharp.RestApi.Dialogflow
/// <summary>
/// Dialogflow mode query controller
/// </summary>
[Authorize]
[Route("v1/[controller]")]
public class QueryController : ControllerBase
{

View file

@ -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<RasaVersionModel> Get()
{
var status = new RasaStatusModel();
status.AvailableProjects = JObject.FromObject(new RasaProjectModel
{
Status = "ready",
AvailableModels = new List<string> { "model_XXXXXX" },
LoadedModels = new List<string> { "model_XXXXXX" }
});
return Ok(status);
}
}
#endif
}

View file

@ -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;
/// <summary>
/// Initialize dialog controller and get a platform instance
/// </summary>
/// <param name="platform"></param>
public ParseController(IBotPlatform platform)
{
_platform = platform;
}
[HttpPost]
public ActionResult<RasaResponse> 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<RasaResponseEntity>
{
},
Text = request.Text
};
}
}
#endif
}

View file

@ -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; }
}
}

View file

@ -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; }
}
}

View file

@ -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<string> AvailableModels { get; set; }
[JsonProperty("loaded_models")]
public List<string> LoadedModels { get; set; }
}
}

View file

@ -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; }
}
}

View file

@ -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<RasaVersionModel> Get()
{
var status = new RasaStatusModel();
status.AvailableProjects = JObject.FromObject(new RasaProjectModel
{
Status = "ready",
AvailableModels = new List<string> { "<model_XXXXXX>" }
});
return Ok(status);
}
}
#endif
}

View file

@ -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<RasaVersionModel> Get()
{
return Ok(new RasaVersionModel
{
Version = "0.13.0",
MinimumCompatibleVersion = "0.13.0"
});
}
}
#endif
}

View file

@ -5,6 +5,10 @@
<RuntimeIdentifiers>Portable;win10-x64;centos.7-x64</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;DEBUG;MODE_RASA</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Remove="App_Data\ModelFiles\**" />
<Compile Remove="App_Data\NewFolder\**" />

View file

@ -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<Startup>()
.Build();
}