This post is intended to be a word of caution for those who are trying to use slot filling in API.AI. If you are an absolute beginner, I recommend going through the step by step guide, and then the webhook tutorial, before reading this article.
What is slot filling?
In their documentation, the API.AI team describes slot filling as follows:
The easiest way to build a dialog for information collection
Slot filling greatly simplifies building dialogs. In fact, you can build a simple parameter collection dialog within a single intent if you’d like!
The current slot filling demos
Student to teacher: “Its not fair. You do all the easy examples on the blackboard, and send us home asking us to do the hard ones as homework!”
That statement reflects how I feel about most online documentation produced by machine learning-based services companies, and certainly how I feel about API.AI’s documentation around slot filling.
For example, here is a simple example from the official docs which shows how to use slot filling and web-hooks together. The issue is, as soon as you add a few real world constraints, you cannot proceed with slot filling that easily.
So I have listed 5 different types of slot filling tasks and give you some suggestions on whether or not to use slot filling for that task. (Note: just because you cannot use slot filling does not mean you cannot use API.AI to do the task. You could and quite likely should use contexts instead. They provide a tradeoff – more work for you, but also more control. I think that it is a good tradeoff to make).
You use slot filling without a webhook in the intent
If you use slot filling and do not use a webhook for fulfillment, it is a fairly simple verdict: definitely use slot filling. There is, of course, a small problem – it is not very likely you can do much useful with such an intent.
You use a webhook, but only after slot filling is complete
This is the use case where you collect all the data via the slot filling, and you only send the data over to the webhook after slot filling is complete. When you do this, you leave the “Use webhook for slot filling” checkbox unchecked.
You use webhooks for the actual slot filling
In this case, as soon as the slot filling collects even a single value, the webhook is called. And then the webhook is called every time a value is collected from the user, until either all values are collected, or the intent is changed from the webhook code. The idea behind this is that the webhook can, based on the initial values supplied, reason about the other values to be collected and fill them in by itself.
Why: The way to do it involves using a contextOut in your webhook response which is based on builtin context templates. Not only is this a fairly inelegant hack, these builtin context templates could be changed on API.AI’s discretion. (It is not seen anywhere in the official docs, for example)
You use webhooks for slot filling and also wish to update the context of the conversation
In this case, you set a conversation context based on the values supplied to the webhook.
Why: API.AI implements slot filling by using non-transparent, transient contexts with somewhat unpredictable lifespans. (But you can see that they are being used by looking at the request JSON coming to your webhook). Combining this with your own contexts is a recipe for trouble.
You use webhooks for slot filling, and you need to use followup events to change the intent
When the slot filling is completed, usually the next intent is selected based on the user’s next message. You can, however, preempt this by using a followUp event to trigger an intent of your choice.
For example, if you wish to validate the input collected by your slot filling, you might choose to use followUp events to trigger some kind of validation intent.
Why: You might have not cleared the temporary contexts that API.AI uses, which could interfere with your agent if the slot filling intent is invoked again for some reason.
If you are going to use slot filling for anything but the simplest of use cases, I don’t recommend it. I consider slot filling a bit of a paradox in its very existence. Maybe an article for the future! 🙂