您检查过演示应用程序吗?https://github.com/watson-developer-cloud/conversation-simple ?
您可以以 JSON 方式将对象添加到上下文。
context.myproperty = "Hello World";
并将其与输入一起发送到服务
相反,在对话服务内,您可以将变量(在本例中为用户名)分配给上一步中提供的文本(在本例中为 input.text)。
通过使用$variablename
(在这种情况下$username
)您可以生成动态响应。
不要让高级响应屏幕中的顺序打扰您,上下文会在输出之前进行处理...
在客户端(在我的例子中是Java)
MessageRequest.Builder messageRequestBuilder = new MessageRequest.Builder();
messageRequestBuilder.inputText("Joe");
messageRequestBuilder.context(question.context); //this context comes from a previous step
ServiceCall<MessageResponse> response = conversationService.message(workspaceId, messageRequestBuilder.build());
MessageResponse mAnswer = response.execute();
Object textObject = mAnswer.getOutput().get("text");
该文本对象将包含:嗨,乔,很高兴认识你。我来这里是为了回答有关......的问题
(节点。)从示例应用程序复制(并删除了一些行)JS 代码
// Create the service wrapper
var conversation = watson.conversation ( {
username: process.env.CONVERSATION_USERNAME || '<username>',
password: process.env.CONVERSATION_PASSWORD || '<password>',
version_date: '2016-07-11',
version: 'v1'
} );
// Endpoint to be called from the client side
app.post ( '/api/message', function (req, res) {
var payload = {
workspace_id: workspace_id,
context: {}
};
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;
}
}
// Send the input to the conversation service
conversation.message ( payload, function (data) {
return res.json ( data );
} );