Skip to content

Commit 6672852

Browse files
committed
✨ add support for workflow polling
1 parent dda75f5 commit 6672852

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/main/java/com/mindee/PredictOptions.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,28 @@ public class PredictOptions {
2424
* size.
2525
*/
2626
Boolean fullText;
27+
/**
28+
* If set, will enqueue to a workflow queue instead of a product's endpoint.
29+
*/
30+
String workflowId;
31+
/**
32+
* If set, will enable Retrieval-Augmented Generation.
33+
* Only works if a valid workflowId is set.
34+
*/
35+
Boolean rag;
2736

2837
@Builder
2938
private PredictOptions(
3039
Boolean allWords,
3140
Boolean fullText,
32-
Boolean cropper
41+
Boolean cropper,
42+
String workflowId,
43+
Boolean rag
3344
) {
3445
this.allWords = allWords == null ? Boolean.FALSE : allWords;
3546
this.fullText = fullText == null ? Boolean.FALSE : fullText;
3647
this.cropper = cropper == null ? Boolean.FALSE : cropper;
48+
this.workflowId = workflowId;
49+
this.rag = rag == null ? Boolean.FALSE : rag;
3750
}
3851
}

src/main/java/com/mindee/http/MindeeHttpApi.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
public final class MindeeHttpApi extends MindeeApi {
4242

4343
private static final ObjectMapper mapper = new ObjectMapper();
44-
private final Function<Endpoint, String> buildBaseUrl = this::buildProductUrl;
45-
private final Function<String, String> buildWorkflowBaseUrl = this::buildWorkflowUrl;
44+
private final Function<Endpoint, String> buildProductPredictUrl = this::buildProductPredictUrl;
45+
private final Function<String, String> buildWorkflowPredictUrl = this::buildWorkflowPredictUrl;
46+
private final Function<String, String> buildWorkflowBaseUrl = this::buildWorkflowExecutionUrl;
4647
/**
4748
* The MindeeSetting needed to make the api call.
4849
*/
@@ -53,24 +54,27 @@ public final class MindeeHttpApi extends MindeeApi {
5354
*/
5455
private final HttpClientBuilder httpClientBuilder;
5556
/**
56-
* The function used to generate the API endpoint URL.
57+
* The function used to generate the synchronous API endpoint URL.
5758
* Only needs to be set if the api calls need to be directed through internal URLs.
5859
*/
5960
private final Function<Endpoint, String> urlFromEndpoint;
60-
6161
/**
62-
* The function used to generate the API endpoint URL for workflow execution calls.
62+
* The function used to generate the asynchronous API endpoint URL.
6363
* Only needs to be set if the api calls need to be directed through internal URLs.
6464
*/
6565
private final Function<Endpoint, String> asyncUrlFromEndpoint;
66+
/**
67+
* The function used to generate the asynchronous API endpoint URL.
68+
* Only needs to be set if the api calls need to be directed through internal URLs.
69+
*/
70+
private final Function<String, String> asyncUrlFromWorkflow;
6671
/**
6772
* The function used to generate the Job status URL for Async calls.
6873
* Only needs to be set if the api calls need to be directed through internal URLs.
6974
*/
7075
private final Function<Endpoint, String> documentUrlFromEndpoint;
71-
7276
/**
73-
* The function used to generate the Job status URL for Async calls.
77+
* The function used to generate the Job status URL for workflow execution calls.
7478
* Only needs to be set if the api calls need to be directed through internal URLs.
7579
*/
7680
private final Function<String, String> workflowUrlFromId;
@@ -82,6 +86,7 @@ public MindeeHttpApi(MindeeSettings mindeeSettings) {
8286
null,
8387
null,
8488
null,
89+
null,
8590
null
8691
);
8792
}
@@ -93,7 +98,8 @@ private MindeeHttpApi(
9398
Function<Endpoint, String> urlFromEndpoint,
9499
Function<Endpoint, String> asyncUrlFromEndpoint,
95100
Function<Endpoint, String> documentUrlFromEndpoint,
96-
Function<String, String> workflowUrlFromEndpoint
101+
Function<String, String> workflowUrlFromEndpoint,
102+
Function<String, String> asyncUrlFromWorkflow
97103
) {
98104
this.mindeeSettings = mindeeSettings;
99105

@@ -106,19 +112,28 @@ private MindeeHttpApi(
106112
if (urlFromEndpoint != null) {
107113
this.urlFromEndpoint = urlFromEndpoint;
108114
} else {
109-
this.urlFromEndpoint = buildBaseUrl.andThen((url) -> url.concat("/predict"));
115+
this.urlFromEndpoint = buildProductPredictUrl.andThen(
116+
(url) -> url.concat("/predict"));
117+
}
118+
119+
if (asyncUrlFromWorkflow != null) {
120+
this.asyncUrlFromWorkflow = asyncUrlFromWorkflow;
121+
} else {
122+
this.asyncUrlFromWorkflow = this.buildWorkflowPredictUrl.andThen(
123+
(url) -> url.concat("/predict_async"));
110124
}
111125

112126
if (asyncUrlFromEndpoint != null) {
113127
this.asyncUrlFromEndpoint = asyncUrlFromEndpoint;
114128
} else {
115-
this.asyncUrlFromEndpoint = this.urlFromEndpoint.andThen((url) -> url.concat("_async"));
129+
this.asyncUrlFromEndpoint = this.buildProductPredictUrl.andThen(
130+
(url) -> url.concat("/predict_async"));
116131
}
117132

118133
if (documentUrlFromEndpoint != null) {
119134
this.documentUrlFromEndpoint = documentUrlFromEndpoint;
120135
} else {
121-
this.documentUrlFromEndpoint = this.buildBaseUrl.andThen(
136+
this.documentUrlFromEndpoint = this.buildProductPredictUrl.andThen(
122137
(url) -> url.concat("/documents/queue/"));
123138
}
124139

@@ -340,7 +355,7 @@ private <ResponseT extends ApiResponse> MindeeHttpException getHttpError(
340355
return new MindeeHttpException(statusCode, message, details, errorCode);
341356
}
342357

343-
private String buildProductUrl(Endpoint endpoint) {
358+
private String buildProductPredictUrl(Endpoint endpoint) {
344359
return this.mindeeSettings.getBaseUrl()
345360
+ "/products/"
346361
+ endpoint.getAccountName()
@@ -350,7 +365,11 @@ private String buildProductUrl(Endpoint endpoint) {
350365
+ endpoint.getVersion();
351366
}
352367

353-
private String buildWorkflowUrl(String workflowId) {
368+
private String buildWorkflowPredictUrl(String workflowId) {
369+
return this.mindeeSettings.getBaseUrl() + "/workflows/" + workflowId;
370+
}
371+
372+
private String buildWorkflowExecutionUrl(String workflowId) {
354373
return this.mindeeSettings.getBaseUrl() + "/workflows/" + workflowId + "/executions";
355374
}
356375

0 commit comments

Comments
 (0)