Skip to content

Commit 21fa2fc

Browse files
Merge pull request #274156 from laujan/251008-update-qs-code-samples
update code
2 parents d97965e + 84dd057 commit 21fa2fc

File tree

4 files changed

+104
-77
lines changed

4 files changed

+104
-77
lines changed

articles/ai-services/document-intelligence/quickstarts/includes/csharp-sdk.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,12 @@ DocumentIntelligenceClient client = new DocumentIntelligenceClient(new Uri(endpo
209209
//sample document
210210
Uri fileUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf");
211211

212-
Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-layout", fileUri);
212+
AnalyzeDocumentContent content = new AnalyzeDocumentContent()
213+
{
214+
UrlSource= fileUri
215+
};
216+
217+
Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-layout", content);
213218

214219
AnalyzeResult result = operation.Value;
215220

@@ -641,7 +646,12 @@ DocumentIntelligenceClient client = new DocumentIntelligenceClient(new Uri(endpo
641646
642647
Uri invoiceUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf");
643648

644-
Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-invoice", invoiceUri);
649+
AnalyzeDocumentContent content = new AnalyzeDocumentContent()
650+
{
651+
UrlSource = invoiceUri
652+
};
653+
654+
Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-invoice", content);
645655

646656
AnalyzeResult result = operation.Value;
647657

articles/ai-services/document-intelligence/quickstarts/includes/java-sdk.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: laujan
66
manager: nitinme
77
ms.service: azure-ai-document-intelligence
88
ms.topic: include
9-
ms.date: 03/25/2024
9+
ms.date: 05/01/2024
1010
ms.author: lajanuar
1111
---
1212
<!-- markdownlint-disable MD025 -->
@@ -284,18 +284,25 @@ public class DocIntelligence {
284284

285285
public static void main(String[] args) {
286286

287-
// create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
287+
// create your `DocumentIntelligenceClient` instance and `AzureKeyCredential` variable
288288
DocumentIntelligenceClient client = new DocumentIntelligenceClientBuilder()
289289
.credential(new AzureKeyCredential(key))
290290
.endpoint(endpoint)
291291
.buildClient();
292292

293293
// sample document
294-
String documentUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf";
295294
String modelId = "prebuilt-layout";
295+
String documentUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf";
296296

297-
SyncPoller <AnalyzeResultOperation, AnalyzeResultOperation> analyzeLayoutResultPoller =
298-
client.beginAnalyzeDocument(modelId, documentUrl);
297+
SyncPoller <AnalyzeResultOperation, AnalyzeResultOperation> analyzeLayoutPoller =
298+
client.beginAnalyzeDocument(modelId,
299+
null,
300+
null,
301+
null,
302+
null,
303+
null,
304+
null,
305+
new AnalyzeDocumentRequest().setUrlSource(documentUrl));
299306

300307
AnalyzeResult analyzeLayoutResult = analyzeLayoutPoller.getFinalResult().getAnalyzeResult();
301308

@@ -658,25 +665,27 @@ public class DocIntelligence {
658665

659666
public static void main(String[] args) {
660667

661-
// create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
662-
DocumentIntelligenceClient client = new DocumentIntelligenceClientBuilder()
663-
.credential(new AzureKeyCredential(key))
664-
.endpoint(endpoint)
665-
.buildClient();
666-
667668
// sample document
668669
String modelId = "prebuilt-invoice";
669670
String invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf";
670671

671672
public static void main(final String[] args) throws IOException {
673+
672674
// Instantiate a client that will be used to call the service.
673675
DocumentIntelligenceClient client = new DocumentIntelligenceClientBuilder()
674-
.credential(new AzureKeyCredential("{key}"))
675-
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
676+
.credential(new AzureKeyCredential(key))
677+
.endpoint(endpoint)
676678
.buildClient();
677679

678-
SyncPoller < OperationResult, AnalyzeResult > analyzeLayoutResultPoller =
679-
client.beginAnalyzeDocument(modelId, invoiceUrl);
680+
SyncPoller<AnalyzeResultOperation, AnalyzeResultOperation > analyzeInvoicesPoller =
681+
client.beginAnalyzeDocument(modelId,
682+
null,
683+
null,
684+
null,
685+
null,
686+
null,
687+
null,
688+
new AnalyzeDocumentRequest().setUrlSource(invoiceUrl));
680689

681690
AnalyzeResult analyzeInvoiceResult = analyzeInvoicesPoller.getFinalResult().getAnalyzeResult();
682691

articles/ai-services/document-intelligence/quickstarts/includes/javascript-sdk.md

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: laujan
66
manager: nitinme
77
ms.service: azure-ai-document-intelligence
88
ms.topic: include
9-
ms.date: 03/25/2024
9+
ms.date: 05/02/2024
1010
ms.author: lajanuar
1111
---
1212
<!-- markdownlint-disable MD025 -->
@@ -146,47 +146,44 @@ Extract text, selection marks, text styles, table structures, and bounding regio
146146
:::moniker range="doc-intel-4.0.0"
147147

148148
```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");
151150

152151
// set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
153152
const key = "<your-key";
154153
const endpoint = "<your-endpoint>";
155154

156155
// 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"
158157

159-
async function main() {
160-
const client = DocumentIntelligenceClient(endpoint, new AzureKeyCredential(key));
158+
async function main() {
159+
const client = DocumentIntelligenceClient(endpoint, {key:key},);
161160

162-
const poller = await client.beginAnalyzeDocument("prebuilt-layout", formUrl);
163161

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+
});
168170

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;
179173

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.");
189179
}
180+
181+
console.log(
182+
"Extracted document:",
183+
document.docType,
184+
`(confidence: ${document.confidence || "<undefined>"})`,
185+
);
186+
console.log("Fields:", document.fields);
190187
}
191188

192189
main().catch((error) => {
@@ -312,8 +309,7 @@ In this example, we analyze an invoice using the **prebuilt-invoice** model.
312309

313310
```javascript
314311

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");
317313

318314
// set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
319315
const key = "<your-key>";
@@ -323,32 +319,44 @@ const { AzureKeyCredential } = require("@azure/core-auth");
323319
invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"
324320

325321
async function main() {
326-
const client = DocumentIntelligenceClient(endpoint, new AzureKeyCredential(key));
327322

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+
);
339325

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);
342347
} 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.");
349349
}
350+
351+
console.log(
352+
"Extracted invoice:",
353+
document.docType,
354+
`(confidence: ${document.confidence || "<undefined>"})`,
355+
);
356+
console.log("Fields:", document.fields);
350357
}
351358

359+
352360
main().catch((error) => {
353361
console.error("An error occurred:", error);
354362
process.exit(1);

articles/ai-services/document-intelligence/quickstarts/includes/python-sdk.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: laujan
66
manager: nitinme
77
ms.service: azure-ai-document-intelligence
88
ms.topic: include
9-
ms.date: 03/25/2024
9+
ms.date: 05/01/2024
1010
ms.author: lajanuar
1111
---
1212
<!-- markdownlint-disable MD025 -->
@@ -135,6 +135,7 @@ import os
135135
from azure.core.credentials import AzureKeyCredential
136136
from azure.ai.documentintelligence import DocumentIntelligenceClient
137137
from azure.ai.documentintelligence.models import AnalyzeResult
138+
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest
138139

139140
# set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
140141
endpoint = "<your-endpoint>"
@@ -168,12 +169,8 @@ def analyze_layout():
168169
)
169170

170171
poller = document_intelligence_client.begin_analyze_document(
171-
"prebuilt-layout", formUrl
172-
)
173-
174-
analyze_request = AnalyzeDocumentRequest(
175-
url_source=formUrl
176-
)
172+
"prebuilt-layout", AnalyzeDocumentRequest(url_source=formUrl
173+
))
177174

178175
result: AnalyzeResult = poller.result()
179176

@@ -548,6 +545,9 @@ Analyze and extract common fields from specific document types using a prebuilt
548545
import os
549546
from azure.core.credentials import AzureKeyCredential
550547
from azure.ai.documentintelligence import DocumentIntelligenceClient
548+
from azure.ai.documentintelligence.models import AnalyzeResult
549+
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest
550+
551551

552552

553553
# set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
@@ -564,7 +564,7 @@ def analyze_invoice():
564564
)
565565

566566
poller = document_intelligence_client.begin_analyze_document(
567-
"prebuilt-invoice", invoiceUrl
567+
"prebuilt-invoice", AnalyzeDocumentRequest(url_source=invoiceUrl
568568
)
569569
invoices = poller.result()
570570

0 commit comments

Comments
 (0)