forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuisActionResolver.cs
552 lines (457 loc) · 19 KB
/
LuisActionResolver.cs
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
namespace Microsoft.Cognitive.LUIS.ActionBinding
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using Newtonsoft.Json.Linq;
[Serializable]
public class LuisActionResolver
{
private readonly IDictionary<string, Type> luisActions;
public LuisActionResolver(params Assembly[] lookupAssemblies)
{
this.luisActions = new Dictionary<string, Type>();
if (lookupAssemblies == null)
{
throw new ArgumentNullException(nameof(lookupAssemblies));
}
foreach (var lookupAssembly in lookupAssemblies)
{
foreach (var info in lookupAssembly.GetTypes().Select(t => new { Type = t, Attribs = t.GetCustomAttributes<LuisActionBindingAttribute>(true) }).Where(o => o.Attribs.Any()))
{
foreach (var intentAttrib in info.Attribs)
{
this.luisActions.Add(intentAttrib.IntentName, info.Type);
}
}
}
}
public static bool AssignValue(ILuisAction action, string paramName, object paramValue)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (string.IsNullOrWhiteSpace(paramName))
{
throw new ArgumentNullException(nameof(paramName));
}
if (paramValue == null)
{
throw new ArgumentNullException(nameof(paramValue));
}
return AssignValue(action, action.GetType().GetProperty(paramName, BindingFlags.Public | BindingFlags.Instance), paramValue);
}
public static async Task<QueryValueResult> QueryValueFromLuisAsync(
ILuisService service,
ILuisAction action,
string paramName,
object paramValue,
CancellationToken token,
Func<PropertyInfo, IEnumerable<EntityRecommendation>, EntityRecommendation> entityExtractor = null)
{
var originalValue = paramValue;
if (service == null)
{
throw new ArgumentNullException(nameof(service));
}
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (string.IsNullOrWhiteSpace(paramName))
{
throw new ArgumentNullException(nameof(paramName));
}
if (paramValue == null)
{
throw new ArgumentNullException(nameof(paramValue));
}
var result = await service.QueryAsync(paramValue.ToString(), token);
var queryIntent = result.Intents.FirstOrDefault();
if (!Intents.None.Equals(queryIntent.Intent, StringComparison.InvariantCultureIgnoreCase))
{
var newIntentName = default(string);
var newAction = new LuisActionResolver(action.GetType().Assembly).ResolveActionFromLuisIntent(result, out newIntentName);
if (newAction != null && !newAction.GetType().Equals(action.GetType()))
{
return new QueryValueResult(false)
{
NewAction = newAction,
NewIntent = newIntentName
};
}
}
var properties = new List<PropertyInfo> { action.GetType().GetProperty(paramName, BindingFlags.Public | BindingFlags.Instance) };
if (!LuisActionResolver.AssignEntitiesToMembers(action, properties, result.Entities, entityExtractor))
{
return new QueryValueResult(AssignValue(action, properties.First(), originalValue));
}
return new QueryValueResult(true);
}
public static LuisActionBindingAttribute GetActionDefinition(ILuisAction action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
return action.GetType().GetCustomAttributes<LuisActionBindingAttribute>(true).FirstOrDefault();
}
public static LuisActionBindingParamAttribute GetParameterDefinition(ILuisAction action, string paramName)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (string.IsNullOrWhiteSpace(paramName))
{
throw new ArgumentNullException(nameof(paramName));
}
var prop = action.GetType().GetProperty(paramName, BindingFlags.Public | BindingFlags.Instance);
// search binding attrib
return prop.GetCustomAttributes<LuisActionBindingParamAttribute>(true).FirstOrDefault();
}
public static bool IsValidContextualAction(ILuisAction action, ILuisAction context, out bool isContextual)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
isContextual = LuisActionResolver.IsContextualAction(action);
if (!isContextual)
{
return false;
}
var validContextualType = typeof(ILuisContextualAction<>).MakeGenericType(context.GetType());
if (validContextualType.IsAssignableFrom(action.GetType()))
{
var prop = validContextualType.GetProperty("Context", BindingFlags.Public | BindingFlags.Instance);
prop.SetValue(action, context);
return true;
}
return false;
}
public static bool IsContextualAction(ILuisAction action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
return action
.GetType()
.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ILuisContextualAction<>));
}
public static bool UpdateIfValidContextualAction(ILuisAction action, ILuisAction context, out bool isContextual)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
isContextual = false;
if (!LuisActionResolver.IsContextualAction(context))
{
return true;
}
var prop = context.GetType().GetProperty("Context", BindingFlags.Public | BindingFlags.Instance);
var actionContext = prop.GetValue(context) as ILuisAction;
return LuisActionResolver.IsValidContextualAction(action, actionContext, out isContextual);
}
public static bool CanStartWithNoContextAction(ILuisAction action, out LuisActionBindingAttribute actionDefinition)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (!LuisActionResolver.IsContextualAction(action))
{
actionDefinition = null;
return true;
}
actionDefinition = LuisActionResolver.GetActionDefinition(action);
return actionDefinition.CanExecuteWithNoContext;
}
public static ILuisAction BuildContextForContextualAction(ILuisAction action, out string intentName)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (!LuisActionResolver.IsContextualAction(action))
{
intentName = null;
return null;
}
var contextType = action
.GetType()
.GetInterfaces()
.First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ILuisContextualAction<>))
.GetGenericArguments()[0];
var result = Activator.CreateInstance(contextType) as ILuisAction;
intentName = LuisActionResolver.GetActionDefinition(result).IntentName;
var isContextual = false;
LuisActionResolver.IsValidContextualAction(action, result, out isContextual);
return result;
}
public ILuisAction ResolveActionFromLuisIntent(LuisResult luisResult)
{
if (luisResult == null)
{
throw new ArgumentNullException(nameof(luisResult));
}
var intentName = default(string);
var unassigned = default(IList<EntityRecommendation>);
return this.ResolveActionFromLuisIntent(luisResult, out intentName, out unassigned);
}
public ILuisAction ResolveActionFromLuisIntent(
LuisResult luisResult,
out string intentName,
out IList<EntityRecommendation> intentEntities,
Func<PropertyInfo, IEnumerable<EntityRecommendation>, EntityRecommendation> entityExtractor = null)
{
intentEntities = default(IList<EntityRecommendation>);
if (luisResult == null)
{
throw new ArgumentNullException(nameof(luisResult));
}
// Has Intent?
intentName = (luisResult.TopScoringIntent ?? luisResult.Intents?.MaxBy(i => i.Score ?? 0d)).Intent;
if (string.IsNullOrWhiteSpace(intentName))
{
return null;
}
// Set out intent entities reference
intentEntities = luisResult.Entities;
// Get Actions that map to this intent
var actionType = default(Type);
if (!this.luisActions.TryGetValue(intentName, out actionType))
{
return null;
}
// Get the action instance and check if it implements ILuisAction
var luisAction = Activator.CreateInstance(actionType) as ILuisAction;
if (luisAction == null)
{
return null;
}
// Try complete parameters from entities
var properties = luisAction.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
LuisActionResolver.AssignEntitiesToMembers(luisAction, properties, luisResult.Entities, entityExtractor);
return luisAction;
}
internal ILuisAction ResolveActionFromLuisIntent(LuisResult luisResult, out string intentName)
{
if (luisResult == null)
{
throw new ArgumentNullException(nameof(luisResult));
}
var unassigned = default(IList<EntityRecommendation>);
return this.ResolveActionFromLuisIntent(luisResult, out intentName, out unassigned);
}
private static bool AssignValue(ILuisAction action, PropertyInfo property, object paramValue)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (property == null)
{
throw new ArgumentNullException(nameof(property));
}
if (paramValue == null)
{
throw new ArgumentNullException(nameof(paramValue));
}
if (property != null && property.CanWrite)
{
// nullable support
var type = property.PropertyType;
type = Nullable.GetUnderlyingType(type) ?? type;
try
{
object value;
// handle LUIS JObjects
paramValue = SanitizeInputValue(type, paramValue);
if (type.IsArray)
{
value = BuildArrayOfValues(action, property, type.GetElementType(), paramValue);
}
else if (type.IsEnum)
{
value = Enum.Parse(type, paramValue.ToString());
}
else
{
value = Convert.ChangeType(paramValue, type);
}
property.SetValue(action, value);
return true;
}
catch (FormatException)
{
// Handle invalid values (Eg. Try Parse '2017' as a Date will fail)
}
}
return false;
}
private static Array BuildArrayOfValues(ILuisAction action, PropertyInfo property, Type elementType, object paramValue)
{
var values = default(IEnumerable<object>);
if (paramValue is IEnumerable<object>)
{
values = paramValue as IEnumerable<object>;
}
else
{
values = paramValue.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(v => v.Trim());
}
if (values.Count() > 0)
{
var idx = 0;
var result = Array.CreateInstance(elementType, values.Count());
foreach (var value in values)
{
result.SetValue(elementType.IsEnum ? Enum.Parse(elementType, value.ToString()) : Convert.ChangeType(value, elementType), idx++);
}
return result;
}
else
{
return null;
}
}
private static object SanitizeInputValue(Type targetType, object value)
{
object result = value;
// handle case where input is JArray returned from LUIS
if (value is JArray)
{
var arrayOfValues = value as JArray;
if (targetType.IsArray)
{
result = arrayOfValues.AsEnumerable<object>();
}
else
{
if (arrayOfValues.Count > 1)
{
throw new FormatException("Cannot assign multiple values to single field");
}
result = arrayOfValues[0];
}
}
return result;
}
private static bool AssignEntitiesToMembers(
ILuisAction action,
IEnumerable<PropertyInfo> properties,
IEnumerable<EntityRecommendation> entities,
Func<PropertyInfo, IEnumerable<EntityRecommendation>, EntityRecommendation> entityExtractor = null)
{
var result = true;
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (properties == null)
{
throw new ArgumentNullException(nameof(properties));
}
if (entities == null)
{
throw new ArgumentNullException(nameof(entities));
}
if (!entities.Any())
{
return !result;
}
// Cross match entities to copy resolution values for custom entities from pairs
foreach (var group in entities.GroupBy(e => e.Entity))
{
if (group.Count() > 1)
{
var entityToUpdate = group.FirstOrDefault(e => !BuiltInTypes.IsBuiltInType(e.Type));
var entityWithValue = group.FirstOrDefault(e => e.Resolution != null);
if (entityToUpdate != null && entityWithValue != null)
{
entityToUpdate.Resolution = entityWithValue.Resolution;
}
}
}
foreach (var property in properties)
{
var matchingEntity = default(EntityRecommendation);
var matchingEntities = default(IEnumerable<EntityRecommendation>);
// Find using custom type from attrib if available
var paramAttrib = property.GetCustomAttributes<LuisActionBindingParamAttribute>(true).FirstOrDefault();
if (paramAttrib != null)
{
matchingEntities = entities.Where(e => e.Type == paramAttrib.CustomType);
}
// Find using property name
if (matchingEntities == null || !matchingEntities.Any())
{
matchingEntities = entities.Where(e => e.Type == property.Name);
}
// Find using builtin type from attrib if available
if ((matchingEntities == null || !matchingEntities.Any()) && paramAttrib != null)
{
matchingEntities = entities.Where(e => e.Type == paramAttrib.BuiltinType);
}
// If callback available then use it
if (matchingEntities.Count() > 1)
{
if (entityExtractor != null)
{
matchingEntity = entityExtractor(property, matchingEntities);
}
}
else
{
matchingEntity = matchingEntities.FirstOrDefault();
}
// Prioritize resolution
if (matchingEntity != null)
{
var paramValue = matchingEntity.Resolution != null && matchingEntity.Resolution.Count > 0
? matchingEntity.Resolution.First().Value
: matchingEntity.Entity;
result &= AssignValue(action, property, paramValue);
}
else if (matchingEntities.Count() > 0
&& matchingEntities.Count(e => e.Resolution != null && e.Resolution.First().Value is JArray) == matchingEntities.Count())
{
var paramValues = new JArray();
foreach (var currentMatchingEntity in matchingEntities)
{
var values = currentMatchingEntity.Resolution.First().Value as JArray;
foreach (var value in values)
{
paramValues.Add(value);
}
}
result &= AssignValue(action, property, paramValues);
}
else
{
result = false;
}
}
return result;
}
}
}