1

我在對話服務中創建了示例培訓數據,現在我正在嘗試爲該服務創建一個後調用使用節點js來創建一個聊天應用程序。我創建了一個後期調用,它正在工作,但並不如預期的那樣。它給了我任何調用的默認響應。爲什麼當我在節點j中創建了一個post調用時,Watson對話對於任何請求都返回相同的默認響應

我開始知道我們需要傳遞我們在響應中獲得的上下文值以進行下一次調用,但不知道該怎麼做。有人可以幫助我。下面是我的代碼

var express = require('express'); 
var conversationV1 = require('watson-developer-cloud/conversation/v1'); 
var bodyParser = require('body-parser'); 
var app = express(); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ 
    extended: false 
})); 
var conversation = new conversationV1({ 
    username: 'xxxxxx-1a06-4a90-xxxxx-xxxxxxxxxxx', 
    password: 'xxxxxxxxxx', 
    version_date: conversationV1.VERSION_DATE_2016_09_20 
}); 
const updateMessage = (input, response) => { 
    var responseText = null; 
    if (!response.output) { 
     response.output = {}; 
    } else { 
     return response; 
    } 
    if (response.intents && response.intents[0]) { 
     var intent = response.intents[0]; 
    if (intent.confidence >= 0.75) { 
     responseText = 'I understood your intent was ' + intent.intent; 
    } else if (intent.confidence >= 0.5) { 
     responseText = 'I think your intent was ' + intent.intent; 
    } else { 
     responseText = 'I did not understand your intent'; 
    } 
} 
response.output.text = responseText; 
return response; 
}; 

app.post('/api/message', (req, res, next) => { 
    const workspace = '254654de-2bfe-423a-92ec-6aa66620625a'; 
    if (!workspace || workspace === '<workspace-id>') { 
     return res.json({ 
      output: { 
       text: 'Please check the workspace' 
      } 
     }); 
    } 
    const payload = { 
     workspace_id: workspace, 
     input: req.body.input || {}, 
     context: req.body.context || {} 
    }; 

    // Send the input to the conversation service 
    conversation.message(payload, (error, data) => { 
     if (error) { 
      return next(error); 
     } 
     return res.json(updateMessage(payload, data)); 
    }); 
}); 

app.listen(process.env.PORT || 3000); 

exports.app = app 

有人可以幫助我,我們如何可以通過上下文,使其work..Can有人help.U可以運行在您的本地測試代碼。

+0

這只是對話的一個副本 - 簡單嗎?所以它應該可以開箱即用。我會建議您查看對話ID不會更改。如果是這樣,那麼問題與您的應用程序。如果沒有,那麼問題在於對話,所以你需要在那裏調試。 –

+0

@ SimonO'Doherty對話標識每次都會更改 – user7350714

+0

如果您更改了任何對話簡單應用程序,請在此處查看。您發佈的內容不夠用。 –

回答

1

無論何時發佈到api /消息端點,您都需要發送上下文。你第一次沒有上下文。上下文將由Watson Conversation創建並返回,然後在此響應中返回:res.json(updateMessage(payload, data))

返回的JSON對象應具有上下文屬性。調用者需要存儲該上下文,然後將其發佈到下一個調用中。因此,主叫方的代碼看起來應該是這樣的(僞):

FIRST CALL:

resp = POST api/message {} 

STORE背景:

ctxt = resp.context 

下一個電話:

resp = POST api/message {context: ctxt} 

STORE CONTEXT(每次):

ctxt = resp.context 

總是更新與服務器返回的上下文調用者的上下文。每當你有一個新的用戶,你重新開始。

你沒有顯示任何調用者的代碼,所以我不確定這是否是你的問題。

0

我有同樣的問題。這裏是我做了什麼:

app.post('/api/message', function (req, res) { 
var workspace = process.env.WORKSPACE_ID || '<workspace-id>'; 
if (!workspace || workspace === '<workspace-id>') { 
return res.json({ 
    'output': { 
    'text': 'Config workspace' 
    } 
}); 
} 

var context; 

var payload = { 
workspace_id: workspace, 
context: {}, 
input: {} 
}; 

if (req.body) { 
if (req.body.input) { 
    payload.input = req.body.input; 
} 
if (req.body.context) { 
    // The client must maintain context/state 
    payload.context = req.body.context; 
} 
} 

conversation.message(payload, function (err, data) { 
if (err) { 
    return res.status(err.code || 500).json(err); 
} 
context = data.context; 
return res.json(updateMessage(payload, data)); 

}); 
}); 

function updateMessage(input, response) { 
var responseText = null; 
if (!response.output) { 
response.output = {}; 
} else { 
return response; 
} 
if (response.intents && response.intents[0]) { 
var intent = response.intents[0]; 
if (intent.confidence >= 0.75) { 
    responseText = 'I understood your intent was ' + intent.intent; 
} else if (intent.confidence >= 0.5) { 
    responseText = 'I think your intent was ' + intent.intent; 
} else { 
    responseText = 'I did not understand your intent'; 
} 
} 
response.output.text = responseText; 
return response; 
} 
module.exports = app; 

如果您使用Visual Studio代碼,那麼你可以檢查相同背景下通過的功能,只是在方面詞點擊。我不是專業人士,但它對我有效。祝你好運!

相關問題