我想通过我的Node应用程序在IBM Watson中以上下文变量的形式将一些值传递给前端。我怎样才能实现它?
我尝试将要添加的值添加到当前上下文变量对象并将其发回。仍然没有帮助。我有什么方法可以做到吗?
修改
现在,我将所需的值作为新的键值对添加到Node app中的上下文对象,如下所示。
...
let user_name = "MJ"
context.user_name = user_name
response.send({
output: output,
context: JSON.stringfy(context)
})
...
在Watson Console中,在我使用过的一个对话节点中,
Hey $user_name, How are you?
但我得到的输出是,
Hey , How are you?
我可以在上下文对象中看到user_name
值,但我不能按照上面提到的方式使用它。有没有其他方法可以这样做?
感谢任何帮助。提前谢谢!
答案 0 :(得分:1)
我遇到了同样的问题。我的解决方案是在调用IBM服务器并请求.json时更改代码:
...
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text,
context: {username : variable},
...
我在Watson Assistant中设置的username
以及上下文,我使用函数通过Query获取名称的变量(因为在我的应用程序中,我通过IFrame调用了聊天机器人。),您可以使用在javascript上设置的任何变量。
答案 1 :(得分:1)
var payload = {
assistantId: assistantId,
sessionId: req.body.session_id,
context: {
skills: {
"main skill": {
user_defined: {
username: 'John Doe'
}
}
}
},
input: {
message_type: 'text',
text: "blah",
},
};
为我工作。在这里看到:https://medium.com/@pranavbhatia_26901/watson-assistant-v2-context-sharing-3ca18626ed0d
答案 2 :(得分:0)
您可以向Context对象添加任何值,可以在Node.JS应用程序中访问它,如果您将该值发送到前端,那么它也应该可以在UI中访问。
下面我提到了Conversation服务的示例欢迎回复。您可以从Conversation服务的响应中访问Context对象,并向该对象添加新的键值对。在回复中,您会看到我正在访问具有值 MJ 的上下文变量用户名,该值已动态添加到上下文中。
`
{
"intents": [],
"entities": [],
"input": {
"text": ""
},
"output": {
"text": ["Hello MJ! How can I help you today?"],
"nodes_visited": ["Conversation Start"],
"log_messages": []
},
"context": {
"username": "MJ",
"conversation_id": "5835fa3b-6a1c-4ec5-92f9-22844684670e",
"system": {
"dialog_stack": [{
"dialog_node": "Conversation Start"
}],
"dialog_turn_counter": 1,
"dialog_request_counter": 1,
"_node_output_map": {
"Conversation Start": [0]
}
}
}
`
现在更新上下文,获取响应并添加新的键值对
`
var convResponse = <responseObj from Conversation call>;
var context = convResponse.context;
//add new value to context
context["new_key"] = "new value";
`
现在,您对Conversation进行下一次调用,使用此更新的上下文而不是您从上一次调用中收到的上下文。您可以将Conversation的响应发送回前端,然后可以向用户显示。