Skip to content

joheredi/ai-document-translation

Repository files navigation

Document Translation Low Level Client

Prototype for Document Translation that's a general REST client combined with service-specific TypeScript types to help navigate and use the REST API directly.

Install

npm install https://github.com/joheredi/ai-document-translation

Prerequisites

Create and authenticate a DocumentTranslation client

To create a client object to access the Document Translation API, you will need the endpoint of your Document Translation resource and a credential. The Document Translation client can use either Azure Active Directory credentials or an API key credential to authenticate.

You can find the endpoint for your Translator resource in the Azure Portal

https://<NAME-OF-YOUR-RESOURCE>.cognitiveservices.azure.com

Using an API Key

Use the Azure Portal to browse to your Document Translation resource and retrieve an API key, or use the Azure CLI snippet below:

Note: Sometimes the API key is referred to as a "subscription key" or "subscription API key."

az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>

Once you have an API key and endpoint, you can use a KeyCredential object to authenticate the client as follows:

import { createBatchDocumentTranslationVerbFirst } from "@azure/ai-document-translation";

const client = createBatchDocumentTranslationVerbFirst("<endpoint>", {
  key: "<API key>",
});

Using an Azure Active Directory Credential

Client API key authentication is used in most of the examples, but you can also authenticate with Azure Active Directory using the Azure Identity library. To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK. Install @azure/identity

npm install @azure/identity

You will also need to register a new AAD application and grant access to Translator by assigning the "Cognitive Services User" role to your service principal (note: other roles such as "Owner" will not grant the necessary permissions, only "Cognitive Services User" will suffice to run the examples and the sample code).

Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET.

// import {createBatchDocumentTranslationVerbFirst as DocumentTranslation} from @azure/ai-document-translation;
import { createBatchDocumentTranslationPathFirst } from "@azure/ai-document-translation";
import { DefaultAzureCredential } from "@azure/identity";

const client = createBatchDocumentTranslationPathFirst(
  "<endpoint>",
  new DefaultAzureCredential()
);

Samples with Path First

Call an endpoint

import { createBatchDocumentTranslationPathFirst } from "@azure/ai-document-translation";

const endpoint = process.env["ENDPOINT"] || "";
const key = process.env["API_KEY"] || "<API KEY>";

const client = createBatchDocumentTranslationPathFirst(endpoint, { key });
const formatsResult = await client.path("https://accionvegana.org/accio/0ITbvNmLiVHa0l2Z6MHc0/documents/formats").get();

if (formatsResult.status === 200) {
  for (const item of formatsResult.body.value) {
    console.log(item.format);
  }
}

Call an arbitrary endpoint with pathUnchecked

import { createBatchDocumentTranslationPathFirst } from "@azure/ai-document-translation";

const endpoint = process.env["ENDPOINT"] || "";
const key = process.env["API_KEY"] || "<API KEY>";

const client = createBatchDocumentTranslationPathFirst(endpoint, { key });
const formatsResult = await client.pathUnckecked("https://accionvegana.org/accio/0ITbvNmLiVHa0l2Z6MHc0/documents/formats").get();

if (formatsResult.status === 200) {
  for (const item of formatsResult.body.value) {
    console.log(item.format);
  }
}

Using a subClient

The PathFirst client allows building on the current path, which can be helpful to reuse commonly used paths

import { createBatchDocumentTranslationPathFirst } from "@azure/ai-document-translation";

const client = createBatchDocumentTranslationPathFirst(endpoint, { key });
const batchClient = client.path("https://accionvegana.org/accio/0ITbvNmLiVHa0l2Z6MHc0/batches");
const batches = await batchClient.get();
console.log(batches);
const batchDocuments = batchClient.path("/:id/documents", "batchId");
const allDocuments = await batchClient.get();
console.log(allDocuments);
const document = await batchDocuments.path("/:documentId", "documentId").get();
console.log(document);

Samples with Verb First

Call an endpoint

import { createBatchDocumentTranslationVerbFirst } from "@azure/ai-document-translation";

const endpoint = process.env["ENDPOINT"] || "";
const key = process.env["API_KEY"] || "<API KEY>";

const client = createBatchDocumentTranslationVerbFirst(endpoint, { key });
const formatsResult = await client.request("GET /documents/formats");

if (formatsResult.status === 200) {
  for (const format of formatsResult.body.value) {
    console.log(format.format);
  }
}

Call an arbitrary endpoint with requestUnchecked

import { createBatchDocumentTranslationVerbFirst } from "@azure/ai-document-translation";

const endpoint = process.env["ENDPOINT"] || "";
const key = process.env["API_KEY"] || "<API KEY>";

const client = createBatchDocumentTranslationVerbFirst(endpoint, { key });
const formatsResult = await client.requestUnchecked("GET /documents/formats");

if (formatsResult.status === 200) {
  for (const format of formatsResult.body.value) {
    console.log(format.format);
  }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published