From ae99ce5ba7b759206401b153f8e2dcd6ffeacca9 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Mon, 1 Oct 2018 21:49:01 -0500 Subject: [PATCH] complete text request function --- BotSharp.Core/Engines/BotEngineBase.cs | 9 +++++- BotSharp.Core/Engines/BotPredictor.cs | 5 +-- .../Engines/Sebis/AgentImporterInSebis.cs | 1 - .../IPlatformBuilder.cs | 4 +++ .../AiRequest/AIRequest.cs | 25 +++++++++++++++ .../AiResponse/AIResponse.cs | 13 ++++++++ Platform.Articulate/ArticulateAi.cs | 32 +++++++++++++++++++ .../Controllers/AgentController.cs | 2 +- .../Controllers/ParseControllercs.cs | 32 +++++++++++++++++-- .../ViewModels/ResponseViewModel.cs | 11 +++++++ 10 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 BotSharp.Platform.Models/AiRequest/AIRequest.cs create mode 100644 BotSharp.Platform.Models/AiResponse/AIResponse.cs create mode 100644 Platform.Articulate/ViewModels/ResponseViewModel.cs diff --git a/BotSharp.Core/Engines/BotEngineBase.cs b/BotSharp.Core/Engines/BotEngineBase.cs index 5c5fdbac..fba2385a 100644 --- a/BotSharp.Core/Engines/BotEngineBase.cs +++ b/BotSharp.Core/Engines/BotEngineBase.cs @@ -6,6 +6,7 @@ using BotSharp.Core.Intents; using BotSharp.Core.Models; using BotSharp.Models.NLP; using BotSharp.Platform.Models; +using BotSharp.Platform.Models.AiRequest; using DotNetToolkit; using EntityFrameworkCore.BootKit; using Microsoft.EntityFrameworkCore; @@ -40,7 +41,13 @@ namespace BotSharp.Core.Engines public AIResponse TextRequest(AIRequest request) { var preditor = new BotPredictor(); - var doc = preditor.Predict(agent, request).Result; + var doc = preditor.Predict(agent, new AiRequest + { + AgentDir = request.AgentDir, + Model = request.Model, + SessionId = request.SessionId, + Text = request.Query.First() + }).Result; var parameters = new Dictionary(); if(doc.Sentences[0].Entities == null) diff --git a/BotSharp.Core/Engines/BotPredictor.cs b/BotSharp.Core/Engines/BotPredictor.cs index 231939b0..ac47db8a 100644 --- a/BotSharp.Core/Engines/BotPredictor.cs +++ b/BotSharp.Core/Engines/BotPredictor.cs @@ -1,6 +1,7 @@ using BotSharp.Core.Abstractions; using BotSharp.Core.Agents; using BotSharp.Core.Models; +using BotSharp.Platform.Models.AiRequest; using DotNetToolkit; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; @@ -19,7 +20,7 @@ namespace BotSharp.Core.Engines { public class BotPredictor { - public async Task Predict(Agent agent, AIRequest request) + public async Task Predict(Agent agent, AiRequest request) { // load model var dir = Path.Combine(request.AgentDir, request.Model); @@ -41,7 +42,7 @@ namespace BotSharp.Core.Engines { new NlpDocSentence { - Text = request.Query.FirstOrDefault() + Text = request.Text } } }; diff --git a/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs b/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs index a87fcbda..bdd56fe8 100644 --- a/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs +++ b/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; -using BotSharp.Core.Adapters.Dialogflow; using BotSharp.Core.Adapters.Sebis; using BotSharp.Core.Agents; using BotSharp.Core.Entities; diff --git a/BotSharp.Platform.Abstraction/IPlatformBuilder.cs b/BotSharp.Platform.Abstraction/IPlatformBuilder.cs index 3fd8f1b0..2f46715f 100644 --- a/BotSharp.Platform.Abstraction/IPlatformBuilder.cs +++ b/BotSharp.Platform.Abstraction/IPlatformBuilder.cs @@ -1,4 +1,6 @@ using BotSharp.Platform.Models; +using BotSharp.Platform.Models.AiRequest; +using BotSharp.Platform.Models.AiResponse; using System; using System.Collections.Generic; using System.Text; @@ -38,5 +40,7 @@ namespace BotSharp.Platform.Abstraction bool SaveAgent(TAgent agent); Task Train(TAgent agent, TrainingCorpus corpus); + + AiResponse TextRequest(AiRequest request); } } diff --git a/BotSharp.Platform.Models/AiRequest/AIRequest.cs b/BotSharp.Platform.Models/AiRequest/AIRequest.cs new file mode 100644 index 00000000..9a924810 --- /dev/null +++ b/BotSharp.Platform.Models/AiRequest/AIRequest.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Platform.Models.AiRequest +{ + public class AiRequest + { + public string AgentId { get; set; } + + public string Text { get; set; } + + public string SessionId { get; set; } + + public bool ResetContexts { get; set; } + + /// + /// What model is used to predict. + /// It's optional. + /// + public string Model { get; set; } + + public string AgentDir { get; set; } + } +} diff --git a/BotSharp.Platform.Models/AiResponse/AIResponse.cs b/BotSharp.Platform.Models/AiResponse/AIResponse.cs new file mode 100644 index 00000000..4842bd96 --- /dev/null +++ b/BotSharp.Platform.Models/AiResponse/AIResponse.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Platform.Models.AiResponse +{ + public class AiResponse + { + public string Speech { get; set; } + + public string Intent { get; set; } + } +} diff --git a/Platform.Articulate/ArticulateAi.cs b/Platform.Articulate/ArticulateAi.cs index baac23d0..1f5292ab 100644 --- a/Platform.Articulate/ArticulateAi.cs +++ b/Platform.Articulate/ArticulateAi.cs @@ -4,8 +4,11 @@ using BotSharp.Core.Engines; using BotSharp.Core.Entities; using BotSharp.Core.Intents; using BotSharp.Core.Models; +using BotSharp.Models.NLP; using BotSharp.Platform.Abstraction; using BotSharp.Platform.Models; +using BotSharp.Platform.Models.AiRequest; +using BotSharp.Platform.Models.AiResponse; using DotNetToolkit; using Platform.Articulate.Models; using System; @@ -192,5 +195,34 @@ namespace Platform.Articulate return true; } + + public AiResponse TextRequest(AiRequest request) + { + var aiResponse = new AiResponse(); + + // Load agent + var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.AgentId); + var model = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last(); + var modelPath = Path.Combine(projectPath, model); + request.AgentDir = projectPath; + request.Model = model; + + var agent = GetAgentById(request.AgentId); + + var preditor = new BotPredictor(); + var doc = preditor.Predict(agent.ToObject(), request).Result; + + var parameters = new Dictionary(); + if (doc.Sentences[0].Entities == null) + { + doc.Sentences[0].Entities = new List(); + } + doc.Sentences[0].Entities.ForEach(x => parameters[x.Entity] = x.Value); + + aiResponse.Intent = doc.Sentences[0].Intent.Label; + aiResponse.Speech = aiResponse.Intent; + + return aiResponse; + } } } diff --git a/Platform.Articulate/Controllers/AgentController.cs b/Platform.Articulate/Controllers/AgentController.cs index 03a93af0..d564c524 100644 --- a/Platform.Articulate/Controllers/AgentController.cs +++ b/Platform.Articulate/Controllers/AgentController.cs @@ -48,7 +48,7 @@ namespace Platform.Articulate.Controllers } // convert to standard Agent structure - agent.Id = Guid.NewGuid().ToString(); + agent.Id = new Random().Next(Int32.MaxValue).ToString(); agent.Name = agent.AgentName; builder.SaveAgent(agent); diff --git a/Platform.Articulate/Controllers/ParseControllercs.cs b/Platform.Articulate/Controllers/ParseControllercs.cs index 3db1c560..889e30db 100644 --- a/Platform.Articulate/Controllers/ParseControllercs.cs +++ b/Platform.Articulate/Controllers/ParseControllercs.cs @@ -1,8 +1,17 @@ -using Microsoft.AspNetCore.Mvc; +using BotSharp.Core.Models; +using BotSharp.NLP; +using BotSharp.Platform.Models.AiRequest; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; +using Platform.Articulate.Models; +using Platform.Articulate.ViewModels; using System; using System.Collections.Generic; +using System.Drawing; +using System.IO; +using System.Linq; using System.Text; +using Console = Colorful.Console; namespace Platform.Articulate.Controllers { @@ -11,11 +20,30 @@ namespace Platform.Articulate.Controllers public class ParseControllercs : ControllerBase { private readonly IConfiguration configuration; + private ArticulateAi builder; + + public ParseControllercs(IConfiguration configuration) + { + builder = new ArticulateAi(); + builder.PlatformConfig = configuration.GetSection("ArticulateAi"); + } [HttpGet("/agent/{agentId}/converse")] public ActionResult ParseText([FromRoute] string agentId, [FromQuery] string text, [FromQuery] string sessionId) { - return Ok(); + var response = new ResponseViewModel(); + + Console.WriteLine($"Got message from {Request.Host}: {text}", Color.Green); + + var aiResponse = builder.TextRequest(new AiRequest + { + AgentId = agentId, + Text = text + }); + + response.TextResponse = aiResponse.Speech; + + return Ok(response); } } #endif diff --git a/Platform.Articulate/ViewModels/ResponseViewModel.cs b/Platform.Articulate/ViewModels/ResponseViewModel.cs new file mode 100644 index 00000000..3b2da95d --- /dev/null +++ b/Platform.Articulate/ViewModels/ResponseViewModel.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Platform.Articulate.ViewModels +{ + public class ResponseViewModel + { + public string TextResponse { get; set; } + } +}