fix dialogflow v1 query post format bug.

This commit is contained in:
Oceania2018 2018-07-18 22:31:17 -05:00
parent fd8f6204e6
commit 66a0e9e60a
11 changed files with 46 additions and 46 deletions

View file

@ -34,10 +34,6 @@
<DefineConstants>TRACE;MODEL_PER_CONTEXTS</DefineConstants> <DefineConstants>TRACE;MODEL_PER_CONTEXTS</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="Engines\Dialogflow\ApiAi.cs" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="DotNetToolkit" Version="1.4.0" /> <PackageReference Include="DotNetToolkit" Version="1.4.0" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="1.6.3" /> <PackageReference Include="EntityFrameworkCore.BootKit" Version="1.6.3" />

View file

@ -12,7 +12,7 @@ namespace BotSharp.Core.Models
public string Name { get; set; } public string Name { get; set; }
[JsonProperty("parameters")] [JsonProperty("parameters")]
public Dictionary<string, string> Parameters { get; set; } public Dictionary<string, object> Parameters { get; set; }
/// <summary> /// <summary>
/// Lifespan of the context measured in requests```` /// Lifespan of the context measured in requests````

View file

@ -1,6 +1,7 @@
using BotSharp.Core.Engines.Dialogflow.Http; using BotSharp.Core.Engines.Dialogflow.Http;
using BotSharp.Core.Models; using BotSharp.Core.Models;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -47,7 +48,8 @@ namespace BotSharp.Core.Engines.Dialogflow
var jsonSettings = new JsonSerializerSettings var jsonSettings = new JsonSerializerSettings
{ {
NullValueHandling = NullValueHandling.Ignore NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
}; };
var jsonRequest = JsonConvert.SerializeObject(request, Formatting.None, jsonSettings); var jsonRequest = JsonConvert.SerializeObject(request, Formatting.None, jsonSettings);

View file

@ -31,7 +31,7 @@ namespace BotSharp.Core.Models
} }
} }
public Dictionary<string, string> Parameters { get; set; } public Dictionary<string, object> Parameters { get; set; }
public AIContext[] Contexts { get; set; } public AIContext[] Contexts { get; set; }

View file

@ -8,18 +8,15 @@ namespace BotSharp.Core.Engines.Dialogflow
{ {
public class ApiAi : ApiAiBase, IBotPlatform public class ApiAi : ApiAiBase, IBotPlatform
{ {
private readonly AIConfiguration config; private AIDataService dataService;
private readonly AIDataService dataService;
public ApiAi(AIConfiguration config)
{
this.config = config;
dataService = new AIDataService(this.config);
}
public AIResponse TextRequest(string text) public AIResponse TextRequest(string text)
{ {
if (dataService == null)
{
dataService = new AIDataService(AiConfig);
}
if (string.IsNullOrEmpty(text)) if (string.IsNullOrEmpty(text))
{ {
throw new ArgumentNullException("text"); throw new ArgumentNullException("text");
@ -35,6 +32,11 @@ namespace BotSharp.Core.Engines.Dialogflow
throw new ArgumentNullException("request"); throw new ArgumentNullException("request");
} }
if(dataService == null)
{
dataService = new AIDataService(AiConfig);
}
return dataService.Request(request); return dataService.Request(request);
} }
@ -45,17 +47,17 @@ namespace BotSharp.Core.Engines.Dialogflow
throw new ArgumentNullException("text"); throw new ArgumentNullException("text");
} }
if (dataService == null)
{
dataService = new AIDataService(AiConfig);
}
return TextRequest(new AIRequest(text, requestExtras)); return TextRequest(new AIRequest(text, requestExtras));
} }
public AIResponse VoiceRequest(Stream voiceStream, RequestExtras requestExtras = null) public void Train()
{ {
if (config.Language == SupportedLanguage.Italian) throw new NotImplementedException();
{
throw new AIServiceException("Sorry, but Italian language now is not supported in Speaktoit recognition. Please use some another speech recognition engine.");
}
return dataService.VoiceRequest(voiceStream, requestExtras);
} }
} }
} }

View file

@ -4,7 +4,7 @@ using System.Text;
namespace BotSharp.Core.Engines.Dialogflow namespace BotSharp.Core.Engines.Dialogflow
{ {
public class ApiAiBase public class ApiAiBase : BotEngineBase
{ {
protected float[] TrimSilence(float[] samples) protected float[] TrimSilence(float[] samples)
{ {

View file

@ -51,7 +51,7 @@ namespace BotSharp.Core.Engines
Source = "agent", Source = "agent",
ResolvedQuery = request.Query.First(), ResolvedQuery = request.Query.First(),
Action = intentResponse?.Action, Action = intentResponse?.Action,
Parameters = intentResponse?.Parameters?.ToDictionary(x => x.Name, x => x.Value), Parameters = intentResponse?.Parameters?.ToDictionary(x => x.Name, x => (object)x.Value),
Score = response.Intent.Confidence, Score = response.Intent.Confidence,
Metadata = new AIResponseMetadata { IntentId = intentResponse?.IntentId, IntentName = intentResponse?.IntentName }, Metadata = new AIResponseMetadata { IntentId = intentResponse?.IntentId, IntentName = intentResponse?.IntentName },
Fulfillment = new AIResponseFulfillment Fulfillment = new AIResponseFulfillment
@ -132,7 +132,7 @@ namespace BotSharp.Core.Engines
var data = new RasaTrainingData var data = new RasaTrainingData
{ {
Entities = entity_synonyms.Select(x => x.ToObject<RasaTraningEntity>()).ToList(), Entities = entity_synonyms.Select(x => x.ToObject<RasaTraningEntity>()).ToList(),
UserSays = common_examples.Select(x => x.Intent.ToObject<RasaIntentExpression>()).ToList() UserSays = common_examples.Select(x => x.ToObject<RasaIntentExpression>()).ToList()
}; };
// meet minimal requirement // meet minimal requirement

View file

@ -1,4 +1,5 @@
using BotSharp.Core.Engines; using BotSharp.Core.Adapters.Rasa;
using BotSharp.Core.Engines;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -6,7 +7,7 @@ using System.Text;
namespace BotSharp.Core.Models namespace BotSharp.Core.Models
{ {
public class RasaIntentExpression : TrainingIntentExpression<RasaIntentExpressionPart> public class RasaIntentExpression : TrainingIntentExpression<TrainingIntentExpressionPart>
{ {
} }

View file

@ -1,5 +1,6 @@
using BotSharp.Core.Agents; using BotSharp.Core.Agents;
using BotSharp.Core.Engines; using BotSharp.Core.Engines;
using BotSharp.Core.Engines.Dialogflow;
using BotSharp.Core.Models; using BotSharp.Core.Models;
using BotSharp.RestApi.Integrations.FacebookMessenger; using BotSharp.RestApi.Integrations.FacebookMessenger;
using DotNetToolkit; using DotNetToolkit;
@ -77,11 +78,11 @@ namespace BotSharp.RestApi.Integrations
{ {
Console.WriteLine($"OnTextMessaged: {message.Message.Text}"); Console.WriteLine($"OnTextMessaged: {message.Message.Text}");
var rasa = new RasaAi(); var ai = new ApiAi();
var agent = rasa.LoadAgent(agentId); var agent = ai.LoadAgent(agentId);
rasa.AiConfig = new AIConfiguration(agent.ClientAccessToken, SupportedLanguage.English) { AgentId = agentId }; ai.AiConfig = new AIConfiguration(agent.ClientAccessToken, SupportedLanguage.English) { AgentId = agentId };
rasa.AiConfig.SessionId = message.Sender.Id; ai.AiConfig.SessionId = message.Sender.Id;
var aiResponse = rasa.TextRequest(new AIRequest { Query = new String[] { message.Message.Text } }); var aiResponse = ai.TextRequest(new AIRequest { Query = new String[] { message.Message.Text } });
var dc = new DefaultDataContextLoader().GetDefaultDc(); var dc = new DefaultDataContextLoader().GetDefaultDc();
var config = dc.Table<AgentIntegration>().FirstOrDefault(x => x.AgentId == agentId && x.Platform == "Facebook Messenger"); var config = dc.Table<AgentIntegration>().FirstOrDefault(x => x.AgentId == agentId && x.Platform == "Facebook Messenger");
@ -91,7 +92,7 @@ namespace BotSharp.RestApi.Integrations
Recipient = message.Sender.ToObject<WebhookMessageRecipient>(), Recipient = message.Sender.ToObject<WebhookMessageRecipient>(),
Message = new WebhookTextMessage Message = new WebhookTextMessage
{ {
Text = aiResponse.Result.Fulfillment.Speech Text = String.IsNullOrEmpty(aiResponse.Result.Fulfillment.Speech) ? aiResponse.Result.Action : aiResponse.Result.Fulfillment.Speech
} }
}); });
} }

View file

@ -57,11 +57,8 @@ namespace BotSharp.UnitTest
[TestMethod] [TestMethod]
public void TrainAgentTest() public void TrainAgentTest()
{ {
var config = new AIConfiguration("", SupportedLanguage.English) { AgentId = BOT_ID };
config.SessionId = Guid.NewGuid().ToString();
var rasa = new RasaAi(); var rasa = new RasaAi();
rasa.LoadAgent(BOT_ID);
rasa.Train(); rasa.Train();
} }
} }

View file

@ -1,4 +1,5 @@
using BotSharp.Core.Engines; using BotSharp.Core.Engines;
using BotSharp.Core.Engines.Dialogflow;
using BotSharp.Core.Models; using BotSharp.Core.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System; using System;
@ -14,32 +15,32 @@ namespace BotSharp.UnitTest
[TestMethod] [TestMethod]
public void TextRequest() public void TextRequest()
{ {
var rasa = new RasaAi(); var ai = new ApiAi();
var agent = rasa.LoadAgent(BOT_ID); var agent = ai.LoadAgent(BOT_ID);
rasa.AiConfig = new AIConfiguration(agent.ClientAccessToken, SupportedLanguage.English) { AgentId = BOT_ID }; ai.AiConfig = new AIConfiguration(agent.ClientAccessToken, SupportedLanguage.English) { AgentId = BOT_ID };
rasa.AiConfig.SessionId = Guid.NewGuid().ToString(); ai.AiConfig.SessionId = Guid.NewGuid().ToString();
// Round 1 // Round 1
var response = rasa.TextRequest(new AIRequest { Query = new String[] { "Can you play country music?" } }); var response = ai.TextRequest(new AIRequest { Query = new String[] { "Can you play country music?" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "music.play"); Assert.AreEqual(response.Result.Metadata.IntentName, "music.play");
Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "music-player-control").Lifespan, 3); Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "music-player-control").Lifespan, 3);
Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "play-music").Lifespan, 5); Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "play-music").Lifespan, 5);
Assert.AreEqual(response.Result.Parameters.First(x => x.Key == "genre").Value, "country"); Assert.AreEqual(response.Result.Parameters.First(x => x.Key == "genre").Value, "country");
// Round 2 // Round 2
response = rasa.TextRequest(new AIRequest { Query = new String[] { "pause it" } }); response = ai.TextRequest(new AIRequest { Query = new String[] { "pause it" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "music_player_control.pause"); Assert.AreEqual(response.Result.Metadata.IntentName, "music_player_control.pause");
Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "music-player-control").Lifespan, 3); Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "music-player-control").Lifespan, 3);
Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "play-music").Lifespan, 4); Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "play-music").Lifespan, 4);
// Round 3 // Round 3
response = rasa.TextRequest(new AIRequest { Query = new String[] { "continue" } }); response = ai.TextRequest(new AIRequest { Query = new String[] { "continue" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "music_player_control.resume"); Assert.AreEqual(response.Result.Metadata.IntentName, "music_player_control.resume");
Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "music-player-control").Lifespan, 3); Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "music-player-control").Lifespan, 3);
Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "play-music").Lifespan, 3); Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "play-music").Lifespan, 3);
// Round 4 // Round 4
response = rasa.TextRequest(new AIRequest { Query = new String[] { "play Hard Times by David Newman" } }); response = ai.TextRequest(new AIRequest { Query = new String[] { "play Hard Times by David Newman" } });
Assert.AreEqual(response.Result.Metadata.IntentName, "music.play"); Assert.AreEqual(response.Result.Metadata.IntentName, "music.play");
Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "music-player-control").Lifespan, 3); Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "music-player-control").Lifespan, 3);
Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "play-music").Lifespan, 5); Assert.AreEqual(response.Result.Contexts.First(x => x.Name == "play-music").Lifespan, 5);