为什么当我在节点js中创build一个post调用时,Watson-conversation对任何请求都返回相同的默认响应

我已经在对话服务中创build了一个示例训练数据,现在我正在尝试使用节点js创build一个聊天应用程序的后期调用。我创build了一个后期调用,它正在工作,但并不如预期的那样。给我任何呼叫的默认响应。

我开始知道我们需要将响应中获得的上下文值传递给下一个调用进行stream,但不知道该怎么做。 有人可以帮助我。 以下是我的代码

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 

有人可以帮助我,我们可以通过上下文,使其工作..可以帮助人们。我可以运行你的本地代码来testing。

无论何时您发布到api /消息端点,都需要发送上下文。 你第一次没有上下文。 上下文将由Watson Conversation创build并返回,然后在此响应中返回: res.json(updateMessage(payload, data))

返回的JSON对象应该有一个上下文属性。 调用者需要存储该上下文,然后将其发布到下一个调用中。 所以,调用者代码应该看起来像这样(伪代码):

第一次打电话:

 resp = POST api/message {} 

商店环境:

 ctxt = resp.context 

下一个呼叫:

 resp = POST api/message {context: ctxt} 

存储上下文(每次):

 ctxt = resp.context 

始终使用服务器返回的上下文更新调用方中的上下文。 每当你有一个新的用户,你重新开始。

你没有给调用者显示任何代码,所以我不确定这是否是你的问题。

我有同样的问题。 这是我所做的:

 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进行编码,那么您可以检查相同的上下文是否正在通过这些函数,只需单击上下文中的单词即可。 我不是专业人士,但它为我工作。 祝你好运!