Skip to content

Instantly share code, notes, and snippets.

@debashisbarman
Created March 8, 2023 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debashisbarman/bc497bb195f3fc9fcfec356d9e7f2bf2 to your computer and use it in GitHub Desktop.
Save debashisbarman/bc497bb195f3fc9fcfec356d9e7f2bf2 to your computer and use it in GitHub Desktop.
NLP Dialogue Manager in JavaScript

Here's an example of how the NLP dialogue manager can be rewritten using a state machine approach to handle the following intents:

  • greet
  • ask_name
  • get_time
  • goodbye
// Define the state machine
const stateMachine = {
  greeting: {
    onEnter: function(context) {
      console.log("Hi there! How can I help you today?");
    },
    transitions: {
      ask_name: "name",
      get_time: "time",
      goodbye: "goodbye"
    }
  },
  name: {
    onEnter: function(context) {
      if (context.name) {
        console.log(`Welcome back, ${context.name}! How can I assist you today?`);
      } else {
        console.log("What's your name?");
      }
    },
    transitions: {
      greet: "greeting",
      get_time: "time",
      ask_time: "time",
      goodbye: "goodbye"
    },
    onExit: function(context, input) {
      context.name = input;
    }
  },
  time: {
    onEnter: function(context) {
      console.log(`It's currently ${new Date().toLocaleTimeString()}.`);
    },
    transitions: {
      greet: "greeting",
      ask_name: "name",
      goodbye: "goodbye"
    }
  },
  goodbye: {
    onEnter: function(context) {
      console.log("Goodbye!");
    },
    transitions: {}
  }
};

// Define the state machine runner
function runStateMachine(initialState) {
  let context = {};
  let currentState = initialState;
  currentState.onEnter(context);

  while (true) {
    let input = prompt("> ");

    // Handle invalid input
    if (!currentState.transitions[input]) {
      console.log("I'm sorry, I didn't understand your question.");
      continue;
    }

    let nextState = stateMachine[currentState.transitions[input]];
    currentState.onExit && currentState.onExit(context, input);
    nextState.onEnter(context);
    currentState = nextState;
  }
}

// Example usage:
runStateMachine(stateMachine.greeting);

This state machine has four states: greeting, name, time, and goodbye. Each state has an onEnter function that is executed when the state is entered, and a transitions object that defines the possible transitions from the current state based on user input. The name state also has an onExit function that is executed when the state is exited, to save the user's name in the context object.

The runStateMachine function takes an initial state as its argument and executes the state machine. It starts in the initial state and enters a loop that reads user input and transitions to the next state based on the input. If the user input is not a valid transition, it prints an error message and remains in the current state. The context object is passed to each state's onEnter and onExit functions, allowing the state machine to maintain context across states.

Note that this is just an example, and a more complex state machine could handle more sophisticated dialogue flows with more states and transitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment