Search in sources :

Example 1 with Response

use of com.ibm.cloud.sdk.core.http.Response 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 2 with Response

use of com.ibm.cloud.sdk.core.http.Response 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
    Authenticator assistantAuthenticator = new IamAuthenticator("<iam_api_key>");
    final Assistant assistantService = new Assistant("2019-02-28", assistantAuthenticator);
    // instantiate the tone analyzer service
    Authenticator toneAuthenticator = new IamAuthenticator("<iam_api_key>");
    ToneAnalyzer toneService = new ToneAnalyzer("2017-09-21", toneAuthenticator);
    // workspace id
    final String workspaceId = "<workspace-id>";
    // maintain history in the context variable - will add a history variable to
    // each of the emotion, social
    // and language tones
    final boolean maintainHistory = false;
    /**
     * Input for the Assistant service: text (String): an input string (the user's conversation
     * turn) and context (Context): 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.
     */
    final String text = "I am happy";
    final Context context = new Context();
    // 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.assistant.v1.model)
    // async call to Tone Analyzer
    ToneOptions toneOptions = new ToneOptions.Builder().text(text).build();
    toneService.tone(toneOptions).enqueue(new ServiceCallback<ToneAnalysis>() {

        @Override
        public void onResponse(Response<ToneAnalysis> toneResponsePayload) {
            // update context with the tone data returned by the Tone Analyzer
            context.setSystem(ToneDetection.updateUserTone(context, toneResponsePayload.getResult(), maintainHistory));
            // create input for message
            MessageInput input = new MessageInput();
            input.setText(text);
            // call Assistant Service with the input and tone-aware context
            MessageOptions messageOptions = new MessageOptions.Builder(workspaceId).input(input).context(context).build();
            assistantService.message(messageOptions).enqueue(new ServiceCallback<MessageResponse>() {

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

                @Override
                public void onFailure(Exception e) {
                }
            });
        }

        @Override
        public void onFailure(Exception e) {
        }
    });
}
Also used : Context(com.ibm.watson.assistant.v1.model.Context) ToneAnalysis(com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) MessageInput(com.ibm.watson.assistant.v1.model.MessageInput) ToneOptions(com.ibm.watson.tone_analyzer.v3.model.ToneOptions) MessageResponse(com.ibm.watson.assistant.v1.model.MessageResponse) Response(com.ibm.cloud.sdk.core.http.Response) ServiceCallback(com.ibm.cloud.sdk.core.http.ServiceCallback) ToneAnalyzer(com.ibm.watson.tone_analyzer.v3.ToneAnalyzer) MessageOptions(com.ibm.watson.assistant.v1.model.MessageOptions) Assistant(com.ibm.watson.assistant.v1.Assistant) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator)

Aggregations

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 MessageInput (com.ibm.watson.assistant.v1.model.MessageInput)2 MessageOptions (com.ibm.watson.assistant.v1.model.MessageOptions)2 MessageResponse (com.ibm.watson.assistant.v1.model.MessageResponse)2 ServiceCallback (com.ibm.cloud.sdk.core.http.ServiceCallback)1 Assistant (com.ibm.watson.assistant.v1.Assistant)1 Context (com.ibm.watson.assistant.v1.model.Context)1 ToneAnalyzer (com.ibm.watson.tone_analyzer.v3.ToneAnalyzer)1 ToneAnalysis (com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis)1 ToneOptions (com.ibm.watson.tone_analyzer.v3.model.ToneOptions)1