Watson 响应一个 API 代码

Watson Responds with one API code

我知道如果我想在对话中向 Watson 发送任何内容,我使用:

var latestResponse = Api.getResponsePayload();
var context = latestResponse.context;
Api.sendRequest("Hi Watson!", context);

我的代码的结果:

我想知道如何让 Watson 在对话中发送内容。我看到了一些例子并尝试过但没有用。有人可以帮忙吗?

我现在不知道如果我做对了,但我的例子是:

// var responseText = null;
   //responseText = {};
 var latestResponse = Api.setResponsePayload(); // I dont know if this is true
  var context = latestResponse.context;
  Api.sendRequest('Hi ' + context); // I try this
  responseText = 'Hi ' + context; // I try this too

这就是我想要的:

您查看 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 );
  } );