Watson Responds with one API code

蓝咒 提交于 2019-12-20 05:16:07

问题


i know if I want send any to Watson in conversation I use the:

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

This result of my code:

I want to know how do I get Watson to send something in the conversation. I saw some examples and tried and it did not work. Can someone help?

I dont now If I'm doing right, but My example is:

// 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

This is what i want:


回答1:


Did you check the demo app at https://github.com/watson-developer-cloud/conversation-simple ?

You can add objects to the context in a JSON way.

context.myproperty = "Hello World";

And send this with the input to the service

The other way around, inside the conversation service, you can assign a variable (in this case username) to the text provided in the previous step (in this case input.text). By using $variablename (in this case $username) you can generate a dynamic response. Don't let the order in the advance response screen disturb you, context is processed before output...

In the client (in my case 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");

This textObject will contain: Hi Joe, nice to meet you. I am here to answer questions about....

(Node.) JS code copied (and removed some lines ) from the sample app

// 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 );
  } );


来源:https://stackoverflow.com/questions/41041442/watson-responds-with-one-api-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!