Dialogflow compatibility improvement 1

This commit is contained in:
Oceania2018 2018-09-21 15:11:57 -05:00
parent 67418fa309
commit 6898d851f7
18 changed files with 101 additions and 90 deletions

1
.gitignore vendored
View file

@ -289,3 +289,4 @@ __pycache__/
/BotSharp.WebHost/PublishOutput
/Data
/docs/_build
*.RestApi.xml

View file

@ -19,4 +19,9 @@
<DefineConstants>TRACE;DEBUG;RASA</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DIALOGFLOW|AnyCPU'">
<DefineConstants>DEBUG;TRACE;DIALOGFLOW;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
<Optimize>false</Optimize>
</PropertyGroup>
</Project>

View file

@ -27,6 +27,12 @@ namespace BotSharp.Core.Engines
/// <returns></returns>
Agent LoadAgentFromFile(string dataDir);
/// <summary>
/// Save agent data to database
/// </summary>
/// <returns></returns>
String SaveAgentToDb();
AIResponse TextRequest(AIRequest request);
Task Train(BotTrainOptions options);

View file

@ -45,6 +45,11 @@ If you feel that this project is helpful to you, please Star on the project, we
<DefineConstants>TRACE;</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DIALOGFLOW|AnyCPU'">
<DefineConstants>DEBUG;TRACE;DIALOGFLOW;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
<Optimize>false</Optimize>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Colorful.Console" Version="1.2.9" />
<PackageReference Include="DotNetToolkit" Version="1.6.0" />

View file

@ -92,22 +92,6 @@ namespace BotSharp.Core.Engines
return agent;
}
/// <summary>
/// Restore a agent instance from backup json files
/// </summary>
/// <param name="importer"></param>
/// <param name="dataDir"></param>
/// <returns></returns>
public bool RestoreAgent<TAgentImporter>(string dataDir) where TAgentImporter : IAgentImporter, new()
{
int row = dc.DbTran(() => {
LoadAgentFromFile(dataDir);
SaveAgent();
});
return row > 0;
}
public Agent LoadAgentFromFile(string dataDir)
{
var meta = LoadMeta(dataDir);
@ -159,19 +143,22 @@ namespace BotSharp.Core.Engines
return JsonConvert.DeserializeObject<AgentImportHeader>(metaJson);
}
public String SaveAgent()
public String SaveAgentToDb()
{
var existedAgent = dc.Table<Agent>().FirstOrDefault(x => x.Id == agent.Id || x.Name == agent.Name);
if (existedAgent == null)
dc.DbTran(() =>
{
dc.Table<Agent>().Add(agent);
return agent.Id;
}
else
{
agent.Id = existedAgent.Id;
return existedAgent.Id;
}
var existedAgent = dc.Table<Agent>().FirstOrDefault(x => x.Id == agent.Id || x.Name == agent.Name);
if (existedAgent == null)
{
dc.Table<Agent>().Add(agent);
}
else
{
agent.Id = existedAgent.Id;
}
});
return agent.Id;
}
public TrainingCorpus GetIntentExpressions(Agent agent)

View file

@ -35,6 +35,11 @@ Naive Bayes Classifier</Description>
<OutputPath>bin\RASA</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DIALOGFLOW|AnyCPU'">
<DefineConstants>DEBUG;TRACE;DIALOGFLOW;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
<Optimize>false</Optimize>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

View file

@ -70,6 +70,7 @@ namespace BotSharp.RestApi
}
var filePath = Path.GetTempFileName();
Console.WriteLine($"Temp file saved to {filePath}");
using (var stream = new FileStream(filePath, FileMode.Create))
{
@ -77,12 +78,19 @@ namespace BotSharp.RestApi
}
string dest = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", uploadedFile.FileName.Split('.').First(), "model_" + DateTime.UtcNow.ToString("MMddyyyyHHmm"));
Console.WriteLine($"Extract zip file to {dest}");
ZipFile.ExtractToDirectory(filePath, dest);
System.IO.File.Delete(filePath);
Console.WriteLine($"LoadAgentFromFile {dest}");
var agent = _platform.LoadAgentFromFile(dest);
#if DIALOGFLOW
_platform.SaveAgentToDb();
#endif
return Ok(agent.Id);
}
@ -91,7 +99,7 @@ namespace BotSharp.RestApi
/// </summary>
/// <param name="agentId"></param>
/// <returns></returns>
[HttpGet("{agentId}")]
/*[HttpGet("{agentId}")]
public string Train([FromRoute] String agentId)
{
string agentDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", agentId);
@ -100,7 +108,7 @@ namespace BotSharp.RestApi
_platform.Train(new BotTrainOptions { AgentDir = agentDir, Model = dest.Split(Path.DirectorySeparatorChar).Last() });
return "";
}
}*/
/// <summary>
/// Dump agent

View file

@ -62,6 +62,7 @@
<WarningLevel>1</WarningLevel>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<DocumentationFile>BotSharp.RestApi.xml</DocumentationFile>
<Optimize>false</Optimize>
</PropertyGroup>
<ItemGroup>
@ -70,6 +71,10 @@
<None Remove="Authentication\**" />
</ItemGroup>
<ItemGroup>
<None Remove="BotSharp.RestApi.xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.2" />
</ItemGroup>

View file

@ -22,13 +22,6 @@
<param name="file"></param>
<returns></returns>
</member>
<member name="M:BotSharp.RestApi.AgentController.Train(System.String)">
<summary>
Train agent
</summary>
<param name="agentId"></param>
<returns></returns>
</member>
<member name="M:BotSharp.RestApi.AgentController.Dump(System.String)">
<summary>
Dump agent
@ -36,6 +29,26 @@
<param name="agentId"></param>
<returns></returns>
</member>
<member name="T:BotSharp.RestApi.Dialogflow.QueryController">
<summary>
Dialogflow mode query controller
</summary>
</member>
<member name="M:BotSharp.RestApi.Dialogflow.QueryController.#ctor(BotSharp.Core.Engines.IBotPlatform)">
<summary>
Initialize dialog controller and get a platform instance
</summary>
<param name="platform"></param>
</member>
<member name="M:BotSharp.RestApi.Dialogflow.QueryController.Query(BotSharp.RestApi.Dialogflow.QueryModel)">
<summary>
The query endpoint is used to process natural language in the form of text.
The query requests return structured data in JSON format with an action and parameters for that action.
Both GET and POST methods return the same JSON response.
</summary>
<param name="request"></param>
<returns></returns>
</member>
<member name="P:BotSharp.RestApi.Dialogflow.QueryModel.Contexts">
<summary>
Array of additional context objects. Should be sent via a POST /query request.
@ -68,41 +81,6 @@
Time zone from IANA Time Zone Database Examples: America/New_York, Europe/Paris
</summary>
</member>
<member name="T:BotSharp.RestApi.Rasa.ParseController">
<summary>
send a text request
</summary>
</member>
<member name="M:BotSharp.RestApi.Rasa.ParseController.#ctor(BotSharp.Core.Engines.IBotPlatform)">
<summary>
Initialize dialog controller and get a platform instance
</summary>
<param name="platform"></param>
</member>
<member name="M:BotSharp.RestApi.Rasa.ParseController.Parse(BotSharp.RestApi.Rasa.RasaRequestModel)">
<summary>
parse request
</summary>
<param name="request"></param>
<returns></returns>
</member>
<member name="T:BotSharp.RestApi.Rasa.StatusController">
<summary>
This returns all the currently available projects.
</summary>
</member>
<member name="M:BotSharp.RestApi.Rasa.StatusController.#ctor(BotSharp.Core.Engines.IBotPlatform)">
<summary>
Initialize status controller and get a platform instance
</summary>
<param name="platform"></param>
</member>
<member name="M:BotSharp.RestApi.Rasa.StatusController.Get">
<summary>
Returns a list of available projects the server can use to fulfill /parse requests.
</summary>
<returns></returns>
</member>
<member name="T:BotSharp.RestApi.Rasa.TrainController">
<summary>
You can post your training data to this endpoint to train a new model for a project.
@ -115,14 +93,6 @@
</summary>
<param name="platform"></param>
</member>
<member name="M:BotSharp.RestApi.Rasa.TrainController.Train(System.String,System.String)">
<summary>
Using the HTTP server, you must specify the project you want to train a new model for to be able to use it during parse requests later on : /train?project=my_project.
</summary>
<param name="model">Model name</param>
<param name="project">Agent name or agent id</param>
<returns></returns>
</member>
<member name="P:BotSharp.RestApi.Integrations.FacebookMessenger.WebhookMessageRecipient.Id">
<summary>
PAGE_ID

View file

@ -76,6 +76,13 @@ namespace BotSharp.RestApi.Dialogflow
// Load agent
string model = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last();
string dataDir = Path.Combine(projectPath, model);
if(!System.IO.File.Exists(Path.Combine(dataDir, "model-meta.json")))
{
return BadRequest("The agent hasn't been trained. Please train the agent before querying.");
}
Console.WriteLine($"LoadAgentFromFile {dataDir}");
_platform.LoadAgentFromFile(dataDir);

View file

@ -44,6 +44,8 @@ namespace BotSharp.RestApi.Rasa
var model = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last();
string dataDir = Path.Combine(projectPath, model);
Console.WriteLine($"LoadAgentFromFile {dataDir}");
var agent = _platform.LoadAgentFromFile(dataDir);
var info = await trainer.Train(agent, new BotTrainOptions

View file

@ -16,6 +16,11 @@
<OutputPath>bin\RASA</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DIALOGFLOW|AnyCPU'">
<Optimize>false</Optimize>
<DefineConstants>DEBUG;TRACE;DIALOGFLOW;NETCOREAPP;NETCOREAPP2_1</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Remove="App_Data\AgentArchive\**" />
<Compile Remove="App_Data\Corpus\**" />

View file

@ -7,4 +7,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RASA|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DIALOGFLOW|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>

View file

@ -35,11 +35,7 @@ namespace BotSharp.WebHost
config.AddJsonFile(setting, optional: false, reloadOnChange: true);
});
})
#if RASA
.UseUrls("http://0.0.0.0:5000")
#else
.UseUrls("http://0.0.0.0:3112")
#endif
.UseStartup<Startup>()
.Build();
}

View file

@ -17,7 +17,6 @@
},
"BotSharp.WebHost": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},

View file

@ -91,6 +91,12 @@ namespace BotSharp.WebHost
{
var info = Configuration.GetSection("Swagger").Get<Info>();
#if DIALOGFLOW
info.Title += " (DIALOGFLOW)";
#elif RASA
info.Title += " (RASA)";
#endif
c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Post, SubmitMethod.Put, SubmitMethod.Patch, SubmitMethod.Delete);
c.ShowExtensions();
c.SwaggerEndpoint(Configuration.GetValue<String>("Swagger:Endpoint"), info.Title);

View file

@ -5,7 +5,7 @@ WORKDIR /source
# copies the rest of your code
COPY . .
# RUN dotnet build
RUN dotnet publish BotSharp.WebHost/BotSharp.WebHost.csproj --configuration RASA --output /app
RUN dotnet publish BotSharp.WebHost/BotSharp.WebHost.csproj --configuration DIALOGFLOW --output /app
# copy Settings folder
WORKDIR /app

View file

@ -3,14 +3,12 @@
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
.. image:: https://raw.githubusercontent.com/Oceania2018/BotSharp/master/BotSharp.WebHost/wwwroot/images/BotSharp.png
:height: 30px
The Open Source AI Bot Platform Builder
======================================================
.. image:: https://img.shields.io/badge/gitter-join%20chat-brightgreen.svg
:target: `gitter`_
The Open Source AI Bot Platform Builder
======================================================
*"Conversation as a platform (CaaP) is the future, so it's perfect that we're already offering the whole toolkits to .NET developers using the BotSharp Bot Platform Builder to build a CaaP. It opens up as much learning power as possible for your robots and precisely control every step of the AI processing pipeline."*
@ -22,6 +20,9 @@ Why we do this? because we all know python is not friendly programming language
BotSharp is in accordance with components princple strictly, decouples every part that needed in the platform builder. So you can choose different UI/UX, or pick up a different NLP Tagger, or select a more advanced algrithm to do NER task. They are all modulized based an unfied interfaces.
.. image:: https://raw.githubusercontent.com/Oceania2018/BotSharp/master/BotSharp.WebHost/wwwroot/images/BotSharp.png
:height: 30px
Some Features
-------------