Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import com.ibm.watsonx.ai.core.auth.IdentityTokenResponse;
import com.ibm.watsonx.ai.core.factory.HttpClientFactory;
import com.ibm.watsonx.ai.core.http.AsyncHttpClient;
Expand Down Expand Up @@ -71,7 +72,7 @@ public CompletableFuture<IdentityTokenResponse> asyncToken(String apiKey, String
throw new RuntimeException(response.body());

}, ExecutorProvider.cpuExecutor())
.thenApplyAsync(r -> r, ExecutorProvider.ioExecutor());
.thenApplyAsync(Function.identity(), ExecutorProvider.ioExecutor());
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -359,7 +360,7 @@ public <T> CompletableFuture<HttpResponse<T>> intercept(HttpRequest request, Bod
assertEquals("io-thread", Thread.currentThread().getName());
threadNames.add(Thread.currentThread().getName());
return chain.proceed(request, bodyHandler)
.thenApplyAsync(r -> r, cpuExecutor)
.thenApplyAsync(Function.identity(), cpuExecutor)
.thenApplyAsync(r -> {
threadNames.add(Thread.currentThread().getName());
return r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Flow;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -625,8 +626,8 @@ void test_executor() throws Exception {
public <T> CompletableFuture<HttpResponse<T>> intercept(HttpRequest request, BodyHandler<T> bodyHandler,
int index, AsyncChain chain) {
return chain.proceed(request, bodyHandler)
.thenApplyAsync(r -> r, cpuExecutor)
.thenApplyAsync(r -> r, ioExecutor);
.thenApplyAsync(Function.identity(), cpuExecutor)
.thenApplyAsync(Function.identity(), ioExecutor);
}
}).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,10 @@ public Thinking build() {
return new Thinking(this);
}
}

@Override
public String toString() {
return "Thinking [enabled=" + enabled + ", includeReasoning=" + includeReasoning + ", extractionTags=" + extractionTags + ", thinkingEffort="
+ thinkingEffort + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* DetectionResponse<DetectionTextResponse> response = detectionService.detect(
* DetectionTextRequest.builder()
* .input("...")
* .detectors(Pii.create(), Hap.builder().threshold(0.3).build())
* .detectors(Pii.ofDefaults(), Hap.builder().threshold(0.3).build())
* .build()
* );
* }</pre>
Expand Down Expand Up @@ -72,7 +72,7 @@ public DetectionResponse<DetectionTextResponse> detect(DetectionTextRequest requ
* DetectionResponse<DetectionTextResponse> response = detectionService.detect(
* DetectionTextRequest.builder()
* .input("...")
* .detectors(Pii.create(), Hap.builder().threshold(0.3).build())
* .detectors(Pii.ofDefaults(), Hap.builder().threshold(0.3).build())
* .build()
* );
* }</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* DetectionTextRequest request =
* DetectionTextRequest.builder()
* .input("...")
* .detectors(Pii.create(), Hap.builder().threshold(0.3).build())
* .detectors(Pii.ofDefaults(), Hap.builder().threshold(0.3).build())
* .build();
* }</pre>
*/
Expand Down Expand Up @@ -52,7 +52,7 @@ public Map<String, Map<String, Object>> getDetectors() {
* DetectionTextRequest request =
* DetectionTextRequest.builder()
* .input("...")
* .detectors(Pii.create(), Hap.builder().threshold(0.3).build())
* .detectors(Pii.ofDefaults(), Hap.builder().threshold(0.3).build())
* .build();
* }</pre>
*
Expand Down Expand Up @@ -109,4 +109,10 @@ public DetectionTextRequest build() {
return new DetectionTextRequest(this);
}
}

@Override
public String toString() {
return "DetectionTextRequest [projectId=" + projectId + ", spaceId=" + spaceId + ", transactionId=" + transactionId + ", input=" + input
+ ", detectors=" + detectors + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@
import java.util.Map;

/**
* Abstract class for all detectors.
* Represents a configurable content detector used by the detection service.
* <p>
* This is an abstract base class, one of its concrete implementations should be used instead:
* <ul>
* <li>{@link Pii} — detects personally identifiable information (PII)</li>
* <li>{@link Hap} — detects hate or profanity content</li>
* <li>{@link GraniteGuardian} — performs general content moderation</li>
* </ul>
* <p>
* Each detector exposes a builder for fluent configuration of parameters.
* <p>
* Detectors are used in a {@code DetectionService} request to specify which kinds of content analysis to perform.
*
* @see Pii
* @see Hap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ private GraniteGuardian(Builder builder) {
super(builder);
}

/**
* Returns an empty {@link GraniteGuardian} detector instance with no properties configured.
* <p>
* This is equivalent to:
*
* <pre>{@code
* GraniteGuardian detector = GraniteGuardian.builder().build();
* }</pre>
*
* @return an empty {@link GraniteGuardian} instance.
*/
public static GraniteGuardian ofDefaults() {
return builder().build();
}

/**
* Returns a new {@link Builder} instance.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ private Hap(Builder builder) {
super(builder);
}

/**
* Returns an empty {@link Hap} detector instance with no properties configured.
* <p>
* This is equivalent to:
*
* <pre>{@code
* Hap detector = Hap.builder().build();
* }</pre>
*
* @return an empty {@link Hap} instance.
*/
public static Hap ofDefaults() {
return builder().build();
}

/**
* Returns a new {@link Builder} instance.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
* <b>Example usage:</b>
*
* <pre>{@code
* Pii detector = Pii.builder()
* .threshold(0.5)
* .build();
* Pii detector = Pii.ofDefaults();
* }</pre>
*/
public final class Pii extends BaseDetector {
Expand All @@ -22,11 +20,17 @@ private Pii(Builder builder) {
}

/**
* Creates a new instance of Pii.
* Returns an empty {@link Pii} detector instance with no properties configured.
* <p>
* This is equivalent to:
*
* <pre>{@code
* Pii detector = Pii.builder().build();
* }</pre>
*
* @return new {@link Pii} instance.
* @return an empty {@link Pii} instance.
*/
public static Pii create() {
public static Pii ofDefaults() {
return builder().build();
}

Expand All @@ -36,9 +40,7 @@ public static Pii create() {
* <b>Example usage:</b>
*
* <pre>{@code
* Pii detector = Pii.builder()
* .threshold(0.5)
* .build();
* Pii detector = Pii.ofDefaults();
* }</pre>
*
* @return {@link Builder} instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ public static Builder builder() {
/**
* Builder class for constructing {@link TextExtractionService} instances with configurable parameters.
*/
public static class Builder extends ProjectService.Builder<Builder> {
public final static class Builder extends ProjectService.Builder<Builder> {
private String cosUrl;
private CosReference documentReference;
private CosReference resultReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import com.ibm.watsonx.ai.core.Json;
import com.ibm.watsonx.ai.core.factory.HttpClientFactory;
import com.ibm.watsonx.ai.core.http.AsyncHttpClient;
Expand Down Expand Up @@ -74,7 +75,7 @@ public CompletableFuture<TokenizationResponse> asyncTokenize(String transactionI

return asyncHttpClient.send(httpRequest.build(), BodyHandlers.ofString())
.thenApplyAsync(r -> Json.fromJson(r.body(), TokenizationResponse.class), ExecutorProvider.cpuExecutor())
.thenApplyAsync(r -> r, ExecutorProvider.ioExecutor());
.thenApplyAsync(Function.identity(), ExecutorProvider.ioExecutor());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void should_build_request_with_correct_parameters() throws Exception {
.input("input")
.spaceId("new-space-id")
.transactionId("transaction-id")
.detectors(Pii.builder().build())
.detectors(Pii.ofDefaults(), Hap.ofDefaults(), GraniteGuardian.ofDefaults())
.build();

service.detect(request);
Expand All @@ -138,6 +138,8 @@ void should_build_request_with_correct_parameters() throws Exception {
"space_id": "new-space-id",
"detectors": {
"pii": {},
"hap": {},
"granite_guardian": {},
}
}
""",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void should_detect_hap() {
var response = detectionService.detect(
DetectionTextRequest.builder()
.input("I kill you")
.detectors(Pii.create(), Hap.builder().threshold(0.3f).build())
.detectors(Pii.ofDefaults(), Hap.builder().threshold(0.3f).build())
.build()
);

Expand All @@ -59,7 +59,7 @@ void should_detect_pii() {
var response = detectionService.detect(
DetectionTextRequest.builder()
.input("My name is George and my phone number is 1234567890")
.detectors(Pii.create(), Hap.builder().threshold(0.3f).build())
.detectors(Pii.ofDefaults(), Hap.builder().threshold(0.3f).build())
.build()
);

Expand All @@ -77,7 +77,7 @@ void should_detect_hap_and_pii() {
var response = detectionService.detect(
DetectionTextRequest.builder()
.input("I kill you with my phone number 1234567890")
.detectors(Pii.create(), Hap.builder().threshold(0.3f).build())
.detectors(Pii.ofDefaults(), Hap.builder().threshold(0.3f).build())
.build()
);

Expand Down
4 changes: 2 additions & 2 deletions samples/detection/src/main/java/com/ibm/detection/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void main(String[] args) throws Exception {
.build();

DetectionTextRequest request = DetectionTextRequest.builder()
.input("I kill you with my phone number 1234567890")
.detectors(Pii.create(), Hap.builder().threshold(0.3f).build())
.input("I hate you with my phone number 1234567890")
.detectors(Pii.ofDefaults(), Hap.builder().threshold(0.3f).build())
.build();

List<DetectionTextResponse> response = detectionService.detect(request).detections();
Expand Down