Initial OwnThink platform

This commit is contained in:
Oceania2018 2019-03-16 11:23:02 -05:00
parent c546a14aa1
commit 84da408789
23 changed files with 432 additions and 10721 deletions

View file

@ -86,4 +86,8 @@ If you feel that this project is helpful to you, please Star on the project, we
<PackageReference Include="TensorFlow.NET" Version="0.4.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Engines\OwnThink\" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.Models.Agents
{
public class AgentCreationViewModel
{
public string Name { get; set; }
public string Description { get; set; }
}
}

View file

@ -60,7 +60,7 @@ namespace BotSharp.Core.Modules
else
{
IModule module = (IModule)Activator.CreateInstance(type);
Console.WriteLine($"Loaded module \"{s.Type}\"", Color.Green);
Console.WriteLine($"Loaded {s.Type.Split('.')[1]} \"{s.Type}\"", Color.Green);
return module;
}
}

View file

@ -1,5 +1,4 @@
using BotSharp.Core.Engines;
using BotSharp.Core.Engines.OwnThink;
using BotSharp.Platform.Abstraction;
using BotSharp.Platform.Models;
using BotSharp.Platform.Models.AiRequest;
@ -140,7 +139,7 @@ namespace BotSharp.Core
// merge last contexts
string contextHash = await GetContextsHash(request);
Console.WriteLine($"TextRequest: {request.Text}, {request.Contexts}, {request.SessionId}");
Console.WriteLine($"TextRequest: {request.Text}, {request.AgentId}, {string.Join(",", request.Contexts)}, {request.SessionId}");
// Load agent
var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.AgentId);
@ -218,27 +217,7 @@ namespace BotSharp.Core
public virtual async Task<TextClassificationResult> FallbackResponse(AiRequest request)
{
var data = new
{
appid = "openbot",
userid = "yener",
spoken = request.Text
};
using (var client = new HttpClient())
{
var response = await client.PostAsync(
"https://api.ownthink.com/bot",
new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"));
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<OwnThinkChatResponse>(content);
return new TextClassificationResult
{
Classifier = "ownthink",
Text = result.Data.Info.Text
};
}
throw new NotImplementedException("FallbackResponse");
}
public virtual async Task<TResult> AssembleResult<TResult>(AiRequest request, AiResponse response)

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="ViewModels\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BotSharp.Core\BotSharp.Core.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.Controllers
{
public class AgentController
{
}
}

View file

@ -0,0 +1,22 @@
using BotSharp.Platform.Models;
using BotSharp.Platform.Models.Intents;
using BotSharp.Platform.Models.MachineLearning;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class AgentModel : AgentBase
{
public AgentModel()
{
}
[JsonProperty("entity_types")]
public List<EntityType> Entities { get; set; }
}
}

View file

@ -0,0 +1,28 @@
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class EntityEntry
{
/// <summary>
/// Guid
/// </summary>
[StringLength(36)]
public String Id { get; set; }
[Required]
[StringLength(36)]
public String EntityId { get; set; }
[MaxLength(64)]
public String Value { get; set; }
[ForeignKey("EntityEntryId")]
public List<EntrySynonym> Synonyms { get; set; }
}
}

View file

@ -0,0 +1,45 @@
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class EntityType
{
/// <summary>
/// Guid
/// </summary>
[StringLength(36)]
public String Id { get; set; }
[Required]
[StringLength(36)]
public String AgentId { get; set; }
[Required]
[MaxLength(64)]
public String Name { get; set; }
[MaxLength(256)]
public String Description { get; set; }
[ForeignKey("EntityId")]
public List<EntityEntry> Entries { get; set; }
public bool IsOverridable { get; set; }
public bool IsEnum { get; set; }
[StringLength(6)]
public string Color { get; set; }
/// <summary>
/// Entries count
/// </summary>
[NotMapped]
public int Count { get; set; }
}
}

View file

@ -0,0 +1,25 @@
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class EntrySynonym
{
/// <summary>
/// Guid
/// </summary>
[StringLength(36)]
public String Id { get; set; }
[Required]
[StringLength(36)]
public String EntityEntryId { get; set; }
[MaxLength(128)]
public String Synonym { get; set; }
}
}

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.Engines.OwnThink
namespace BotSharp.Platform.OwnThink.Models
{
public class OwnThinkApi
{

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.Engines.OwnThink
namespace BotSharp.Platform.OwnThink.Models
{
public class OwnThinkChatResponse
{

View file

@ -0,0 +1,34 @@
using BotSharp.Core;
using BotSharp.Core.AgentStorage;
using BotSharp.Core.ContextStorage;
using BotSharp.Core.Modules;
using BotSharp.Platform.Abstraction;
using BotSharp.Platform.OwnThink.Models;
using BotSharp.Platform.Models;
using BotSharp.Platform.Models.Contexts;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
namespace BotSharp.Platform.OwnThink
{
public class ModuleInjector : IModule
{
public void ConfigureServices(IServiceCollection services, IConfiguration config)
{
services.AddSingleton<IPlatformBuilder<AgentModel>, OwnThinkAi<AgentModel>>();
services.AddSingleton<OwnThinkAi<AgentModel>>();
AgentStorageServiceRegister.Register<AgentModel>(services);
PlatformConfigServiceRegister.Register<PlatformSettings>("ownThinkAi", services, config);
ContextStorageServiceRegister.Register<AIContext>(services);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
}
}
}

View file

@ -0,0 +1,186 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BotSharp.Core;
using BotSharp.Platform.Abstraction;
using BotSharp.Platform.Models;
using DotNetToolkit;
using BotSharp.Platform.OwnThink.Models;
using System.IO;
using Microsoft.Extensions.Configuration;
using BotSharp.Platform.Models.Intents;
using BotSharp.Platform.Models.AiResponse;
using BotSharp.Platform.Models.AiRequest;
using System.Text.RegularExpressions;
using BotSharp.Platform.Models.Contexts;
using Newtonsoft.Json;
using System.Net.Http;
namespace BotSharp.Platform.OwnThink
{
public class OwnThinkAi<TAgent> :
PlatformBuilderBase<TAgent>,
IPlatformBuilder<TAgent>
where TAgent : AgentModel
{
IConfiguration config;
public OwnThinkAi(IAgentStorageFactory<TAgent> agentStorageFactory, IContextStorageFactory<AIContext> contextStorageFactory, IPlatformSettings settings, IConfiguration config)
:base(agentStorageFactory, contextStorageFactory, settings)
{
this.config = config;
}
public async Task<TrainingCorpus> ExtractorCorpus(TAgent agent)
{
var corpus = new TrainingCorpus
{
Entities = new List<TrainingEntity>(),
UserSays = new List<TrainingIntentExpression<TrainingIntentExpressionPart>>()
};
agent.Entities.ForEach(entity =>
{
corpus.Entities.Add(new TrainingEntity
{
Entity = entity.Name,
Values = entity.Entries.Select(x => new TrainingEntitySynonym
{
Value = x.Value,
Synonyms = x.Synonyms.Select(y => y.Synonym).ToList()
}).ToList()
});
});
agent.Intents.ForEach(intent =>
{
// filter unexpected intents
if(intent.Name != "Default Fallback Intent")
{
// caculate contexts hash
intent.ContextHash = String.Join('_', intent.Contexts.OrderBy(x => x.Name).Select(x => x.Name)).GetMd5Hash();
intent.UserSays.ForEach(say => {
corpus.UserSays.Add(new TrainingIntentExpression<TrainingIntentExpressionPart>
{
Intent = intent.Name,
Text = String.Join("", say.Data.Select(x => x.Text)),
Entities = say.Data.Where(x => !String.IsNullOrEmpty(x.Meta))
.Select(x => new TrainingIntentExpressionPart
{
Value = x.Text,
Entity = x.Meta,
Start = x.Start
})
.ToList(),
ContextHash = intent.ContextHash
});
});
}
});
return corpus;
}
public override async Task<TextClassificationResult> FallbackResponse(AiRequest request)
{
if (config.GetValue<bool>("overrideFallback"))
{
var data = new
{
appid = "openbot",
userid = "yener",
spoken = request.Text
};
using (var client = new HttpClient())
{
var response = await client.PostAsync(
"https://api.ownthink.com/bot",
new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"));
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<OwnThinkChatResponse>(content);
return new TextClassificationResult
{
Classifier = "ownthink",
Text = result.Data.Info.Text
};
}
}
else
{
return await base.FallbackResponse(request);
}
}
public override async Task<TResult> AssembleResult<TResult>(AiRequest request, AiResponse response)
{
var intent = Agent.Intents.Find(x => x.Name == response.Intent);
var presetResponse = intent.Responses.FirstOrDefault();
// format messages
presetResponse.Messages = presetResponse.Messages.Where(x => x.Speech.Length > 0).ToList();
if (presetResponse.Messages.Count == 0)
{
presetResponse.Messages.Add(new IntentResponseMessage
{
Speech = "\"" + intent.Name + "\""
});
}
// fill parameters
presetResponse.Parameters.ForEach(p =>
{
var entity = response.Entities.FirstOrDefault(x => x.Entity == p.DataType);
p.Value = entity?.Value;
});
var matches = Regex.Matches(presetResponse.Messages.Random().Speech, "\".*?\"").Cast<Match>();
var speech = matches.Count() == 0 ? String.Empty : matches.ToList().Random().Value;
var contexts = HandleContexts(request.SessionId, presetResponse);
throw new NotImplementedException("AssembleResult");
/*var aiResponse = new AIResponseResult
{
ResolvedQuery = response.ResolvedQuery,
Action = presetResponse.Action,
Metadata = new AIResponseMetadata
{
IntentName = response.Intent
},
Intent = response.Intent,
Fulfillment = new AIResponseFulfillment
{
Messages = presetResponse.Messages.ToList<object>(),
Speech = speech.Length > 1 ? speech.Substring(1, speech.Length - 2) : String.Empty
},
Score = response.Score,
Source = response.Source,
Contexts = contexts.ToArray(),
Parameters = presetResponse.Parameters.Where(x => !String.IsNullOrEmpty(x.Value)).ToDictionary(item => item.Name, item => (object)item.Value)
};
return (TResult)(object)aiResponse;*/
}
private List<AIContext> HandleContexts(string sessionId, IntentResponse response)
{
var newContexts = response.Contexts.Select(x => new AIContext
{
Name = x.Name,
Lifespan = x.Lifespan,
Parameters = response.Parameters.Select(p => new KeyValuePair<string, object>(p.Name, p.Value)).ToDictionary(d => d.Key, d => d.Value == null ? String.Empty : d.Value)
}).ToList();
// persist
var ctxStore = contextStorageFactory.Get();
ctxStore.Persist(sessionId, newContexts.ToArray());
return newContexts;
}
}
}

View file

@ -0,0 +1,13 @@
using BotSharp.Core;
using BotSharp.Platform.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink
{
public class PlatformSettings : PlatformSettingsBase
{
}
}

File diff suppressed because it is too large Load diff

View file

@ -91,6 +91,8 @@
<ProjectReference Include="..\BotSharp.Channel.FacebookMessenger\BotSharp.Channel.FacebookMessenger.csproj" />
<ProjectReference Include="..\BotSharp.Channel.Weixin\BotSharp.Channel.Weixin.csproj" />
<ProjectReference Include="..\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj" />
<ProjectReference Include="..\BotSharp.Platform.OwnThink\BotSharp.Platform.OwnThink.csproj" />
<ProjectReference Include="..\BotSharp.Platform.Rasa\BotSharp.Platform.Rasa.csproj" />
</ItemGroup>
<ItemGroup>
@ -103,6 +105,9 @@
<Content Update="Settings\channels.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="Settings\OwnThinkAi.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="Settings\DialogflowAi.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View file

@ -2,7 +2,7 @@
"version": "0.2.0",
"assemblies": "BotSharp.Core",
"platformModuleName": "DialogflowAi",
"platformModuleName": "OwnThinkAi",
"overrideFallback": false,
"modules": [
@ -14,6 +14,10 @@
"Name": "RasaAi",
"Type": "BotSharp.Platform.Rasa"
},
{
"Name": "OwnThink",
"Type": "BotSharp.Platform.OwnThink"
},
{
"Name": "WeixinChannel",
"Type": "BotSharp.Channel.Weixin"

View file

@ -1,9 +1,3 @@
{
"TokenAuthentication": {
"SecretKey": "lfo54FYneUCJNL2EjP9ZxQ==",
"Issuer": "BotSharp",
"Audience": "BotSharp",
"CookieName": "token",
"Subject": "BotSharp"
}
}

View file

@ -0,0 +1,9 @@
{
// if you want to override platform setting, please set corresponding value,
// otherwise you don't need this section.
"ownThinkAi": {
"botEngine": "BotSharpNLU",
"agentStorage": "AgentStorageInFile",
"contextStorage": "ContextStorageInFile"
}
}

View file

@ -0,0 +1,2 @@
{
}

View file

@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Channel.FacebookMe
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Rasa", "BotSharp.Platform.Rasa\BotSharp.Platform.Rasa.csproj", "{2EDE5F82-9219-4827-B636-13717DCDDF01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Platform.OwnThink", "BotSharp.Platform.OwnThink\BotSharp.Platform.OwnThink.csproj", "{96820DD6-0806-40A3-943A-7F2EF69E8EDB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -81,6 +83,14 @@ Global
{2EDE5F82-9219-4827-B636-13717DCDDF01}.Release|Any CPU.Build.0 = Release|Any CPU
{2EDE5F82-9219-4827-B636-13717DCDDF01}.Release|x64.ActiveCfg = Release|Any CPU
{2EDE5F82-9219-4827-B636-13717DCDDF01}.Release|x64.Build.0 = Release|Any CPU
{96820DD6-0806-40A3-943A-7F2EF69E8EDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96820DD6-0806-40A3-943A-7F2EF69E8EDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96820DD6-0806-40A3-943A-7F2EF69E8EDB}.Debug|x64.ActiveCfg = Debug|Any CPU
{96820DD6-0806-40A3-943A-7F2EF69E8EDB}.Debug|x64.Build.0 = Debug|Any CPU
{96820DD6-0806-40A3-943A-7F2EF69E8EDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96820DD6-0806-40A3-943A-7F2EF69E8EDB}.Release|Any CPU.Build.0 = Release|Any CPU
{96820DD6-0806-40A3-943A-7F2EF69E8EDB}.Release|x64.ActiveCfg = Release|Any CPU
{96820DD6-0806-40A3-943A-7F2EF69E8EDB}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE