UNPKG

mindee

Version:

Mindee Client Library for Node.js

177 lines (176 loc) 6.21 kB
import { DataSchema, InputSource } from "./input"; import { InferenceResponse, JobResponse } from "./parsing/v2"; import { MindeeApiV2 } from "./http/mindeeApiV2"; import { StringDict } from "./parsing/common"; /** * Parameters for the internal polling loop in {@link ClientV2.enqueueAndGetInference | enqueueAndGetInference()} . * * Default behavior: * - `initialDelaySec` = 2s * - `delaySec` = 1.5s * - `maxRetries` = 80 * * Validation rules: * - `initialDelaySec` >= 1 * - `delaySec` >= 1 * - `maxRetries` >= 2 * * The `initialTimerOptions` and `recurringTimerOptions` objects let you pass an * `AbortSignal` or make the timer `unref`-ed to the `setTimeout()`. * * @category ClientV2 * @example * const params = { * initialDelaySec: 4, * delaySec: 2, * maxRetries: 50 * }; * * const inference = await client.enqueueAndGetInference(inputDoc, params); */ export interface PollingOptions { /** Number of seconds to wait *before the first poll*. */ initialDelaySec?: number; /** Interval in seconds between two consecutive polls. */ delaySec?: number; /** Maximum number of polling attempts (including the first one). */ maxRetries?: number; /** Options passed to the initial `setTimeout()`. */ initialTimerOptions?: { ref?: boolean; signal?: AbortSignal; }; /** Options passed to every recurring `setTimeout()`. */ recurringTimerOptions?: { ref?: boolean; signal?: AbortSignal; }; } /** * Parameters accepted by the asynchronous **inference** v2 endpoint. * * All fields are optional except `modelId`. * * @category ClientV2 * @example * const params = { * modelId: "YOUR_MODEL_ID", * rag: true, * alias: "YOUR_ALIAS", * webhookIds: ["YOUR_WEBHOOK_ID_1", "YOUR_WEBHOOK_ID_2"], * pollingOptions: { * initialDelaySec: 2, * delaySec: 1.5, * } * }; */ export interface InferenceParameters { /** Model ID to use for the inference. **Required** */ modelId: string; /** Use Retrieval-Augmented Generation during inference. */ rag?: boolean; /** Extract the entire text from the document as strings, and fill the `rawText` attribute. */ rawText?: boolean; /** Calculate bounding box polygons for values, and fill the `locations` attribute of fields. */ polygon?: boolean; /** Calculate confidence scores for values, and fill the `confidence` attribute of fields. * Useful for automation.*/ confidence?: boolean; /** Use an alias to link the file to your own DB. If empty, no alias will be used. */ alias?: string; /** Additional text context used by the model during inference. * *Not recommended*, for specific use only. */ textContext?: string; /** Webhook IDs to call after all processing is finished. * If empty, no webhooks will be used. */ webhookIds?: string[]; /** Client-side polling configuration (see {@link PollingOptions}). */ pollingOptions?: PollingOptions; /** By default, the file is closed once the upload is finished. * Set to `false` to keep it open. */ closeFile?: boolean; /** * Dynamic changes to the data schema of the model for this inference. * Not recommended, for specific use only. */ dataSchema?: DataSchema | StringDict | string; } /** * Options for the V2 Mindee Client. * * @category ClientV2 * @example * const client = new MindeeClientV2({ * apiKey: "YOUR_API_KEY", * throwOnError: true, * debug: false * }); */ export interface ClientOptions { /** Your API key for all endpoints. */ apiKey?: string; /** Raise an `Error` on errors. */ throwOnError?: boolean; /** Log debug messages. */ debug?: boolean; } /** * Mindee Client V2 class that centralizes most basic operations. * * @category ClientV2 */ export declare class ClientV2 { #private; /** Mindee API handler. */ protected mindeeApi: MindeeApiV2; /** * @param {ClientOptions} options options for the initialization of a client. */ constructor({ apiKey, throwOnError, debug }?: ClientOptions); /** * Checks the Data Schema. * @param params Input Inference parameters. */ validateDataSchema(params: InferenceParameters): void; /** * Send the document to an asynchronous endpoint and return its ID in the queue. * @param inputSource file or URL to parse. * @param params parameters relating to prediction options. * @category Asynchronous * @returns a `Promise` containing the job (queue) corresponding to a document. */ enqueueInference(inputSource: InputSource, params: InferenceParameters): Promise<JobResponse>; /** * Retrieves an inference. * * @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 a `Job`, which also contains a `Document` if the * parsing is complete. */ getInference(inferenceId: string): Promise<InferenceResponse>; /** * Get the status of an inference that was previously enqueued. * Can be used for polling. * * @param jobId 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 a `Job`, which also contains a `Document` if the * parsing is complete. */ getJob(jobId: string): Promise<JobResponse>; /** * Send a document to an endpoint and poll the server until the result is sent or * until the maximum number of tries is reached. * * @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. */ enqueueAndGetInference(inputSource: InputSource, params: InferenceParameters): Promise<InferenceResponse>; }