Conversational AI Design and Practice
GitHubAuthor
  • Preface
    • Syllabus
    • Schedule
  • 0. Getting Started
    • 0.1. Environment Setup
    • 0.2. Quiz
  • 1. Exploration
    • 1.1. Overview
    • 1.2. Project Ideas
    • 1.3. Quiz
  • 2. Dialogue Graph
    • 2.1. Emora STDM
    • 2.2. State Transition
    • 2.3. Matching Strategy
    • 2.4. Multi-turn Dialogue
    • 2.5. Quiz
  • 3. Contextual Understanding
    • 3.1. Natex
    • 3.2. Ontology
    • 3.4. Regular Expression
    • 3.5. Macro
    • 3.5. Quiz
  • 4. Interaction Design
    • 4.1. State Referencing
    • 4.2. Advanced Interaction
    • 4.3. Compound States
    • 4.4. Global Transition
    • 4.5. Saving and Loading
    • 4.6. Quiz
  • 5. LM-based Matching
    • 5.1. Language Models
    • 5.2. Quickstart with GPT
    • 5.3. Information Extraction
    • 5.4. Quiz
  • 6. Conversational Analysis
    • 6.1. H2H vs. H2M
    • 6.2. Team Evaluation
    • 6.3. Quiz
  • Project
    • Projects
    • Proposal Guidelines
    • Final Report Guidelines
  • Supplements
    • LINC Course
    • Page 1
Powered by GitBook

©2023 Emory University - All rights reserved

On this page
  • Dialogue Flow
  • Branching
  • Error Handling
  • Code Snippet

Was this helpful?

Export as PDF
  1. 2. Dialogue Graph

2.2. State Transition

Create a dialogue flow using state transitions.

Previous2.1. Emora STDMNext2.3. Matching Strategy

Last updated 2 years ago

Was this helpful?

Dialogue Flow

Let us create a dictionary called transitions and name its initial state as start:

transitions = {'state': 'start'}
  • #1: transitions is a .

Add transitions to conduct a single-turn dialogue:

transitions = {
    'state': 'start',
    '`Hello. How are you?`': {
        'good': {
            '`Glad to hear that you are doing well :)`': 'end'
        }
    }
}
  • #3: the system begins the dialogue by saying, "Hello. How are you?".

  • #4: the system matches the user input with 'good'.

  • #5: the system responds to the user with "Glad to hear that ..." and recognizes that it is the final state end.

S: Hello. How are you?
U: Good!
S: Glad to hear that you are doing well :)

There are two ways to create a string in Python, using single quotes (e.g., 'Hello') and double quotes (e.g., "Hello"). When you use single quotes, any single quote inside the string needs to be escaped by a backslash (e.g., 'I\'m "good"!'). Similarly, when you use double quotes, any double quote needs to be escaped (e.g., "I'm \"good\"!").

Create the dialogue flow df that expects the initial state start and the final state end, which must match the names of the initial and final states in transitions:

from emora_stdm import DialogueFlow
df = DialogueFlow('start', end_state='end')

Load the transitions to the dialogue flow df:

df.load_transitions(transitions)

Finally, run the dialogue flow:

if __name__ == '__main__':
    df.run()
  1. Enter inputs such as "good", "Good", or "Good!" (separately) and see how the system responds. Does the system respond differently?

  2. How does the system respond to inputs like "fantastic" or "bad"?

  1. If the input does not match 'good' exactly, the system throws an error because no exception is currently handled in our code.

Branching

Let us add a transition such that it can also handle the input 'bad':

transitions = {
    'state': 'start',
    '`Hello. How are you?`': {
        'good': {
            '`Glad to hear that you are doing well :)`': 'end'
        },
        'bad': {
            '`I hope your day gets better soon :(`': 'end'
        }
    }
}
  • #7-9: a new transition for the 'bad' condition.

When you load the new transitions and run the dialogue flow, it now gives proper responses to both 'good' and 'bad':

S: Hello. How are you?
U: Good
S: Glad to hear that you are doing well :)
S: Hello. How are you?
U: Bad
S: I hope your day gets better soon :(

Error Handling

Even with branching, it still throws errors for other inputs. Let us add an error transition to set the default statement for all the other inputs:

transitions = {
    'state': 'start',
    '`Hello. How are you?`': {
        'good': {
            '`Glad to hear that you are doing well :)`': 'end'
        },
        'bad': {
            '`I hope your day gets better soon :(`': 'end'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'end'
        }
    }
}
  • #10-12: an error transition to generate the default response.

S: Hello. How are you?
U: It could be better.
S: Sorry, I didn't understand you.

Make sure to put a default error statement for every branching; otherwise, it will throw an exception during runtime, which can be detrimental.

Code Snippet

from emora_stdm import DialogueFlow

def state_transition() -> DialogueFlow:
    transitions = {
        'state': 'start',
        '`Hello. How are you?`': {
            'good': {
                '`Glad to hear that you are doing well :)`': 'end'
            },
            'bad': {
                '`I hope your day gets better soon :(`': 'end'
            },
            'error': {
                '`Sorry, I didn\'t understand you.`': 'end'
            }
        }
    }

    df = DialogueFlow('start', end_state='end')
    df.load_transitions(transitions)
    return df

if __name__ == '__main__':
    state_transition().run()

All keys and values must be in the type, where literals need to be surrounded by reversed primes (e.g., '`Hello. How are you?`').

#1: the import statement should be on top of the source code (see ).

#2:

#1:

User inputs get automatically lowercased and before matching. Thus, the system gives the same response to those inputs.

#3: although Python is a dynamically-typed language, it allows you to indicate the type of a variable or a function using (since Python 3.5).

dictionary
string
dialogue_graph.py
keyword vs. positional arguments
top-level code environment
tokenized
typing