Microsoft MVP, Chatbot and Web Perf SME

Menu

Custom BotFramework Intent Service Implementation

Developing a chatbot with language understanding capabilities is a huge leap from basic pattern recognition on the input to match to specific commands.

If you have a botframework chatbot, you’re currently limited to using LUIS as your NLP (Natural Language Processing) provider via the various Luis classes: LuisDialog, LuisModelAttribute, and LuisService.

If you’re trying to compare alternative NLP services, such as kitt.ai or wit.ai or even Alexa, then implementing support for another NLP service in Botframework for this can be a bit tricky.

In this article I’ll show you one approach to decoupling your botframework bot from a specific NLP solution.

LUIS

Before we create our own implementation, let’s look at the one that already exists within BotFramework: the LuisDialog and it’s associated henchmen – LuisModelAttribute and LuisService.

LuisModel Attribute

By adding that at the top of your dialog class, a message received to that dialog will end up being passed on to the LUIS app defined by the “subscription key” and “model Id” values, and the resulting intent in the response will be used to determine which method to fire, thanks to the LuisIntent method-level attribute:

[LuisIntent("intent name")]

For example, the class GetDetailsDialog could be configured like this:

LuisDialog

The LuisDialog type replaces IDialog in your class definition:

publicclass GetDetailsDialog : IDialog

becomes

publicclass GetDetailsDialog : LuisDialog<object>

This class has a MakeServicesFromAttributes method which is quite self-explanatory; using the LuisModel attributes on the class that extends LuisDialog it creates some instances of a LuisService class:

Decoupling the LuisDialog

Now let’s try to implement something similar and even have a LUIS default implementation to show how it could work.

Instead of having cascading dialogs for mapping to multiple LUIS apps, I’m just assuming any dialog can potentially respond to the incoming message; i.e., no hierarchy. I’m sure you can build this in yourself if you wanted to though.

Dialog

Let’s assume you’d like to be able to mark up any IDialog with an attribute which would allow it to be mapped to an NLP intent. I would want something like this to work:

Depending on you current project type and project structure, you might want to use the Assembly reflection to get the current, executing, entry, or specifically named assembly to load the implementations:

Pretty simple implementation, right? Theoretically you’d need to implement your own MapToIntentResponse method to take the LUIS service’s json response and map it to the IntentResponse data structure – however the structure actually maps exactly to the IntentResponse I’ve used here, so no mapping is actually needed.

Summary

With the example implementations here, you can see it’s not too hard to create your own intent service that doesn’t rely on the default LuisDialog; a new attribute, a new intent service, and a bit of wiring up, and we’re almost there.

Post navigation

2 thoughts on “Custom BotFramework Intent Service Implementation”

Hi!
Great work – it’s just what I’m looking for 🙂 I tried implementing your code, but I have some errors. Is it possible to get the project-code? I think my errors are caused by how i implement the code in a project and I dont’t know if I still need a messages controller. Hope you can help!