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
75 changes: 52 additions & 23 deletions src/main/java/com/mindee/InferenceParameters.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.mindee;

import com.mindee.v2.clientOptions.BaseParameters;
import java.util.Objects;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;

/**
* Options to pass when calling methods using the API V2.
*/
@Getter
@Data
public final class InferenceParameters {
/**
* Model ID to use for the inference (required).
*/
private final String modelId;
@EqualsAndHashCode(callSuper = true)
public final class InferenceParameters extends BaseParameters {
/**
* Enhance extraction accuracy with Retrieval-Augmented Generation.
*/
Expand All @@ -31,19 +29,6 @@ public final class InferenceParameters {
* Calculate confidence scores for all fields.
*/
private final Boolean confidence;
/**
* Optional alias for the file.
*/
private final String alias;
/**
* 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.
Expand All @@ -54,6 +39,50 @@ public final class InferenceParameters {
*/
private final String dataSchema;

private InferenceParameters(
String modelId,
String alias,
String[] webhookIds,
AsyncPollingOptions pollingOptions,
Boolean rag,
Boolean rawText,
Boolean polygon,
Boolean confidence,
String textContext,
String dataSchema
) {
super(modelId, alias, webhookIds, pollingOptions);
this.rag = rag;
this.rawText = rawText;
this.polygon = polygon;
this.confidence = confidence;
this.textContext = textContext;
this.dataSchema = dataSchema;
}

public MultipartEntityBuilder buildHttpBody(MultipartEntityBuilder builder) {
builder = super.buildHttpBody(builder);
if (this.getRag() != null) {
builder.addTextBody("rag", this.getRag().toString().toLowerCase());
}
if (this.getRawText() != null) {
builder.addTextBody("raw_text", this.getRawText().toString().toLowerCase());
}
if (this.getPolygon() != null) {
builder.addTextBody("polygon", this.getPolygon().toString().toLowerCase());
}
if (this.getConfidence() != null) {
builder.addTextBody("confidence", this.getConfidence().toString().toLowerCase());
}
if (this.getTextContext() != null) {
builder.addTextBody("text_context", this.getTextContext());
}
if (this.getDataSchema() != null) {
builder.addTextBody("data_schema", this.getDataSchema());
}
return builder;
}

/**
* Create a new builder.
*
Expand Down Expand Up @@ -145,13 +174,13 @@ public Builder pollingOptions(AsyncPollingOptions pollingOptions) {
public InferenceParameters build() {
return new InferenceParameters(
modelId,
alias,
webhookIds,
pollingOptions,
rag,
rawText,
polygon,
confidence,
alias,
webhookIds,
pollingOptions,
textContext,
dataSchema
);
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/mindee/MindeeClientV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mindee.parsing.v2.ErrorResponse;
import com.mindee.parsing.v2.InferenceResponse;
import com.mindee.parsing.v2.JobResponse;
import com.mindee.v2.clientOptions.BaseParameters;
import java.io.IOException;

/**
Expand Down Expand Up @@ -36,7 +37,7 @@ public MindeeClientV2(MindeeApiV2 mindeeApi) {
*/
public JobResponse enqueueInference(
LocalInputSource inputSource,
InferenceParameters params
BaseParameters params
) throws IOException {
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
}
Expand All @@ -46,7 +47,7 @@ public JobResponse enqueueInference(
*/
public JobResponse enqueueInference(
URLInputSource inputSource,
InferenceParameters params
BaseParameters params
) throws IOException {
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
}
Expand Down Expand Up @@ -75,7 +76,7 @@ public InferenceResponse getInference(String inferenceId) {

/**
* Send a local file to an async queue, poll, and parse when complete.
*
*
* @param inputSource The input source to send.
* @param options The options to send along with the file.
* @return an instance of {@link InferenceResponse}.
Expand All @@ -84,7 +85,7 @@ public InferenceResponse getInference(String inferenceId) {
*/
public InferenceResponse enqueueAndGetInference(
LocalInputSource inputSource,
InferenceParameters options
BaseParameters options
) throws IOException, InterruptedException {
validatePollingOptions(options.getPollingOptions());
JobResponse job = enqueueInference(inputSource, options);
Expand All @@ -93,7 +94,7 @@ public InferenceResponse enqueueAndGetInference(

/**
* Send a local file to an async queue, poll, and parse when complete.
*
*
* @param inputSource The input source to send.
* @param options The options to send along with the file.
* @return an instance of {@link InferenceResponse}.
Expand All @@ -102,7 +103,7 @@ public InferenceResponse enqueueAndGetInference(
*/
public InferenceResponse enqueueAndGetInference(
URLInputSource inputSource,
InferenceParameters options
BaseParameters options
) throws IOException, InterruptedException {
validatePollingOptions(options.getPollingOptions());
JobResponse job = enqueueInference(inputSource, options);
Expand All @@ -111,14 +112,14 @@ public InferenceResponse enqueueAndGetInference(

/**
* Common logic for polling an asynchronous job for local & url files.
*
*
* @param initialJob The initial job response.
* @return an instance of {@link InferenceResponse}.
* @throws InterruptedException Throws if interrupted.
*/
private InferenceResponse pollAndFetch(
JobResponse initialJob,
InferenceParameters options
BaseParameters options
) throws InterruptedException {
Thread.sleep((long) (options.getPollingOptions().getInitialDelaySec() * 1000));

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/mindee/http/MindeeApiV2.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.mindee.http;

import com.mindee.InferenceParameters;
import com.mindee.input.LocalInputSource;
import com.mindee.input.URLInputSource;
import com.mindee.parsing.v2.ErrorResponse;
import com.mindee.parsing.v2.InferenceResponse;
import com.mindee.parsing.v2.JobResponse;
import com.mindee.v2.clientOptions.BaseParameters;
import java.io.IOException;

/**
Expand All @@ -14,36 +14,36 @@
public abstract class MindeeApiV2 extends MindeeApiCommon {
/**
* Send a file to the prediction queue with a local file.
*
*
* @param inputSource Local input source from URL.
* @param options parameters.
*/
public abstract JobResponse reqPostInferenceEnqueue(
LocalInputSource inputSource,
InferenceParameters options
BaseParameters options
) throws IOException;

/**
* Send a file to the prediction queue with a remote file.
*
*
* @param inputSource Remote input source from URL.
* @param options parameters.
*/
public abstract JobResponse reqPostInferenceEnqueue(
URLInputSource inputSource,
InferenceParameters options
BaseParameters options
) throws IOException;

/**
* Attempts to poll the queue.
*
*
* @param jobId id of the job to get.
*/
public abstract JobResponse reqGetJob(String jobId);

/**
* Retrieves the inference from a 302 redirect.
*
*
* @param inferenceId ID of the inference to poll.
*/
abstract public InferenceResponse reqGetInference(String inferenceId);
Expand Down
45 changes: 5 additions & 40 deletions src/main/java/com/mindee/http/MindeeHttpApiV2.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mindee.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindee.InferenceParameters;
import com.mindee.MindeeException;
import com.mindee.MindeeSettingsV2;
import com.mindee.input.LocalInputSource;
Expand All @@ -10,6 +9,7 @@
import com.mindee.parsing.v2.ErrorResponse;
import com.mindee.parsing.v2.InferenceResponse;
import com.mindee.parsing.v2.JobResponse;
import com.mindee.v2.clientOptions.BaseParameters;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -68,10 +68,7 @@ private MindeeHttpApiV2(MindeeSettingsV2 mindeeSettings, HttpClientBuilder httpC
* @return A job response.
*/
@Override
public JobResponse reqPostInferenceEnqueue(
LocalInputSource inputSource,
InferenceParameters options
) {
public JobResponse reqPostInferenceEnqueue(LocalInputSource inputSource, BaseParameters options) {
String url = this.mindeeSettings.getBaseUrl() + "/products/extraction/enqueue";
HttpPost post = buildHttpPost(url);

Expand All @@ -84,7 +81,7 @@ public JobResponse reqPostInferenceEnqueue(
ContentType.DEFAULT_BINARY,
inputSource.getFilename()
);
post.setEntity(buildHttpBody(builder, options));
post.setEntity(options.buildHttpBody(builder).build());
return executeEnqueue(post);
}

Expand All @@ -96,17 +93,14 @@ public JobResponse reqPostInferenceEnqueue(
* @return A job response.
*/
@Override
public JobResponse reqPostInferenceEnqueue(
URLInputSource inputSource,
InferenceParameters options
) {
public JobResponse reqPostInferenceEnqueue(URLInputSource inputSource, BaseParameters options) {
String url = this.mindeeSettings.getBaseUrl() + "/products/extraction/enqueue";
HttpPost post = buildHttpPost(url);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.EXTENDED);
builder.addTextBody("url", inputSource.getUrl());
post.setEntity(buildHttpBody(builder, options));
post.setEntity(options.buildHttpBody(builder).build());
return executeEnqueue(post);
}

Expand Down Expand Up @@ -224,35 +218,6 @@ private MindeeHttpExceptionV2 getHttpError(ClassicHttpResponse response) {
}
}

private HttpEntity buildHttpBody(MultipartEntityBuilder builder, InferenceParameters params) {
builder.addTextBody("model_id", params.getModelId());
if (params.getRag() != null) {
builder.addTextBody("rag", params.getRag().toString().toLowerCase());
}
if (params.getRawText() != null) {
builder.addTextBody("raw_text", params.getRawText().toString().toLowerCase());
}
if (params.getPolygon() != null) {
builder.addTextBody("polygon", params.getPolygon().toString().toLowerCase());
}
if (params.getConfidence() != null) {
builder.addTextBody("confidence", params.getConfidence().toString().toLowerCase());
}
if (params.getAlias() != null) {
builder.addTextBody("alias", params.getAlias());
}
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();
}

private HttpPost buildHttpPost(String url) {
HttpPost post;
try {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/mindee/parsing/SummaryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.mindee.parsing.standard.LineItemField;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
Expand All @@ -21,7 +23,7 @@ public static String formatAmount(Double amountValue) {
if (amountValue == null) {
return "";
}
DecimalFormat df = new DecimalFormat("0.00###");
DecimalFormat df = new DecimalFormat("0.00###", new DecimalFormatSymbols(Locale.US));
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(5);
return df.format(amountValue);
Expand Down
Loading
Loading