add conversation controller.

This commit is contained in:
Oceania2018 2018-05-06 12:41:36 -05:00
parent d511d84226
commit bc045488f0
11 changed files with 85 additions and 72 deletions

View file

@ -36,6 +36,13 @@ namespace BotSharp.Core.Agents
[StringLength(32)]
public String DeveloperAccessToken { get; set; }
/// <summary>
/// Who created this bot
/// </summary>
[Required]
[StringLength(36)]
public String UserId { get; set; }
[ForeignKey("AgentId")]
public List<Intent> Intents { get; set; }

View file

@ -0,0 +1,24 @@
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace BotSharp.Core.Conversations
{
[Table("Bot_Conversation")]
public class Conversation : DbRecord, IDbRecord
{
[Required]
[StringLength(36)]
public String AgentId { get; set; }
[Required]
[StringLength(36)]
public String UserId { get; set; }
[Required]
public DateTime StartTime { get; set; }
}
}

View file

@ -5,14 +5,14 @@ using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace BotSharp.Core.Sessions
namespace BotSharp.Core.Conversations
{
[Table("Bot_SessionContext")]
public class SessionContext : DbRecord, IDbRecord
[Table("Bot_ConversationContext")]
public class ConversationContext : DbRecord, IDbRecord
{
[Required]
[StringLength(36)]
public String SessionId { get; set; }
public String ConversationId { get; set; }
[Required]
[MaxLength(64)]

View file

@ -45,10 +45,8 @@ namespace BotSharp.Core.Engines
/// <param name="importor"></param>
/// <param name="agentId"></param>
/// <returns></returns>
public Agent RestoreAgent(IAgentImporter importer, String agentId)
public Agent RestoreAgent(IAgentImporter importer, String agentId, string dataDir)
{
string dataDir = $"{Database.ContentRootPath}\\App_Data\\DbInitializer\\Agents\\";
// Load agent summary
agent = importer.LoadAgent(agentId, dataDir);

View file

@ -1,7 +1,7 @@
using BotSharp.Core.Agents;
using BotSharp.Core.Intents;
using BotSharp.Core.Models;
using BotSharp.Core.Sessions;
using BotSharp.Core.Conversations;
using DotNetToolkit;
using EntityFrameworkCore.BootKit;
using Microsoft.EntityFrameworkCore;
@ -31,8 +31,8 @@ namespace BotSharp.Core.Engines
Database dc = rasa.dc;
// Merge input contexts
var contexts = dc.Table<SessionContext>()
.Where(x => x.SessionId == rasa.AiConfig.SessionId && x.Lifespan > 0)
var contexts = dc.Table<ConversationContext>()
.Where(x => x.ConversationId == rasa.AiConfig.SessionId && x.Lifespan > 0)
.ToList()
.Select(x => new AIContext { Name = x.Context.ToLower(), Lifespan = x.Lifespan })
.ToList();
@ -148,7 +148,7 @@ namespace BotSharp.Core.Engines
// override if exists, otherwise add, delete if lifespan is zero
dc.DbTran(() =>
{
var sessionContexts = dc.Table<SessionContext>().Where(x => x.SessionId == rasa.AiConfig.SessionId).ToList();
var sessionContexts = dc.Table<ConversationContext>().Where(x => x.ConversationId == rasa.AiConfig.SessionId).ToList();
// minus 1 round
sessionContexts.Where(x => !intentResponse.Contexts.Select(ctx => ctx.Name).Contains(x.Context))
@ -163,7 +163,7 @@ namespace BotSharp.Core.Engines
{
if (ctx.Lifespan == 0)
{
dc.Table<SessionContext>().Remove(session1);
dc.Table<ConversationContext>().Remove(session1);
}
else
{
@ -172,9 +172,9 @@ namespace BotSharp.Core.Engines
}
else
{
dc.Table<SessionContext>().Add(new SessionContext
dc.Table<ConversationContext>().Add(new ConversationContext
{
SessionId = rasa.AiConfig.SessionId,
ConversationId = rasa.AiConfig.SessionId,
Context = ctx.Name,
Lifespan = ctx.Lifespan
});
@ -182,8 +182,8 @@ namespace BotSharp.Core.Engines
});
});
aiResponse.Result.Contexts = dc.Table<SessionContext>()
.Where(x => x.SessionId == rasa.AiConfig.SessionId)
aiResponse.Result.Contexts = dc.Table<ConversationContext>()
.Where(x => x.ConversationId == rasa.AiConfig.SessionId)
.Select(x => new AIContext { Name = x.Context.ToLower(), Lifespan = x.Lifespan })
.ToArray();
@ -192,7 +192,7 @@ namespace BotSharp.Core.Engines
private static IRestResponse<RasaResponse> CallRasa(string projectId, string text, string model)
{
var client = new RestClient($"{Database.Configuration.GetSection("Rasa:Host").Value}");
var client = new RestClient($"{Database.Configuration.GetSection("Rasa:Nlu").Value}");
var rest = new RestRequest("parse", Method.POST);
string json = JsonConvert.SerializeObject(new { Project = projectId, Q = text, Model = model },
@ -212,8 +212,8 @@ namespace BotSharp.Core.Engines
Database dc = rasa.dc;
// Merge input contexts
var contexts = dc.Table<SessionContext>()
.Where(x => x.SessionId == rasa.AiConfig.SessionId && x.Lifespan > 0)
var contexts = dc.Table<ConversationContext>()
.Where(x => x.ConversationId == rasa.AiConfig.SessionId && x.Lifespan > 0)
.ToList()
.Select(x => new AIContext { Name = x.Context.ToLower(), Lifespan = x.Lifespan })
.ToList();
@ -350,7 +350,7 @@ namespace BotSharp.Core.Engines
// override if exists, otherwise add, delete if lifespan is zero
dc.DbTran(() =>
{
var sessionContexts = dc.Table<SessionContext>().Where(x => x.SessionId == rasa.AiConfig.SessionId).ToList();
var sessionContexts = dc.Table<ConversationContext>().Where(x => x.ConversationId == rasa.AiConfig.SessionId).ToList();
// minus 1 round
sessionContexts.Where(x => !intentResponse.Contexts.Select(ctx => ctx.Name).Contains(x.Context))
@ -365,7 +365,7 @@ namespace BotSharp.Core.Engines
{
if (ctx.Lifespan == 0)
{
dc.Table<SessionContext>().Remove(session1);
dc.Table<ConversationContext>().Remove(session1);
}
else
{
@ -374,9 +374,9 @@ namespace BotSharp.Core.Engines
}
else
{
dc.Table<SessionContext>().Add(new SessionContext
dc.Table<ConversationContext>().Add(new ConversationContext
{
SessionId = rasa.AiConfig.SessionId,
ConversationId = rasa.AiConfig.SessionId,
Context = ctx.Name,
Lifespan = ctx.Lifespan
});
@ -384,8 +384,8 @@ namespace BotSharp.Core.Engines
});
});
aiResponse.Result.Contexts = dc.Table<SessionContext>()
.Where(x => x.SessionId == rasa.AiConfig.SessionId)
aiResponse.Result.Contexts = dc.Table<ConversationContext>()
.Where(x => x.ConversationId == rasa.AiConfig.SessionId)
.Select(x => new AIContext { Name = x.Context.ToLower(), Lifespan = x.Lifespan })
.ToArray();
@ -425,7 +425,7 @@ namespace BotSharp.Core.Engines
NullValueHandling = NullValueHandling.Ignore
});
var client = new RestClient($"{Database.Configuration.GetSection("Rasa:Host").Value}");
var client = new RestClient($"{Database.Configuration.GetSection("Rasa:Nlu").Value}");
var rest = new RestRequest("train", Method.POST);
rest.AddQueryParameter("project", console.agent.Id);
rest.AddParameter("application/json", json, ParameterType.RequestBody);

View file

@ -47,7 +47,8 @@ namespace BotSharp.UnitTest
var rasa = new RasaAi(dc);
var importer = new AgentImporterInDialogflow();
var agent = rasa.RestoreAgent(importer, BOT_NAME);
string dataDir = $"{Database.ContentRootPath}\\App_Data\\DbInitializer\\Agents\\";
var agent = rasa.RestoreAgent(importer, BOT_NAME, dataDir);
agent.Id = BOT_ID;
agent.ClientAccessToken = BOT_CLIENT_TOKEN;
agent.DeveloperAccessToken = BOT_DEVELOPER_TOKEN;

View file

@ -57,11 +57,8 @@
<None Remove="App_Data\DbInitializer\Agents\Dialogflow\VirtualAssistant\package.json" />
<None Remove="App_Data\DbInitializer\Agents\Rasa\2b6a288e-d891-40c6-96ce-6a0cf324545c.json" />
<None Remove="Settings\settings.app.json" />
<None Remove="Settings\settings.auth.json" />
<None Remove="Settings\settings.aws.json" />
<None Remove="Settings\settings.bot.json" />
<None Remove="Settings\settings.db.json" />
<None Remove="Settings\settings.swagger.json" />
</ItemGroup>
<ItemGroup>
@ -215,21 +212,12 @@
<Content Include="Settings\settings.app.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Settings\settings.auth.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Settings\settings.aws.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Settings\settings.bot.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Settings\settings.db.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Settings\settings.swagger.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>

View file

@ -17,6 +17,34 @@ namespace BotSharp.UnitTest
config.SessionId = Guid.NewGuid().ToString();
var rasa = new RasaAi(dc, config);
// Round 1
var response = rasa.TextRequest(new AIRequest { Query = new String[] { "Hi, Voiceweb" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "Wakeup");
// Round 2
response = rasa.TextRequest(new AIRequest { Query = new String[] { "I'm going to apple store to buy iphone 10" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "Transfer2SalesBot");
// Round 3
response = rasa.TextRequest(new AIRequest { Query = new String[] { "Yes" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "Transfer2SalesBot - address");
// Round 4
response = rasa.TextRequest(new AIRequest { Query = new String[] { "Sure" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "Transfer2SalesBot - confirm address");
// Round 5
response = rasa.TextRequest(new AIRequest { Query = new String[] { "That's right" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "Transfer2SalesBot - payment");
// Round 6
response = rasa.TextRequest(new AIRequest { Query = new String[] { "Yes" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "Transfer2SalesBot - place work order");
// Round 7
response = rasa.TextRequest(new AIRequest { Query = new String[] { "byebye" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "Byebye");
}
}
}

View file

@ -1,11 +0,0 @@
{
"TokenAuthentication": {
"SecretKey": "QEMYOjgkNEq/krV1Ouzz8w==",
"Subject": "OpenBotKit",
"Issuer": "Haiping Chen",
"Audience": "Haiping Chen",
"TokenPath": "/token",
"CookieName": "token",
"LoginPath": "/login"
}
}

View file

@ -1,10 +0,0 @@
{
"AWS": {
"AWSRegionEndPoint": "us-east-1",
"AWSSecretKey": "",
"AWSAccessKey": "",
"AWSEncoding": "utf-8",
"SESVerifiedEmail": "",
"AWSBucketPrefix": ""
}
}

View file

@ -1,12 +0,0 @@
{
"Swagger": {
"Version": "v1",
"Title": "Open Chatbot Kit",
"Description": "OpenBotKit API",
"TermsOfService": "MIT",
"Contact": {
"Name": "Haiping Chen",
"Email": "haiping008@gmail.com"
}
}
}