From bc045488f03022a477390e9c7c9f4642c5cd583d Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 6 May 2018 12:41:36 -0500 Subject: [PATCH] add conversation controller. --- BotSharp.Core/Agents/Agent.cs | 7 ++++ BotSharp.Core/Conversations/Conversation.cs | 24 ++++++++++++ .../ConversationContext.cs} | 8 ++-- BotSharp.Core/Engines/RasaAi.cs | 4 +- BotSharp.Core/Engines/RequestExtension.cs | 38 +++++++++---------- BotSharp.UnitTest/AgentTest.cs | 3 +- BotSharp.UnitTest/BotSharp.UnitTest.csproj | 12 ------ BotSharp.UnitTest/IntentTest.cs | 28 ++++++++++++++ BotSharp.UnitTest/Settings/settings.auth.json | 11 ------ BotSharp.UnitTest/Settings/settings.aws.json | 10 ----- .../Settings/settings.swagger.json | 12 ------ 11 files changed, 85 insertions(+), 72 deletions(-) create mode 100644 BotSharp.Core/Conversations/Conversation.cs rename BotSharp.Core/{Sessions/SessionContext.cs => Conversations/ConversationContext.cs} (67%) delete mode 100644 BotSharp.UnitTest/Settings/settings.auth.json delete mode 100644 BotSharp.UnitTest/Settings/settings.aws.json delete mode 100644 BotSharp.UnitTest/Settings/settings.swagger.json diff --git a/BotSharp.Core/Agents/Agent.cs b/BotSharp.Core/Agents/Agent.cs index c24dc327..ee60cb35 100644 --- a/BotSharp.Core/Agents/Agent.cs +++ b/BotSharp.Core/Agents/Agent.cs @@ -36,6 +36,13 @@ namespace BotSharp.Core.Agents [StringLength(32)] public String DeveloperAccessToken { get; set; } + /// + /// Who created this bot + /// + [Required] + [StringLength(36)] + public String UserId { get; set; } + [ForeignKey("AgentId")] public List Intents { get; set; } diff --git a/BotSharp.Core/Conversations/Conversation.cs b/BotSharp.Core/Conversations/Conversation.cs new file mode 100644 index 00000000..14b2fd7f --- /dev/null +++ b/BotSharp.Core/Conversations/Conversation.cs @@ -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; } + } +} diff --git a/BotSharp.Core/Sessions/SessionContext.cs b/BotSharp.Core/Conversations/ConversationContext.cs similarity index 67% rename from BotSharp.Core/Sessions/SessionContext.cs rename to BotSharp.Core/Conversations/ConversationContext.cs index 54b3704b..6fb14225 100644 --- a/BotSharp.Core/Sessions/SessionContext.cs +++ b/BotSharp.Core/Conversations/ConversationContext.cs @@ -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)] diff --git a/BotSharp.Core/Engines/RasaAi.cs b/BotSharp.Core/Engines/RasaAi.cs index ffd85318..8cb7a3bf 100644 --- a/BotSharp.Core/Engines/RasaAi.cs +++ b/BotSharp.Core/Engines/RasaAi.cs @@ -45,10 +45,8 @@ namespace BotSharp.Core.Engines /// /// /// - 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); diff --git a/BotSharp.Core/Engines/RequestExtension.cs b/BotSharp.Core/Engines/RequestExtension.cs index 5c09db90..ed25a9d8 100644 --- a/BotSharp.Core/Engines/RequestExtension.cs +++ b/BotSharp.Core/Engines/RequestExtension.cs @@ -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() - .Where(x => x.SessionId == rasa.AiConfig.SessionId && x.Lifespan > 0) + var contexts = dc.Table() + .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().Where(x => x.SessionId == rasa.AiConfig.SessionId).ToList(); + var sessionContexts = dc.Table().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().Remove(session1); + dc.Table().Remove(session1); } else { @@ -172,9 +172,9 @@ namespace BotSharp.Core.Engines } else { - dc.Table().Add(new SessionContext + dc.Table().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() - .Where(x => x.SessionId == rasa.AiConfig.SessionId) + aiResponse.Result.Contexts = dc.Table() + .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 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() - .Where(x => x.SessionId == rasa.AiConfig.SessionId && x.Lifespan > 0) + var contexts = dc.Table() + .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().Where(x => x.SessionId == rasa.AiConfig.SessionId).ToList(); + var sessionContexts = dc.Table().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().Remove(session1); + dc.Table().Remove(session1); } else { @@ -374,9 +374,9 @@ namespace BotSharp.Core.Engines } else { - dc.Table().Add(new SessionContext + dc.Table().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() - .Where(x => x.SessionId == rasa.AiConfig.SessionId) + aiResponse.Result.Contexts = dc.Table() + .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); diff --git a/BotSharp.UnitTest/AgentTest.cs b/BotSharp.UnitTest/AgentTest.cs index 5ead8885..4a9d7ae5 100644 --- a/BotSharp.UnitTest/AgentTest.cs +++ b/BotSharp.UnitTest/AgentTest.cs @@ -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; diff --git a/BotSharp.UnitTest/BotSharp.UnitTest.csproj b/BotSharp.UnitTest/BotSharp.UnitTest.csproj index 7f6c8ca5..fc2ae61d 100644 --- a/BotSharp.UnitTest/BotSharp.UnitTest.csproj +++ b/BotSharp.UnitTest/BotSharp.UnitTest.csproj @@ -57,11 +57,8 @@ - - - @@ -215,21 +212,12 @@ PreserveNewest - - PreserveNewest - - - PreserveNewest - PreserveNewest PreserveNewest - - PreserveNewest - diff --git a/BotSharp.UnitTest/IntentTest.cs b/BotSharp.UnitTest/IntentTest.cs index 67009a6d..5396ffb5 100644 --- a/BotSharp.UnitTest/IntentTest.cs +++ b/BotSharp.UnitTest/IntentTest.cs @@ -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"); } } } diff --git a/BotSharp.UnitTest/Settings/settings.auth.json b/BotSharp.UnitTest/Settings/settings.auth.json deleted file mode 100644 index c8d08264..00000000 --- a/BotSharp.UnitTest/Settings/settings.auth.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "TokenAuthentication": { - "SecretKey": "QEMYOjgkNEq/krV1Ouzz8w==", - "Subject": "OpenBotKit", - "Issuer": "Haiping Chen", - "Audience": "Haiping Chen", - "TokenPath": "/token", - "CookieName": "token", - "LoginPath": "/login" - } -} \ No newline at end of file diff --git a/BotSharp.UnitTest/Settings/settings.aws.json b/BotSharp.UnitTest/Settings/settings.aws.json deleted file mode 100644 index 88d79907..00000000 --- a/BotSharp.UnitTest/Settings/settings.aws.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "AWS": { - "AWSRegionEndPoint": "us-east-1", - "AWSSecretKey": "", - "AWSAccessKey": "", - "AWSEncoding": "utf-8", - "SESVerifiedEmail": "", - "AWSBucketPrefix": "" - } -} \ No newline at end of file diff --git a/BotSharp.UnitTest/Settings/settings.swagger.json b/BotSharp.UnitTest/Settings/settings.swagger.json deleted file mode 100644 index a1a35359..00000000 --- a/BotSharp.UnitTest/Settings/settings.swagger.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "Swagger": { - "Version": "v1", - "Title": "Open Chatbot Kit", - "Description": "OpenBotKit API", - "TermsOfService": "MIT", - "Contact": { - "Name": "Haiping Chen", - "Email": "haiping008@gmail.com" - } - } -}