complete text request function

This commit is contained in:
Oceania2018 2018-10-01 21:49:01 -05:00
parent b4a8d4919c
commit ae99ce5ba7
10 changed files with 127 additions and 7 deletions

View file

@ -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<String, Object>();
if(doc.Sentences[0].Entities == null)

View file

@ -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<NlpDoc> Predict(Agent agent, AIRequest request)
public async Task<NlpDoc> 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
}
}
};

View file

@ -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;

View file

@ -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<bool> Train(TAgent agent, TrainingCorpus corpus);
AiResponse TextRequest(AiRequest request);
}
}

View file

@ -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; }
/// <summary>
/// What model is used to predict.
/// It's optional.
/// </summary>
public string Model { get; set; }
public string AgentDir { get; set; }
}
}

View file

@ -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; }
}
}

View file

@ -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<Agent>(), request).Result;
var parameters = new Dictionary<String, Object>();
if (doc.Sentences[0].Entities == null)
{
doc.Sentences[0].Entities = new List<NlpEntity>();
}
doc.Sentences[0].Entities.ForEach(x => parameters[x.Entity] = x.Value);
aiResponse.Intent = doc.Sentences[0].Intent.Label;
aiResponse.Speech = aiResponse.Intent;
return aiResponse;
}
}
}

View file

@ -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);

View file

@ -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<AgentModel> builder;
public ParseControllercs(IConfiguration configuration)
{
builder = new ArticulateAi<AgentModel>();
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

View file

@ -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; }
}
}