Search in sources :

Example 1 with Context

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

the class ConversationTest method testSendMessageWithAlternateIntents.

/**
 * Test send message. use some different MessageOptions options like context and other public methods
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSendMessageWithAlternateIntents() throws IOException, InterruptedException {
    MessageResponse mockResponse = loadFixture(FIXTURE, MessageResponse.class);
    server.enqueue(jsonResponse(mockResponse));
    Context contextTemp = new Context();
    contextTemp.put("name", "Myname");
    InputData inputTemp = new InputData.Builder("My text").build();
    MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID).input(inputTemp).alternateIntents(false).context(contextTemp).entities(null).intents(null).build();
    // execute first request
    MessageResponse serviceResponse = service.message(options).execute();
    // first request
    RecordedRequest request = server.takeRequest();
    String path = StringUtils.join(PATH_MESSAGE, "?", VERSION, "=2018-02-16");
    assertEquals(path, request.getPath());
    assertArrayEquals(new String[] { "Do you want to get a quote?" }, serviceResponse.getOutput().getText().toArray(new String[0]));
    assertEquals(request.getMethod(), "POST");
    assertNotNull(request.getHeader(HttpHeaders.AUTHORIZATION));
    assertEquals("{\"input\":{\"text\":\"My text\"},\"alternate_intents\":false," + "\"context\":{\"name\":\"Myname\"}}", request.getBody().readUtf8());
    assertEquals(serviceResponse, mockResponse);
}
Also used : Context(com.ibm.watson.developer_cloud.conversation.v1.model.Context) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MessageOptions(com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions) MessageResponse(com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse) InputData(com.ibm.watson.developer_cloud.conversation.v1.model.InputData) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 2 with Context

use of com.ibm.watson.developer_cloud.assistant.v1.model.Context 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 3 with Context

use of com.ibm.watson.developer_cloud.assistant.v1.model.Context 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 4 with Context

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

the class AssistantTest method testSendMessageWithAlternateIntents.

/**
 * Test send message. use some different MessageOptions options like context and other public methods
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSendMessageWithAlternateIntents() throws IOException, InterruptedException {
    MessageResponse mockResponse = loadFixture(FIXTURE, MessageResponse.class);
    server.enqueue(jsonResponse(mockResponse));
    Context contextTemp = new Context();
    contextTemp.put("name", "Myname");
    InputData inputTemp = new InputData.Builder("My text").build();
    MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID).input(inputTemp).alternateIntents(false).context(contextTemp).entities(null).intents(null).build();
    // execute first request
    MessageResponse serviceResponse = service.message(options).execute();
    // first request
    RecordedRequest request = server.takeRequest();
    String path = StringUtils.join(PATH_MESSAGE, "?", VERSION, "=2018-02-16");
    assertEquals(path, request.getPath());
    assertArrayEquals(new String[] { "Do you want to get a quote?" }, serviceResponse.getOutput().getText().toArray(new String[0]));
    assertEquals(request.getMethod(), "POST");
    assertNotNull(request.getHeader(HttpHeaders.AUTHORIZATION));
    assertEquals("{\"input\":{\"text\":\"My text\"},\"alternate_intents\":false," + "\"context\":{\"name\":\"Myname\"}}", request.getBody().readUtf8());
    assertEquals(serviceResponse, mockResponse);
}
Also used : Context(com.ibm.watson.developer_cloud.assistant.v1.model.Context) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) 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) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 5 with Context

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

the class ConversationServiceIT 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.conversation.v1.model.Context) MessageOptions(com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions) RuntimeIntent(com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeIntent) RuntimeEntity(com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeEntity) MessageResponse(com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse) Test(org.junit.Test)

Aggregations

MessageResponse (com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse)4 Test (org.junit.Test)4 JsonObject (com.google.gson.JsonObject)3 MessageOptions (com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions)3 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)3 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 Context (com.ibm.watson.developer_cloud.assistant.v1.model.Context)2 DialogNode (com.ibm.watson.developer_cloud.assistant.v1.model.DialogNode)2 Context (com.ibm.watson.developer_cloud.conversation.v1.model.Context)2 MessageOptions (com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions)2 MessageResponse (com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Assistant (com.ibm.watson.developer_cloud.assistant.v1.Assistant)1 InputData (com.ibm.watson.developer_cloud.assistant.v1.model.InputData)1 RuntimeEntity (com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeEntity)1 RuntimeIntent (com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent)1 InputData (com.ibm.watson.developer_cloud.conversation.v1.model.InputData)1 RuntimeEntity (com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeEntity)1 RuntimeIntent (com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeIntent)1 ServiceCallback (com.ibm.watson.developer_cloud.http.ServiceCallback)1