2017-12-18 05:30:20 +00:00
|
|
|
|
using Bot.Rasa.Agents;
|
2017-12-30 20:26:35 +00:00
|
|
|
|
using Bot.Rasa.Entities;
|
|
|
|
|
|
using EntityFrameworkCore.BootKit;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-12-18 05:30:20 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2017-12-30 20:26:35 +00:00
|
|
|
|
using System.IO;
|
2017-12-18 05:30:20 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bot.Rasa.Console
|
|
|
|
|
|
{
|
|
|
|
|
|
public class RasaConsole
|
|
|
|
|
|
{
|
2017-12-30 20:26:35 +00:00
|
|
|
|
private Database dc { get; set; }
|
|
|
|
|
|
public static RasaOptions Options { get; set; }
|
|
|
|
|
|
public static IConfiguration Configuration { get; set; }
|
2017-12-18 05:30:20 +00:00
|
|
|
|
|
2017-12-30 20:26:35 +00:00
|
|
|
|
public RasaConsole(Database dc)
|
2017-12-18 05:30:20 +00:00
|
|
|
|
{
|
|
|
|
|
|
this.dc = dc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-30 20:26:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Restore a agent instance from json file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="agentId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public Agent RestoreAgent(String agentId)
|
2017-12-18 05:30:20 +00:00
|
|
|
|
{
|
2017-12-30 20:26:35 +00:00
|
|
|
|
string json = File.ReadAllText($"{Options.ContentRootPath}\\App_Data\\DbInitializer\\Agents\\{agentId}.json");
|
|
|
|
|
|
var agent = JsonConvert.DeserializeObject<Agent>(json);
|
|
|
|
|
|
|
|
|
|
|
|
agent.Id = agentId;
|
|
|
|
|
|
agent.EntityTypes.ForEach(entityType =>
|
|
|
|
|
|
{
|
|
|
|
|
|
entityType.Items = entityType.Values.Select(x => new EntityItem
|
|
|
|
|
|
{
|
|
|
|
|
|
Value = x
|
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
agent.Intents.ForEach(intent => {
|
|
|
|
|
|
|
|
|
|
|
|
intent.Expressions.ForEach(expression =>
|
|
|
|
|
|
{
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return agent;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Dump agent train data to json file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="agentId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool DumpAgent(String agentId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Agent LoadAgent(String agentId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return dc.Agent().Include(x => x.Intents).FirstOrDefault(x => x.Id == agentId);
|
2017-12-18 05:30:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-30 20:26:35 +00:00
|
|
|
|
public String CreateAgent(Agent agent)
|
2017-12-18 05:30:20 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (dc.Agent().Any(x => x.Name == agent.Name)) return String.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
dc.Agent().Add(agent);
|
|
|
|
|
|
|
|
|
|
|
|
return agent.Id;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|