2017-12-18 05:30:20 +00:00
|
|
|
|
using Bot.Rasa.Agents;
|
2017-12-18 13:31:15 +00:00
|
|
|
|
using Bot.Rasa.Models;
|
2017-12-18 05:30:20 +00:00
|
|
|
|
using CustomEntityFoundation;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
using RestSharp;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bot.Rasa.Console
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class RequestExtension
|
|
|
|
|
|
{
|
|
|
|
|
|
public static AgentResponse TextRequest(this RasaConsole console, String agentId, String text)
|
|
|
|
|
|
{
|
|
|
|
|
|
var client = new RestClient($"{console.options.HostUrl}");
|
|
|
|
|
|
|
|
|
|
|
|
var request = new RestRequest("parse?project={project}&q={text}", Method.GET);
|
|
|
|
|
|
request.AddUrlSegment("project", agentId);
|
|
|
|
|
|
request.AddUrlSegment("text", text);
|
|
|
|
|
|
|
|
|
|
|
|
var response = client.Execute<AgentResponse>(request);
|
|
|
|
|
|
|
|
|
|
|
|
return response.Data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-18 13:31:15 +00:00
|
|
|
|
public static bool Train(this RasaConsole console, EntityDbContext dc, String agentId)
|
2017-12-18 05:30:20 +00:00
|
|
|
|
{
|
2017-12-18 13:31:15 +00:00
|
|
|
|
var agent = dc.Agent().Find(agentId);
|
|
|
|
|
|
var corpus = agent.GrabCorpus(dc);
|
2017-12-18 05:30:20 +00:00
|
|
|
|
|
2017-12-18 13:31:15 +00:00
|
|
|
|
string json = JsonConvert.SerializeObject(new { rasa_nlu_data = corpus },
|
|
|
|
|
|
new JsonSerializerSettings
|
2017-12-18 05:30:20 +00:00
|
|
|
|
{
|
2017-12-18 13:31:15 +00:00
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
|
|
|
|
});
|
2017-12-18 05:30:20 +00:00
|
|
|
|
|
2017-12-18 13:31:15 +00:00
|
|
|
|
var client = new RestClient($"{console.options.HostUrl}");
|
|
|
|
|
|
var request = new RestRequest("train", Method.POST);
|
|
|
|
|
|
request.AddQueryParameter("project", agentId);
|
2017-12-18 05:30:20 +00:00
|
|
|
|
request.AddParameter("application/json", json, ParameterType.RequestBody);
|
|
|
|
|
|
|
|
|
|
|
|
var response = client.Execute(request);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|