Remove the BotSharp.Platform.OwnThink project and related configuration

This commit is contained in:
丁川 2019-09-06 19:04:55 +08:00
parent 7fc36ba61c
commit d9408964d7
22 changed files with 2 additions and 732 deletions

View file

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

View file

@ -1,56 +0,0 @@
using BotSharp.Platform.Models.Agents;
using BotSharp.Platform.OwnThink.Models;
using BotSharp.Platform.OwnThink.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Platform.OwnThink.Controllers
{
[Route("v1/[controller]")]
public class AgentController : ControllerBase
{
private OwnThinkAi<AgentModel> builder;
/// <summary>
/// Initialize dialog controller and get a platform instance
/// </summary>
/// <param name="platform"></param>
public AgentController(OwnThinkAi<AgentModel> platform)
{
builder = platform;
}
/// <summary>
/// Create agent
/// </summary>
/// <param name="requestViewModel"></param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(AgentCreationResponseViewModel), 200)]
public async Task<IActionResult> Create(AgentCreationRequestViewModel requestViewModel)
{
var agent = new AgentModel()
{
Id = Guid.NewGuid().ToString(),
Name = requestViewModel.Name,
Description = requestViewModel.Name,
AppId = requestViewModel.AppId,
ClientAccessToken = Guid.NewGuid().ToString("N"),
DeveloperAccessToken = Guid.NewGuid().ToString("N")
};
await builder.SaveAgent(agent);
return Ok(new AgentCreationResponseViewModel
{
AgentId = agent.Id,
AppId = agent.AppId,
Name = agent.Name,
ClientAccessToken = agent.ClientAccessToken
});
}
}
}

View file

@ -1,51 +0,0 @@
using BotSharp.Platform.Models.AiRequest;
using BotSharp.Platform.OwnThink.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Platform.OwnThink.Controllers
{
[Route("v1/[controller]")]
public class QueryController : ControllerBase
{
private OwnThinkAi<AgentModel> builder;
public QueryController(OwnThinkAi<AgentModel> platform)
{
builder = platform;
}
[HttpGet, HttpPost]
public async Task<ActionResult<OwnThinkAiResponse>> Query(OwnThinkAiRequest request)
{
String clientAccessToken = (User.Identity as ClaimsIdentity).Claims.FirstOrDefault(x => x.Type == "UserId")?.Value;
// find a model according to clientAccessToken
var agents = await builder.GetAllAgents();
var agent = agents.FirstOrDefault(x => x.ClientAccessToken == clientAccessToken);
if (agent == null)
{
return BadRequest("The agent not found.");
}
var aIResponse = await builder.TextRequest<AIResponseResult>(new AiRequest
{
Text = request.Spoken,
AgentId = agent.Id,
SessionId = request.AppId
});
return new OwnThinkAiResponse
{
};
}
}
}

View file

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class AIResponseFulfillment
{
public string Speech { get; set; }
public List<Object> Messages { get; set; }
}
}

View file

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class AIResponseMetadata
{
public string IntentId { get; set; }
public string IntentName { get; set; }
}
}

View file

@ -1,127 +0,0 @@
using BotSharp.Platform.Models.AiResponse;
using BotSharp.Platform.Models.Contexts;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class AIResponseResult : AiResponse
{
public Boolean ActionIncomplete { get; set; }
public String Action { get; set; }
public Dictionary<string, object> Parameters { get; set; }
public AIContext[] Contexts { get; set; }
public AIResponseMetadata Metadata { get; set; }
public AIResponseFulfillment Fulfillment { get; set; }
[JsonIgnore]
public bool HasParameters
{
get
{
return Parameters != null && Parameters.Count > 0;
}
}
public string GetStringParameter(string name, string defaultValue = "")
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
if (Parameters.ContainsKey(name))
{
return Parameters[name].ToString();
}
return defaultValue;
}
public int GetIntParameter(string name, int defaultValue = 0)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
if (Parameters.ContainsKey(name))
{
var parameterValue = Parameters[name].ToString();
int result;
if (int.TryParse(parameterValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
{
return result;
}
float floatResult;
if (float.TryParse(parameterValue, NumberStyles.Float, CultureInfo.InvariantCulture, out floatResult))
{
result = Convert.ToInt32(floatResult);
return result;
}
}
return defaultValue;
}
public float GetFloatParameter(string name, float defaultValue = 0)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
if (Parameters.ContainsKey(name))
{
var parameterValue = Parameters[name].ToString();
float result;
if (float.TryParse(parameterValue, NumberStyles.Float, CultureInfo.InvariantCulture, out result))
{
return result;
}
}
return defaultValue;
}
public JObject GetJsonParameter(string name, JObject defaultValue = null)
{
if (string.IsNullOrEmpty("name"))
{
throw new ArgumentNullException(nameof(name));
}
if (Parameters.ContainsKey(name))
{
var parameter = Parameters[name].ToString();
if (parameter != null)
{
return JObject.FromObject(parameter);
}
}
return defaultValue;
}
public AIContext GetContext(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Name must be not empty", nameof(name));
}
return Contexts?.FirstOrDefault(c => string.Equals(c.Name, name, StringComparison.CurrentCultureIgnoreCase));
}
}
}

View file

@ -1,24 +0,0 @@
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 string AppId { get; set; }
public AgentModel()
{
}
[JsonProperty("entity_types")]
public List<EntityType> Entities { get; set; }
}
}

View file

@ -1,28 +0,0 @@
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

@ -1,45 +0,0 @@
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

@ -1,25 +0,0 @@
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

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class OwnThinkAiRequest
{
public string AppId { get; set; }
public string Spoken { get; set; }
}
}

View file

@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class OwnThinkAiResponse
{
public OwnThinkChatResponseData Data { get; set; }
public string Message { get; set; }
}
public class OwnThinkChatResponseData
{
public OwnThinkChatResponseDataInfo Info { get; set; }
public int Type { get; set; }
}
public class OwnThinkChatResponseDataInfo
{
public string Text { get; set; }
}
}

View file

@ -1,11 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class OwnThinkApi
{
}
}

View file

@ -1,34 +0,0 @@
using BotSharp.Core;
using BotSharp.Core.AgentStorage;
using BotSharp.Core.ContextStorage;
using BotSharp.Core.Modules;
using BotSharp.Platform.Abstractions;
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

@ -1,186 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BotSharp.Core;
using BotSharp.Platform.Abstractions;
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<OwnThinkAiResponse>(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

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

View file

@ -1,14 +0,0 @@
using BotSharp.Platform.Models.Agents;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace BotSharp.Platform.OwnThink.ViewModels
{
public class AgentCreationRequestViewModel : AgentCreationRequestModel
{
[Required]
public string AppId { get; set; }
}
}

View file

@ -1,12 +0,0 @@
using BotSharp.Platform.Models.Agents;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.ViewModels
{
public class AgentCreationResponseViewModel : AgentCreationResponseModel
{
public string AppId { get; set; }
}
}

View file

@ -102,9 +102,6 @@
<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

@ -18,10 +18,6 @@
"Name": "Articulate",
"Type": "BotSharp.Platform.Articulate"
},
{
"Name": "OwnThink",
"Type": "BotSharp.Platform.OwnThink"
},
{
"Name": "WeixinChannel",
"Type": "BotSharp.Channel.Weixin"

View file

@ -1,9 +0,0 @@
{
// 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

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
# Visual Studio Version 16
VisualStudioVersion = 16.0.29230.61
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Core", "BotSharp.Core\BotSharp.Core.csproj", "{95780673-2A1A-4953-962F-C46CBFDD07FF}"
EndProject
@ -17,8 +17,6 @@ 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
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Articulate", "BotSharp.Platform.Articulate\BotSharp.Platform.Articulate.csproj", "{7290AC89-88DE-4CF8-946A-A1BB0E4C30EB}"
EndProject
Global
@ -85,14 +83,6 @@ 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
{7290AC89-88DE-4CF8-946A-A1BB0E4C30EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7290AC89-88DE-4CF8-946A-A1BB0E4C30EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7290AC89-88DE-4CF8-946A-A1BB0E4C30EB}.Debug|x64.ActiveCfg = Debug|Any CPU