首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我在节点js中创建post调用时,为什么Watson-conversation为任何请求返回相同的默认响应

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

Stack Overflow用户
提问于 2017-04-18 14:09:51
回答 1查看 505关注 0票数 1

我已经在会话服务中创建了一个样本训练数据,现在我正在尝试使用node js为该服务创建一个post调用,以创建一个聊天应用程序。我创建了一个post调用,它正在工作,但不是因为expected.It为我提供了任何调用的默认响应。

我开始知道我们需要传递在对下一个调用的响应中获得的上下文值,以执行flow.But,但不确定如何做到这一点。有人能帮我一下吗。下面是我的代码

代码语言:javascript
运行
复制
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有人帮助。你可以运行代码在您的本地测试。

EN

回答 1

Stack Overflow用户

发布于 2017-04-22 07:23:32

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

返回的JSON对象应该有一个上下文属性。调用者需要存储该上下文,然后在下一次调用中发布它。因此,调用者代码应该类似于(伪代码):

第一次调用:

代码语言:javascript
运行
复制
resp = POST api/message {}

存储上下文:

代码语言:javascript
运行
复制
ctxt = resp.context

下一个呼叫:

代码语言:javascript
运行
复制
resp = POST api/message {context: ctxt}

存储上下文(每次):

代码语言:javascript
运行
复制
ctxt = resp.context

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43464805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档