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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const mindeeClient = new mindee.Client(
);

// Set inference parameters
const inferenceParams = {
const extractionParams = {
modelId: modelId,

// Options: set to `true` or `false` to override defaults
Expand All @@ -32,10 +32,10 @@ const inferenceParams = {
const inputSource = new mindee.PathInput({ inputPath: filePath });

// Send for processing
const response = mindeeClient.enqueueAndGetInference(
mindee.v2.ExtractionInference,
const response = mindeeClient.enqueueAndGetResult(
mindee.v2.product.Extraction,
inputSource,
inferenceParams
extractionParams
);

// Handle the response Promise
Expand Down
12 changes: 0 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,8 @@ export * as v1 from "./v1/index.js";
export * as v2 from "./v2/index.js";
export {
Client,
ExtractionParameters,
DataSchema,
InferenceFile,
InferenceModel,
ClassificationInference,
ClassificationResponse,
CropInference,
CropResponse,
ExtractionInference,
ExtractionResponse,
OcrInference,
OcrResponse,
SplitInference,
SplitResponse,
JobResponse,
ErrorResponse,
} from "./v2/index.js";
Expand Down
2 changes: 1 addition & 1 deletion src/input/localInputSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export abstract class LocalInputSource extends InputSource {
);
}
this.inputType = inputType;
logger.debug(`New local input source of type: ${inputType}`);
logger.debug(`Initialized local input source of type: ${inputType}`);
}

protected async checkMimetype(): Promise<string> {
Expand Down
1 change: 1 addition & 0 deletions src/input/urlInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class UrlInput extends InputSource {
super();
this.url = url;
this.dispatcher = dispatcher ?? getGlobalDispatcher();
logger.debug("Initialized URL input source.");
}

async init() {
Expand Down
34 changes: 17 additions & 17 deletions src/v2/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Command, OptionValues } from "commander";
import { Client } from "./client.js";
import { PathInput } from "../input/index.js";
import * as console from "console";
import { BaseInference } from "@/v2/parsing/inference/index.js";
import { BaseProduct } from "@/v2/product/baseProduct.js";
import {
BaseInference,
ClassificationInference,
CropInference,
ExtractionInference,
OcrInference,
SplitInference,
InferenceResponseConstructor,
} from "@/v2/parsing/inference/index.js";
Classification,
Crop,
Extraction,
Ocr,
Split,
} from "@/v2/product/index.js";

const program = new Command();

Expand All @@ -27,14 +27,14 @@ function initClient(options: OptionValues): Client {
}

async function enqueueAndGetInference(
responseType: InferenceResponseConstructor<any>,
product: typeof BaseProduct,
inputPath: string,
options: OptionValues
): Promise<void> {
const mindeeClient = initClient(options);
const inputSource = new PathInput({ inputPath: inputPath });
const response = await mindeeClient.enqueueAndGetInference(
responseType,
const response = await mindeeClient.enqueueAndGetResult(
product,
inputSource,
{
modelId: options.model,
Expand Down Expand Up @@ -78,11 +78,11 @@ export function cli() {
.option("-k, --api-key <api_key>", "your Mindee API key");

const inferenceTypes = [
{ name: "extract", description: "Extract data from a document.", responseType: ExtractionInference },
{ name: "crop", description: "Crop a document.", responseType: CropInference },
{ name: "split", description: "Split a document into pages.", responseType: SplitInference },
{ name: "ocr", description: "Read text from a document.", responseType: OcrInference },
{ name: "classify", description: "Classify a document.", responseType: ClassificationInference },
{ name: "extract", description: "Extract data from a document.", product: Extraction },
{ name: "crop", description: "Crop a document.", product: Crop },
{ name: "split", description: "Split a document into pages.", product: Split },
{ name: "ocr", description: "Read text from a document.", product: Ocr },
{ name: "classify", description: "Classify a document.", product: Classification },
];

for (const inference of inferenceTypes) {
Expand All @@ -96,7 +96,7 @@ export function cli() {
options: OptionValues,
) {
const allOptions = { ...program.opts(), ...options };
return enqueueAndGetInference(inference.responseType, inputPath, allOptions);
return enqueueAndGetInference(inference.product, inputPath, allOptions);
});
}

Expand Down
106 changes: 34 additions & 72 deletions src/v2/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@ import { InputSource } from "@/input/index.js";
import { MindeeError } from "@/errors/index.js";
import { errorHandler } from "@/errors/handler.js";
import { LOG_LEVELS, logger } from "@/logger.js";
import {
ErrorResponse,
JobResponse,
InferenceResponseConstructor,
BaseInference,
BaseInferenceResponse,
CropInference,
OcrInference,
SplitInference,
ExtractionInference,
} from "./parsing/index.js";
import { ErrorResponse, JobResponse } from "./parsing/index.js";
import { MindeeApiV2 } from "./http/mindeeApiV2.js";
import { MindeeHttpErrorV2 } from "./http/errors.js";
import { ExtractionParameters, UtilityParameters, ValidatedPollingOptions } from "./client/index.js";
import { ValidatedPollingOptions } from "./client/index.js";
import { BaseProduct } from "@/v2/product/baseProduct.js";

/**
* Options for the V2 Mindee Client.
Expand All @@ -39,12 +30,6 @@ export interface ClientOptions {
dispatcher?: Dispatcher;
}

type InferenceParameters =
| UtilityParameters
| ExtractionParameters
| ConstructorParameters<typeof UtilityParameters>[0]
| ConstructorParameters<typeof ExtractionParameters>[0];

/**
* Mindee Client V2 class that centralizes most basic operations.
*
Expand Down Expand Up @@ -73,41 +58,20 @@ export class Client {
logger.debug("Client V2 Initialized");
}

#getParametersClassFromInference<T extends BaseInference>(
inferenceClass: InferenceResponseConstructor<T>,
params: any,
): ExtractionParameters | UtilityParameters {
if (params instanceof ExtractionParameters || params instanceof UtilityParameters) {
return params;
}
switch (inferenceClass as any) {
case CropInference:
return new UtilityParameters(params);
case OcrInference:
return new UtilityParameters(params);
case SplitInference:
return new UtilityParameters(params);
case ExtractionInference:
return new ExtractionParameters(params);
default:
throw new Error("Unsupported inference class.");
}
}

async enqueueInference<T extends BaseInference>(
responseType: InferenceResponseConstructor<T>,
async enqueue<P extends typeof BaseProduct>(
product: P,
inputSource: InputSource,
params: InferenceParameters,
params: InstanceType<P["parameters"]> | ConstructorParameters<P["parameters"]>[0],
): Promise<JobResponse> {
if (inputSource === undefined) {
throw new MindeeError("An input document is required.");
}
const paramsInstance = this.#getParametersClassFromInference(
responseType, params
);
const paramsInstance = params instanceof product.parameters
? params
: new product.parameters(params);
await inputSource.init();
const jobResponse = await this.mindeeApi.reqPostInferenceEnqueue(
responseType, inputSource, paramsInstance
const jobResponse = await this.mindeeApi.reqPostProductEnqueue(
product, inputSource, paramsInstance
);
if (jobResponse.job.id === undefined || jobResponse.job.id.length === 0) {
logger.error(`Failed enqueueing:\n${jobResponse.getRawHttp()}`);
Expand All @@ -120,26 +84,26 @@ export class Client {
}

/**
* Retrieves an inference.
* Retrieves the result of a previously enqueued request.
*
* @param responseType class of the inference to retrieve.
* @param product the product to retrieve.
* @param inferenceId id of the queue to poll.
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
* @category Asynchronous
* @returns a `Promise` containing the inference.
*/
async getInference<T extends BaseInference>(
responseType: InferenceResponseConstructor<T>,
async getResult<P extends typeof BaseProduct>(
product: P,
inferenceId: string
): Promise<BaseInferenceResponse<T>> {
): Promise<InstanceType<P["response"]>> {
logger.debug(
`Attempting to get inference with ID: ${inferenceId} using response type: ${responseType.name}`
`Attempting to get inference with ID: ${inferenceId} using response type: ${product.name}`
);
return await this.mindeeApi.reqGetInference(responseType, inferenceId);
return await this.mindeeApi.reqGetResult(product, inferenceId);
}

/**
* Get the status of an inference that was previously enqueued.
* Get the processing status of a previously enqueued request.
* Can be used for polling.
*
* @param jobId id of the queue to poll.
Expand All @@ -153,33 +117,31 @@ export class Client {
}

/**
* Send a document to an endpoint and poll the server until the result is sent or
* Enqueue a request and poll the server until the result is sent or
* until the maximum number of tries is reached.
*
* @param responseType class of the inference to retrieve.
* @param product the product to retrieve.
* @param inputSource file or URL to parse.
* @param params parameters relating to prediction options.
*
* @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`.
* @category Synchronous
* @returns a `Promise` containing parsing results.
*/
async enqueueAndGetInference<T extends BaseInference>(
responseType: InferenceResponseConstructor<T>,
async enqueueAndGetResult<P extends typeof BaseProduct>(
product: P,
inputSource: InputSource,
params: InferenceParameters
): Promise<BaseInferenceResponse<T>> {
const paramsInstance = this.#getParametersClassFromInference(
responseType, params
);
params: InstanceType<P["parameters"]> | ConstructorParameters<P["parameters"]>[0],
): Promise<InstanceType<P["response"]>> {
const paramsInstance = new product.parameters(params);

const pollingOptions = paramsInstance.getValidatedPollingOptions();

const jobResponse: JobResponse = await this.enqueueInference(
responseType, inputSource, paramsInstance
const jobResponse: JobResponse = await this.enqueue(
product, inputSource, paramsInstance
);
return await this.pollForInference(
responseType, pollingOptions, jobResponse.job.id
return await this.pollForResult(
product, pollingOptions, jobResponse.job.id
);
}

Expand All @@ -188,11 +150,11 @@ export class Client {
* until the maximum number of tries is reached.
* @protected
*/
protected async pollForInference<T extends BaseInference>(
responseType: InferenceResponseConstructor<T>,
protected async pollForResult<P extends typeof BaseProduct>(
product: typeof BaseProduct,
pollingOptions: ValidatedPollingOptions,
queueId: string,
): Promise<BaseInferenceResponse<T>> {
): Promise<InstanceType<P["response"]>> {
logger.debug(
`Waiting ${pollingOptions.initialDelaySec} seconds before polling.`
);
Expand Down Expand Up @@ -220,7 +182,7 @@ export class Client {
break;
}
if (pollResults.job.status === "Processed") {
return this.getInference(responseType, pollResults.job.id);
return this.getResult(product, pollResults.job.id);
}
await setTimeout(
pollingOptions.delaySec * 1000,
Expand Down
4 changes: 1 addition & 3 deletions src/v2/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export { DataSchema } from "./dataSchema.js";
export type { PollingOptions, ValidatedPollingOptions } from "./pollingOptions.js";
export { ExtractionParameters } from "./extractionParameters.js";
export { UtilityParameters } from "./utilityParameters.js";
export { BaseParameters } from "./baseParameters.js";
Loading
Loading