您是否查看了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");
此文本对象将包含: 嗨,乔,很高兴认识你。我是来回答关于....的问题的。
(Node.) 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 );
} );