Task<T[]> Fetch(string sessionId)
This commit is contained in:
parent
3c7c3c183b
commit
e57d062c87
|
|
@ -21,13 +21,12 @@ If you feel that this project is helpful to you, please Star on the project, we
|
|||
<RepositoryType>MIT</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/Oceania2018/BotSharp</RepositoryUrl>
|
||||
<PackageTags>NLU, Chatbot, Bot, AI Bot, Artificial Intelligence</PackageTags>
|
||||
<Version>1.8.0</Version>
|
||||
<Version>1.9.0</Version>
|
||||
<PackageReleaseNotes>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.</PackageReleaseNotes>
|
||||
Support dialogue status tracking.</PackageReleaseNotes>
|
||||
<Copyright>Since 2018 Haiping Chen</Copyright>
|
||||
<PackageProjectUrl>https://github.com/Oceania2018/BotSharp</PackageProjectUrl>
|
||||
<AssemblyVersion>1.8.0.0</AssemblyVersion>
|
||||
<AssemblyVersion>1.9.0.0</AssemblyVersion>
|
||||
<PackageIconUrl>https://raw.githubusercontent.com/Oceania2018/BotSharp/master/BotSharp.WebHost/wwwroot/images/BotSharp.png</PackageIconUrl>
|
||||
<PackageLicenseUrl>https://github.com/Oceania2018/BotSharp/blob/master/LICENSE</PackageLicenseUrl>
|
||||
</PropertyGroup>
|
||||
|
|
|
|||
|
|
@ -28,6 +28,20 @@ namespace BotSharp.Core.ContextStorage
|
|||
}
|
||||
}
|
||||
|
||||
public async Task<T[]> Fetch(string sessionId)
|
||||
{
|
||||
string dataPath = Path.Combine(storageDir, sessionId + ".json");
|
||||
if (File.Exists(dataPath))
|
||||
{
|
||||
string json = File.ReadAllText(dataPath);
|
||||
return JsonConvert.DeserializeObject<T[]>(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(T[]);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> Persist(string sessionId, T[] context)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(context, new JsonSerializerSettings
|
||||
|
|
|
|||
|
|
@ -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<TAgent> Storage { get; set; }
|
||||
|
||||
private readonly IAgentStorageFactory<TAgent> agentStorageFactory;
|
||||
private readonly IPlatformSettings settings;
|
||||
protected readonly IAgentStorageFactory<TAgent> agentStorageFactory;
|
||||
protected readonly IContextStorageFactory<AIContext> contextStorageFactory;
|
||||
protected readonly IPlatformSettings settings;
|
||||
|
||||
public PlatformBuilderBase(IAgentStorageFactory<TAgent> agentStorageFactory, IPlatformSettings settings)
|
||||
public PlatformBuilderBase(IAgentStorageFactory<TAgent> agentStorageFactory, IContextStorageFactory<AIContext> contextStorageFactory, IPlatformSettings settings)
|
||||
{
|
||||
this.agentStorageFactory = agentStorageFactory;
|
||||
this.contextStorageFactory = contextStorageFactory;
|
||||
this.settings = settings;
|
||||
GetAgentStorage();
|
||||
}
|
||||
|
|
@ -132,10 +136,10 @@ namespace BotSharp.Core
|
|||
|
||||
public virtual async Task<TResult> TextRequest<TResult>(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<TResult>(request, aiResponse);
|
||||
}
|
||||
|
||||
private async Task<string> 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<TextClassificationResult> FallbackResponse(AiRequest request)
|
||||
{
|
||||
var data = new
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>0.2.1</Version>
|
||||
<Version>0.3.0</Version>
|
||||
<RepositoryUrl>https://github.com/Oceania2018/BotSharp</RepositoryUrl>
|
||||
<Authors>Haiping Chen</Authors>
|
||||
</PropertyGroup>
|
||||
|
|
|
|||
|
|
@ -8,5 +8,6 @@ namespace BotSharp.Platform.Abstraction
|
|||
public interface IContextStorage<T>
|
||||
{
|
||||
Task<bool> Persist(string sessionId, T[] context);
|
||||
Task<T[]> Fetch(string sessionId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<AIContext> contextStorageFactory;
|
||||
|
||||
public DialogflowAi(IAgentStorageFactory<TAgent> agentStorageFactory, IContextStorageFactory<AIContext> contextStorageFactory, IPlatformSettings settings, IConfiguration config)
|
||||
:base(agentStorageFactory, settings)
|
||||
:base(agentStorageFactory, contextStorageFactory, settings)
|
||||
{
|
||||
this.config = config;
|
||||
this.contextStorageFactory = contextStorageFactory;
|
||||
}
|
||||
|
||||
public async Task<TrainingCorpus> ExtractorCorpus(TAgent agent)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
using Newtonsoft.Json;
|
||||
using BotSharp.Platform.Models.Contexts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
|
|
|||
|
|
@ -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<string, object> Parameters { get; set; }
|
||||
|
||||
public List<NlpEntity> Entities { get; set; }
|
||||
|
||||
public AIContext[] Contexts { get; set; }
|
||||
|
||||
public AIResponseMetadata Metadata { get; set; }
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using BotSharp.Platform.Models.Contexts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using BotSharp.Platform.Models.Contexts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>0.2.1</Version>
|
||||
<Version>0.3.0</Version>
|
||||
<PackageProjectUrl>https://github.com/Oceania2018/BotSharp</PackageProjectUrl>
|
||||
<Authors>Haipin Chen</Authors>
|
||||
</PropertyGroup>
|
||||
|
|
|
|||
|
|
@ -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<string, object> Parameters { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Lifespan of the context measured in requests````
|
||||
/// </summary>
|
||||
[JsonProperty("lifespan")]
|
||||
public int Lifespan { get; set; }
|
||||
|
||||
public AIContext()
|
||||
|
|
@ -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<TAgent> agentStorageFactory, IPlatformSettings settings)
|
||||
: base(agentStorageFactory, settings)
|
||||
public RasaAi(IAgentStorageFactory<TAgent> agentStorageFactory, IContextStorageFactory<AIContext> contextStorageFactory, IPlatformSettings settings)
|
||||
: base(agentStorageFactory, contextStorageFactory, settings)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue