JavaScript 用 Azure DocumentIntelligence (旧称 FormRecognizer) REST クライアント ライブラリ - バージョン 1.0.0-beta.2

ドキュメントからコンテンツ、レイアウト、構造化データを抽出します。

このライブラリを使用するには、 REST クライアント のドキュメント に大きく依存してください

注: Form Recognizerはドキュメント インテリジェンスにブランド変更されました。 移行ガイド@azure/ai-form-recognizerを から に@azure-rest/ai-document-intelligenceチェックしてください。

主要リンク:

このバージョンのクライアント ライブラリは、既定でサービスの "2024-02-29-preview" バージョンになります。

SDK のバージョンとサービスのサポートされる API バージョンの関係を次の表に示します。

SDK バージョン サポートされている API バージョンのサービス
1.0.0-beta.2 2024-02-29-preview
1.0.0-beta.1 2023-10-31-preview

"prebuilt-document"などの"prebuilt-businessCard"廃止されたモデルについては、古いサービス API バージョンを通じて古い@azure/ai-form-recognizerライブラリに依存してください。 詳細については、「 Changelog」を参照してください。

次の表は、各クライアントとそのサポートされている API バージョンの関係を示しています。

サービス API のバージョン サポートされるクライアント パッケージ
2024-02-29-preview DocumentIntelligenceClient @azure-rest/ai-document-intelligence バージョン 1.0.0-beta.2
2023-10-31-preview DocumentIntelligenceClient @azure-rest/ai-document-intelligence バージョン 1.0.0-beta.1
2023-07-31 DocumentAnalysisClient と DocumentModelAdministrationClient @azure/ai-form-recognizer バージョン ^5.0.0
2022-08-01 DocumentAnalysisClient と DocumentModelAdministrationClient @azure/ai-form-recognizer バージョン ^4.0.0

はじめに

現在サポートされている環境

  • Node.js の LTS バージョン

前提条件

@azure-rest/ai-document-intelligence パッケージのインストール

を使用して、JavaScript 用の Azure DocumentIntelligence(旧称FormRecognizer) REST クライアント REST クライアント ライブラリを npmインストールします。

npm install @azure-rest/ai-document-intelligence

DocumentIntelligenceClient を作成して認証する

Azure Active Directory (AAD) トークン資格情報を使用するには、@azure/ID ライブラリから取得した目的の資格情報の種類のインスタンスを指定します。

AAD で認証するには、最初 npm に をインストールする必要があります。 @azure/identity

セットアップ後、使用する資格情報@azure/identityの種類を選択できます。 たとえば、 DefaultAzureCredential を使用してクライアントを認証できます。

AAD アプリケーションのクライアント ID、テナント ID、クライアント シークレットの値を環境変数として設定します(AZURE_CLIENT_ID、AZURE_TENANT_ID、AZURE_CLIENT_SECRET

トークン資格情報の使用

import DocumentIntelligence from "@azure-rest/ai-document-intelligence";

const client = DocumentIntelligence(
  process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"],
  new DefaultAzureCredential()
);

API キーの使用

import DocumentIntelligence from "@azure-rest/ai-document-intelligence";

const client = DocumentIntelligence(process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"], {
  key: process.env["DOCUMENT_INTELLIGENCE_API_KEY"],
});

ドキュメント モデル

事前構築済みレイアウトの分析 (urlSource)

const initialResponse = await client
  .path("/documentModels/{modelId}:analyze", "prebuilt-layout")
  .post({
    contentType: "application/json",
    body: {
      urlSource:
        "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/6704eff082aaaf2d97c1371a28461f512f8d748a/sdk/formrecognizer/ai-form-recognizer/assets/forms/Invoice_1.pdf",
    },
    queryParameters: { locale: "en-IN" },
  });

事前構築済みレイアウトの分析 (base64Source)

import fs from "fs";
import path from "path";

const filePath = path.join(ASSET_PATH, "forms", "Invoice_1.pdf");
const base64Source = fs.readFileSync(filePath, { encoding: "base64" });
const initialResponse = await client
  .path("/documentModels/{modelId}:analyze", "prebuilt-layout")
  .post({
    contentType: "application/json",
    body: {
      base64Source,
    },
    queryParameters: { locale: "en-IN" },
  });

最初の応答からポーラーを作成し続ける

import {
  getLongRunningPoller,
  AnalyzeResultOperationOutput,
  isUnexpected,
} from "@azure-rest/ai-document-intelligence";

if (isUnexpected(initialResponse)) {
  throw initialResponse.body.error;
}
const poller = await getLongRunningPoller(client, initialResponse);
const result = (await poller.pollUntilDone()).body as AnalyzeResultOperationOutput;
console.log(result);
// {
//   status: 'succeeded',
//   createdDateTime: '2023-11-10T13:31:31Z',
//   lastUpdatedDateTime: '2023-11-10T13:31:34Z',
//   analyzeResult: {
//     apiVersion: '2023-10-31-preview',
//     .
//     .
//     .
//     contentFormat: 'text'
//   }
// }

Markdown コンテンツの形式

Markdown コンテンツ形式と既定のプレーン テキストを使用した出力をサポート します。 現時点では、これは "事前構築済みレイアウト" でのみサポートされています。 Markdown コンテンツ形式は、チャットまたは自動化の使用シナリオで LLM を使用するためのよりわかりやすい形式と見なされます。

サービスは、Markdown 形式の GFM 仕様 (GitHub Flavored Markdown) に従います。 また、結果のコンテンツ形式を示すために、値 "text" または "markdown" を持つ新しい contentFormat プロパティも導入します。

import DocumentIntelligence from "@azure-rest/ai-document-intelligence";
const client = DocumentIntelligence(process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"], {
  key: process.env["DOCUMENT_INTELLIGENCE_API_KEY"],
});

const initialResponse = await client
  .path("/documentModels/{modelId}:analyze", "prebuilt-layout")
  .post({
    contentType: "application/json",
    body: {
      urlSource:
        "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/6704eff082aaaf2d97c1371a28461f512f8d748a/sdk/formrecognizer/ai-form-recognizer/assets/forms/Invoice_1.pdf",
    },
    queryParameters: { outputContentFormat: "markdown" }, // <-- new query parameter
  });

クエリ フィールド

この機能フラグを指定すると、サービスは queryFields クエリ パラメーターで指定されたフィールドの値をさらに抽出して、モデルでフォールバックとして定義されている既存のフィールドを補完します。

await client.path("/documentModels/{modelId}:analyze", "prebuilt-layout").post({
  contentType: "application/json",
  body: { urlSource: "..." },
  queryParameters: {
    features: ["queryFields"],
    queryFields: ["NumberOfGuests", "StoreNumber"],
  }, // <-- new query parameter
});

分割オプション

以前 @azure/ai-form-recognizer のライブラリでサポートされていた以前の API バージョンでは、ドキュメントの分割と分類操作 ("/documentClassifiers/{classifierId}:analyze") では、常に入力ファイルを複数のドキュメントに分割しようとしました。

より広範なシナリオのセットを有効にするために、サービスでは、新しい "2023-10-31-preview" サービス バージョンで "分割" クエリ パラメーターが導入されています。 サポートされている値を次に示します。

  • split: "auto"

    サービスが分割する場所を決定できるようにします。

  • split: "none"

    ファイル全体が 1 つのドキュメントとして扱われます。 分割は実行されません。

  • split: "perPage"

    各ページは個別のドキュメントとして扱われます。 各空のページは、独自のドキュメントとして保持されます。

ドキュメント分類子の #Build

import {
  DocumentClassifierBuildOperationDetailsOutput,
  getLongRunningPoller,
  isUnexpected,
} from "@azure-rest/ai-document-intelligence";

const containerSasUrl = (): string =>
  process.env["DOCUMENT_INTELLIGENCE_TRAINING_CONTAINER_SAS_URL"];
const initialResponse = await client.path("/documentClassifiers:build").post({
  body: {
    classifierId: `customClassifier${getRandomNumber()}`,
    description: "Custom classifier description",
    docTypes: {
      foo: {
        azureBlobSource: {
          containerUrl: containerSasUrl(),
        },
      },
      bar: {
        azureBlobSource: {
          containerUrl: containerSasUrl(),
        },
      },
    },
  },
});

if (isUnexpected(initialResponse)) {
  throw initialResponse.body.error;
}
const poller = await getLongRunningPoller(client, initialResponse);
const response = (await poller.pollUntilDone())
  .body as DocumentClassifierBuildOperationDetailsOutput;
console.log(response);
//  {
//    operationId: '31466834048_f3ee629e-73fb-48ab-993b-1d55d73ca460',
//    kind: 'documentClassifierBuild',
//    status: 'succeeded',
//    .
//    .
//    result: {
//      classifierId: 'customClassifier10978',
//      createdDateTime: '2023-11-09T12:45:56Z',
//      .
//      .
//      description: 'Custom classifier description'
//    },
//    apiVersion: '2023-10-31-preview'
//  }

情報を取得する

const response = await client.path("/info").get();
if (isUnexpected(response)) {
  throw response.body.error;
}
console.log(response.body.customDocumentModels.limit);
// 20000

ドキュメント モデルの一覧表示

import { paginate } from "@azure-rest/ai-document-intelligence";
const response = await client.path("/documentModels").get();
if (isUnexpected(response)) {
  throw response.body.error;
}

const modelsInAccount: string[] = [];
for await (const model of paginate(client, response)) {
  console.log(model.modelId);
}

トラブルシューティング

ログ記録

ログの記録を有効にすると、エラーに関する有用な情報を明らかにするのに役立つ場合があります。 HTTP 要求と応答のログを表示するには、環境変数 AZURE_LOG_LEVELinfo に設定します。 または、@azure/loggersetLogLevel を呼び出して、実行時にログ記録を有効にすることもできます。

const { setLogLevel } = require("@azure/logger");

setLogLevel("info");

ログを有効にする方法の詳細については、@azure/logger パッケージに関するドキュメントを参照してください。