@@ -6,7 +6,7 @@ author: laujan
6
6
manager : nitinme
7
7
ms.service : azure-ai-document-intelligence
8
8
ms.topic : include
9
- ms.date : 03/25 /2024
9
+ ms.date : 05/02 /2024
10
10
ms.author : lajanuar
11
11
---
12
12
<!-- markdownlint-disable MD025 -->
@@ -146,47 +146,44 @@ Extract text, selection marks, text styles, table structures, and bounding regio
146
146
:::moniker range="doc-intel-4.0.0"
147
147
148
148
```javascript
149
- const { DocumentIntelligenceClient } = require("@azure-rest/ai-document-intelligence");
150
- const { AzureKeyCredential } = require("@azure/core-auth");
149
+ const DocumentIntelligenceClient = require("@azure-rest/ai-document-intelligence");
151
150
152
151
// set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
153
152
const key = "<your-key";
154
153
const endpoint = "<your-endpoint>";
155
154
156
155
// sample document
157
- const formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
156
+ const formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
158
157
159
- async function main() {
160
- const client = DocumentIntelligenceClient(endpoint, new AzureKeyCredential( key) );
158
+ async function main() {
159
+ const client = DocumentIntelligenceClient(endpoint, { key:key}, );
161
160
162
- const poller = await client.beginAnalyzeDocument("prebuilt-layout", formUrl);
163
161
164
- const {
165
- pages,
166
- tables
167
- } = await poller.pollUntilDone();
162
+ const initialResponse = await client
163
+ .path("/documentModels/{modelId}:analyze", "prebuilt-layout")
164
+ .post({
165
+ contentType: "application/json",
166
+ body: {
167
+ urlSource: formUrl
168
+ },
169
+ });
168
170
169
- if (pages.length <= 0) {
170
- console.log("No pages were extracted from the document.");
171
- } else {
172
- console.log("Pages:");
173
- for (const page of pages) {
174
- console.log("- Page", page.pageNumber, `(unit: ${page.unit})`);
175
- console.log(` ${page.width}x${page.height}, angle: ${page.angle}`);
176
- console.log(` ${page.lines.length} lines, ${page.words.length} words`);
177
- }
178
- }
171
+ const poller = await getLongRunningPoller(client, initialResponse);
172
+ const analyzeResult = (await poller.pollUntilDone()).body.analyzeResult;
179
173
180
- if (tables.length <= 0) {
181
- console.log("No tables were extracted from the document.");
182
- } else {
183
- console.log("Tables:");
184
- for (const table of tables) {
185
- console.log(
186
- `- Extracted table: ${table.columnCount} columns, ${table.rowCount} rows (${table.cells.length} cells)`
187
- );
188
- }
174
+ const documents = analyzeResult?.documents;
175
+
176
+ const document = documents && documents[0];
177
+ if (!document) {
178
+ throw new Error("Expected at least one document in the result.");
189
179
}
180
+
181
+ console.log(
182
+ "Extracted document:",
183
+ document.docType,
184
+ `(confidence: ${document.confidence || "<undefined>"})`,
185
+ );
186
+ console.log("Fields:", document.fields);
190
187
}
191
188
192
189
main().catch((error) => {
@@ -312,8 +309,7 @@ In this example, we analyze an invoice using the **prebuilt-invoice** model.
312
309
313
310
``` javascript
314
311
315
- const { DocumentIntelligenceClient } = require (" @azure-rest/ai-document-intelligence" );
316
- const { AzureKeyCredential } = require (" @azure/core-auth" );
312
+ const DocumentIntelligenceClient = require (" @azure-rest/ai-document-intelligence" );
317
313
318
314
// set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
319
315
const key = " <your-key>" ;
@@ -323,32 +319,44 @@ const { AzureKeyCredential } = require("@azure/core-auth");
323
319
invoiceUrl = " https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"
324
320
325
321
async function main () {
326
- const client = DocumentIntelligenceClient (endpoint, new AzureKeyCredential (key));
327
322
328
- const poller = await client .beginAnalyzeDocument (" prebuilt-invoice" , invoiceUrl);
329
- if (pages .length <= 0 ) {
330
- console .log (" No pages were extracted from the document." );
331
- } else {
332
- console .log (" Pages:" );
333
- for (const page of pages) {
334
- console .log (" - Page" , page .pageNumber , ` (unit: ${ page .unit } )` );
335
- console .log (` ${ page .width } x${ page .height } , angle: ${ page .angle } ` );
336
- console .log (` ${ page .lines .length } lines, ${ page .words .length } words` );
337
- }
338
- }
323
+ const client = DocumentIntelligenceClient (endpoint, {key: key},
324
+ );
339
325
340
- if (tables .length <= 0 ) {
341
- console .log (" No tables were extracted from the document." );
326
+ const initialResponse = await client
327
+ .path (" /documentModels/{modelId}:analyze" , " prebuilt-invoice" )
328
+ .post ({
329
+ contentType: " application/json" ,
330
+ body: {
331
+ // The Document Intelligence service will access the URL to the invoice image and extract data from it
332
+ urlSource: invoiceUrl,
333
+ },
334
+ });
335
+
336
+
337
+ const poller = await getLongRunningPoller (client, initialResponse);
338
+
339
+ poller .onProgress ((state ) => console .log (" Operation:" , state .result , state .status ));
340
+ const analyzeResult = (await poller .pollUntilDone ()).body .analyzeResult ;
341
+
342
+ const documents = analyzeResult? .documents ;
343
+
344
+ const result = documents && documents[0 ];
345
+ if (result) {
346
+ console .log (result .fields );
342
347
} else {
343
- console .log (" Tables:" );
344
- for (const table of tables) {
345
- console .log (
346
- ` - Extracted table: ${ table .columnCount } columns, ${ table .rowCount } rows (${ table .cells .length } cells)`
347
- );
348
- }
348
+ throw new Error (" Expected at least one invoice in the result." );
349
349
}
350
+
351
+ console .log (
352
+ " Extracted invoice:" ,
353
+ document .docType ,
354
+ ` (confidence: ${ document .confidence || " <undefined>" } )` ,
355
+ );
356
+ console .log (" Fields:" , document .fields );
350
357
}
351
358
359
+
352
360
main ().catch ((error ) => {
353
361
console .error (" An error occurred:" , error);
354
362
process .exit (1 );
0 commit comments