Skip to content

Commit 7f6572c

Browse files
committed
✨ add RAG search API
1 parent f36c0a3 commit 7f6572c

36 files changed

Lines changed: 879 additions & 138 deletions

‎src/main/java/com/mindee/v2/MindeeClient.java‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
import com.mindee.input.LocalInputSource;
44
import com.mindee.input.URLInputSource;
5-
import com.mindee.v2.clientoptions.BaseParameters;
5+
import com.mindee.v2.clientoptions.BaseProductParameters;
6+
import com.mindee.v2.clientoptions.BaseSearchParameters;
67
import com.mindee.v2.clientoptions.PollingOptions;
78
import com.mindee.v2.http.MindeeApiV2;
89
import com.mindee.v2.http.MindeeHttpApiV2;
910
import com.mindee.v2.http.MindeeHttpExceptionV2;
1011
import com.mindee.v2.parsing.CommonResponse;
1112
import com.mindee.v2.parsing.JobResponse;
1213
import com.mindee.v2.parsing.error.ErrorResponse;
14+
import com.mindee.v2.parsing.search.BaseSearchResponse;
1315
import com.mindee.v2.parsing.search.SearchResponse;
1416
import com.mindee.v2.product.extraction.ExtractionResponse;
1517
import java.io.IOException;
@@ -44,7 +46,7 @@ public MindeeClient(MindeeApiV2 mindeeApi) {
4446
*/
4547
public JobResponse enqueue(
4648
LocalInputSource inputSource,
47-
BaseParameters params
49+
BaseProductParameters params
4850
) throws IOException {
4951
return mindeeApi.reqPostEnqueue(inputSource, params);
5052
}
@@ -55,7 +57,10 @@ public JobResponse enqueue(
5557
* @param inputSource The URL input source to send.
5658
* @param params The parameters to send along with the file.
5759
*/
58-
public JobResponse enqueue(URLInputSource inputSource, BaseParameters params) throws IOException {
60+
public JobResponse enqueue(
61+
URLInputSource inputSource,
62+
BaseProductParameters params
63+
) throws IOException {
5964
inputSource.validateSecure();
6065
return mindeeApi.reqPostEnqueue(inputSource, params);
6166
}
@@ -68,7 +73,7 @@ public JobResponse getJob(String jobId) {
6873
if (jobId == null || jobId.trim().isEmpty()) {
6974
throw new IllegalArgumentException("jobId must not be null or blank.");
7075
}
71-
return mindeeApi.reqGetJob(jobId);
76+
return mindeeApi.reqGetJobById(jobId);
7277
}
7378

7479
/**
@@ -82,7 +87,7 @@ public <TResponse extends CommonResponse> TResponse getResult(
8287
if (inferenceId == null || inferenceId.trim().isEmpty()) {
8388
throw new IllegalArgumentException("inferenceId must not be null or blank.");
8489
}
85-
return mindeeApi.reqGetResult(responseClass, inferenceId);
90+
return mindeeApi.reqGetResultById(responseClass, inferenceId);
8691
}
8792

8893
/**
@@ -96,7 +101,7 @@ public <TResponse extends CommonResponse> TResponse getResultFromUrl(
96101
if (inferenceUrl == null || inferenceUrl.trim().isEmpty()) {
97102
throw new IllegalArgumentException("inferenceUrl must not be null or blank.");
98103
}
99-
return mindeeApi.reqGetResultFromUrl(responseClass, inferenceUrl);
104+
return mindeeApi.reqGetResultByUrl(responseClass, inferenceUrl);
100105
}
101106

102107
/**
@@ -112,7 +117,7 @@ public <TResponse extends CommonResponse> TResponse getResultFromUrl(
112117
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
113118
Class<TResponse> responseClass,
114119
LocalInputSource inputSource,
115-
BaseParameters params
120+
BaseProductParameters params
116121
) throws IOException, InterruptedException {
117122
return enqueueAndGetResult(
118123
responseClass,
@@ -136,7 +141,7 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
136141
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
137142
Class<TResponse> responseClass,
138143
LocalInputSource inputSource,
139-
BaseParameters params,
144+
BaseProductParameters params,
140145
PollingOptions pollingOptions
141146
) throws IOException, InterruptedException {
142147
JobResponse job = enqueue(inputSource, params);
@@ -156,7 +161,7 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
156161
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
157162
Class<TResponse> responseClass,
158163
URLInputSource inputSource,
159-
BaseParameters params
164+
BaseProductParameters params
160165
) throws IOException, InterruptedException {
161166
return enqueueAndGetResult(
162167
responseClass,
@@ -180,19 +185,33 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
180185
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
181186
Class<TResponse> responseClass,
182187
URLInputSource inputSource,
183-
BaseParameters params,
188+
BaseProductParameters params,
184189
PollingOptions pollingOptions
185190
) throws IOException, InterruptedException {
186191
inputSource.validateSecure();
187192
JobResponse job = enqueue(inputSource, params);
188193
return pollAndFetch(responseClass, job, pollingOptions);
189194
}
190195

196+
/**
197+
* Search for resources matching the given criteria.
198+
*
199+
* @param searchParameters Search parameters
200+
*/
201+
public <TSearchResponse extends BaseSearchResponse> TSearchResponse search(
202+
Class<TSearchResponse> responseClass,
203+
BaseSearchParameters searchParameters
204+
) {
205+
return mindeeApi.search(responseClass, searchParameters);
206+
}
207+
191208
/**
192209
* Return all models.
193210
*
194211
* @return an instance of {@link SearchResponse}
212+
* @deprecated Use {@link #search} instead.
195213
*/
214+
@Deprecated
196215
public SearchResponse searchModels() {
197216
return searchModels(null, null);
198217
}
@@ -202,7 +221,9 @@ public SearchResponse searchModels() {
202221
*
203222
* @param modelName name of the model to search for
204223
* @return an instance of {@link SearchResponse}
224+
* @deprecated Use {@link #search} instead.
205225
*/
226+
@Deprecated
206227
public SearchResponse searchModels(String modelName) {
207228
return searchModels(modelName, null);
208229
}
@@ -213,7 +234,9 @@ public SearchResponse searchModels(String modelName) {
213234
* @param modelName name of the model to search for
214235
* @param modelType type of the model to search for
215236
* @return an instance of {@link SearchResponse}
237+
* @deprecated Use {@link #search} instead.
216238
*/
239+
@Deprecated
217240
public SearchResponse searchModels(String modelName, String modelType) {
218241
return mindeeApi.reqGetSearchModels(modelName, modelType);
219242
}

src/main/java/com/mindee/v2/clientoptions/BaseParameters.java renamed to src/main/java/com/mindee/v2/clientoptions/BaseProductParameters.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.mindee.v2.clientoptions;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
35
import java.util.Objects;
46
import lombok.Data;
5-
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
67

78
@Data
8-
public abstract class BaseParameters {
9+
public abstract class BaseProductParameters {
910
/**
10-
* Model ID to use for the inference (required).
11+
* Model ID to use for the inference. Required.
1112
*/
1213
protected final String modelId;
1314
/**
14-
* Optional alias for the file.
15+
* Optional: a free-form string to tag the request with your own identifier.
16+
* For example, an internal document ID, reference number, or database key.
17+
* If set, it will be included in the job and result responses.
1518
*/
1619
protected final String alias;
1720
/**
@@ -20,15 +23,19 @@ public abstract class BaseParameters {
2023
*/
2124
protected final String[] webhookIds;
2225

23-
public MultipartEntityBuilder buildHttpBody(MultipartEntityBuilder builder) {
24-
builder.addTextBody("model_id", this.getModelId());
25-
if (this.getAlias() != null) {
26-
builder.addTextBody("alias", this.getAlias());
26+
public Map<String, String> getRequestParameters() {
27+
var parameters = new HashMap<String, String>();
28+
29+
parameters.put("model_id", this.getModelId());
30+
31+
if (this.getAlias() != null && !this.getAlias().isBlank()) {
32+
parameters.put("alias", getAlias());
2733
}
2834
if (this.getWebhookIds().length > 0) {
29-
builder.addTextBody("webhook_ids", String.join(",", this.getWebhookIds()));
35+
parameters.put("webhook_ids", String.join(",", this.getWebhookIds()));
3036
}
31-
return builder;
37+
38+
return parameters;
3239
}
3340

3441
protected static abstract class BaseBuilder<T extends BaseBuilder<T>> {
@@ -42,7 +49,8 @@ protected T self() {
4249
}
4350

4451
protected BaseBuilder(String modelId) {
45-
this.modelId = Objects.requireNonNull(modelId, "modelId must not be null");
52+
this.modelId = Objects
53+
.requireNonNull(modelId, "The model ID is required in product parameters");
4654
}
4755

4856
/** Set an alias for the uploaded document. */
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.mindee.v2.clientoptions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import lombok.Data;
6+
7+
/**
8+
* Base parameters for searches.
9+
*/
10+
@Data
11+
public abstract class BaseSearchParameters {
12+
/**
13+
* 1-based page index.
14+
*/
15+
protected final Integer page;
16+
/**
17+
* Number of items per page.
18+
*/
19+
protected final Integer perPage;
20+
21+
/**
22+
* Gets the request parameters for the search request.
23+
*/
24+
public Map<String, String> getRequestParameters() {
25+
var parameters = new HashMap<String, String>();
26+
27+
if (this.getPage() != null && this.getPage() > 0) {
28+
parameters.put("page", String.valueOf(getPage()));
29+
}
30+
if (this.getPerPage() != null && this.getPerPage() > 0) {
31+
parameters.put("per_page", String.valueOf(getPerPage()));
32+
}
33+
34+
return parameters;
35+
}
36+
37+
protected static abstract class BaseBuilder<T extends BaseBuilder<T>> {
38+
protected Integer page;
39+
protected Integer perPage;
40+
41+
@SuppressWarnings("unchecked")
42+
protected T self() {
43+
return (T) this;
44+
}
45+
46+
protected BaseBuilder() {
47+
}
48+
49+
/**
50+
* 1-based page index.
51+
*/
52+
public T page(Integer page) {
53+
this.page = page;
54+
return self();
55+
}
56+
57+
/**
58+
* Number of items per page.
59+
*/
60+
public T perPage(Integer perPage) {
61+
this.perPage = perPage;
62+
return self();
63+
}
64+
}
65+
66+
}

‎src/main/java/com/mindee/v2/http/MindeeApiV2.java‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import com.mindee.http.MindeeApiCommon;
55
import com.mindee.input.LocalInputSource;
66
import com.mindee.input.URLInputSource;
7-
import com.mindee.v2.clientoptions.BaseParameters;
7+
import com.mindee.v2.clientoptions.BaseProductParameters;
8+
import com.mindee.v2.clientoptions.BaseSearchParameters;
89
import com.mindee.v2.parsing.CommonResponse;
910
import com.mindee.v2.parsing.JobResponse;
1011
import com.mindee.v2.parsing.error.ErrorResponse;
12+
import com.mindee.v2.parsing.search.BaseSearchResponse;
1113
import com.mindee.v2.parsing.search.SearchResponse;
14+
import com.mindee.v2.product.ProductAttributes;
1215
import java.io.IOException;
1316

1417
/**
@@ -23,7 +26,7 @@ public abstract class MindeeApiV2 extends MindeeApiCommon {
2326
*/
2427
public abstract JobResponse reqPostEnqueue(
2528
LocalInputSource inputSource,
26-
BaseParameters options
29+
BaseProductParameters options
2730
) throws IOException;
2831

2932
/**
@@ -34,22 +37,22 @@ public abstract JobResponse reqPostEnqueue(
3437
*/
3538
public abstract JobResponse reqPostEnqueue(
3639
URLInputSource inputSource,
37-
BaseParameters options
40+
BaseProductParameters options
3841
) throws IOException;
3942

4043
/**
4144
* Attempts to poll the queue.
4245
*
4346
* @param jobId id of the job to get.
4447
*/
45-
public abstract JobResponse reqGetJob(String jobId);
48+
public abstract JobResponse reqGetJobById(String jobId);
4649

4750
/**
4851
* Retrieves the inference from a 302 redirect.
4952
*
5053
* @param inferenceId ID of the inference to poll.
5154
*/
52-
public abstract <TResponse extends CommonResponse> TResponse reqGetResult(
55+
public abstract <TResponse extends CommonResponse> TResponse reqGetResultById(
5356
Class<TResponse> responseClass,
5457
String inferenceId
5558
);
@@ -58,17 +61,26 @@ public abstract <TResponse extends CommonResponse> TResponse reqGetResult(
5861
* Retrieves the inference from a given URL.
5962
* The inference will only be available after it has finished processing.
6063
*/
61-
public abstract <TResponse extends CommonResponse> TResponse reqGetResultFromUrl(
64+
public abstract <TResponse extends CommonResponse> TResponse reqGetResultByUrl(
6265
Class<TResponse> responseClass,
6366
String inferenceUrl
6467
);
6568

69+
/**
70+
* Retrieves a list of resources with the given criteria.
71+
*/
72+
public abstract <TSearchResponse extends BaseSearchResponse> TSearchResponse search(
73+
Class<TSearchResponse> responseClass,
74+
BaseSearchParameters searchParameters
75+
);
76+
6677
/**
6778
* Retrieves a list of models.
6879
*
6980
* @param modelName search term for model name
7081
* @param modelType search term for model type
7182
*/
83+
@Deprecated
7284
public abstract SearchResponse reqGetSearchModels(String modelName, String modelType);
7385

7486
/**
@@ -84,8 +96,10 @@ protected ErrorResponse makeUnknownError(int statusCode) {
8496
);
8597
}
8698

87-
protected ProductInfo getResponseProductInfo(Class<? extends CommonResponse> responseClass) {
88-
var productInfo = responseClass.getAnnotation(ProductInfo.class);
99+
protected ProductAttributes getResponseProductInfo(
100+
Class<? extends CommonResponse> responseClass
101+
) {
102+
var productInfo = responseClass.getAnnotation(ProductAttributes.class);
89103
if (productInfo == null) {
90104
throw new MindeeException(
91105
"The class " + responseClass.getSimpleName() + " is not annotated with @ProductInfo"
@@ -94,8 +108,10 @@ protected ProductInfo getResponseProductInfo(Class<? extends CommonResponse> res
94108
return productInfo;
95109
}
96110

97-
protected ProductInfo getParamsProductInfo(Class<? extends BaseParameters> responseClass) {
98-
var productInfo = responseClass.getAnnotation(ProductInfo.class);
111+
protected ProductAttributes getParamsProductInfo(
112+
Class<? extends BaseProductParameters> responseClass
113+
) {
114+
var productInfo = responseClass.getAnnotation(ProductAttributes.class);
99115
if (productInfo == null) {
100116
throw new MindeeException(
101117
"The class " + responseClass.getSimpleName() + " is not annotated with @ProductInfo"

0 commit comments

Comments
 (0)