Search in sources :

Example 16 with Example

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

the class Assistant method createExample.

/**
 * Create user input example.
 *
 * <p>Add a new user input example to an intent.
 *
 * <p>If you want to add multiple examples with a single API call, consider using the **[Update
 * intent](#update-intent)** method instead.
 *
 * @param createExampleOptions the {@link CreateExampleOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link Example}
 */
public ServiceCall<Example> createExample(CreateExampleOptions createExampleOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createExampleOptions, "createExampleOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", createExampleOptions.workspaceId());
    pathParamsMap.put("intent", createExampleOptions.intent());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/intents/{intent}/examples", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "createExample");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    if (createExampleOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(createExampleOptions.includeAudit()));
    }
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("text", createExampleOptions.text());
    if (createExampleOptions.mentions() != null) {
        contentJson.add("mentions", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createExampleOptions.mentions()));
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Example> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Example>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) Example(com.ibm.watson.assistant.v1.model.Example) JsonObject(com.google.gson.JsonObject)

Example 17 with Example

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

the class Assistant method getExample.

/**
 * Get user input example.
 *
 * <p>Get information about a user input example.
 *
 * @param getExampleOptions the {@link GetExampleOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Example}
 */
public ServiceCall<Example> getExample(GetExampleOptions getExampleOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getExampleOptions, "getExampleOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", getExampleOptions.workspaceId());
    pathParamsMap.put("intent", getExampleOptions.intent());
    pathParamsMap.put("text", getExampleOptions.text());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/intents/{intent}/examples/{text}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "getExample");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    if (getExampleOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(getExampleOptions.includeAudit()));
    }
    ResponseConverter<Example> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Example>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) Example(com.ibm.watson.assistant.v1.model.Example)

Example 18 with Example

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

the class AssistantServiceIT method testUpdateExample.

/**
 * Test updateExample.
 */
@Test
public void testUpdateExample() {
    createExampleIntent();
    // gotta be unique
    String exampleText = "Howdy " + UUID.randomUUID().toString();
    // gotta be unique
    String exampleText2 = "Howdy " + UUID.randomUUID().toString();
    CreateExampleOptions createOptions = new CreateExampleOptions.Builder(workspaceId, exampleIntent, exampleText).build();
    service.createExample(createOptions).execute();
    try {
        UpdateExampleOptions updateOptions = new UpdateExampleOptions.Builder(workspaceId, exampleIntent, exampleText).newText(exampleText2).build();
        Example response = service.updateExample(updateOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getExampleText());
        assertEquals(response.getExampleText(), exampleText2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteExampleOptions deleteOptions = new DeleteExampleOptions.Builder(workspaceId, exampleIntent, exampleText2).build();
        service.deleteExample(deleteOptions).execute();
    }
}
Also used : CreateExampleOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateExampleOptions) CreateExample(com.ibm.watson.developer_cloud.assistant.v1.model.CreateExample) Example(com.ibm.watson.developer_cloud.assistant.v1.model.Example) DeleteExampleOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteExampleOptions) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) UpdateExampleOptions(com.ibm.watson.developer_cloud.assistant.v1.model.UpdateExampleOptions) Test(org.junit.Test)

Example 19 with Example

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

the class AssistantServiceIT method testListExamples.

/**
 * Test listExamples.
 */
@Test
public void testListExamples() {
    createExampleIntent();
    // gotta be unique
    String exampleText = "Howdy " + UUID.randomUUID().toString();
    try {
        ListExamplesOptions listOptions = new ListExamplesOptions.Builder(workspaceId, exampleIntent).includeAudit(true).build();
        ExampleCollection ecResponse = service.listExamples(listOptions).execute();
        assertNotNull(ecResponse);
        assertNotNull(ecResponse.getExamples());
        assertNotNull(ecResponse.getPagination());
        assertNotNull(ecResponse.getPagination().getRefreshUrl());
        // nextUrl may be null
        Date start = new Date();
        // Now add an example and make sure we get it back
        CreateExampleOptions createOptions = new CreateExampleOptions.Builder(workspaceId, exampleIntent, exampleText).build();
        service.createExample(createOptions).execute();
        long count = ecResponse.getExamples().size();
        ExampleCollection ecResponse2 = service.listExamples(listOptions.newBuilder().pageLimit(count + 1).includeAudit(true).build()).execute();
        assertNotNull(ecResponse2);
        assertNotNull(ecResponse2.getExamples());
        List<Example> examples = ecResponse2.getExamples();
        assertTrue(examples.size() > count);
        Example exResponse = null;
        for (Example resp : examples) {
            if (resp.getExampleText().equals(exampleText)) {
                exResponse = resp;
                break;
            }
        }
        assertNotNull(exResponse);
        Date now = new Date();
        assertTrue(fuzzyBefore(exResponse.getCreated(), now));
        assertTrue(fuzzyAfter(exResponse.getCreated(), start));
        assertTrue(fuzzyBefore(exResponse.getUpdated(), now));
        assertTrue(fuzzyAfter(exResponse.getUpdated(), start));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteExampleOptions deleteOptions = new DeleteExampleOptions.Builder(workspaceId, exampleIntent, exampleText).build();
        service.deleteExample(deleteOptions).execute();
    }
}
Also used : ExampleCollection(com.ibm.watson.developer_cloud.assistant.v1.model.ExampleCollection) Date(java.util.Date) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ListExamplesOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListExamplesOptions) CreateExampleOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateExampleOptions) CreateExample(com.ibm.watson.developer_cloud.assistant.v1.model.CreateExample) Example(com.ibm.watson.developer_cloud.assistant.v1.model.Example) DeleteExampleOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteExampleOptions) Test(org.junit.Test)

Example 20 with Example

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

the class AssistantServiceIT method testCreateIntent.

/**
 * Test createIntent.
 */
@Test
public void testCreateIntent() {
    // gotta be unique
    String intentName = "Hello" + UUID.randomUUID().toString();
    String intentDescription = "Description of " + intentName;
    String intentExample = "Example of " + intentName;
    List<CreateExample> intentExamples = new ArrayList<CreateExample>();
    intentExamples.add(new CreateExample.Builder().text(intentExample).build());
    Date start = new Date();
    CreateIntentOptions createOptions = new CreateIntentOptions.Builder(workspaceId, intentName).description(intentDescription).examples(intentExamples).build();
    Intent response = service.createIntent(createOptions).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getIntentName());
        assertEquals(response.getIntentName(), intentName);
        assertNotNull(response.getDescription());
        assertEquals(response.getDescription(), intentDescription);
        Date now = new Date();
        ListExamplesOptions listOptions = new ListExamplesOptions.Builder(workspaceId, intentName).includeAudit(true).build();
        ExampleCollection ecResponse = service.listExamples(listOptions).execute();
        assertNotNull(ecResponse);
        assertNotNull(ecResponse.getExamples());
        List<Example> examples = ecResponse.getExamples();
        assertTrue(examples.size() == 1);
        assertEquals(examples.get(0).getExampleText(), intentExample);
        assertTrue(fuzzyBefore(examples.get(0).getCreated(), now));
        assertTrue(fuzzyAfter(examples.get(0).getCreated(), start));
        assertTrue(fuzzyBefore(examples.get(0).getUpdated(), now));
        assertTrue(fuzzyAfter(examples.get(0).getUpdated(), start));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteIntentOptions deleteOptions = new DeleteIntentOptions.Builder(workspaceId, intentName).build();
        service.deleteIntent(deleteOptions).execute();
    }
}
Also used : CreateExample(com.ibm.watson.developer_cloud.assistant.v1.model.CreateExample) CreateIntentOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateIntentOptions) ArrayList(java.util.ArrayList) ExampleCollection(com.ibm.watson.developer_cloud.assistant.v1.model.ExampleCollection) Intent(com.ibm.watson.developer_cloud.assistant.v1.model.Intent) CreateIntent(com.ibm.watson.developer_cloud.assistant.v1.model.CreateIntent) RuntimeIntent(com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent) Date(java.util.Date) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ListExamplesOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListExamplesOptions) CreateExample(com.ibm.watson.developer_cloud.assistant.v1.model.CreateExample) Example(com.ibm.watson.developer_cloud.assistant.v1.model.Example) DeleteIntentOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteIntentOptions) Test(org.junit.Test)

Aggregations

NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)14 UnauthorizedException (com.ibm.watson.developer_cloud.service.exception.UnauthorizedException)14 Test (org.junit.Test)14 Example (com.ibm.watson.assistant.v1.model.Example)10 Example (com.ibm.watson.developer_cloud.assistant.v1.model.Example)10 Example (com.ibm.watson.developer_cloud.conversation.v1.model.Example)10 Date (java.util.Date)10 CreateExample (com.ibm.watson.developer_cloud.assistant.v1.model.CreateExample)7 CreateExample (com.ibm.watson.developer_cloud.conversation.v1.model.CreateExample)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)7 Test (org.testng.annotations.Test)7 JsonObject (com.google.gson.JsonObject)6 Mention (com.ibm.watson.assistant.v1.model.Mention)6 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)4 CreateIntent (com.ibm.watson.assistant.v1.model.CreateIntent)4 CreateExampleOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateExampleOptions)4