Files
Deyaaeldeen AlmahallawiandGitHub 07131a27f7 [document intelligence] Use safe secrets (#33551)
Overhauls tests and samples for Azure Cognitive Services for Language
SDKs by implementing safe secret standards:
- Updates `test-resources.bicep` to not output API keys nor SAS tokens
- Updates the tests to retrieve API keys using the arm client
- Updates the tests to create SAS tokens for Storage container access
- Updates the samples and snippets to use Entra ID authentication
- Fix live tests by enabling SAS access to the persistent storage
account

Live tests run:
https://dev.azure.com/azure-sdk/internal/_build/results?buildId=4694655&view=results
2025-03-27 17:02:52 -07:00

60 lines
2.3 KiB
JavaScript

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* This sample shows how to extract elements of a receipt from a URL to a file using the prebuilt receipt model. Rather
* than using the `PrebuiltModels.Receipt` document model, this sample shows the use of the prebuilt model by ID,
* resulting in a weaker type that exactly mirrors the model's field schema at runtime.
*
* The prebuilt receipt model can return several fields. For a detailed list of the fields supported by the
* receipt model, see the `Receipt` type in the documentation, or refer to the following link:
*
* https://aka.ms/azsdk/documentitelligence/receiptfieldschema
*
* @summary use the "prebuilt-receipt" model ID to extract data from a receipt document (weakly-typed)
*/
const DocumentIntelligence = require("@azure-rest/ai-document-intelligence").default,
{ getLongRunningPoller, isUnexpected } = require("@azure-rest/ai-document-intelligence");
const { DefaultAzureCredential } = require("@azure/identity");
require("dotenv/config");
async function main() {
const client = DocumentIntelligence(
process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"] || "<cognitive services endpoint>",
new DefaultAzureCredential(),
);
const initialResponse = await client
.path("/documentModels/{modelId}:analyze", "prebuilt-receipt")
.post({
contentType: "application/json",
body: {
// The Document Intelligence service will access the following URL to a receipt image and extract data from it
urlSource:
"https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/receipt/contoso-receipt.png",
},
});
if (isUnexpected(initialResponse)) {
throw initialResponse.body.error;
}
const poller = getLongRunningPoller(client, initialResponse);
await 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 receipt in the result.");
}
}
main().catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});