Open Danscap opened 6 months ago
Here is an example that works.
const DocumentIntelligence = require("@azure-rest/ai-document-intelligence").default,
{ getLongRunningPoller, isUnexpected } = require("@azure-rest/ai-document-intelligence");
const { AzureKeyCredential } = require("@azure/core-auth");
const { config } = require("dotenv")
config()
// sample document
const invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"
// set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
const key = process.env.KEY;
const endpoint = process.env.ENDPOINT;
async function main() {
const client = DocumentIntelligence(endpoint, new AzureKeyCredential(key));
const initialResponse = await client
.path("/documentModels/{modelId}:analyze", "prebuilt-invoice")
.post({
contentType: "application/json",
body: {
// The Document Intelligence service will access the URL to the invoice image and extract data from it
urlSource: invoiceUrl,
},
});
if (isUnexpected(initialResponse)) {
throw initialResponse.body.error;
}
const poller = await getLongRunningPoller(client, initialResponse);
poller.onProgress((state) => console.log("Operation:", state.result, state.status));
const analyzeResult = (await poller.pollUntilDone()).body.analyzeResult;
const documents = analyzeResult?.documents;
const result = documents && documents[0];
if (result) {
console.log(result.fields);
} else {
throw new Error("Expected at least one invoice in the result.");
}
console.log(
"Extracted invoice:",
result.docType,
`(confidence: ${result.confidence || "<undefined>"})`,
);
console.log("Fields:", result.fields);
}
main().catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});
DocumentIntelligence
is the default export of the SDK.
createClient
method can also be imported which returns an object of type DocumentIntelligenceClient
.
Moreover, you can name it however you want while importing a default export and it will work exactly the same.
For instance,
const DocumentIntelligenceClient = require("@azure-rest/ai-document-intelligence").default,
{ getLongRunningPoller, isUnexpected } = require("@azure-rest/ai-document-intelligence");
Edit 5/24/2024 5:50 PM I have tried running the example from the legacy 3.1 documents and do not run into this issue. It seems this is only an issue with the v4.0 documentation.
Output is `Pages:
Original I have tried running the example from the v 4.0 documentation and received this error. I am running Node 16.13.0 on Windows 10.
I'm not sure what I am doing wrong or if the example has faulty code. Any guidance or resolution would be helpful. Thank you.
This issue is for a: (mark with an
x
)Minimal steps to reproduce
Any log messages given by the failure
Expected/desired behavior
OS and Version?
Versions
Mention any other details that might be useful