Search in sources :

Example 6 with MessageResponse

use of com.ibm.watson.assistant.v1.model.MessageResponse 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) {
        }
    });
}
Also used : ToneAnalysis(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis) HashMap(java.util.HashMap) MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) ServiceCallback(com.ibm.watson.developer_cloud.http.ServiceCallback) ToneAnalyzer(com.ibm.watson.developer_cloud.tone_analyzer.v3.ToneAnalyzer) MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) Assistant(com.ibm.watson.developer_cloud.assistant.v1.Assistant)

Example 7 with MessageResponse

use of com.ibm.watson.assistant.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.

the class AssistantExample method main.

public static void main(String[] args) throws Exception {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    Assistant service = new Assistant("2019-02-28", authenticator);
    MessageInput input = new MessageInput();
    input.setText("Hi");
    MessageOptions options = new MessageOptions.Builder("<workspaceId>").input(input).build();
    // sync
    MessageResponse response = service.message(options).execute().getResult();
    System.out.println(response);
    // async
    service.message(options).enqueue(new ServiceCallback<MessageResponse>() {

        @Override
        public void onResponse(Response<MessageResponse> response) {
            System.out.println(response.getResult());
        }

        @Override
        public void onFailure(Exception e) {
        }
    });
    // RxJava
    Single<Response<MessageResponse>> observableRequest = service.message(options).reactiveRequest();
    observableRequest.subscribeOn(Schedulers.single()).subscribe(new Consumer<Response<MessageResponse>>() {

        @Override
        public void accept(Response<MessageResponse> response) throws Exception {
            System.out.println(response.getResult());
        }
    });
    Thread.sleep(5000);
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) MessageResponse(com.ibm.watson.assistant.v1.model.MessageResponse) MessageInput(com.ibm.watson.assistant.v1.model.MessageInput) MessageResponse(com.ibm.watson.assistant.v1.model.MessageResponse) Response(com.ibm.cloud.sdk.core.http.Response) MessageOptions(com.ibm.watson.assistant.v1.model.MessageOptions) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator)

Example 8 with MessageResponse

use of com.ibm.watson.assistant.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.

the class AssistantServiceIT method testReadme.

/**
 * Test README.
 */
@Test
public void testReadme() {
    InputData input = new InputData.Builder("Hi").build();
    MessageOptions options = new MessageOptions.Builder(workspaceId).input(input).build();
    MessageResponse response = service.message(options).execute();
    System.out.println(response);
}
Also used : MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) InputData(com.ibm.watson.developer_cloud.assistant.v1.model.InputData) Test(org.junit.Test)

Example 9 with MessageResponse

use of com.ibm.watson.assistant.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.

the class AssistantServiceIT method testSendMessages.

/**
 * Test send messages.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSendMessages() throws InterruptedException {
    final String[] messages = new String[] { "turn ac on", "turn right", "no", "yes" };
    Context context = null;
    for (final String message : messages) {
        MessageOptions request = new MessageOptions.Builder(workspaceId).input(new InputData.Builder(message).build()).alternateIntents(true).context(context).nodesVisitedDetails(true).build();
        if (message.equals("yes")) {
            RuntimeIntent offTopic = new RuntimeIntent();
            offTopic.setIntent("off_topic");
            offTopic.setConfidence(1.0);
            request = request.newBuilder().addIntent(offTopic).build();
        }
        MessageResponse response = service.message(request).execute();
        assertMessageFromService(response);
        assertNotNull(response.getOutput().getNodesVisitedDetails());
        for (RuntimeEntity entity : response.getEntities()) {
            assertNotNull(entity.getGroups());
        }
        context = new Context();
        context.putAll(response.getContext());
        Thread.sleep(500);
    }
}
Also used : Context(com.ibm.watson.developer_cloud.assistant.v1.model.Context) MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) RuntimeIntent(com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent) RuntimeEntity(com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeEntity) MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) Test(org.junit.Test)

Example 10 with MessageResponse

use of com.ibm.watson.assistant.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.

the class AssistantServiceIT method testExample.

/**
 * Test Example.
 */
@Test
public void testExample() {
    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
    }
}
Also used : CompletableFuture(jersey.repackaged.jsr166e.CompletableFuture) MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) OutputData(com.ibm.watson.developer_cloud.assistant.v1.model.OutputData) InputData(com.ibm.watson.developer_cloud.assistant.v1.model.InputData) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 MessageResponse (com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse)8 MessageOptions (com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions)7 JsonObject (com.google.gson.JsonObject)6 MessageResponse (com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 MessageOptions (com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions)5 HashMap (java.util.HashMap)5 MessageResponse (com.ibm.watson.assistant.v1.model.MessageResponse)4 InputData (com.ibm.watson.developer_cloud.assistant.v1.model.InputData)4 InputData (com.ibm.watson.developer_cloud.conversation.v1.model.InputData)4 MessageInput (com.ibm.watson.assistant.v1.model.MessageInput)3 MessageOptions (com.ibm.watson.assistant.v1.model.MessageOptions)3 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)3 CompletableFuture (jersey.repackaged.jsr166e.CompletableFuture)3 JsonParser (com.google.gson.JsonParser)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)2 Response (com.ibm.cloud.sdk.core.http.Response)2 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)2