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

82 lines
2.9 KiB
JavaScript

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* This sample shows how to programmatically build a model with a single document type using a training data set.
*
* The Document Intelligence service expects the training data to be organized and labeled according to a particular
* convention and stored in an Azure Storage container. For more information about creating a training data set, please
* see the information at the following link to the service's documentation:
*
* https://aka.ms/azsdk/documentitelligence/buildtrainingset
*
* @summary build a model with a single document type from a training data set
*/
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 random = Date.now().toString();
const modelId =
(process.env.CUSTOM_MODEL_ID || "<model id>") + random.substring(random.length - 6);
const trainingDataSasUrl =
process.env.CUSTOM_MODEL_TRAINING_DATA_SAS_URL || "<training data container SAS url>";
const initialResponse = await client.path("/documentModels:build").post({
body: {
buildMode: "template",
modelId,
azureBlobSource: {
containerUrl: trainingDataSasUrl,
},
},
});
if (isUnexpected(initialResponse)) {
throw initialResponse.body.error;
}
const poller = getLongRunningPoller(client, initialResponse);
const model = (await poller.pollUntilDone()).body.result;
if (!model) {
throw new Error("Expected a DocumentModelDetailsOutput response.");
}
console.log("Model ID:", model.modelId);
console.log("Description:", model.description);
console.log("Created:", model.createdDateTime);
// A model may contain several document types, which describe the possible object structures of fields extracted using
// this model
console.log("Document Types:");
for (const [docType, { description, fieldSchema: schema }] of Object.entries(
model.docTypes || {},
)) {
console.log(`- Name: "${docType}"`);
console.log(` Description: "${description}"`);
// For simplicity, this example will only show top-level field names
console.log(" Fields:");
if (!schema) {
console.log(" <no fields>");
continue;
}
for (const [fieldName, fieldSchema] of Object.entries(schema)) {
console.log(` - "${fieldName}" (${fieldSchema.type})`);
console.log(` ${fieldSchema.description || "<no description>"}`);
}
}
}
main().catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});