use of com.ibm.watson.developer_cloud.assistant.v1.Assistant in project java-sdk by watson-developer-cloud.
the class AssistantServiceIT method pingBadCredentialsThrowsException.
@Test(expected = UnauthorizedException.class)
public void pingBadCredentialsThrowsException() {
Assistant badService = new Assistant("2018-02-16", "foo", "bar");
MessageOptions options = new MessageOptions.Builder(workspaceId).build();
badService.message(options).execute();
}
use of com.ibm.watson.developer_cloud.assistant.v1.Assistant in project java-sdk by watson-developer-cloud.
the class AssistantTest method testAssistantWithNullWorkspaceId.
/**
* Negative - Test assistant with null workspaceId.
*/
@Test(expected = IllegalArgumentException.class)
public void testAssistantWithNullWorkspaceId() {
MessageOptions options = new MessageOptions.Builder().build();
service.message(options).execute();
}
use of com.ibm.watson.developer_cloud.assistant.v1.Assistant in project java-sdk by watson-developer-cloud.
the class AssistantTest method testAssistantWithEmptyWorkspaceId.
/**
* Negative - Test assistant with empty workspaceId.
*/
@Test(expected = IllegalArgumentException.class)
public void testAssistantWithEmptyWorkspaceId() {
MessageOptions options = new MessageOptions.Builder("").build();
service.message(options).execute();
}
use of com.ibm.watson.developer_cloud.assistant.v1.Assistant in project java-sdk by watson-developer-cloud.
the class AssistantExample method main.
public static void main(String[] args) throws Exception {
Assistant service = new Assistant("2018-02-16");
service.setUsernameAndPassword("<username>", "<password>");
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);
// async
service.message(options).enqueue(new ServiceCallback<MessageResponse>() {
@Override
public void onResponse(MessageResponse response) {
System.out.println(response);
}
@Override
public void onFailure(Exception e) {
}
});
// rx callback
service.message(options).rx().thenApply(new CompletableFuture.Fun<MessageResponse, OutputData>() {
@Override
public OutputData apply(MessageResponse message) {
return message.getOutput();
}
}).thenAccept(new CompletableFuture.Action<OutputData>() {
@Override
public void accept(OutputData output) {
System.out.println(output);
}
});
// rx async callback
service.message(options).rx().thenApplyAsync(new CompletableFuture.Fun<MessageResponse, OutputData>() {
@Override
public OutputData apply(MessageResponse message) {
return message.getOutput();
}
}).thenAccept(new CompletableFuture.Action<OutputData>() {
@Override
public void accept(OutputData output) {
System.out.println(output);
}
});
// rx sync
try {
MessageResponse rxMessageResponse = service.message(options).rx().get();
System.out.println(rxMessageResponse);
} catch (Exception ex) {
// Handle exception
}
}
use of com.ibm.watson.developer_cloud.assistant.v1.Assistant in project java-sdk by watson-developer-cloud.
the class AssistantToneAnalyzerIntegrationExample method main.
public static void main(String[] args) throws Exception {
// instantiate the assistant service
Assistant assistantService = new Assistant("2018-02-16");
assistantService.setUsernameAndPassword("<username>", "<password>");
// instantiate the tone analyzer service
ToneAnalyzer toneService = new ToneAnalyzer("2017-09-21");
toneService.setUsernameAndPassword("<username>", "<password>");
// workspace id
String workspaceId = "<workspace-id>";
// maintain history in the context variable - will add a history variable to
// each of the emotion, social
// and language tones
boolean maintainHistory = false;
/**
* Input for the Assistant service: input (String): an input string (the user's conversation turn) and context
* (Map<String,Object>: any context that needs to be maintained - either added by the client app or passed in the
* response from the Assistant service on the previous conversation turn.
*/
String input = "I am happy";
Map<String, Object> context = new HashMap<>();
// UPDATE CONTEXT HERE IF CONTINUING AN ONGOING CONVERSATION
// set local context variable to the context from the last response from the
// Assistant Service
// (see the getContext() method of the MessageResponse class in
// com.ibm.watson.developer_cloud.assistant.v1.model)
// async call to Tone Analyzer
ToneOptions toneOptions = new ToneOptions.Builder().text(input).build();
toneService.tone(toneOptions).enqueue(new ServiceCallback<ToneAnalysis>() {
@Override
public void onResponse(ToneAnalysis toneResponsePayload) {
// update context with the tone data returned by the Tone Analyzer
ToneDetection.updateUserTone(context, toneResponsePayload, maintainHistory);
// call Assistant Service with the input and tone-aware context
MessageOptions messageOptions = new MessageOptions.Builder(workspaceId).input(new InputData.Builder(input).build()).context(context).build();
assistantService.message(messageOptions).enqueue(new ServiceCallback<MessageResponse>() {
@Override
public void onResponse(MessageResponse response) {
System.out.println(response);
}
@Override
public void onFailure(Exception e) {
}
});
}
@Override
public void onFailure(Exception e) {
}
});
}
Aggregations