Slots

As you write patterns for user commands, you can add slots to these commands. Slots are ‘variables’ in user’s phrases: they represent variable pieces of information you expect to occur in the user’s input.

Slots allows Alan AI to identify and retrieve important facts from the user’s speech. By having this data, the AI agent can respond to the user’s request appropriately. For example, when building a flight booking AI agent, it needs to capture the departure and destination, departure date, number of passengers and so on.

To define a slot, use the $ character followed by brackets (). In brackets, define the slot name followed by a colon : and a list of possible values.

In the example below, the city name is defined as a slot, and the command can be invoked by the following phrases:

  • Find a flight to London

  • Find a flight to Madrid

  • Find a flight to Tokio

Dialog script
intent('Find a flight to $(CITY: London, Paris, New York...)', p => {
    p.play('Searching for a flight...');
})

Slot values

A slot is an object that has a set of predefined fields and methods. All slot types have the following fields:

  • value

  • name

  • type

and so on.

The value field is filled automatically when an intent with the slot is matched. This field contains the captured user’s input.

Note

To add slot values to response functions - play() and reply(), use the standard JavaScript syntax for template strings - backticks. You can also add slot values to the string line.

In the example below, the city name is displayed in the Alan AI Studio logs and pronounced by the AI agent in response.

Dialog script
intent('Find a flight to $(CITY: London, Paris, New York...)', p => {
    p.play(`Searching for a flight to ${p.CITY.value}...`);
    console.log(p.CITY.value);
});

Multiple slot values

If you need to capture multiple slot values at once, you can add a slot to a pattern several times. When such an intent is matched, all pieces of data captured with the slot are collected to an array of values.

In such a case, p.SLOTNAME.value would contain only the value captured with the first slot instance in the pattern.

  • To access the list of all values in the array, use the slot name followed by the underscore character, for example: p.SLOTNAME_.

  • To access a specific element in the array, add the element index, for example: p.SLOTNAME_[0].value.

Each element in the array has its own .value field. The value field is populated with the value obtained from the user’s input: p.SLOTNAME_[index].value.

Assume you want to allow users to search for flights to different destinations. In this case, you can add the following intent to the dialog script:

Dialog script
intent('Find a flight to $(CITY: London, Paris, New York...) or $(CITY: London, Paris, New York...)', p => {
    p.play({command: 'findFlight', destination01: p.CITY_[0].value, destination02: p.CITY_[1].value});
    p.play(`Searching for a flight to ${p.CITY_[0].value}... and ${p.CITY_[1].value}`);
})

Slot name limitations

Mind the following limitations for slot names:

  • Slot names must start with a letter.

  • You can use the following characters in slot names: A-Z, a-z, 0-9 and underscore (_).

  • The following names are reserved and cannot be used as slot names: DATE, TIME, NUMBER, ORDINAL.