How to use Multiple Luis Models in same class
I am developing a bot which will respond to users query to my private use cases. Similarly i would like to enable bot to answer some of common questions like Weather or directions etc. So i built a own app in Luis and also trying to use Pre-built cortana intents. Sample below
[LuisModel("c413b2ef-382c-45bd-8ff0-f76dad0e2a821", "697asfc173ce6f40aca4c99e7d38assad6cad")]
// [LuisModel("18407c0a-ba15-4deb-b22b-226b6822e434", "8253aa167f87437486ef7a338da088v5")]
public class myClass: LuisDialog<T> { }
In the above code i have commented out one Luis Model. Can i use both in the Single class? I tried in the SDK, where my both the models are recognized by Luis Query Builder, but what happend was when my first model identifies as "None" is the best match intent, it just Post the Message to the user and waits for another ping. Please tell me how can i achive this?
We will create an implementation of ILuisService for each LuisModel attribute found on the class:
https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/Dialogs/LuisDialog.cs#L221
You might consider overriding BestIntentFrom:
https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/Dialogs/LuisDialog.cs#L148
to purposely ignore high-scoring "none" intents.
In the V3 it doesn't happened: The onDefault() handler, it essentially gets triggered when all of the models reported a top intent of “None”.
@willportnoy Can you suggest me how i can override the BestIntentFrom in LuisDialog. When i provided 2 Luis Models, the call is happening async and when one of the model detects the Intent, it posts to Bot.
I'm sorry I missed this question. If you override BestIntentFrom, you can decide the logic for which intent is used to dispatch the LUIS result to a method in the LuisDialog.
I'm going to close this issue, but feel free to open a new issue for further discussions.
Sorry for re opening this issue after a long. Somehow my code above throws error. So requesting your help to review it again. Flow Details below My Root Dialog will be inherited by various classes(modules) in the project which is inheriting IDialog. In the MessageReceived method of RootDialog i tried to collect all the classes which is inheriting my Root class, then finding the Task Handlers for the INTENTS of Luis. Code Snippet below.
protected IDictionary<string, BotActivityHandler> GetHandlersByIntent()
{
if (handler?.Count > 0)
{
return handler.ToDictionary(kv => kv.Key, kv => kv.Value);
}
else
{
var classCollection = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => type.IsSubclassOf(typeof(BaseDialog)));
handler = new List<KeyValuePair<string, BotActivityHandler>>();
foreach (var item in classCollection)
{
handler.AddRange(EnumerateHandlers(Activator.CreateInstance(item)).ToList());
}
return handler.ToDictionary(kv => kv.Key, kv => kv.Value);
}
}
public class LuisEnumerator
{
internal static IEnumerable<KeyValuePair<string, BotActivityHandler>> EnumerateHandlers(object dialog)
{
var type = dialog.GetType();
var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (var method in methods)
{
var intents = method.GetCustomAttributes<BotActivityAttribute>(inherit: true).ToArray();
BotActivityHandler intentHandler = null;
try
{
intentHandler = (BotActivityHandler)Delegate.CreateDelegate(typeof(BotActivityHandler), dialog, method, throwOnBindFailure: false);
}
catch (ArgumentException ex)
{
// "Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type."
// https://github.com/Microsoft/BotBuilder/issues/634
// https://github.com/Microsoft/BotBuilder/issues/435
}
if (intentHandler != null)
{
var intentNames = intents.Select(i => i.ActivityName).DefaultIfEmpty(method.Name);
foreach (var intentName in intentNames)
{
var key = string.IsNullOrWhiteSpace(intentName) ? string.Empty : intentName;
yield return new KeyValuePair<string, BotActivityHandler>(intentName, intentHandler);
}
}
else
{
if (intents.Length > 0)
{
throw new Microsoft.Bot.Builder.Dialogs.InvalidIntentHandlerException(string.Join(";", intents.Select(i => i.ActivityName)), method);
}
}
}
}
}
This code was working properly for some few months, As we are expanding our Bot to more users and start testing, getting some error in GetHandlersByIntent as below
Index was Outside the bound of array.
We couldn't identify the root cause for this issue, because Bot will start working after 2-3 hours. Can anyone help me understand what i am doing wrong here?
@b4usat I don't see any obvious index accesses in that code, so I'm surprised to see the "index out of bounds" exception. You might consider looking at the dispatch dialog as it more flexibly composes multiple LUIS models and regular expressions and other scorable actions.