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
40 changes: 40 additions & 0 deletions docs/code_samples/workflow_polling.txt
Original file line number Diff line number Diff line change
@@ -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());
});
7 changes: 2 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ export class Client {
): Promise<AsyncPredictResponse<T>> {
const endpoint =
params?.endpoint ?? this.#initializeOTSEndpoint<T>(productClass);
if (params.workflowId) {
endpoint.useWorkflowId(params.workflowId);
}
if (inputSource === undefined) {
throw new Error("The 'parse' function requires an input document.");
}
Expand All @@ -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<T>(productClass, rawResponse.data);
Expand Down
53 changes: 31 additions & 22 deletions src/http/endpoint.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -175,14 +175,16 @@ export class Endpoint extends BaseEndpoint {
* @param fullText
* @param cropper
* @param rag
* @param workflowId
*/
protected sendFileForPrediction(
input: InputSource,
predictUrl: string,
includeWords: boolean = false,
fullText: boolean = false,
cropper: boolean = false,
rag: boolean = false
rag: boolean = false,
workflowId: string | undefined = undefined
): Promise<EndpointResponse> {
return new Promise((resolve, reject) => {
const searchParams = new URLSearchParams();
Expand All @@ -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}`;
}
Expand All @@ -237,35 +243,38 @@ export class Endpoint extends BaseEndpoint {
*/
#predictReqPost(
input: InputSource,
includeWords = false,
fullText = false,
cropper = false
includeWords: boolean = false,
fullText: boolean = false,
cropper: boolean = false
): Promise<EndpointResponse> {
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<EndpointResponse> {
return this.sendFileForPrediction(
input,
"predict_async",
includeWords,
fullText,
cropper,
rag
rag,
workflowId
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_code_samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down