4.1. State Referencing

Start State

Let us create transitions for a virtual agent that handles time, weather, and playing music:

transitions = {
    'state': 'start',
    '`What can I do for you?`': {
        '[{time, clock}]': {
            '`It\'s 3PM.`': 'end'
        },
        '[{weather, forecast}]': {
            '`It\'s sunny outside`': 'end'
        },
        '[play, raining tacos]': {
            '`It\'s raining tacos. From out of the sky ...`': 'end'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'start'
        }
    }
}
  • #5: shows the current time (although it is correct only twice a day at the moment).

  • #8: informs the current weather in Sunnyville.

  • #11: plays the song "Raining Tacos".

  • #14: print the error message and references to the start state.

S: What can I do for you?
U: What time is it now?
S: It's 3PM.

Notice that when the user input does not match any of the conditions, it prints the error message and loops back to the start state.

Custom States

It is possible to name any transition you create as a state:

transitions = {
    'state': 'start',
    '`What can I do for you?`': {
        '[{time, clock}]': {
            'state': 'time',
            '`It\'s 3PM.`': 'end'
        },
        '[{weather, forecast}]': {
            'state': 'weather',
            '`It\'s sunny outside`': 'end'
        },
        '[play, raining tacos]': {
            'state': 'play_raining_tacos',
            '`It\'s raining tacos. From out of the sky ...`': 'end'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'play_raining_tacos'
        }
    }
}
  • #6: names the state as time.

  • #9: names the state as weather.

  • #13: names the state as play_raining_tacos.

  • #17: references to the play_raining_tacos state.

S: What can I do for you?
U: Play rainy taco
S: Sorry, I didn't understand you. It's raining tacos. From out of the sky ...

Exit State

State referencing can be abused to create transitions that never end (infinite loop). It is important to design an exit state so you can terminate the conversation without throwing errors:

transitions = {
    'state': 'start',
    '`What can I do for you?`': {
        '[{time, clock}]': {
            'state': 'time',
            '`It\'s 3PM.`': 'start'
        },
        '[{weather, forecast}]': {
            'state': 'weather',
            '`It\'s sunny outside`': 'start'
        },
        '[play, raining tacos]': {
            'state': 'play_raining_tacos',
            '`It\'s raining tacos. From out of the sky ...`': 'start'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'start'
        },
        '[exit]': {
            'state': 'exit',
            '`Goodbye!`': 'end'
        }
    }
}
  • #6,10,14,17: loops back to the start state.

  • #19-21: creates an exit state to terminate the dialogue.

S: What can I do for you?
U: time
S: It's 3PM. What can I do for you?
U: weather
S: It's sunny outside What can I do for you?
U: play raining tacos
S: It's raining tacos. From out of the sky ... What can I do for you?
U: exit
S: Goodbye!

What is the main difference between an error transition and an exit state?

Last updated

©2023 Emory University - All rights reserved