Thursday 1 June 2017

Context not returning properly in wit.ai Node SDK

I have modified the interactive.js in node-wit SDK to pass the user entered message into runActions. Instead of using the command line prompt here I implemented socket.io event message. When I try to run in this implementation the context of the message is not return to the next prompt, So the wit again asks different question other than what story I trained.

Original:

'use strict';

const {DEFAULT_MAX_STEPS} = require('./config');
const logger = require('./log.js');
const readline = require('readline');
const uuid = require('uuid');

module.exports = (wit, initContext, maxSteps) => {
  let context = typeof initContext === 'object' ? initContext : {};
  const sessionId = uuid.v1();
  const steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS;
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });
  rl.setPrompt('> ');
  const prompt = () => {
    rl.prompt();
    rl.write(null, {ctrl: true, name: 'e'});
  };
  prompt();
  rl.on('line', (line) => {
    line = line.trim();
    if (!line) {
      return prompt();
    }

    wit.runActions(sessionId, line, context, steps)
    .then((ctx) => {
      context = ctx;
      prompt();
    })
    .catch(err => console.error(err))
  });
};

Modified:

'use strict';

const {DEFAULT_MAX_STEPS} = require('./config');
const logger = require('./log.js');
const readline = require('readline');
const uuid = require('uuid');

module.exports = (wit, socket, initContext, maxSteps) => {
  socket.on('new message', function(data){
    console.log("Got query from user " + data.username + "--" + data.message);
    let context = typeof initContext === 'object' ? initContext : {};
    const sessionId = uuid.v1();
    const steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS;
    var line = data.message
    wit.runActions(sessionId, line, context, steps)
    .then((ctx) => {
      context = ctx;
    })
    .catch(err => console.error(err))
    //}
    });
};



via Karthikeyan Prakash

No comments:

Post a Comment