complete text request function
This commit is contained in:
parent
b4a8d4919c
commit
ae99ce5ba7
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
25
BotSharp.Platform.Models/AiRequest/AIRequest.cs
Normal file
25
BotSharp.Platform.Models/AiRequest/AIRequest.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
13
BotSharp.Platform.Models/AiResponse/AIResponse.cs
Normal file
13
BotSharp.Platform.Models/AiResponse/AIResponse.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
11
Platform.Articulate/ViewModels/ResponseViewModel.cs
Normal file
11
Platform.Articulate/ViewModels/ResponseViewModel.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue