Watson Conversation 中的 MessageResponse returns 空指针

MessageResponse returns null pointer in Watson Conversation

MessageResponse 给出 NullPointerException.

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
// Credentials of Workspace of Conversation
service.setApiKey("API_KEY");
service.setUsernameAndPassword("USERNAME", "PASSWORD");
MessageRequest newMessage = new MessageRequest.Builder()
  .inputText(request.getQuery())
  .build();

// Workspace ID of Conversation current workspace
String workspaceId = "WORKSPACEID";
service.setSkipAuthentication(true);
MessageResponse response = service.message(workspaceId, newMessage)
  .execute();

根据 IBM 开发人员 (@German) 的说法:"The Watson services currently use Basic Auth so instead of an api_key you will use username and password. In order to get the credentials, you need to bind the service you want to use (e.g. Question and Answer) to a Bluemix application."

检查以下示例。

尝试使用来自Java SDK的以下代码:

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2017_05_26);
service.setUsernameAndPassword("<username>", "<password>"); //Please make sure if this username and password is the Service Credentials from the Service that you have created to use Conversation

InputData input = new InputData.Builder("Hi").build();
MessageOptions options = new MessageOptions.Builder(workspaceId).input(input).build();

// sync
MessageResponse response = service.message(options).execute();
System.out.println(response);

其他示例:

MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build();

MessageResponse response = service.message(WORKSPACE_ID,newMessage).execute();

context = response.getContext();    
System.out.println(response);
  • 您可以使用 Watson Conversation 查看 官方 example
  • 请参阅 Watson 服务使用入门指南(第 5 步)here

Conversation 服务不使用 api_keyusernamepassword

您的代码段中有两个错误: 1. 使用对话时不需要setApiKey()。 1. service.setSkipAuthentication(true); 将指示 SDK 忽略服务凭据,因此不会在每次请求时将它们发送到服务器。

您只需要删除行 service.setSkipAuthentication(true);

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
// Credentials of Workspace of Conversation
// BTW: This are not your Bluemix credentials!
service.setUsernameAndPassword("USERNAME", "PASSWORD");

MessageRequest newMessage = new MessageRequest.Builder()
  .inputText("Hi! this is my first message to Watson")
  .build();

MessageResponse response = service.message("WORKSPACEID", newMessage)
  .execute();
System.out.println(response);