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
65 lines
2.4 KiB
JavaScript
65 lines
2.4 KiB
JavaScript
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
/**
|
|
* This sample shows how to copy a model from one resource to another. The model is created with a new model ID (and
|
|
* optionally a new description) in the destination resource, but will have the same document types as the source model.
|
|
*
|
|
* @summary copy a model from one resource to another
|
|
*/
|
|
|
|
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 random = Date.now().toString();
|
|
const destinationModelId =
|
|
(process.env.CUSTOM_MODEL_ID || "<model id>") + random.substring(random.length - 6);
|
|
|
|
// The authorization must be created by the destination resource.
|
|
// const destinationClient = new DocumentModelAdministrationClient(endpoint, credential);
|
|
const destinationClient = DocumentIntelligence(
|
|
process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"] || "<cognitive services endpoint>",
|
|
new DefaultAzureCredential(),
|
|
);
|
|
// const authorization = await destinationClient.getCopyAuthorization(destinationModelId);
|
|
const targetAuth = await destinationClient.path("/documentModels:authorizeCopy").post({
|
|
body: {
|
|
modelId: destinationModelId,
|
|
},
|
|
});
|
|
if (isUnexpected(targetAuth)) {
|
|
throw targetAuth.body.error;
|
|
}
|
|
const sourceEndpoint = process.env.DOCUMENT_INTELLIGENCE_SOURCE_ENDPOINT || "<source endpoint>";
|
|
const sourceModelId = process.env.COPY_SOURCE_MODEL_ID || "<source model id>";
|
|
|
|
// Then, the source resource can initiate the copy operation.
|
|
const sourceClient = DocumentIntelligence(sourceEndpoint, {
|
|
key: process.env.DOCUMENT_INTELLIGENCE_SOURCE_API_KEY || "<source api key>",
|
|
});
|
|
|
|
const copyInitResponse = await sourceClient
|
|
.path("/documentModels/{modelId}:copyTo", sourceModelId)
|
|
.post({
|
|
body: targetAuth.body,
|
|
});
|
|
|
|
if (isUnexpected(copyInitResponse)) {
|
|
throw copyInitResponse.body.error;
|
|
}
|
|
const copyPoller = getLongRunningPoller(sourceClient, copyInitResponse);
|
|
const model = (await copyPoller.pollUntilDone()).body.result;
|
|
|
|
console.log("Model ID:", model.modelId);
|
|
console.log("Description:", model.description);
|
|
console.log("Created:", model.createdDateTime);
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error("An error occurred:", error);
|
|
process.exit(1);
|
|
});
|