diff --git a/BotSharp.Core/BotSharp.Core.csproj b/BotSharp.Core/BotSharp.Core.csproj index 7336006b..71421e23 100644 --- a/BotSharp.Core/BotSharp.Core.csproj +++ b/BotSharp.Core/BotSharp.Core.csproj @@ -21,13 +21,12 @@ If you feel that this project is helpful to you, please Star on the project, we MIT https://github.com/Oceania2018/BotSharp NLU, Chatbot, Bot, AI Bot, Artificial Intelligence - 1.8.0 + 1.9.0 Monthly Update. -Integrated with Tencent Wechat. -If you feel that this project is helpful to you, please Star on the project, we will be very grateful. +Support dialogue status tracking. Since 2018 Haiping Chen https://github.com/Oceania2018/BotSharp - 1.8.0.0 + 1.9.0.0 https://raw.githubusercontent.com/Oceania2018/BotSharp/master/BotSharp.WebHost/wwwroot/images/BotSharp.png https://github.com/Oceania2018/BotSharp/blob/master/LICENSE diff --git a/BotSharp.Core/ContextStorage/ContextStorageInFile.cs b/BotSharp.Core/ContextStorage/ContextStorageInFile.cs index 3647456c..39d0b9ff 100644 --- a/BotSharp.Core/ContextStorage/ContextStorageInFile.cs +++ b/BotSharp.Core/ContextStorage/ContextStorageInFile.cs @@ -28,6 +28,20 @@ namespace BotSharp.Core.ContextStorage } } + public async Task Fetch(string sessionId) + { + string dataPath = Path.Combine(storageDir, sessionId + ".json"); + if (File.Exists(dataPath)) + { + string json = File.ReadAllText(dataPath); + return JsonConvert.DeserializeObject(json); + } + else + { + return default(T[]); + } + } + public async Task Persist(string sessionId, T[] context) { var json = JsonConvert.SerializeObject(context, new JsonSerializerSettings diff --git a/BotSharp.Core/PlatformBuilderBase.cs b/BotSharp.Core/PlatformBuilderBase.cs index 903f4311..5fdbb221 100644 --- a/BotSharp.Core/PlatformBuilderBase.cs +++ b/BotSharp.Core/PlatformBuilderBase.cs @@ -3,10 +3,12 @@ using BotSharp.Platform.Abstraction; using BotSharp.Platform.Models; using BotSharp.Platform.Models.AiRequest; using BotSharp.Platform.Models.AiResponse; +using BotSharp.Platform.Models.Contexts; using BotSharp.Platform.Models.Entities; using BotSharp.Platform.Models.Intents; using BotSharp.Platform.Models.MachineLearning; using DotNetToolkit; +using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -25,12 +27,14 @@ namespace BotSharp.Core public IAgentStorage Storage { get; set; } - private readonly IAgentStorageFactory agentStorageFactory; - private readonly IPlatformSettings settings; + protected readonly IAgentStorageFactory agentStorageFactory; + protected readonly IContextStorageFactory contextStorageFactory; + protected readonly IPlatformSettings settings; - public PlatformBuilderBase(IAgentStorageFactory agentStorageFactory, IPlatformSettings settings) + public PlatformBuilderBase(IAgentStorageFactory agentStorageFactory, IContextStorageFactory contextStorageFactory, IPlatformSettings settings) { this.agentStorageFactory = agentStorageFactory; + this.contextStorageFactory = contextStorageFactory; this.settings = settings; GetAgentStorage(); } @@ -132,10 +136,10 @@ namespace BotSharp.Core public virtual async Task TextRequest(AiRequest request) { - string contexts = String.Join("_", request.Contexts); - string contextHash = contexts.GetMd5Hash(); + // merge last contexts + string contextHash = await GetContextsHash(request); - Console.WriteLine($"TextRequest: {request.Text}, {contexts}, {request.SessionId}"); + Console.WriteLine($"TextRequest: {request.Text}, {request.Contexts}, {request.SessionId}"); // Load agent var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.AgentId); @@ -193,6 +197,24 @@ namespace BotSharp.Core return await AssembleResult(request, aiResponse); } + private async Task GetContextsHash(AiRequest request) + { + var ctxStore = contextStorageFactory.Get(); + var contexts = await ctxStore.Fetch(request.SessionId); + for(int i = 0; i < contexts.Length; i++) + { + var ctx = contexts[i]; + if (ctx.Lifespan > 0 && !request.Contexts.Exists(x => x == ctx.Name)) + { + request.Contexts.Add(ctx.Name); + } + } + + request.Contexts = request.Contexts.OrderBy(x => x).ToList(); + + return String.Join("_", request.Contexts).GetMd5Hash(); + } + public virtual async Task FallbackResponse(AiRequest request) { var data = new diff --git a/BotSharp.Platform.Abstraction/BotSharp.Platform.Abstraction.csproj b/BotSharp.Platform.Abstraction/BotSharp.Platform.Abstraction.csproj index f67cdf19..728fe526 100644 --- a/BotSharp.Platform.Abstraction/BotSharp.Platform.Abstraction.csproj +++ b/BotSharp.Platform.Abstraction/BotSharp.Platform.Abstraction.csproj @@ -3,7 +3,7 @@ netstandard2.0 true - 0.2.1 + 0.3.0 https://github.com/Oceania2018/BotSharp Haiping Chen diff --git a/BotSharp.Platform.Abstraction/IContextStorage.cs b/BotSharp.Platform.Abstraction/IContextStorage.cs index 71ebe592..aaeada1d 100644 --- a/BotSharp.Platform.Abstraction/IContextStorage.cs +++ b/BotSharp.Platform.Abstraction/IContextStorage.cs @@ -8,5 +8,6 @@ namespace BotSharp.Platform.Abstraction public interface IContextStorage { Task Persist(string sessionId, T[] context); + Task Fetch(string sessionId); } } diff --git a/BotSharp.Platform.Dialogflow/DialogflowAi.cs b/BotSharp.Platform.Dialogflow/DialogflowAi.cs index 2271828e..63339f5d 100644 --- a/BotSharp.Platform.Dialogflow/DialogflowAi.cs +++ b/BotSharp.Platform.Dialogflow/DialogflowAi.cs @@ -15,6 +15,7 @@ using BotSharp.Platform.Models.AiResponse; using BotSharp.Platform.Models.AiRequest; using Turing.NET; using System.Text.RegularExpressions; +using BotSharp.Platform.Models.Contexts; namespace BotSharp.Platform.Dialogflow { @@ -24,13 +25,11 @@ namespace BotSharp.Platform.Dialogflow where TAgent : AgentModel { IConfiguration config; - IContextStorageFactory contextStorageFactory; public DialogflowAi(IAgentStorageFactory agentStorageFactory, IContextStorageFactory contextStorageFactory, IPlatformSettings settings, IConfiguration config) - :base(agentStorageFactory, settings) + :base(agentStorageFactory, contextStorageFactory, settings) { this.config = config; - this.contextStorageFactory = contextStorageFactory; } public async Task ExtractorCorpus(TAgent agent) diff --git a/BotSharp.Platform.Dialogflow/Models/AIRequest.cs b/BotSharp.Platform.Dialogflow/Models/AIRequest.cs index b65eac77..75796e88 100644 --- a/BotSharp.Platform.Dialogflow/Models/AIRequest.cs +++ b/BotSharp.Platform.Dialogflow/Models/AIRequest.cs @@ -1,4 +1,4 @@ -using Newtonsoft.Json; +using BotSharp.Platform.Models.Contexts; using System; using System.Collections.Generic; using System.Text; diff --git a/BotSharp.Platform.Dialogflow/Models/AIResponseResult.cs b/BotSharp.Platform.Dialogflow/Models/AIResponseResult.cs index 14d71599..444ef5e9 100644 --- a/BotSharp.Platform.Dialogflow/Models/AIResponseResult.cs +++ b/BotSharp.Platform.Dialogflow/Models/AIResponseResult.cs @@ -1,5 +1,5 @@ using BotSharp.Platform.Models.AiResponse; -using BotSharp.Platform.Models.Entities; +using BotSharp.Platform.Models.Contexts; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -18,8 +18,6 @@ namespace BotSharp.Platform.Dialogflow.Models public Dictionary Parameters { get; set; } - public List Entities { get; set; } - public AIContext[] Contexts { get; set; } public AIResponseMetadata Metadata { get; set; } diff --git a/BotSharp.Platform.Dialogflow/Models/DialogflowIntentResponse.cs b/BotSharp.Platform.Dialogflow/Models/DialogflowIntentResponse.cs index 34ede6bc..111c1d81 100644 --- a/BotSharp.Platform.Dialogflow/Models/DialogflowIntentResponse.cs +++ b/BotSharp.Platform.Dialogflow/Models/DialogflowIntentResponse.cs @@ -1,4 +1,5 @@ -using System; +using BotSharp.Platform.Models.Contexts; +using System; using System.Collections.Generic; using System.Text; diff --git a/BotSharp.Platform.Dialogflow/Models/RequestExtras.cs b/BotSharp.Platform.Dialogflow/Models/RequestExtras.cs index eb7e603a..62a44150 100644 --- a/BotSharp.Platform.Dialogflow/Models/RequestExtras.cs +++ b/BotSharp.Platform.Dialogflow/Models/RequestExtras.cs @@ -1,4 +1,5 @@ -using System; +using BotSharp.Platform.Models.Contexts; +using System; using System.Collections.Generic; using System.Text; diff --git a/BotSharp.Platform.Dialogflow/ModuleInjector.cs b/BotSharp.Platform.Dialogflow/ModuleInjector.cs index 7c236750..87e53e78 100644 --- a/BotSharp.Platform.Dialogflow/ModuleInjector.cs +++ b/BotSharp.Platform.Dialogflow/ModuleInjector.cs @@ -5,6 +5,7 @@ using BotSharp.Core.Modules; using BotSharp.Platform.Abstraction; using BotSharp.Platform.Dialogflow.Models; using BotSharp.Platform.Models; +using BotSharp.Platform.Models.Contexts; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; diff --git a/BotSharp.Platform.Models/BotSharp.Platform.Models.csproj b/BotSharp.Platform.Models/BotSharp.Platform.Models.csproj index f2313d12..8ef3381a 100644 --- a/BotSharp.Platform.Models/BotSharp.Platform.Models.csproj +++ b/BotSharp.Platform.Models/BotSharp.Platform.Models.csproj @@ -3,7 +3,7 @@ netstandard2.0 true - 0.2.1 + 0.3.0 https://github.com/Oceania2018/BotSharp Haipin Chen diff --git a/BotSharp.Platform.Dialogflow/Models/AIContext.cs b/BotSharp.Platform.Models/Contexts/AIContext.cs similarity index 69% rename from BotSharp.Platform.Dialogflow/Models/AIContext.cs rename to BotSharp.Platform.Models/Contexts/AIContext.cs index be0fd6a4..227449fc 100644 --- a/BotSharp.Platform.Dialogflow/Models/AIContext.cs +++ b/BotSharp.Platform.Models/Contexts/AIContext.cs @@ -1,23 +1,18 @@ -using Newtonsoft.Json; -using System; +using System; using System.Collections.Generic; using System.Text; -namespace BotSharp.Platform.Dialogflow.Models +namespace BotSharp.Platform.Models.Contexts { - [JsonObject] public class AIContext { - [JsonProperty("name")] public string Name { get; set; } - [JsonProperty("parameters")] public Dictionary Parameters { get; set; } /// /// Lifespan of the context measured in requests```` /// - [JsonProperty("lifespan")] public int Lifespan { get; set; } public AIContext() diff --git a/BotSharp.Platform.Rasa/RasaAi.cs b/BotSharp.Platform.Rasa/RasaAi.cs index f1080949..4210d6f5 100644 --- a/BotSharp.Platform.Rasa/RasaAi.cs +++ b/BotSharp.Platform.Rasa/RasaAi.cs @@ -4,6 +4,7 @@ using BotSharp.Platform.Abstraction; using BotSharp.Platform.Models; using BotSharp.Platform.Models.AiRequest; using BotSharp.Platform.Models.AiResponse; +using BotSharp.Platform.Models.Contexts; using BotSharp.Platform.Rasa.Models; using System.Collections.Generic; using System.Threading.Tasks; @@ -19,8 +20,8 @@ namespace BotSharp.Platform.Rasa where TAgent : AgentModel { - public RasaAi(IAgentStorageFactory agentStorageFactory, IPlatformSettings settings) - : base(agentStorageFactory, settings) + public RasaAi(IAgentStorageFactory agentStorageFactory, IContextStorageFactory contextStorageFactory, IPlatformSettings settings) + : base(agentStorageFactory, contextStorageFactory, settings) { }