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
  • Saving
  • Loading

Was this helpful?

Export as PDF
  1. 4. Interaction Design

4.5. Saving and Loading

Saving

Let us create a simple transition that counts how many times the user visits:

def visits() -> DialogueFlow:
    transitions = {
        'state': 'start',
        '`It\'s your` #VISITS `visit!`': 'end'
    }

    macros = {
        'VISITS': MacroVisits()
    }

    df = DialogueFlow('start', end_state='end')
    df.load_transitions(transitions)
    df.add_macros(macros)
    return df
  • #4: calls the macro #VISITS defined in #7.

MacroVisits can be defined as follow:

class MacroVisits(Macro):
    def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
        vn = 'VISITS'
        if vn not in vars:
            vars[vn] = 1
            return 'first'
        else:
            count = vars[vn] + 1
            vars[vn] = count
            match count:
                case 2: return 'second'
                case 3: return 'third'
                case default: return '{}th'.format(count)
S: It's your first visit!
def save(df: DialogueFlow, varfile: str):
    df.run()
    d = {k: v for k, v in df.vars().items() if not k.startswith('_')}
    pickle.dump(d, open(varfile, 'wb'))

save(visits(), 'resources/visits.pkl')
  • #1: takes a dialogue flow df and a file path varfile for saving the variable dictionary to.

  • #3: creates a dictionary by copying only user-generated variables.

After running this code, you will see the visits.pkl file saved under the resources directory.

Loading

The following code shows how to load the saved dictionary to a new dialogue flow:

def load(df: DialogueFlow, varfile: str):
    d = pickle.load(open(varfile, 'rb'))
    df.vars().update(d)
    df.run()
    save(df, varfile)
  • #1: takes a dialogue flow df and a file path varfile for loading the variable dictionary from.

  • #2: opens a readable (r) and binary (b) file and loads the object as a dictionary.

  • #3: adds all variables in the loaded dictioinary to the variable dictionary of df.

  • #5: saves the new variable dictionary to the same file.

S: It's your second visit!
S: It's your third visit!
S: It's your 4th visit!
Previous4.4. Global TransitionNext4.6. Quiz

Last updated 2 years ago

Was this helpful?

#10-13: uses the statements to return the appropriate literal.

The challenge is that we must save the number of visits to make this macro effective (and correct). This can be achieved by saving the variable dictionary into a binary file using the built-in Python object serialization called :

#4: a writable (w) and binary (b) file and dumps the dictionry object into the file.

match
pickle
opens