1
votes

I am trying to implement a chat assistant, using angular as client and calling Watson Assistant API from Java side.

For the first call am sending an empty input message and when I receive the context in the response am setting it to next message input to maintain the conversation context.

Here is the Java code

import javax.annotation.PostConstruct;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.xxxx.repapp.assistant.MessageInput;
import com.ibm.watson.developer_cloud.assistant.v1.Assistant;
import com.ibm.watson.developer_cloud.assistant.v1.model.InputData;
import com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions;
import com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse;
import com.ibm.watson.developer_cloud.service.exception.NotFoundException;
import com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException;
import com.ibm.watson.developer_cloud.service.exception.ServiceResponseException;

@RestController
@RequestMapping("/app")
public class ConversationResource {

    private static final String WORKSPACE_ID = "";
    private static final String USERNAME = "";
    private static final String PASSWORD = "";

    Assistant service = new Assistant("2018-02-16");

    @PostConstruct
    public void init() {
        service.setUsernameAndPassword(USERNAME, PASSWORD);
    }

    @RequestMapping(value = "rest/conversation/message", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public MessageResponse consecutiveMessages(@RequestBody MessageInput messageInput) {
        try {
            InputData input = new InputData.Builder(messageInput.getText()).build();
            MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID).input(input)
                    .context(messageInput.getContext()).build();
            MessageResponse response = service.message(options).execute();
            return response;
        } catch (NotFoundException e) {
            // Handle Not Found (404) exception
        } catch (RequestTooLargeException e) {
            // Handle Request Too Large (413) exception
        } catch (ServiceResponseException e) {
            // Base class for all exceptions caused by error responses from the
            // service
            System.out.println("Service returned status code " + e.getStatusCode() + ": " + e.getMessage());
        }
        return null;
    }

    @RequestMapping(value = "rest/conversation/initialMessage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public MessageResponse initialMessage() {
        InputData input = new InputData.Builder("").build();
        MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID).input(input).build();

        // sync way
        MessageResponse response = service.message(options).execute();
        return response;

        // async way
        /*
         * service.message(options).enqueue(new
         * ServiceCallback<MessageResponse>() {
         * 
         * @Override public void onResponse(MessageResponse response) {
         * System.out.println(response); }
         * 
         * @Override public void onFailure(Exception e) { } });
         */
    }

}

MessageInput class is

import com.ibm.watson.developer_cloud.assistant.v1.model.Context;

public class MessageInput extends com.ibm.watson.developer_cloud.assistant.v1.model.MessageInput{

    private Context context;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = contextId;
    }   
}

When i have to continue the conversation, the conversation in getting restarted without maintaining context. Please look at the below flow of conversation.

welcome intent response

{
  "output": {
    "text": [
      "Hello I am foo assistant. How can I help you?"
    ],
    "nodes_visited": [
      "Welcome"
    ],
    "log_messages": []
  },
  "input": {
    "text": ""
  },
  "intents": [],
  "entities": [],
  "context": {
    "conversation_id": "b935786e-7e9d-499e-bb65-b6ddd23605a0",
    "system": {
      "dialog_stack": [
        {
          "dialog_node": "root"
        }
      ],
      "dialog_turn_counter": 1.0,
      "dialog_request_counter": 1.0,
      "_node_output_map": {
        "Welcome": {
          "0": [
            0.0
          ]
        }
      },
      "branch_exited": true,
      "branch_exited_reason": "completed"
    }
  }
}

MessageOptions set in next request after setting input text and context

{
  "workspaceId": "*************",
  "input": {
    "text": "submit meter read"
  },
  "context": {
    "output": {
      "text": [
        "Hello I am foo assistant. How can I help you?"
      ],
      "nodes_visited": [
        "Welcome"
      ],
      "log_messages": []
    },
    "input": {
      "text": ""
    },
    "intents": [],
    "entities": [],
    "context": {
      "conversation_id": "b935786e-7e9d-499e-bb65-b6ddd23605a0",
      "system": {
        "dialog_stack": [
          {
            "dialog_node": "root"
          }
        ],
        "dialog_turn_counter": 1,
        "dialog_request_counter": 1,
        "_node_output_map": {
          "Welcome": {
            "0": [
              0
            ]
          }
        },
        "branch_exited": true,
        "branch_exited_reason": "completed"
      }
    }
  }
}
null
{
  "output": {
    "text": [
      "Hello I am foo assistant. How can I help you?"
    ],
    "nodes_visited": [
      "Welcome"
    ],
    "log_messages": []
  },
  "input": {
    "text": ""
  },
  "intents": [],
  "entities": [],
  "context": {
    "conversation_id": "b935786e-7e9d-499e-bb65-b6ddd23605a0",
    "system": {
      "dialog_stack": [
        {
          "dialog_node": "root"
        }
      ],
      "dialog_turn_counter": 1,
      "dialog_request_counter": 1,
      "_node_output_map": {
        "Welcome": {
          "0": [
            0
          ]
        }
      },
      "branch_exited": true,
      "branch_exited_reason": "completed"
    }
  }
}
null
{
  "text": "submit meter read"
}

Message Options next time after inputting meter read date

{
  "workspaceId": "*************",
  "input": {
    "text": "2018-09-09"
  },
  "context": {
    "output": {
      "text": [
        "Please enter meter read date"
      ],
      "nodes_visited": [
        "node_1_1534239058536",
        "handler_4_1534239240239",
        "slot_2_1534239240239"
      ],
      "log_messages": []
    },
    "input": {
      "text": "submit meter read"
    },
    "intents": [
      {
        "intent": "submit_meter_read",
        "confidence": 0.9712400913238526
      }
    ],
    "entities": [],
    "context": {
      "output": {
        "text": [
          "Hello I am foo assistant. How can I help you?"
        ],
        "nodes_visited": [
          "Welcome"
        ],
        "log_messages": []
      },
      "input": {
        "text": ""
      },
      "intents": [],
      "entities": [],
      "context": {
        "conversation_id": "b935786e-7e9d-499e-bb65-b6ddd23605a0",
        "system": {
          "dialog_stack": [
            {
              "dialog_node": "root"
            }
          ],
          "dialog_turn_counter": 1,
          "dialog_request_counter": 1,
          "_node_output_map": {
            "Welcome": {
              "0": [
                0
              ]
            }
          },
          "branch_exited": true,
          "branch_exited_reason": "completed"
        }
      },
      "conversation_id": "ccfba8ad-06e2-4a74-95e3-20f8251c264c",
      "system": {
        "dialog_stack": [
          {
            "dialog_node": "slot_2_1534239240239",
            "state": "in_progress"
          }
        ],
        "dialog_turn_counter": 1,
        "dialog_request_counter": 1,
        "_node_output_map": {}
      }
    }
  }
}

Now the response expected should continue to ask one more slot to fill, but its getting resetted, if you observe below response.

{
  "output": {
    "text": [
      "I didn't understand. You can try rephrasing. I can help you with things like meter read submission, raising a complaint, and giving some general information."
    ],
    "nodes_visited": [
      "Anything else"
    ],
    "log_messages": []
  },
  "input": {
    "text": "2018-09-09"
  },
  "intents": [],
  "entities": [
    {
      "entity": "sys-number",
      "location": [
        0.0,
        4.0
      ],
      "value": "2018",
      "confidence": 1.0,
      "metadata": {
        "numeric_value": 2018.0
      }
    },
    {
      "entity": "sys-date",
      "location": [
        0.0,
        10.0
      ],
      "value": "2018-09-09",
      "confidence": 1.0,
      "metadata": {
        "calendar_type": "GREGORIAN",
        "timezone": "GMT"
      }
    },
    {
      "entity": "sys-number",
      "location": [
        4.0,
        7.0
      ],
      "value": "-9",
      "confidence": 1.0,
      "metadata": {
        "numeric_value": -9.0
      }
    },
    {
      "entity": "sys-number",
      "location": [
        7.0,
        10.0
      ],
      "value": "-9",
      "confidence": 1.0,
      "metadata": {
        "numeric_value": -9.0
      }
    }
  ],
  "context": {
    "output": {
      "text": [
        "Please enter meter read date"
      ],
      "nodes_visited": [
        "node_1_1534239058536",
        "handler_4_1534239240239",
        "slot_2_1534239240239"
      ],
      "log_messages": []
    },
    "input": {
      "text": "submit meter read"
    },
    "intents": [
      {
        "intent": "submit_meter_read",
        "confidence": 0.9712400913238526
      }
    ],
    "entities": [],
    "context": {
      "output": {
        "text": [
          "Hello I am foo assistant. How can I help you?"
        ],
        "nodes_visited": [
          "Welcome"
        ],
        "log_messages": []
      },
      "input": {
        "text": ""
      },
      "intents": [],
      "entities": [],
      "context": {
        "conversation_id": "b935786e-7e9d-499e-bb65-b6ddd23605a0",
        "system": {
          "dialog_stack": [
            {
              "dialog_node": "root"
            }
          ],
          "dialog_turn_counter": 1.0,
          "dialog_request_counter": 1.0,
          "_node_output_map": {
            "Welcome": {
              "0": [
                0.0
              ]
            }
          },
          "branch_exited": true,
          "branch_exited_reason": "completed"
        }
      },
      "conversation_id": "ccfba8ad-06e2-4a74-95e3-20f8251c264c",
      "system": {
        "dialog_stack": [
          {
            "dialog_node": "slot_2_1534239240239",
            "state": "in_progress"
          }
        ],
        "dialog_turn_counter": 1.0,
        "dialog_request_counter": 1.0,
        "_node_output_map": {}
      }
    },
    "conversation_id": "39f82cb9-e0f1-47b9-845e-892c0d589aed",
    "system": {
      "dialog_stack": [
        {
          "dialog_node": "root"
        }
      ],
      "dialog_turn_counter": 1.0,
      "dialog_request_counter": 1.0,
      "_node_output_map": {
        "Anything else": {
          "0": [
            0.0,
            0.0,
            2.0,
            1.0
          ]
        }
      },
      "branch_exited": true,
      "branch_exited_reason": "completed"
    }
  }
}

Where am missing in the above code, please help me out maintaining conversation flow. Thanks

1
Why do you have multiple context fields in the data?data_henrik
that i don't know, whatever i receive from watson assistant api in previous response am sending in the next request, by setting it to context in MessageOptions.Vasu Babu
Does the builder pattern keep adding context object? thought it would override.Vasu Babu

1 Answers

1
votes

You need to send the context obtained from the response. You seem to retrieve the context off the input object. Where is the context information transfered from the response to your MessageInput object?

You probably have seen this example for two consecutive messages: https://github.com/watson-developer-cloud/java-sdk/tree/master/assistant