Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/main/java/com/mindee/InferenceParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Data
public final class InferenceParameters {
/**
* ID of the model (required).
* Model ID to use for the inference (required).
*/
private final String modelId;
/**
Expand All @@ -36,18 +36,21 @@ public final class InferenceParameters {
*/
private final String alias;
/**
* IDs of webhooks to propagate the API response to (may be empty).
* Webhook IDs to call after all processing is finished. If empty, no webhooks will be used.
*/
private final String[] webhookIds;
/**
* Polling options. Set only if having timeout issues.
*/
private final AsyncPollingOptions pollingOptions;

/**
* Additional text context used by the model during inference. Not recommended, for specific use only.
*/
private final String textContext;
/**
* Dynamic changes to the data schema of the model for this inference.
*/
private final String dataSchema;

/**
* Create a new builder.
Expand All @@ -72,6 +75,7 @@ public static final class Builder {
private String alias;
private String[] webhookIds = new String[]{};
private String textContext;
private String dataSchema;
private AsyncPollingOptions pollingOptions = AsyncPollingOptions.builder().build();

private Builder(String modelId) {
Expand Down Expand Up @@ -123,6 +127,12 @@ public Builder textContext(String textContext) {
return this;
}

/** Provide additional text context used by the model during inference. */
public Builder dataSchema(String dataSchema) {
this.dataSchema = dataSchema;
return this;
}

/** Set polling options. */
public Builder pollingOptions(AsyncPollingOptions pollingOptions) {
this.pollingOptions = pollingOptions;
Expand All @@ -140,7 +150,8 @@ public InferenceParameters build() {
alias,
webhookIds,
pollingOptions,
textContext
textContext,
dataSchema
);
}
}
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/com/mindee/MindeeClientV2.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.mindee;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindee.http.MindeeApiV2;
import com.mindee.http.MindeeHttpApiV2;
import com.mindee.http.MindeeHttpExceptionV2;
import com.mindee.input.LocalInputSource;
import com.mindee.input.LocalResponse;
import com.mindee.input.URLInputSource;
import com.mindee.parsing.v2.ErrorResponse;
import com.mindee.parsing.v2.InferenceResponse;
Expand All @@ -18,18 +16,18 @@
public class MindeeClientV2 {
private final MindeeApiV2 mindeeApi;

/** Uses an API-key read from the environment variables. */
/** Uses an API key read from the environment variables. */
public MindeeClientV2() {
this(createDefaultApiV2(""));
}

/** Uses the supplied API-key. */
/** Uses the supplied API key. */
public MindeeClientV2(String apiKey) {
this(createDefaultApiV2(apiKey));
}


/** Inject both a PDF implementation and a HTTP implementation. */
/** Inject both a PDF implementation and an HTTP implementation. */
public MindeeClientV2(MindeeApiV2 mindeeApi) {
this.mindeeApi = mindeeApi;
}
Expand All @@ -39,7 +37,8 @@ public MindeeClientV2(MindeeApiV2 mindeeApi) {
*/
public JobResponse enqueueInference(
LocalInputSource inputSource,
InferenceParameters params) throws IOException {
InferenceParameters params
) throws IOException {
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
}

Expand All @@ -49,7 +48,8 @@ public JobResponse enqueueInference(
*/
public JobResponse enqueueInference(
URLInputSource inputSource,
InferenceParameters params) throws IOException {
InferenceParameters params
) throws IOException {
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
}

Expand All @@ -72,7 +72,6 @@ public InferenceResponse getInference(String inferenceId) {
if (inferenceId == null || inferenceId.trim().isEmpty()) {
throw new IllegalArgumentException("inferenceId must not be null or blank.");
}

return mindeeApi.reqGetInference(inferenceId);
}

Expand All @@ -86,8 +85,8 @@ public InferenceResponse getInference(String inferenceId) {
*/
public InferenceResponse enqueueAndGetInference(
LocalInputSource inputSource,
InferenceParameters options) throws IOException, InterruptedException {

InferenceParameters options
) throws IOException, InterruptedException {
validatePollingOptions(options.getPollingOptions());
JobResponse job = enqueueInference(inputSource, options);
return pollAndFetch(job, options);
Expand All @@ -105,8 +104,8 @@ public InferenceResponse enqueueAndGetInference(
*/
public InferenceResponse enqueueAndGetInference(
URLInputSource inputSource,
InferenceParameters options) throws IOException, InterruptedException {

InferenceParameters options
) throws IOException, InterruptedException {
validatePollingOptions(options.getPollingOptions());
JobResponse job = enqueueInference(inputSource, options);
return pollAndFetch(job, options);
Expand All @@ -119,8 +118,10 @@ public InferenceResponse enqueueAndGetInference(
* @return an instance of {@link InferenceResponse}.
* @throws InterruptedException Throws if interrupted.
*/
private InferenceResponse pollAndFetch(JobResponse initialJob,
InferenceParameters options) throws InterruptedException {
private InferenceResponse pollAndFetch(
JobResponse initialJob,
InferenceParameters options
) throws InterruptedException {
Thread.sleep((long) (options.getPollingOptions().getInitialDelaySec() * 1000));

JobResponse resp = initialJob;
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/mindee/http/MindeeHttpApiV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,6 @@ private HttpEntity buildHttpBody(
MultipartEntityBuilder builder,
InferenceParameters params
) {
if (params.getTextContext() != null) {
builder.addTextBody(
"text_context",
params.getTextContext().toLowerCase()
);
}

builder.addTextBody("model_id", params.getModelId());
if (params.getRag() != null) {
builder.addTextBody("rag", params.getRag().toString().toLowerCase());
Expand All @@ -276,6 +269,12 @@ private HttpEntity buildHttpBody(
if (params.getWebhookIds().length > 0) {
builder.addTextBody("webhook_ids", String.join(",", params.getWebhookIds()));
}
if (params.getTextContext() != null) {
builder.addTextBody("text_context", params.getTextContext());
}
if (params.getDataSchema() != null) {
builder.addTextBody("data_schema", params.getDataSchema());
}
return builder.build();
}

Expand Down
47 changes: 40 additions & 7 deletions src/test/java/com/mindee/MindeeClientV2IT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.mindee;

import static com.mindee.TestingUtilities.getResourcePath;
import static com.mindee.TestingUtilities.getV2ResourcePath;
import static com.mindee.TestingUtilities.readFileAsString;
import static org.junit.jupiter.api.Assertions.*;

import com.mindee.http.MindeeHttpExceptionV2;
import com.mindee.input.LocalInputSource;
import com.mindee.input.URLInputSource;
Expand All @@ -14,10 +19,6 @@
import java.io.IOException;
import org.junit.jupiter.api.*;

import static com.mindee.TestingUtilities.getResourcePath;
import static com.mindee.TestingUtilities.getV1ResourcePathString;
import static org.junit.jupiter.api.Assertions.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tag("integration")
@DisplayName("MindeeV2 – Integration Tests")
Expand Down Expand Up @@ -45,7 +46,7 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep
.rawText(true)
.polygon(null)
.confidence(null)
.alias("java-integration-test")
.alias("java-integration-test_multipage")
.textContext(null)
.pollingOptions(
AsyncPollingOptions.builder()
Expand Down Expand Up @@ -90,12 +91,12 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep
@DisplayName("Filled, single-page image – enqueue & parse must succeed")
void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedException {
LocalInputSource source = new LocalInputSource(
getV1ResourcePathString("products/financial_document/default_sample.jpg"));
getV2ResourcePath("products/financial_document/default_sample.jpg"));

InferenceParameters params = InferenceParameters
.builder(modelId)
.rag(false)
.alias("java-integration-test")
.alias("java-integration-test_single-page")
.textContext("this is an invoice")
.build();

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

@Test
@DisplayName("Data Schema Replace – enqueue & parse must succeed")
void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedException {
LocalInputSource source = new LocalInputSource(
getV2ResourcePath("products/financial_document/default_sample.jpg"));

InferenceParameters params = InferenceParameters
.builder(modelId)
.rag(false)
.alias("java-integration-test_data-schema-replace")
.dataSchema(readFileAsString(getV2ResourcePath("inference/data_schema_replace_param.json")))
.build();

InferenceResponse response = mindeeClient.enqueueAndGetInference(source, params);
assertNotNull(response);
Inference inference = response.getInference();
assertNotNull(inference);

InferenceResult result = inference.getResult();
assertNotNull(result);

RawText rawText = result.getRawText();
assertNull(rawText);

InferenceFields fields = result.getFields();
assertNotNull(fields);

SimpleField supplierName = fields.getSimpleField("test_replace");
assertNotNull(supplierName);
assertEquals("a test value", supplierName.getStringValue());
}


@Test
@DisplayName("Invalid model ID – enqueue must raise 422")
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/mindee/TestingUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public static String getV1ResourcePathString(String filePath) {
return getV1ResourcePath(filePath).toString();
}

public static String readFileAsString(Path path)
throws IOException
{
byte[] encoded = Files.readAllBytes(path);
return new String(encoded);
}

public static void assertStringEqualsFile(String expected, String filePath) throws IOException {
String[] actualLines = expected.split(System.lineSeparator());
List<String> expectedLines = Files.readAllLines(Paths.get(filePath));
Expand Down
16 changes: 5 additions & 11 deletions src/test/java/com/mindee/parsing/v2/InferenceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.mindee.parsing.v2;

import static com.mindee.TestingUtilities.getV2ResourcePath;
import static com.mindee.TestingUtilities.readFileAsString;
import static org.junit.jupiter.api.Assertions.*;

import com.mindee.geometry.Point;
import com.mindee.geometry.Polygon;
import com.mindee.input.LocalResponse;
Expand All @@ -12,16 +16,13 @@
import com.mindee.parsing.v2.field.ObjectField;
import com.mindee.parsing.v2.field.DynamicField.FieldType;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static com.mindee.TestingUtilities.getV2ResourcePath;
import static org.junit.jupiter.api.Assertions.*;

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

private String readFileAsString(String path)
throws IOException
{
byte[] encoded = Files.readAllBytes(getV2ResourcePath(path));
return new String(encoded);
}


@Nested
@DisplayName("Inference on blank file")
Expand Down Expand Up @@ -525,7 +519,7 @@ class RstDisplay {
@DisplayName("rst display must be parsed and exposed")
void rstDisplay_mustBeAccessible() throws IOException {
InferenceResponse resp = loadInference("inference/standard_field_types.json");
String rstRef = readFileAsString("inference/standard_field_types.rst");
String rstRef = readFileAsString(getV2ResourcePath("inference/standard_field_types.rst"));
Inference inference = resp.getInference();
assertNotNull(inference);
assertEquals(rstRef, resp.getInference().toString());
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources
Loading