mirror of
https://github.com/hanzoai/azure-sdk-for-js.git
synced 2026-10-30 19:51:51 +00:00
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
56 lines
1.9 KiB
JavaScript
56 lines
1.9 KiB
JavaScript
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
/**
|
|
* This sample shows how to analyze a document using a model with a given ID. The model ID may refer to any model,
|
|
* whether custom, prebuilt, composed, etc.
|
|
*
|
|
* @summary analyze a document using a model by ID
|
|
*/
|
|
|
|
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 modelId = process.env.DOCUMENT_INTELLIGENCE_CUSTOM_MODEL_ID || "<custom model ID>"; // "prebuilt-layout";
|
|
|
|
const initialResponse = await client.path("/documentModels/{modelId}:analyze", modelId).post({
|
|
contentType: "application/json",
|
|
body: {
|
|
urlSource:
|
|
"https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/receipt/contoso-receipt.png",
|
|
},
|
|
queryParameters: { locale: "en-IN" },
|
|
});
|
|
if (isUnexpected(initialResponse)) {
|
|
throw initialResponse.body.error;
|
|
}
|
|
const poller = getLongRunningPoller(client, initialResponse);
|
|
const analyzeResult = (await poller.pollUntilDone()).body.analyzeResult;
|
|
|
|
const documents = analyzeResult?.documents;
|
|
|
|
const document = documents && documents[0];
|
|
if (!document) {
|
|
throw new Error("Expected at least one document in the result.");
|
|
}
|
|
|
|
console.log(
|
|
"Extracted document:",
|
|
document.docType,
|
|
`(confidence: ${document.confidence || "<undefined>"})`,
|
|
);
|
|
console.log("Fields:", document.fields);
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error("An error occurred:", error);
|
|
process.exit(1);
|
|
});
|