Luillyfe
Luillyfe

Reputation: 6842

Context in a Watson's conversation is not working in the way It supposed to work

If I set the context object in a Watson's conversation I expect that it remains the nodes it previously visited I mean:

enter image description here

After firing an greetings' Intent, if I type 'bye' (a goodbyes Intent). It should fire a goodbyes's Intent but it just works in the test tool.

enter image description here

Here is the code in my Nodejs app:

let context = {}
const conversation = new ConversationV1({
    username: 'myUsername',
    password: 'myPassword',
    url: 'https://gateway.watsonplatform.net/conversation/api',
    version_date: '2017-05-26'
})

conversation.message({ workspace_id: workspaceId}, function (err, response) {
    if (err) {
        console.log(err)
    } else {
        context = response.context
    }
})

sendMessage = (message = null) => new Promise((resolve, reject) => {
        conversation.message({
            input: {text: message},
            workspace_id: workspaceId,
            context: context
        }, function (err, response) {
            if (err) {
                reject(err)
            } else {
                resolve(response.output.text)
            }
        })
    }

Although the conversation_id is always the same. I am always getting the anythingelse's Intent response instead of goodbyes' Intent.

{ intents: [ { intent: 'greetings', confidence: 1 } ],
  entities: [],
  input: { text: 'hi' },
  output: 
   { text: [ 'It is nice to talk to you, again !' ],
     nodes_visited: [ 'greetings' ],
     log_messages: [] },
  context: 
   { conversation_id: '7cc96494-d108-4dc9-95c4-63c174f20b4c',
     system: 
      { dialog_stack: [Object],
        dialog_turn_counter: 2,
        dialog_request_counter: 2,
        _node_output_map: [Object] } } }

{ intents: [ { intent: 'goodbytes', confidence: 1 } ],
  entities: [],
  input: { text: 'bye' },
  output: 
   { text: [ 'I didn\'t understand. You can try rephrasing.' ],
     nodes_visited: [ 'Anything else' ],
     log_messages: [] },
  context: 
   { conversation_id: '7cc96494-d108-4dc9-95c4-63c174f20b4c',
     system: 
      { dialog_stack: [Object],
        dialog_turn_counter: 2,
        dialog_request_counter: 2,
        _node_output_map: [Object],
        branch_exited: true,
        branch_exited_reason: 'completed' } } }

Upvotes: 0

Views: 1011

Answers (2)

Luillyfe
Luillyfe

Reputation: 6842

I just discover that the important part of the context object to keep the 'context' of the conversation and work with nested intents is keep passing the new context object that you have in every http response.

let context = {}
sendMessage = (message = null) => new Promise((resolve, reject) => {
        conversation.message({
            input: {text: message},
            workspace_id: workspaceId,
            context: context
        }, function (err, response) {
            if (err) {
                reject(err)
            } else {
                context = response.context
                resolve(response.output.text)
            }
        })
    }

that means you have to update the context object in every http request:

context = response.context

as you can see in every http response you are updating the _node_output_map:

{ conversation_id: '1bb7b7e3-2bc2-4686-8f5e-8e25cdff7ff8',
  system: 
   { dialog_stack: [ [Object] ],
     dialog_turn_counter: 1,
     dialog_request_counter: 1,
     _node_output_map: { Welcome: [Object] },
     branch_exited: true,
     branch_exited_reason: 'completed' } }

{ conversation_id: '1bb7b7e3-2bc2-4686-8f5e-8e25cdff7ff8',
  system: 
   { dialog_stack: [ [Object] ],
     dialog_turn_counter: 2,
     dialog_request_counter: 2,
     _node_output_map: { Welcome: [Object], greetings: [Object] } } }

Upvotes: 0

Bene
Bene

Reputation: 209

Dialog is stateless itself and you have to maintain this using the context var.

detailed solution here:

https://developer.ibm.com/answers/questions/287623/how-do-you-maintain-the-conversation-context-when/

let me know if this solved your problem.

Upvotes: 1

Related Questions