|
| 1 | +/* |
| 2 | + * Copyright IBM Corp. 2025 - 2025 |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package com.ibm.detection; |
| 6 | + |
| 7 | +import java.net.URI; |
| 8 | +import java.time.Duration; |
| 9 | +import java.util.List; |
| 10 | +import org.eclipse.microprofile.config.Config; |
| 11 | +import org.eclipse.microprofile.config.ConfigProvider; |
| 12 | +import com.ibm.watsonx.ai.detection.DetectionService; |
| 13 | +import com.ibm.watsonx.ai.detection.DetectionTextRequest; |
| 14 | +import com.ibm.watsonx.ai.detection.DetectionTextResponse; |
| 15 | +import com.ibm.watsonx.ai.detection.detector.Hap; |
| 16 | +import com.ibm.watsonx.ai.detection.detector.Pii; |
| 17 | + |
| 18 | +public class App { |
| 19 | + |
| 20 | + private static final Config config = ConfigProvider.getConfig(); |
| 21 | + |
| 22 | + public static void main(String[] args) throws Exception { |
| 23 | + |
| 24 | + try { |
| 25 | + |
| 26 | + var url = URI.create(config.getValue("WATSONX_URL", String.class)); |
| 27 | + var apiKey = config.getValue("WATSONX_API_KEY", String.class); |
| 28 | + var projectId = config.getValue("WATSONX_PROJECT_ID", String.class); |
| 29 | + |
| 30 | + DetectionService detectionService = DetectionService.builder() |
| 31 | + .apiKey(apiKey) |
| 32 | + .projectId(projectId) |
| 33 | + .timeout(Duration.ofSeconds(60)) |
| 34 | + .baseUrl(url) |
| 35 | + .build(); |
| 36 | + |
| 37 | + DetectionTextRequest request = DetectionTextRequest.builder() |
| 38 | + .input("I kill you with my phone number 1234567890") |
| 39 | + .detectors(Pii.create(), Hap.builder().threshold(0.3f).build()) |
| 40 | + .build(); |
| 41 | + |
| 42 | + List<DetectionTextResponse> response = detectionService.detect(request).detections(); |
| 43 | + |
| 44 | + System.out.println(response); |
| 45 | + System.exit(0); |
| 46 | + |
| 47 | + } catch (Exception e) { |
| 48 | + e.printStackTrace(); |
| 49 | + } finally { |
| 50 | + System.exit(0); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments