diff --git a/docs/code_samples/workflow_polling.txt b/docs/code_samples/workflow_polling.txt new file mode 100644 index 000000000..e0e01cfaa --- /dev/null +++ b/docs/code_samples/workflow_polling.txt @@ -0,0 +1,40 @@ +const mindee = require("mindee"); +// for TS or modules: +// import * as mindee from "mindee"; + +const workflowId: string = "workflow-id"; + +// Init a new client +const mindeeClient = new mindee.Client({ apiKey: "my-api-key" }); + +// Load a file from disk +const inputSource = mindeeClient.docFromPath( + "/path/to/the/file.ext" +); + +const customEndpoint = mindeeClient.createEndpoint( + "my-endpoint", + "my-account", + "my-version" // Defaults to "1" +); + +const workflowParams = { + rag: true, + workflowId: workflowId, + endpoint: customEndpoint +}; + +// Parse the file asynchronously on a workflow queue +const asyncApiResponse = mindeeClient.enqueueAndParse( + mindee.product.GeneratedV1, + inputSource, + workflowParams + // Optionally: send an alias & priority + // { alias: "my-alias", priority: ExecutionPriority.low } +); + +// Handle the response Promise +asyncApiResponse.then((resp) => { + // print a string summary + console.log(resp.document?.toString()); +}); \ No newline at end of file diff --git a/src/client.ts b/src/client.ts index 7d6c0aac9..750d9a174 100644 --- a/src/client.ts +++ b/src/client.ts @@ -209,9 +209,6 @@ export class Client { ): Promise> { const endpoint = params?.endpoint ?? this.#initializeOTSEndpoint(productClass); - if (params.workflowId) { - endpoint.useWorkflowId(params.workflowId); - } if (inputSource === undefined) { throw new Error("The 'parse' function requires an input document."); } @@ -221,8 +218,8 @@ export class Client { fullText: this.getBooleanParam(params.fullText), pageOptions: params?.pageOptions, cropper: this.getBooleanParam(params.cropper), - workflowId: params.workflowId, - rag: this.getBooleanParam(params.rag) + rag: this.getBooleanParam(params.rag), + workflowId: params.workflowId }); return new AsyncPredictResponse(productClass, rawResponse.data); diff --git a/src/http/endpoint.ts b/src/http/endpoint.ts index d387338b0..31806c206 100644 --- a/src/http/endpoint.ts +++ b/src/http/endpoint.ts @@ -1,8 +1,7 @@ import { RequestOptions } from "https"; import { URLSearchParams } from "url"; import FormData from "form-data"; -import { InputSource } from "../input"; -import { LocalInputSource } from "../input"; +import { InputSource, LocalInputSource } from "../input"; import { handleError } from "./error"; import { ApiSettings } from "./apiSettings"; import { BaseEndpoint, EndpointResponse } from "./baseEndpoint"; @@ -84,7 +83,8 @@ export class Endpoint extends BaseEndpoint { params.includeWords, params.fullText, params.cropper, - params.rag + params.rag, + params.workflowId ); if (!isValidAsyncResponse(response)) { handleError(this.urlName, response, this.extractStatusMessage(response)); @@ -175,6 +175,7 @@ export class Endpoint extends BaseEndpoint { * @param fullText * @param cropper * @param rag + * @param workflowId */ protected sendFileForPrediction( input: InputSource, @@ -182,7 +183,8 @@ export class Endpoint extends BaseEndpoint { includeWords: boolean = false, fullText: boolean = false, cropper: boolean = false, - rag: boolean = false + rag: boolean = false, + workflowId: string | undefined = undefined ): Promise { return new Promise((resolve, reject) => { const searchParams = new URLSearchParams(); @@ -209,8 +211,12 @@ export class Endpoint extends BaseEndpoint { form.append("include_mvision", "true"); } const headers = { ...this.settings.baseHeaders, ...form.getHeaders() }; - - let path = `${this.urlRoot}/${predictUrl}`; + let path: string; + if (workflowId === undefined) { + path = `${this.urlRoot}/${predictUrl}`; + } else { + path = `/v1/workflows/${workflowId}/predict_async`; + } if (searchParams.toString().length > 0) { path += `?${searchParams}`; } @@ -237,27 +243,29 @@ export class Endpoint extends BaseEndpoint { */ #predictReqPost( input: InputSource, - includeWords = false, - fullText = false, - cropper = false + includeWords: boolean = false, + fullText: boolean = false, + cropper: boolean = false ): Promise { return this.sendFileForPrediction(input, "predict", includeWords, fullText, cropper); } - /** - * Make a request to POST a document for async prediction. - * @param input - * @param includeWords - * @param fullText - * @param cropper - * @param rag - */ #predictAsyncReqPost( + /** + * Make a request to POST a document for async prediction. + * @param input + * @param includeWords + * @param fullText + * @param cropper + * @param rag + * @param workflowId + */ input: InputSource, - includeWords = false, - fullText = false, - cropper = false, - rag = false + includeWords: boolean = false, + fullText: boolean = false, + cropper: boolean = false, + rag: boolean = false, + workflowId: string | undefined = undefined ): Promise { return this.sendFileForPrediction( input, @@ -265,7 +273,8 @@ export class Endpoint extends BaseEndpoint { includeWords, fullText, cropper, - rag + rag, + workflowId ); } diff --git a/tests/test_code_samples.sh b/tests/test_code_samples.sh index 9839cead4..cfb8430a7 100755 --- a/tests/test_code_samples.sh +++ b/tests/test_code_samples.sh @@ -13,7 +13,7 @@ cd ../test_code_samples npm install ../mindee-api-nodejs/dist --ignore-scripts --no-bin-links cd - -for f in $(find docs/code_samples -maxdepth 1 -name "*.txt" -not -name "workflow_execution.txt" | sort -h) +for f in $(find docs/code_samples -maxdepth 1 -name "*.txt" -not -name "workflow_*.txt" | sort -h) do echo "###############################################" echo "${f}"