Share via


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-businessCard""prebuilt-document"사용 중지된 모델에 대한 이전 서비스 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 구독 이 있어야 합니다.

@azure-rest/ai-document-intelligence 패키지를 설치합니다.

을 사용하여 JavaScript용 Azure DocumentIntelligence(이전의FormRecognizer) REST 클라이언트 REST 클라이언트 라이브러리를 npm설치합니다.

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

DocumentIntelligenceClient 만들기 및 인증

AAD(Azure Active Directory) 토큰 자격 증명을 사용하려면 @azure/ID 라이브러리에서 가져온 원하는 자격 증명 형식의 instance 제공합니다.

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"

    전체 파일은 단일 문서로 처리됩니다. 분할이 수행되지 않습니다.

  • 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_LEVEL 환경 변수를 info로 설정합니다. 또는 @azure/logger에서 setLogLevel을 호출하여 런타임에 로깅을 사용하도록 설정할 수 있습니다.

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

setLogLevel("info");

로그를 사용하는 방법에 대한 자세한 내용은 @azure/logger package docs를 참조하세요.