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
59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
/**
|
|
* This sample shows how to use a custom classifier to get the document type (class) of a document.
|
|
*
|
|
* @summary use a custom classifier to classify a document
|
|
*/
|
|
|
|
import type { AnalyzeOperationOutput } from "@azure-rest/ai-document-intelligence";
|
|
import DocumentIntelligence, {
|
|
getLongRunningPoller,
|
|
isUnexpected,
|
|
} from "@azure-rest/ai-document-intelligence";
|
|
import { DefaultAzureCredential } from "@azure/identity";
|
|
import "dotenv/config";
|
|
|
|
async function main(): Promise<void> {
|
|
const client = DocumentIntelligence(
|
|
process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"] || "<cognitive services endpoint>",
|
|
new DefaultAzureCredential(),
|
|
);
|
|
const documentUrl =
|
|
"https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/invoice/Invoice_1.pdf";
|
|
|
|
const classifierId = process.env.CUSTOM_CLASSIFIER_ID ?? "<classifier id>";
|
|
const initialResponse = await client
|
|
.path("/documentClassifiers/{classifierId}:analyze", classifierId)
|
|
.post({
|
|
contentType: "application/json",
|
|
body: {
|
|
urlSource: documentUrl,
|
|
},
|
|
});
|
|
|
|
if (isUnexpected(initialResponse)) {
|
|
throw initialResponse.body.error;
|
|
}
|
|
|
|
const poller = getLongRunningPoller(client, initialResponse);
|
|
const analyzeResult = ((await poller.pollUntilDone()).body as AnalyzeOperationOutput)
|
|
.analyzeResult;
|
|
|
|
if (analyzeResult?.documents === undefined || analyzeResult.documents.length === 0) {
|
|
throw new Error("Failed to extract any documents.");
|
|
}
|
|
|
|
for (const document of analyzeResult.documents) {
|
|
console.log(
|
|
`Extracted a document with type '${document.docType}' on page ${document.boundingRegions?.[0].pageNumber} (confidence: ${document.confidence})`,
|
|
);
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error("An error occurred:", error);
|
|
process.exit(1);
|
|
});
|