diff --git a/BotSharp.Core/BotSharp.Core.csproj b/BotSharp.Core/BotSharp.Core.csproj
index 417c2c50..a250ecdd 100644
--- a/BotSharp.Core/BotSharp.Core.csproj
+++ b/BotSharp.Core/BotSharp.Core.csproj
@@ -86,4 +86,8 @@ If you feel that this project is helpful to you, please Star on the project, we
+
+
+
+
diff --git a/BotSharp.Core/Models/Agents/AgentCreationViewModel.cs b/BotSharp.Core/Models/Agents/AgentCreationViewModel.cs
new file mode 100644
index 00000000..85b3c93e
--- /dev/null
+++ b/BotSharp.Core/Models/Agents/AgentCreationViewModel.cs
@@ -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; }
+ }
+}
diff --git a/BotSharp.Core/Modules/ModulesStartup.cs b/BotSharp.Core/Modules/ModulesStartup.cs
index c6d462ee..60b8029a 100644
--- a/BotSharp.Core/Modules/ModulesStartup.cs
+++ b/BotSharp.Core/Modules/ModulesStartup.cs
@@ -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;
}
}
diff --git a/BotSharp.Core/PlatformBuilderBase.cs b/BotSharp.Core/PlatformBuilderBase.cs
index f79fa366..c59c4a61 100644
--- a/BotSharp.Core/PlatformBuilderBase.cs
+++ b/BotSharp.Core/PlatformBuilderBase.cs
@@ -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 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(content);
-
- return new TextClassificationResult
- {
- Classifier = "ownthink",
- Text = result.Data.Info.Text
- };
- }
+ throw new NotImplementedException("FallbackResponse");
}
public virtual async Task AssembleResult(AiRequest request, AiResponse response)
diff --git a/BotSharp.Platform.OwnThink/BotSharp.Platform.OwnThink.csproj b/BotSharp.Platform.OwnThink/BotSharp.Platform.OwnThink.csproj
new file mode 100644
index 00000000..9b2b8c48
--- /dev/null
+++ b/BotSharp.Platform.OwnThink/BotSharp.Platform.OwnThink.csproj
@@ -0,0 +1,15 @@
+
+
+
+ netcoreapp2.2
+
+
+
+
+
+
+
+
+
+
+
diff --git a/BotSharp.Platform.OwnThink/Controllers/AgentController.cs b/BotSharp.Platform.OwnThink/Controllers/AgentController.cs
new file mode 100644
index 00000000..842b96d7
--- /dev/null
+++ b/BotSharp.Platform.OwnThink/Controllers/AgentController.cs
@@ -0,0 +1,10 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace BotSharp.Platform.OwnThink.Controllers
+{
+ public class AgentController
+ {
+ }
+}
diff --git a/BotSharp.Platform.OwnThink/Models/AgentModel.cs b/BotSharp.Platform.OwnThink/Models/AgentModel.cs
new file mode 100644
index 00000000..c02d915b
--- /dev/null
+++ b/BotSharp.Platform.OwnThink/Models/AgentModel.cs
@@ -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 Entities { get; set; }
+ }
+}
diff --git a/BotSharp.Platform.OwnThink/Models/EntityEntry.cs b/BotSharp.Platform.OwnThink/Models/EntityEntry.cs
new file mode 100644
index 00000000..9f8e9d73
--- /dev/null
+++ b/BotSharp.Platform.OwnThink/Models/EntityEntry.cs
@@ -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
+ {
+ ///
+ /// Guid
+ ///
+ [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 Synonyms { get; set; }
+ }
+}
diff --git a/BotSharp.Platform.OwnThink/Models/EntityType.cs b/BotSharp.Platform.OwnThink/Models/EntityType.cs
new file mode 100644
index 00000000..763a76a7
--- /dev/null
+++ b/BotSharp.Platform.OwnThink/Models/EntityType.cs
@@ -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
+ {
+ ///
+ /// Guid
+ ///
+ [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 Entries { get; set; }
+
+ public bool IsOverridable { get; set; }
+
+ public bool IsEnum { get; set; }
+
+ [StringLength(6)]
+ public string Color { get; set; }
+
+ ///
+ /// Entries count
+ ///
+ [NotMapped]
+ public int Count { get; set; }
+ }
+}
diff --git a/BotSharp.Platform.OwnThink/Models/EntrySynonym.cs b/BotSharp.Platform.OwnThink/Models/EntrySynonym.cs
new file mode 100644
index 00000000..a952103e
--- /dev/null
+++ b/BotSharp.Platform.OwnThink/Models/EntrySynonym.cs
@@ -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
+ {
+ ///
+ /// Guid
+ ///
+ [StringLength(36)]
+ public String Id { get; set; }
+
+ [Required]
+ [StringLength(36)]
+ public String EntityEntryId { get; set; }
+
+ [MaxLength(128)]
+ public String Synonym { get; set; }
+ }
+}
diff --git a/BotSharp.Core/Engines/OwnThink/OwnThinkApi.cs b/BotSharp.Platform.OwnThink/Models/OwnThinkApi.cs
similarity index 72%
rename from BotSharp.Core/Engines/OwnThink/OwnThinkApi.cs
rename to BotSharp.Platform.OwnThink/Models/OwnThinkApi.cs
index d62225f7..549f75f1 100644
--- a/BotSharp.Core/Engines/OwnThink/OwnThinkApi.cs
+++ b/BotSharp.Platform.OwnThink/Models/OwnThinkApi.cs
@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
-namespace BotSharp.Core.Engines.OwnThink
+namespace BotSharp.Platform.OwnThink.Models
{
public class OwnThinkApi
{
diff --git a/BotSharp.Core/Engines/OwnThink/OwnThinkChatResponse.cs b/BotSharp.Platform.OwnThink/Models/OwnThinkChatResponse.cs
similarity index 91%
rename from BotSharp.Core/Engines/OwnThink/OwnThinkChatResponse.cs
rename to BotSharp.Platform.OwnThink/Models/OwnThinkChatResponse.cs
index a7c4b2a3..9f2c6732 100644
--- a/BotSharp.Core/Engines/OwnThink/OwnThinkChatResponse.cs
+++ b/BotSharp.Platform.OwnThink/Models/OwnThinkChatResponse.cs
@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
-namespace BotSharp.Core.Engines.OwnThink
+namespace BotSharp.Platform.OwnThink.Models
{
public class OwnThinkChatResponse
{
diff --git a/BotSharp.Platform.OwnThink/ModuleInjector.cs b/BotSharp.Platform.OwnThink/ModuleInjector.cs
new file mode 100644
index 00000000..c4071abc
--- /dev/null
+++ b/BotSharp.Platform.OwnThink/ModuleInjector.cs
@@ -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, OwnThinkAi>();
+ services.AddSingleton>();
+ AgentStorageServiceRegister.Register(services);
+ PlatformConfigServiceRegister.Register("ownThinkAi", services, config);
+ ContextStorageServiceRegister.Register(services);
+ }
+
+ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
+ {
+
+ }
+ }
+}
diff --git a/BotSharp.Platform.OwnThink/OwnThinkAi.cs b/BotSharp.Platform.OwnThink/OwnThinkAi.cs
new file mode 100644
index 00000000..0ecaf42d
--- /dev/null
+++ b/BotSharp.Platform.OwnThink/OwnThinkAi.cs
@@ -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 :
+ PlatformBuilderBase,
+ IPlatformBuilder
+ where TAgent : AgentModel
+ {
+ IConfiguration config;
+
+ public OwnThinkAi(IAgentStorageFactory agentStorageFactory, IContextStorageFactory contextStorageFactory, IPlatformSettings settings, IConfiguration config)
+ :base(agentStorageFactory, contextStorageFactory, settings)
+ {
+ this.config = config;
+ }
+
+ public async Task ExtractorCorpus(TAgent agent)
+ {
+ var corpus = new TrainingCorpus
+ {
+ Entities = new List(),
+ UserSays = new List>()
+ };
+
+ 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
+ {
+ 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 FallbackResponse(AiRequest request)
+ {
+ if (config.GetValue("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(content);
+
+ return new TextClassificationResult
+ {
+ Classifier = "ownthink",
+ Text = result.Data.Info.Text
+ };
+ }
+ }
+ else
+ {
+ return await base.FallbackResponse(request);
+ }
+ }
+
+ public override async Task AssembleResult(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();
+ 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