Skip to content

Commit 7ed7dd7

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

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-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: 29 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,25 @@ 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((url) -> url.concat("/predict"));
116+
}
117+
118+
if (asyncUrlFromWorkflow != null) {
119+
this.asyncUrlFromWorkflow = asyncUrlFromWorkflow;
120+
} else {
121+
this.asyncUrlFromWorkflow = this.buildWorkflowPredictUrl.andThen((url) -> url.concat("predict_async"));
110122
}
111123

112124
if (asyncUrlFromEndpoint != null) {
113125
this.asyncUrlFromEndpoint = asyncUrlFromEndpoint;
114126
} else {
115-
this.asyncUrlFromEndpoint = this.urlFromEndpoint.andThen((url) -> url.concat("_async"));
127+
this.asyncUrlFromEndpoint = this.buildProductPredictUrl.andThen((url) -> url.concat("predict_async"));
116128
}
117129

118130
if (documentUrlFromEndpoint != null) {
119131
this.documentUrlFromEndpoint = documentUrlFromEndpoint;
120132
} else {
121-
this.documentUrlFromEndpoint = this.buildBaseUrl.andThen(
133+
this.documentUrlFromEndpoint = this.buildProductPredictUrl.andThen(
122134
(url) -> url.concat("/documents/queue/"));
123135
}
124136

@@ -340,7 +352,7 @@ private <ResponseT extends ApiResponse> MindeeHttpException getHttpError(
340352
return new MindeeHttpException(statusCode, message, details, errorCode);
341353
}
342354

343-
private String buildProductUrl(Endpoint endpoint) {
355+
private String buildProductPredictUrl(Endpoint endpoint) {
344356
return this.mindeeSettings.getBaseUrl()
345357
+ "/products/"
346358
+ endpoint.getAccountName()
@@ -350,7 +362,11 @@ private String buildProductUrl(Endpoint endpoint) {
350362
+ endpoint.getVersion();
351363
}
352364

353-
private String buildWorkflowUrl(String workflowId) {
365+
private String buildWorkflowPredictUrl(String workflowId) {
366+
return this.mindeeSettings.getBaseUrl() + "/workflows/" + workflowId;
367+
}
368+
369+
private String buildWorkflowExecutionUrl(String workflowId) {
354370
return this.mindeeSettings.getBaseUrl() + "/workflows/" + workflowId + "/executions";
355371
}
356372

0 commit comments

Comments
 (0)