BotSharp/Bot.Rasa/Console/RequestExtension.cs

62 lines
2.1 KiB
C#
Raw Normal View History

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-30 20:26:35 +00:00
using EntityFrameworkCore.BootKit;
2017-12-18 05:30:20 +00:00
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)
{
2017-12-30 20:26:35 +00:00
var client = new RestClient($"{RasaConsole.Options.HostUrl}");
2017-12-18 05:30:20 +00:00
2017-12-30 20:26:35 +00:00
var request = new RestRequest("parse", Method.POST);
string json = JsonConvert.SerializeObject(new { Project = agentId, Q = text },
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
request.AddParameter("application/json", json, ParameterType.RequestBody);
2017-12-18 05:30:20 +00:00
var response = client.Execute<AgentResponse>(request);
return response.Data;
}
2017-12-30 20:26:35 +00:00
/// <summary>
/// Need two categories at least
/// </summary>
/// <param name="console"></param>
/// <param name="dc"></param>
/// <param name="agentId"></param>
/// <returns></returns>
public static bool Train(this RasaConsole console, Database dc, String agentId)
2017-12-18 05:30:20 +00:00
{
2018-01-02 18:05:35 +00:00
var agent = dc.Table<Agent>().Find(agentId);
2017-12-18 13:31:15 +00:00
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-30 20:26:35 +00:00
var client = new RestClient($"{RasaConsole.Options.HostUrl}");
2017-12-18 13:31:15 +00:00
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);
2017-12-30 20:26:35 +00:00
return response.IsSuccessful;
2017-12-18 05:30:20 +00:00
}
}
}