Add fallback function.

This commit is contained in:
Oceania2018 2018-10-25 21:30:59 -05:00
parent c5abe02f77
commit 562b5dd7c5
10 changed files with 70 additions and 33 deletions

View file

@ -1,16 +1,13 @@
using BotSharp.Core.Abstractions;
using BotSharp.NLP;
using BotSharp.NLP.Classify;
using BotSharp.NLP.Txt2Vec;
using BotSharp.Platform.Models;
using BotSharp.Platform.Models.AiResponse;
using BotSharp.Platform.Models.MachineLearning;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Core.Engines.BotSharp

View file

@ -2,6 +2,7 @@
using BotSharp.NLP;
using BotSharp.NLP.Tokenize;
using BotSharp.Platform.Models;
using BotSharp.Platform.Models.AiResponse;
using BotSharp.Platform.Models.MachineLearning;
using Microsoft.Extensions.Configuration;
using System;

View file

@ -1,5 +1,6 @@
using BotSharp.Core.Abstractions;
using BotSharp.NLP.Tokenize;
using BotSharp.Platform.Models.AiResponse;
using BotSharp.Platform.Models.Entities;
using System;
using System.Collections.Generic;

View file

@ -158,31 +158,15 @@ namespace BotSharp.Core
if (predictedIntent.Confidence < Agent.MlConfig.MinConfidence)
{
var data = new
predictedIntent = await FallbackResponse(request);
predictedIntent.Confidence = Agent.MlConfig.MinConfidence;
predictedIntent.Label = "fallback";
Agent.Intents.Add(new Intent
{
token = "openbot",
info = request.Text
};
using (var client = new HttpClient())
{
var response = await client.PostAsync(
"https://api.ownthink.com/bot",
new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"));
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<JObject>(content);
predictedIntent = new TextClassificationResult
{
Confidence = Agent.MlConfig.MinConfidence,
Classifier = "ownthink",
Label = "fallback"
};
Agent.Intents.Add(new Intent
{
Name = predictedIntent.Label,
Responses = new List<IntentResponse>
Name = predictedIntent.Label,
Responses = new List<IntentResponse>
{
new IntentResponse
{
@ -191,14 +175,13 @@ namespace BotSharp.Core
{
new IntentResponseMessage
{
Speech = "[\"" + result["text"] + "\"]",
Speech = "\"" + predictedIntent.Text + "\"",
Type = AIResponseMessageType.Text
}
}
}
}
});
}
});
}
var aiResponse = new AiResponse
@ -215,6 +198,30 @@ namespace BotSharp.Core
return await AssembleResult<TResult>(aiResponse);
}
public virtual async Task<TextClassificationResult> FallbackResponse(AiRequest request)
{
var data = new
{
token = "openbot",
info = request.Text
};
using (var client = new HttpClient())
{
var response = await client.PostAsync(
"https://api.ownthink.com/bot",
new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"));
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<JObject>(content);
return new TextClassificationResult
{
Classifier = "ownthink",
Text = result["text"].ToString()
};
}
}
public virtual async Task<TResult> AssembleResult<TResult>(AiResponse response)
{
throw new NotImplementedException();

View file

@ -52,5 +52,7 @@ namespace BotSharp.Platform.Abstraction
Task<TResult> TextRequest<TResult>(AiRequest request);
Task<TResult> AssembleResult<TResult>(AiResponse response);
Task<TextClassificationResult> FallbackResponse(AiRequest request);
}
}

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.Engines
namespace BotSharp.Platform.Models.AiResponse
{
public class TextClassificationResult
{
@ -10,6 +10,8 @@ namespace BotSharp.Core.Engines
public String Label { get; set; }
public string Text { get; set; }
public double Confidence { get; set; }
}
}

View file

@ -84,7 +84,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\botsharp-dialogflow\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj" />
<ProjectReference Include="..\..\botsharp-channel-weixin\BotSharp.Channel.Weixin\BotSharp.Channel.Weixin.csproj" />
<ProjectReference Include="..\BotSharp.Core\BotSharp.Core.csproj" />
</ItemGroup>

View file

@ -3,6 +3,7 @@
"assemblies": "BotSharp.Core",
"platformModuleName": "DialogflowAi",
"overrideFallback": false,
"modules": [
/*{

View file

@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Models",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Dialogflow", "..\botsharp-dialogflow\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj", "{6A7E5DC1-3AE3-42C1-8F67-3131783646EB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Channel.Weixin", "..\botsharp-channel-weixin\BotSharp.Channel.Weixin\BotSharp.Channel.Weixin.csproj", "{640AB037-2338-4550-A21A-0DB7C2A350F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
ARTICULATE|Any CPU = ARTICULATE|Any CPU
@ -232,6 +234,30 @@ Global
{6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Test|Any CPU.Build.0 = Debug|Any CPU
{6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Test|x64.ActiveCfg = Debug|Any CPU
{6A7E5DC1-3AE3-42C1-8F67-3131783646EB}.Test|x64.Build.0 = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.ARTICULATE|Any CPU.ActiveCfg = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.ARTICULATE|Any CPU.Build.0 = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.ARTICULATE|x64.ActiveCfg = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.ARTICULATE|x64.Build.0 = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Debug|x64.ActiveCfg = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Debug|x64.Build.0 = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.DIALOGFLOW|Any CPU.ActiveCfg = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.DIALOGFLOW|Any CPU.Build.0 = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.DIALOGFLOW|x64.ActiveCfg = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.DIALOGFLOW|x64.Build.0 = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.RASA|Any CPU.ActiveCfg = Release|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.RASA|Any CPU.Build.0 = Release|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.RASA|x64.ActiveCfg = Release|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.RASA|x64.Build.0 = Release|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Release|Any CPU.Build.0 = Release|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Release|x64.ActiveCfg = Release|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Release|x64.Build.0 = Release|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Test|Any CPU.ActiveCfg = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Test|Any CPU.Build.0 = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Test|x64.ActiveCfg = Debug|Any CPU
{640AB037-2338-4550-A21A-0DB7C2A350F1}.Test|x64.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE