Watson 对话中的语境未按预期运作

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

如果我在 Watson 的对话中设置上下文对象,我希望它仍然是它之前访问过的节点,我的意思是:

在触发问候 Intent 后,如果我输入 'bye'(再见 Intent)。它应该触发再见的 Intent,但它只在测试工具中有效。

这是我的 Nodejs 应用程序中的代码:

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' } } }

对话框本身是无状态的,您必须使用上下文变量来维护它。

详细解决方案在这里:

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

让我知道这是否解决了您的问题。

我刚刚发现上下文对象保持对话 'context' 和使用嵌套意图的重要部分是不断传递每个 http 响应中的新上下文对象。

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)
            }
        })
    }

这意味着您必须在每个 http 请求中更新上下文对象:

context = response.context

正如您在每个 http 响应中看到的那样,您正在更新 _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] } } }