forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogflowAi.cs
More file actions
182 lines (161 loc) · 6.84 KB
/
DialogflowAi.cs
File metadata and controls
182 lines (161 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BotSharp.Core;
using BotSharp.Platform.Abstractions;
using BotSharp.Platform.Models;
using DotNetToolkit;
using BotSharp.Platform.Dialogflow.Models;
using System.IO;
using Microsoft.Extensions.Configuration;
using BotSharp.Platform.Models.Intents;
using BotSharp.Platform.Models.AiResponse;
using BotSharp.Platform.Models.AiRequest;
using Turing.NET;
using System.Text.RegularExpressions;
using BotSharp.Platform.Models.Contexts;
using Ding.Log;
using Ding.Serialization;
namespace BotSharp.Platform.Dialogflow
{
public class DialogflowAi<TAgent> :
PlatformBuilderBase<TAgent>,
IPlatformBuilder<TAgent>
where TAgent : AgentModel
{
IConfiguration config;
public DialogflowAi(IAgentStorageFactory<TAgent> agentStorageFactory, IContextStorageFactory<AIContext> contextStorageFactory, IPlatformSettings settings, IConfiguration config)
: base(agentStorageFactory, contextStorageFactory, settings)
{
this.config = config;
}
public async Task<TrainingCorpus> ExtractorCorpus(TAgent agent)
{
var corpus = new TrainingCorpus
{
Entities = new List<TrainingEntity>(),
UserSays = new List<TrainingIntentExpression<TrainingIntentExpressionPart>>()
};
agent.Entities.ForEach(entity =>
{
corpus.Entities.Add(new TrainingEntity
{
Entity = entity.Name,
Values = entity.Entries.Select(x => new TrainingEntitySynonym
{
Value = x.Value,
Synonyms = x.Synonyms.Select(y => y.Synonym).ToList()
}).ToList()
});
});
agent.Intents.ForEach(intent =>
{
// filter unexpected intents
if(intent.Name != "Default Fallback Intent")
{
// caculate contexts hash
intent.ContextHash = String.Join('_', intent.Contexts.OrderBy(x => x.Name).Select(x => x.Name)).GetMd5Hash();
intent.UserSays.ForEach(say => {
corpus.UserSays.Add(new TrainingIntentExpression<TrainingIntentExpressionPart>
{
Intent = intent.Name,
Text = String.Join("", say.Data.Select(x => x.Text)),
Entities = say.Data.Where(x => !String.IsNullOrEmpty(x.Meta))
.Select(x => new TrainingIntentExpressionPart
{
Value = x.Text,
Entity = x.Meta,
Start = x.Start
})
.ToList(),
ContextHash = intent.ContextHash
});
});
}
});
return corpus;
}
public override async Task<TextClassificationResult> FallbackResponse(AiRequest request)
{
if (config.GetValue<bool>("overrideFallback"))
{
var turing = new TuringAgent(config);
var tulingResponse = turing.Request(new TuringRequest
{
Perception = new TuringRequestPerception
{
InputText = new TuringInputText { Text = request.Text }
}
});
var result = tulingResponse.Results.FirstOrDefault(x => x.ResultType == "text").Values.Text;
return new TextClassificationResult
{
Classifier = "turing",
Text = tulingResponse.Results.FirstOrDefault(x => x.ResultType == "text").Values.Text
};
}
else
{
return await base.FallbackResponse(request);
}
}
public override async Task<TResult> AssembleResult<TResult>(AiRequest request, AiResponse response)
{
var intent = Agent.Intents.Find(x => x.Name == response.Intent);
var presetResponse = intent.Responses.FirstOrDefault();
// format messages
presetResponse.Messages = presetResponse.Messages.Where(x => x.Speech.Length > 0).ToList();
if (presetResponse.Messages.Count == 0)
{
presetResponse.Messages.Add(new IntentResponseMessage
{
Speech = "\"" + intent.Name + "\""
});
}
// fill parameters
presetResponse.Parameters.ForEach(p =>
{
var entity = response.Entities.FirstOrDefault(x => x.Entity == p.DataType);
p.Value = entity?.Value;
});
var matches = Regex.Matches(presetResponse.Messages.Random().Speech, "\".*?\"").Cast<Match>();
var speech = matches.Count() == 0 ? String.Empty : matches.ToList().Random().Value;
var contexts = HandleContexts(request.SessionId, presetResponse);
var aiResponse = new AIResponseResult
{
ResolvedQuery = response.ResolvedQuery,
Action = presetResponse.Action,
Metadata = new AIResponseMetadata
{
IntentName = response.Intent
},
Intent = response.Intent,
Fulfillment = new AIResponseFulfillment
{
Messages = presetResponse.Messages.ToList<object>(),
Speech = speech.Length > 1 ? speech.Substring(1, speech.Length - 2) : String.Empty
},
Score = response.Score,
Source = response.Source,
Contexts = contexts.ToArray(),
Parameters = presetResponse.Parameters.Where(x => !String.IsNullOrEmpty(x.Value)).ToDictionary(item => item.Name, item => (object)item.Value)
};
return (TResult)(object)aiResponse;
}
private List<AIContext> HandleContexts(string sessionId, IntentResponse response)
{
var newContexts = response.Contexts.Select(x => new AIContext
{
Name = x.Name,
Lifespan = x.Lifespan,
Parameters = response.Parameters.Select(p => new KeyValuePair<string, object>(p.Name, p.Value)).ToDictionary(d => d.Key, d => d.Value == null ? String.Empty : d.Value)
}).ToList();
// persist
var ctxStore = contextStorageFactory.Get();
ctxStore.Persist(sessionId, newContexts.ToArray());
return newContexts;
}
}
}