Use slot filling in fulfillment

December 5, 2018

Overview

Slot filling simplifies your agent by letting you obtain multiple required parameters within a single intent. If any parameters are missing from a user utterance, your agent will automatically prompt users for the required parameters—either dynamically through your webhook or statically for one missing parameter at a time.

This post walks you through creating a slot-filling agent that constructs an Adlibs story using a country, year, and color the user specifies. You’ll learn all the necessary steps and considerations to have your agent use slot filling, with or without fulfillment.

Here, however, we will more closely examine slot filling with fulfillment.

The basic steps to slot filling include the following:

  1. Specifying annotated training phrases that include the parameters you want to extract from a user utterance. These parameters are the "slots" that you want users to "fill" for a particular intent.
  2. 1. Providing a few static prompt phrases for each parameter (not using fulfillment for parameter collection). Dialogflow uses one of the prompt phrases for each parameter to ask users for missing slots, individually.

    OR

    2. Having your fulfillment handle the missing slots— this lets you set up customized logic depending on which combinations of slots are missing, minimizes the number of responses back to the user, and allows you to create dynamic prompt phrases, which enables you to capture multiple missing parameters.
  3. Note: This sample follows B2. You can use option 1 for simplicity but option 2 provides a better user experience in most cases.
  4. Returning a final response only after all the required slots are filled (the final response can also be defined in your webhook).

Benefits of using a webhook for slot-filling

Instead of letting Dialogflow handle the prompting of parameters, you can route any slot filling logic in your fulfillment, which enables you to return dynamic prompts. Consider the following benefits of using fulfillment to handle slot filling:

  • You can access the values of required parameters from user utterances through the automatic setting of output contexts field in the fulfillment request JSON object.
  • You can use the incomplete data that the slot-filling feature provides to customize the response to the user in order in order to obtain the outstanding parameters.
  • You can control slot-filling behavior to end the conversation gracefully if the user doesn’t provide all the necessary information within a reasonable amount of attempts. (By default, Dialogflow repeatedly asks users to fill missing slots, regardless of how many failed attempts.)
  • You can re-route users to another piece of logic if they are having trouble filling the required slots.

Quick deploy option

If you’d prefer to skip the step-by-step instructions and additional information, you can go ahead and use our pre-made template below to configure your slot-filling agent. You can elect to do this option and follow along, but ensure that the Firebase function is deployed from the Fulfillment section of the agent.

Intents setup

In this section, we’ll go over how to configure and customize your agent’s intents.

Customize Default Welcome Intent

First, you need to initialize a custom welcome response inside the Default Welcome Intent that tells users what information our agent needs to proceed. To do so, follow these steps:

  1. In the Responses section of the Default Welcome Intent, add the following: Hey there, welcome to Adlibs! All I need is a country, year, and color for a great story.
  2. Click SAVE.

Create makeAdlibStory intent

You’ll now create and modify the main intent that will be doing the slot filling work, which contains the agent’s training phrases and parameters for collecting the required information. To create this intent, follow these steps:

  1. Click on the + next to Intents in the Dialogflow console.
  2. Name the intent makeAdlibStory.
  3. Click SAVE.

Add training phrases

Next, you need to add some training phrases for the agent. These are utterances you would reasonably expect from users. You can add as many training phrases as you see fit; the more training phrases you add, the more your agent will understand. It is recommended to use at least 10-15 phrases. To add training phrases, follow these steps:

  1. Navigate to the Training phrases section of the makeAdlibStory intent.
  2. Add the following training phrases:
    • Canada green 1955
    • Orange iceland 1985
    • Brazil 1970 yellow
  3. Click SAVE.

alt_text

Annotate training phrases

The highlighted words in your training phrases represent entities that are extracted as parameters at runtime. If you are using built-in system entities (@sys.date, @sys.time, @sys.color, etc.), then you can bypass needing to manually input them in the entities section of the console. Entities allow you to annotate phrases so that Dialogflow can better parse data from user utterances. In essence, entities categorize data so it can be parsed more easily.

In general, Dialogflow entities allow for automatic annotation of the training phrases.

The following entities within each training phrase are automatically annotated:

  • @sys.geo-country
  • @sys.number
  • @sys.color

The related parameters are also created. For every part of a training phrase that you annotate with an entity, a corresponding parameter stores the extracted value from the utterance. In the Action and parameters table, you can see your entities and associated parameter names, as shown in the following screenshot:

alt_text

Make parameters required

Based on the welcome message, the agent has three required slots to fill. Each required parameter should be marked as “required” in order to ensure the information is collected. To mark the parameters as required, do the following:

  1. Navigate to the Action and parameters section of the makeAdlibStory intent.
  2. Check the REQUIRED box for all three of the parameters.

These parameters are passed to fulfillment if fulfillment is enabled for that intent. You can then use these parameters to process information and repeat them back in your response, specifically in this case to form an adlib.

Add static prompts for each required parameter

If your fulfillment fails, then Dialogflow returns static responses instead, so if this occurs then you know the webhook is down. Since these parameters are marked as required, they must be captured. These responses are specific prompts to the user to collect the missing parameter. Not only is this a good way to tell if your fulfillment is working, but the conversation with your users won’t be disrupted in the case of fulfillment failure. To add static responses, do the following:

  1. To add a prompt for each required parameter, click Define prompts under the Prompts column of the Action and parameters table. (The Prompts column is revealed when you click the REQUIRED option.)
  2. Add the following prompts to each required parameter:
    • geo-country parameter: Oops, you forgot $geo-country
    • number parameter: You forgot $number, give me any number
    • color parameter: Please tell me a $color

Add response as backup

Like with prompts for required parameters, a response provided in the Dialogflow console will be used in the case of fulfillment failure. To add a backup response, do the following:

  1. Navigate to the Responses section of the makeAdlibStory intent.
  2. Add the following response: So according to Professor David, back in the year $number, the nearly extinct yet exquisite, $color -bellied fox was introduced to $geo-country and now flourishes in over 236 distinct regions.
  3. Enable Set this intent as end of conversation. This will make sure your agent isn’t expecting further input from the user.*
  4. Select Enable webhook call for this intent as well as Enable webhook call for slot filling.

    *Note: You can include the response in your fulfillment webhook (index.js), in your makeAdlibStory intent, or both. If you choose to include a final response in both, then the response set in the intent will be run as a backup only if fulfillment fails. We will discuss that to a greater extent later in this post.

alt_text

Set up fulfillment

In the fulfillment index.js snippet shown below, you can see that the fulfillment response to the user, once all the required slots are filled, is slightly different from the response we declared under the intent makeAdlibStory. The final response set inside the intent acts as a fail-safe in case your fulfillment isn’t working properly—it is entirely optional to have this redundancy since we declared the final response (lines 46-48) in the fulfillment.

In addition, you can see the makeAdlibs function handler is mapped to the intent created earlier, makeAdlibStory, in lines 52-54.

alt_text

Below are the step by step instructions for enabling fulfillment, which allows for dynamic prompting to users for missing parameters within the makeAdlibStory intent. The power behind slot filling largely comes from the basis that this can be done within a single intent; also, output contexts are automatically created and managed.

  1. In the Dialogflow console, go to Fulfillment in the left-menu and toggle the button next to Inline Editor to ENABLED. This automatically creates a Cloud Function for Firebase to use as fulfillment.
  2. Under makeAdlibStory intent, select Enable webhook call for this intent AND Enable webhook for slot-filling at the bottom. Click SAVE.
  3. Navigate back to Fulfillment and copy and paste the following into the index.js tab of the inline editor:

    'use strict';
    
    const admin = require('firebase-admin');
    const functions = require('firebase-functions');
    const {WebhookClient} = require('dialogflow-fulfillment');
    
    process.env.DEBUG = 'dialogflow:debug';
    
    const ADLIBS_INTENT = 'makeAdlibStory';
    
    exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    const agent = new WebhookClient({request, response});
    console.log('Req headers: ' + JSON.stringify(request.headers));
    console.log('Req body: ' + JSON.stringify(request.body));
    
    function makeAdlibs(agent) {
        const [color, nation, year] = [agent.parameters['color'], agent.parameters['geo-country'], agent.parameters['number']];
    
        let missingSlots = [];
        if (!color) { missingSlots.push('color'); }
        if (!nation) { missingSlots.push('nation'); }
        if (!year) { missingSlots.push('year'); }
    
        if (missingSlots.length === 1){
        agent.add(`Looks like you didn't provide a ${missingSlots[0]}`);
        }
        else if (missingSlots.length === 2){
            agent.add(`Ok, I need two more things, a ${missingSlots[0]} and ${missingSlots[1]}`);
        }
        else if (missingSlots.length === 3){
            agent.add(`Ok, I need all 3 things still: a ${missingSlots[0]}, ${missingSlots[1]}, and ${missingSlots[2]}`);
        } else {
        agent.add(`So according to Professor David, back in the year
        ${year}, the nearly extinct yet exquisite,
        ${color}-bellied fox was introduced to ${nation} and now flourishes in over 361 regions.`);
        }
    }
    
    let intentMap = new Map();
    intentMap.set(ADLIBS_INTENT, makeAdlibs);
    agent.handleRequest(intentMap);
    });
    
  4. Copy and paste the following into the package.json tab of the inline editor:

    {
    "name": "dialogflowFirebaseFulfillment",
    "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
    "version": "0.0.1",
    "private": true,
    "license": "Apache Version 2.0",
    "author": "Google Inc.",
    "engines": {
        "node": "6"
    },
    "scripts": {
        "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
        "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
    },
    "dependencies": {
        "actions-on-google": "2.2.0",
        "firebase-admin": "^5.13.1",
        "firebase-functions": "^2.0.2",
        "dialogflow-fulfillment": "0.6.1"
    }
    }
  5. Click Deploy at the bottom of the inline editor.

Test agent in simulator

You can now converse with your agent using Dialogflow’s simulator within the console. In the Try it now text field on the right side of the console, type hi to begin a conversation with your agent, and then provide input based on the agent’s prompts:

Screenshot of simulator showing working agent Screenshot of simulator showing working agent

Further reading

If you’re interested in seeing more slot filling, there are two Dialogflow slot-filling samples up on Github: Telephony & Google Sheets and Basic Slot Filling. You can also check out the official documentation on slot-filling, as well as the documentation about different entity types.

Now, you have all that you need to know to make a Dialogflow agent with slot filling!

Posted by Sarah Dwyer, Developer Relations Quality Engineer, and Lauren Ward, Technical Editor





Subscribe Via Email

Enter your email address:

Delivered by FeedBurner

About

Welcome to the Dialogflow blog. Subscribe to get product updates, best practices, and tutorials directly in your inbox.

Resources

Follow us on social