Skip to content

Commit 3d8f7fa

Browse files
authored
✨ add data-schema replace to v2 (#287)
1 parent 9ea511e commit 3d8f7fa

File tree

7 files changed

+89
-44
lines changed

7 files changed

+89
-44
lines changed

src/main/java/com/mindee/InferenceParameters.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@Data
1212
public final class InferenceParameters {
1313
/**
14-
* ID of the model (required).
14+
* Model ID to use for the inference (required).
1515
*/
1616
private final String modelId;
1717
/**
@@ -36,18 +36,21 @@ public final class InferenceParameters {
3636
*/
3737
private final String alias;
3838
/**
39-
* IDs of webhooks to propagate the API response to (may be empty).
39+
* Webhook IDs to call after all processing is finished. If empty, no webhooks will be used.
4040
*/
4141
private final String[] webhookIds;
4242
/**
4343
* Polling options. Set only if having timeout issues.
4444
*/
4545
private final AsyncPollingOptions pollingOptions;
46-
4746
/**
4847
* Additional text context used by the model during inference. Not recommended, for specific use only.
4948
*/
5049
private final String textContext;
50+
/**
51+
* Dynamic changes to the data schema of the model for this inference.
52+
*/
53+
private final String dataSchema;
5154

5255
/**
5356
* Create a new builder.
@@ -72,6 +75,7 @@ public static final class Builder {
7275
private String alias;
7376
private String[] webhookIds = new String[]{};
7477
private String textContext;
78+
private String dataSchema;
7579
private AsyncPollingOptions pollingOptions = AsyncPollingOptions.builder().build();
7680

7781
private Builder(String modelId) {
@@ -123,6 +127,12 @@ public Builder textContext(String textContext) {
123127
return this;
124128
}
125129

130+
/** Provide additional text context used by the model during inference. */
131+
public Builder dataSchema(String dataSchema) {
132+
this.dataSchema = dataSchema;
133+
return this;
134+
}
135+
126136
/** Set polling options. */
127137
public Builder pollingOptions(AsyncPollingOptions pollingOptions) {
128138
this.pollingOptions = pollingOptions;
@@ -140,7 +150,8 @@ public InferenceParameters build() {
140150
alias,
141151
webhookIds,
142152
pollingOptions,
143-
textContext
153+
textContext,
154+
dataSchema
144155
);
145156
}
146157
}

src/main/java/com/mindee/MindeeClientV2.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.mindee;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import com.mindee.http.MindeeApiV2;
54
import com.mindee.http.MindeeHttpApiV2;
65
import com.mindee.http.MindeeHttpExceptionV2;
76
import com.mindee.input.LocalInputSource;
8-
import com.mindee.input.LocalResponse;
97
import com.mindee.input.URLInputSource;
108
import com.mindee.parsing.v2.ErrorResponse;
119
import com.mindee.parsing.v2.InferenceResponse;
@@ -18,18 +16,18 @@
1816
public class MindeeClientV2 {
1917
private final MindeeApiV2 mindeeApi;
2018

21-
/** Uses an API-key read from the environment variables. */
19+
/** Uses an API key read from the environment variables. */
2220
public MindeeClientV2() {
2321
this(createDefaultApiV2(""));
2422
}
2523

26-
/** Uses the supplied API-key. */
24+
/** Uses the supplied API key. */
2725
public MindeeClientV2(String apiKey) {
2826
this(createDefaultApiV2(apiKey));
2927
}
3028

3129

32-
/** Inject both a PDF implementation and a HTTP implementation. */
30+
/** Inject both a PDF implementation and an HTTP implementation. */
3331
public MindeeClientV2(MindeeApiV2 mindeeApi) {
3432
this.mindeeApi = mindeeApi;
3533
}
@@ -39,7 +37,8 @@ public MindeeClientV2(MindeeApiV2 mindeeApi) {
3937
*/
4038
public JobResponse enqueueInference(
4139
LocalInputSource inputSource,
42-
InferenceParameters params) throws IOException {
40+
InferenceParameters params
41+
) throws IOException {
4342
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
4443
}
4544

@@ -49,7 +48,8 @@ public JobResponse enqueueInference(
4948
*/
5049
public JobResponse enqueueInference(
5150
URLInputSource inputSource,
52-
InferenceParameters params) throws IOException {
51+
InferenceParameters params
52+
) throws IOException {
5353
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
5454
}
5555

@@ -72,7 +72,6 @@ public InferenceResponse getInference(String inferenceId) {
7272
if (inferenceId == null || inferenceId.trim().isEmpty()) {
7373
throw new IllegalArgumentException("inferenceId must not be null or blank.");
7474
}
75-
7675
return mindeeApi.reqGetInference(inferenceId);
7776
}
7877

@@ -86,8 +85,8 @@ public InferenceResponse getInference(String inferenceId) {
8685
*/
8786
public InferenceResponse enqueueAndGetInference(
8887
LocalInputSource inputSource,
89-
InferenceParameters options) throws IOException, InterruptedException {
90-
88+
InferenceParameters options
89+
) throws IOException, InterruptedException {
9190
validatePollingOptions(options.getPollingOptions());
9291
JobResponse job = enqueueInference(inputSource, options);
9392
return pollAndFetch(job, options);
@@ -105,8 +104,8 @@ public InferenceResponse enqueueAndGetInference(
105104
*/
106105
public InferenceResponse enqueueAndGetInference(
107106
URLInputSource inputSource,
108-
InferenceParameters options) throws IOException, InterruptedException {
109-
107+
InferenceParameters options
108+
) throws IOException, InterruptedException {
110109
validatePollingOptions(options.getPollingOptions());
111110
JobResponse job = enqueueInference(inputSource, options);
112111
return pollAndFetch(job, options);
@@ -119,8 +118,10 @@ public InferenceResponse enqueueAndGetInference(
119118
* @return an instance of {@link InferenceResponse}.
120119
* @throws InterruptedException Throws if interrupted.
121120
*/
122-
private InferenceResponse pollAndFetch(JobResponse initialJob,
123-
InferenceParameters options) throws InterruptedException {
121+
private InferenceResponse pollAndFetch(
122+
JobResponse initialJob,
123+
InferenceParameters options
124+
) throws InterruptedException {
124125
Thread.sleep((long) (options.getPollingOptions().getInitialDelaySec() * 1000));
125126

126127
JobResponse resp = initialJob;

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,6 @@ private HttpEntity buildHttpBody(
250250
MultipartEntityBuilder builder,
251251
InferenceParameters params
252252
) {
253-
if (params.getTextContext() != null) {
254-
builder.addTextBody(
255-
"text_context",
256-
params.getTextContext().toLowerCase()
257-
);
258-
}
259-
260253
builder.addTextBody("model_id", params.getModelId());
261254
if (params.getRag() != null) {
262255
builder.addTextBody("rag", params.getRag().toString().toLowerCase());
@@ -276,6 +269,12 @@ private HttpEntity buildHttpBody(
276269
if (params.getWebhookIds().length > 0) {
277270
builder.addTextBody("webhook_ids", String.join(",", params.getWebhookIds()));
278271
}
272+
if (params.getTextContext() != null) {
273+
builder.addTextBody("text_context", params.getTextContext());
274+
}
275+
if (params.getDataSchema() != null) {
276+
builder.addTextBody("data_schema", params.getDataSchema());
277+
}
279278
return builder.build();
280279
}
281280

src/test/java/com/mindee/MindeeClientV2IT.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.mindee;
22

3+
import static com.mindee.TestingUtilities.getResourcePath;
4+
import static com.mindee.TestingUtilities.getV2ResourcePath;
5+
import static com.mindee.TestingUtilities.readFileAsString;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
38
import com.mindee.http.MindeeHttpExceptionV2;
49
import com.mindee.input.LocalInputSource;
510
import com.mindee.input.URLInputSource;
@@ -14,10 +19,6 @@
1419
import java.io.IOException;
1520
import org.junit.jupiter.api.*;
1621

17-
import static com.mindee.TestingUtilities.getResourcePath;
18-
import static com.mindee.TestingUtilities.getV1ResourcePathString;
19-
import static org.junit.jupiter.api.Assertions.*;
20-
2122
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2223
@Tag("integration")
2324
@DisplayName("MindeeV2 – Integration Tests")
@@ -45,7 +46,7 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep
4546
.rawText(true)
4647
.polygon(null)
4748
.confidence(null)
48-
.alias("java-integration-test")
49+
.alias("java-integration-test_multipage")
4950
.textContext(null)
5051
.pollingOptions(
5152
AsyncPollingOptions.builder()
@@ -90,12 +91,12 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep
9091
@DisplayName("Filled, single-page image – enqueue & parse must succeed")
9192
void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedException {
9293
LocalInputSource source = new LocalInputSource(
93-
getV1ResourcePathString("products/financial_document/default_sample.jpg"));
94+
getV2ResourcePath("products/financial_document/default_sample.jpg"));
9495

9596
InferenceParameters params = InferenceParameters
9697
.builder(modelId)
9798
.rag(false)
98-
.alias("java-integration-test")
99+
.alias("java-integration-test_single-page")
99100
.textContext("this is an invoice")
100101
.build();
101102

@@ -133,6 +134,38 @@ void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedExc
133134
assertEquals("John Smith", supplierName.getStringValue());
134135
}
135136

137+
@Test
138+
@DisplayName("Data Schema Replace – enqueue & parse must succeed")
139+
void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedException {
140+
LocalInputSource source = new LocalInputSource(
141+
getV2ResourcePath("products/financial_document/default_sample.jpg"));
142+
143+
InferenceParameters params = InferenceParameters
144+
.builder(modelId)
145+
.rag(false)
146+
.alias("java-integration-test_data-schema-replace")
147+
.dataSchema(readFileAsString(getV2ResourcePath("inference/data_schema_replace_param.json")))
148+
.build();
149+
150+
InferenceResponse response = mindeeClient.enqueueAndGetInference(source, params);
151+
assertNotNull(response);
152+
Inference inference = response.getInference();
153+
assertNotNull(inference);
154+
155+
InferenceResult result = inference.getResult();
156+
assertNotNull(result);
157+
158+
RawText rawText = result.getRawText();
159+
assertNull(rawText);
160+
161+
InferenceFields fields = result.getFields();
162+
assertNotNull(fields);
163+
164+
SimpleField supplierName = fields.getSimpleField("test_replace");
165+
assertNotNull(supplierName);
166+
assertEquals("a test value", supplierName.getStringValue());
167+
}
168+
136169

137170
@Test
138171
@DisplayName("Invalid model ID – enqueue must raise 422")

src/test/java/com/mindee/TestingUtilities.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public static String getV1ResourcePathString(String filePath) {
2626
return getV1ResourcePath(filePath).toString();
2727
}
2828

29+
public static String readFileAsString(Path path)
30+
throws IOException
31+
{
32+
byte[] encoded = Files.readAllBytes(path);
33+
return new String(encoded);
34+
}
35+
2936
public static void assertStringEqualsFile(String expected, String filePath) throws IOException {
3037
String[] actualLines = expected.split(System.lineSeparator());
3138
List<String> expectedLines = Files.readAllLines(Paths.get(filePath));

src/test/java/com/mindee/parsing/v2/InferenceTest.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.mindee.parsing.v2;
22

3+
import static com.mindee.TestingUtilities.getV2ResourcePath;
4+
import static com.mindee.TestingUtilities.readFileAsString;
5+
import static org.junit.jupiter.api.Assertions.*;
6+
37
import com.mindee.geometry.Point;
48
import com.mindee.geometry.Polygon;
59
import com.mindee.input.LocalResponse;
@@ -12,16 +16,13 @@
1216
import com.mindee.parsing.v2.field.ObjectField;
1317
import com.mindee.parsing.v2.field.DynamicField.FieldType;
1418
import java.io.IOException;
15-
import java.nio.file.Files;
1619
import java.util.HashMap;
1720
import java.util.LinkedHashMap;
1821
import java.util.List;
1922
import java.util.Map;
2023
import org.junit.jupiter.api.DisplayName;
2124
import org.junit.jupiter.api.Nested;
2225
import org.junit.jupiter.api.Test;
23-
import static com.mindee.TestingUtilities.getV2ResourcePath;
24-
import static org.junit.jupiter.api.Assertions.*;
2526

2627
@DisplayName("MindeeV2 - Inference Tests")
2728
class InferenceTest {
@@ -31,13 +32,6 @@ private InferenceResponse loadInference(String filePath) throws IOException {
3132
return localResponse.deserializeResponse(InferenceResponse.class);
3233
}
3334

34-
private String readFileAsString(String path)
35-
throws IOException
36-
{
37-
byte[] encoded = Files.readAllBytes(getV2ResourcePath(path));
38-
return new String(encoded);
39-
}
40-
4135

4236
@Nested
4337
@DisplayName("Inference on blank file")
@@ -525,7 +519,7 @@ class RstDisplay {
525519
@DisplayName("rst display must be parsed and exposed")
526520
void rstDisplay_mustBeAccessible() throws IOException {
527521
InferenceResponse resp = loadInference("inference/standard_field_types.json");
528-
String rstRef = readFileAsString("inference/standard_field_types.rst");
522+
String rstRef = readFileAsString(getV2ResourcePath("inference/standard_field_types.rst"));
529523
Inference inference = resp.getInference();
530524
assertNotNull(inference);
531525
assertEquals(rstRef, resp.getInference().toString());

src/test/resources

0 commit comments

Comments
 (0)