Search in sources :

Example 1 with Intent

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

the class ConversationServiceIT method createExampleIntent.

public void createExampleIntent() {
    exampleIntent = "Hello";
    try {
        CreateIntentOptions createOptions = new CreateIntentOptions.Builder(workspaceId, exampleIntent).description("Example Intent").build();
        service.createIntent(createOptions).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
}
Also used : CreateIntentOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateIntentOptions) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException)

Example 2 with Intent

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

the class ConversationTest method testCreateIntentOptionsBuilder.

/**
 * Test CreateIntentOptions builder.
 */
@Test
public void testCreateIntentOptionsBuilder() {
    String intent = "anIntent";
    CreateExample intentExample0 = new CreateExample.Builder().text("intentExample0").build();
    CreateExample intentExample1 = new CreateExample.Builder().text("intentExample1").build();
    CreateIntentOptions createOptions = new CreateIntentOptions.Builder().workspaceId(WORKSPACE_ID).intent(intent).addExample(intentExample0).addExample(intentExample1).build();
    assertEquals(createOptions.workspaceId(), WORKSPACE_ID);
    assertEquals(createOptions.intent(), intent);
    assertEquals(createOptions.examples().size(), 2);
    assertEquals(createOptions.examples().get(0), intentExample0);
    assertEquals(createOptions.examples().get(1), intentExample1);
    CreateIntentOptions.Builder builder = createOptions.newBuilder();
    CreateExample intentExample2 = new CreateExample.Builder().text("intentExample2").build();
    builder.examples(Arrays.asList(intentExample2));
    CreateIntentOptions options2 = builder.build();
    assertEquals(options2.workspaceId(), WORKSPACE_ID);
    assertEquals(options2.intent(), intent);
    assertEquals(options2.examples().size(), 1);
    assertEquals(options2.examples().get(0), intentExample2);
}
Also used : CreateExample(com.ibm.watson.developer_cloud.conversation.v1.model.CreateExample) CreateIntentOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateIntentOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 3 with Intent

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

the class ConversationTest method testSendMessage.

/**
 * Test send message.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSendMessage() throws IOException, InterruptedException {
    String text = "I'd like to get insurance to for my home";
    MessageResponse mockResponse = loadFixture(FIXTURE, MessageResponse.class);
    server.enqueue(jsonResponse(mockResponse));
    InputData input = new InputData.Builder(text).build();
    RuntimeIntent intent = new RuntimeIntent();
    intent.setIntent("turn_off");
    intent.setConfidence(0.0);
    RuntimeEntity entity = new RuntimeEntity();
    entity.setEntity("car");
    entity.setValue("ford");
    MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID).input(input).addIntent(intent).addEntity(entity).alternateIntents(true).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));
    String expected = "{" + "\"input\":{\"text\":\"I'd like to get insurance to for my home\"}," + "\"intents\":[{\"confidence\":0.0,\"intent\":\"turn_off\"}]," + "\"entities\":[{\"value\":\"ford\",\"entity\":\"car\"}]," + "\"alternate_intents\":true" + "}";
    JsonParser parser = new JsonParser();
    JsonObject expectedObj = parser.parse(expected).getAsJsonObject();
    JsonObject actualObj = parser.parse(request.getBody().readUtf8()).getAsJsonObject();
    assertTrue(expectedObj.equals(actualObj));
    assertEquals(serviceResponse, mockResponse);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) 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) JsonObject(com.google.gson.JsonObject) InputData(com.ibm.watson.developer_cloud.conversation.v1.model.InputData) JsonParser(com.google.gson.JsonParser) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 4 with Intent

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

the class Conversation method createIntent.

/**
 * Create intent.
 *
 * Create a new intent.
 *
 * @param createIntentOptions the {@link CreateIntentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Intent}
 */
public ServiceCall<Intent> createIntent(CreateIntentOptions createIntentOptions) {
    Validator.notNull(createIntentOptions, "createIntentOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "intents" };
    String[] pathParameters = { createIntentOptions.workspaceId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("intent", createIntentOptions.intent());
    if (createIntentOptions.description() != null) {
        contentJson.addProperty("description", createIntentOptions.description());
    }
    if (createIntentOptions.examples() != null) {
        contentJson.add("examples", GsonSingleton.getGson().toJsonTree(createIntentOptions.examples()));
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Intent.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject) Intent(com.ibm.watson.developer_cloud.conversation.v1.model.Intent)

Example 5 with Intent

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

the class Conversation method getIntent.

/**
 * Get intent.
 *
 * Get information about an intent, optionally including all intent content.
 *
 * @param getIntentOptions the {@link GetIntentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link IntentExport}
 */
public ServiceCall<IntentExport> getIntent(GetIntentOptions getIntentOptions) {
    Validator.notNull(getIntentOptions, "getIntentOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "intents" };
    String[] pathParameters = { getIntentOptions.workspaceId(), getIntentOptions.intent() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    if (getIntentOptions.export() != null) {
        builder.query("export", String.valueOf(getIntentOptions.export()));
    }
    if (getIntentOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(getIntentOptions.includeAudit()));
    }
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(IntentExport.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) IntentExport(com.ibm.watson.developer_cloud.conversation.v1.model.IntentExport)

Aggregations

Test (org.junit.Test)11 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)9 UnauthorizedException (com.ibm.watson.developer_cloud.service.exception.UnauthorizedException)9 CreateExample (com.ibm.watson.developer_cloud.conversation.v1.model.CreateExample)7 CreateIntentOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateIntentOptions)7 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)7 ArrayList (java.util.ArrayList)7 JsonObject (com.google.gson.JsonObject)6 Date (java.util.Date)6 DeleteIntentOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteIntentOptions)5 Intent (com.ibm.watson.developer_cloud.assistant.v1.model.Intent)4 Example (com.ibm.watson.developer_cloud.conversation.v1.model.Example)4 Intent (com.ibm.watson.developer_cloud.conversation.v1.model.Intent)4 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)3 CreateIntent (com.ibm.watson.developer_cloud.conversation.v1.model.CreateIntent)3 ExampleCollection (com.ibm.watson.developer_cloud.conversation.v1.model.ExampleCollection)3 IntentExport (com.ibm.watson.developer_cloud.conversation.v1.model.IntentExport)3 RuntimeIntent (com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeIntent)3 CreateExample (com.ibm.watson.developer_cloud.assistant.v1.model.CreateExample)2 CreateIntent (com.ibm.watson.developer_cloud.assistant.v1.model.CreateIntent)2