DocumentAnalysisClient class

用于与表单识别器服务分析功能的交互的客户端。

例子:

表单识别器服务和客户端支持两种身份验证方式:

Azure Active Directory

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

API 密钥(订阅密钥)

import { AzureKeyCredential, DocumentAnalysisClient } from "@azure/ai-form-recognizer";

const credential = new AzureKeyCredential("<API key>");
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

构造函数

DocumentAnalysisClient(string, KeyCredential, DocumentAnalysisClientOptions)

从资源终结点和静态 API 密钥(KeyCredential)创建 DocumentAnalysisClient 实例

例:

import { AzureKeyCredential, DocumentAnalysisClient } from "@azure/ai-form-recognizer";

const credential = new AzureKeyCredential("<API key>");
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);
DocumentAnalysisClient(string, TokenCredential, DocumentAnalysisClientOptions)

从资源终结点和 Azure 标识 TokenCredential创建 DocumentAnalysisClient 实例。

有关使用 Azure Active Directory 进行身份验证的详细信息,请参阅 @azure/identity 包。

例:

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

方法

beginAnalyzeDocument(string, FormRecognizerRequestBody, AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>)

使用模型的唯一 ID 从输入中提取数据。

此作支持自定义模型和预生成模型。 例如,若要使用预生成的发票模型,请提供模型 ID“prebuilt-invoice”,或使用更简单的预生成布局模型,提供模型 ID“prebuilt-layout”。

AnalyzeResult 中生成的字段取决于用于分析的模型,并且任何提取的文档字段中的值取决于模型中的文档类型(如果有)及其相应的字段架构。

例子

此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS ReadableStream对象、浏览器 BlobArrayBuffer。 正文的内容将上传到服务进行分析。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { createReadStream } from "node:fs";
import { PrebuiltReceiptModel } from "../samples-dev/prebuilt/prebuilt-receipt.js";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const path = "<path to a document>";
const readStream = createReadStream(path);

// The PrebuiltReceiptModel `DocumentModel` instance encodes both the model ID and a stronger return type for the operation
const poller = await client.beginAnalyzeDocument(PrebuiltReceiptModel, readStream, {
  onProgress: ({ status }) => {
    console.log(`status: ${status}`);
  },
});

const {
  documents: [receiptDocument],
} = await poller.pollUntilDone();

// The fields of the document constitute the extracted receipt data.
const receipt = receiptDocument.fields;

if (receipt === undefined) {
  throw new Error("Expected at least one receipt in analysis result.");
}

console.log(`Receipt data (${receiptDocument.docType})`);
console.log("  Merchant Name:", receipt.merchantName?.value);

// The items of the receipt are an example of a `DocumentArrayValue`
if (receipt.items !== undefined) {
  console.log("Items:");
  for (const { properties: item } of receipt.items.values) {
    console.log("- Description:", item.description?.value);
    console.log("  Total Price:", item.totalPrice?.value);
  }
}

console.log("  Total:", receipt.total?.value);
beginAnalyzeDocument<Result>(DocumentModel<Result>, FormRecognizerRequestBody, AnalyzeDocumentOptions<Result>)

使用具有已知强类型文档架构的模型(DocumentModel)从输入中提取数据。

AnalyzeResult 中生成的字段取决于用于分析的模型。 在 TypeScript 中,此方法重载的结果类型从输入 DocumentModel的类型推断。

例子

此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS ReadableStream对象、浏览器 BlobArrayBuffer。 正文的内容将上传到服务进行分析。

如果提供的输入是字符串,它将被视为要分析的文档位置的 URL。 有关详细信息,请参阅 beginAnalyzeDocumentFromUrl 方法。 使用 URL 时,首选使用此方法,并且仅在此方法中提供 URL 支持以实现向后兼容性。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { createReadStream } from "node:fs";
import { PrebuiltReceiptModel } from "../samples-dev/prebuilt/prebuilt-receipt.js";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const path = "<path to a document>";
const readStream = createReadStream(path);

// The PrebuiltReceiptModel `DocumentModel` instance encodes both the model ID and a stronger return type for the operation
const poller = await client.beginAnalyzeDocument(PrebuiltReceiptModel, readStream, {
  onProgress: ({ status }) => {
    console.log(`status: ${status}`);
  },
});

const {
  documents: [receiptDocument],
} = await poller.pollUntilDone();

// The fields of the document constitute the extracted receipt data.
const receipt = receiptDocument.fields;

if (receipt === undefined) {
  throw new Error("Expected at least one receipt in analysis result.");
}

console.log(`Receipt data (${receiptDocument.docType})`);
console.log("  Merchant Name:", receipt.merchantName?.value);

// The items of the receipt are an example of a `DocumentArrayValue`
if (receipt.items !== undefined) {
  console.log("Items:");
  for (const { properties: item } of receipt.items.values) {
    console.log("- Description:", item.description?.value);
    console.log("  Total Price:", item.totalPrice?.value);
  }
}

console.log("  Total:", receipt.total?.value);
beginAnalyzeDocumentFromUrl(string, string, AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>)

使用模型的唯一 ID 从输入中提取数据。

此作支持自定义模型和预生成模型。 例如,若要使用预生成的发票模型,请提供模型 ID“prebuilt-invoice”,或使用更简单的预生成布局模型,提供模型 ID“prebuilt-layout”。

AnalyzeResult 中生成的字段取决于用于分析的模型,并且任何提取的文档字段中的值取决于模型中的文档类型(如果有)及其相应的字段架构。

例子

此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。

import { DefaultAzureCredential } from "@azure/identity";
import {
  DocumentAnalysisClient,
  DocumentStringField,
  DocumentArrayField,
  DocumentObjectField,
} from "@azure/ai-form-recognizer";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const poller = await client.beginAnalyzeDocumentFromUrl(
  "prebuilt-receipt",
  // The Document Intelligence service will access the following URL to a receipt image and extract data from it
  "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/receipt/contoso-receipt.png",
);
poller.onProgress((state) => console.log("Operation:", state.modelId, state.status));

const { documents } = await poller.pollUntilDone();

const result = documents && documents[0];
if (result) {
  const receipt = result.fields;
  console.log("=== Receipt Information ===");
  console.log("Type:", result.docType);
  console.log("Merchant:", (receipt["MerchantName"] as DocumentStringField).value);

  console.log("Items:");
  for (const { properties: item } of ((receipt["Items"] as DocumentArrayField).values ||
    []) as DocumentObjectField[]) {
    console.log("- Description:", (item["Description"] as DocumentStringField).value);
    console.log("  Total Price:", (item["TotalPrice"] as DocumentStringField).value);
  }
} else {
  throw new Error("Expected at least one receipt in the result.");
}
beginAnalyzeDocumentFromUrl<Result>(DocumentModel<Result>, string, AnalyzeDocumentOptions<Result>)

使用具有已知强类型文档架构的模型(DocumentModel)从输入中提取数据。

AnalyzeResult 中生成的字段取决于用于分析的模型。 在 TypeScript 中,此方法重载的结果类型从输入 DocumentModel的类型推断。

例子

此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { PrebuiltReceiptModel } from "../samples-dev/prebuilt/prebuilt-receipt.js";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const poller = await client.beginAnalyzeDocumentFromUrl(
  PrebuiltReceiptModel,
  // The Document Intelligence service will access the following URL to a receipt image and extract data from it
  "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/receipt/contoso-receipt.png",
);

const {
  documents: [document],
} = await poller.pollUntilDone();

// Use of PrebuiltModels.Receipt above (rather than the raw model ID), as it adds strong typing of the model's output
if (document) {
  const { merchantName, items, total } = document.fields;

  console.log("=== Receipt Information ===");
  console.log("Type:", document.docType);
  console.log("Merchant:", merchantName && merchantName.value);

  console.log("Items:");
  for (const item of (items && items.values) || []) {
    const { description, totalPrice } = item.properties;

    console.log("- Description:", description && description.value);
    console.log("  Total Price:", totalPrice && totalPrice.value);
  }

  console.log("Total:", total && total.value);
} else {
  throw new Error("Expected at least one receipt in the result.");
}
beginClassifyDocument(string, FormRecognizerRequestBody, ClassifyDocumentOptions)

使用其 ID 提供的自定义分类器对文档进行分类。

此方法生成一个长时间运行的作(轮询器),最终将生成 AnalyzeResult。 此类型与 beginAnalyzeDocumentbeginAnalyzeDocumentFromUrl相同,但结果将仅包含其字段的一小部分。 仅填充 documents 字段和 pages 字段,只返回最少的页面信息。 documents 字段将包含有关所有已标识文档及其分类为 docType 的信息。

此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS ReadableStream对象、浏览器 BlobArrayBuffer。 正文的内容将上传到服务进行分析。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { createReadStream } from "node:fs";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const path = "<path to a document>";
const readStream = createReadStream(path);

const poller = await client.beginClassifyDocument("<classifier id>", readStream);

const result = await poller.pollUntilDone();

if (result?.documents?.length === 0) {
  throw new Error("Failed to extract any documents.");
}

for (const document of result.documents) {
  console.log(
    `Extracted a document with type '${document.docType}' on page ${document.boundingRegions?.[0].pageNumber} (confidence: ${document.confidence})`,
  );
}
beginClassifyDocumentFromUrl(string, string, ClassifyDocumentOptions)

使用 ID 提供的自定义分类器从 URL 对文档进行分类。

此方法生成一个长时间运行的作(轮询器),最终将生成 AnalyzeResult。 此类型与 beginAnalyzeDocumentbeginAnalyzeDocumentFromUrl相同,但结果将仅包含其字段的一小部分。 仅填充 documents 字段和 pages 字段,只返回最少的页面信息。 documents 字段将包含有关所有已标识文档及其分类为 docType 的信息。

此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const documentUrl =
  "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/invoice/Invoice_1.pdf";

const poller = await client.beginClassifyDocumentFromUrl("<classifier id>", documentUrl);

const result = await poller.pollUntilDone();

if (result?.documents?.length === 0) {
  throw new Error("Failed to extract any documents.");
}

for (const document of result.documents) {
  console.log(
    `Extracted a document with type '${document.docType}' on page ${document.boundingRegions?.[0].pageNumber} (confidence: ${document.confidence})`,
  );
}

构造函数详细信息

DocumentAnalysisClient(string, KeyCredential, DocumentAnalysisClientOptions)

从资源终结点和静态 API 密钥(KeyCredential)创建 DocumentAnalysisClient 实例

例:

import { AzureKeyCredential, DocumentAnalysisClient } from "@azure/ai-form-recognizer";

const credential = new AzureKeyCredential("<API key>");
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);
new DocumentAnalysisClient(endpoint: string, credential: KeyCredential, options?: DocumentAnalysisClientOptions)

参数

endpoint

string

Azure 认知服务实例的终结点 URL

credential
KeyCredential

包含认知服务实例订阅密钥的 KeyCredential

options
DocumentAnalysisClientOptions

用于配置客户端中所有方法的可选设置

DocumentAnalysisClient(string, TokenCredential, DocumentAnalysisClientOptions)

从资源终结点和 Azure 标识 TokenCredential创建 DocumentAnalysisClient 实例。

有关使用 Azure Active Directory 进行身份验证的详细信息,请参阅 @azure/identity 包。

例:

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);
new DocumentAnalysisClient(endpoint: string, credential: TokenCredential, options?: DocumentAnalysisClientOptions)

参数

endpoint

string

Azure 认知服务实例的终结点 URL

credential
TokenCredential

@azure/identity 包中的 TokenCredential 实例

options
DocumentAnalysisClientOptions

用于配置客户端中所有方法的可选设置

方法详细信息

beginAnalyzeDocument(string, FormRecognizerRequestBody, AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>)

使用模型的唯一 ID 从输入中提取数据。

此作支持自定义模型和预生成模型。 例如,若要使用预生成的发票模型,请提供模型 ID“prebuilt-invoice”,或使用更简单的预生成布局模型,提供模型 ID“prebuilt-layout”。

AnalyzeResult 中生成的字段取决于用于分析的模型,并且任何提取的文档字段中的值取决于模型中的文档类型(如果有)及其相应的字段架构。

例子

此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS ReadableStream对象、浏览器 BlobArrayBuffer。 正文的内容将上传到服务进行分析。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { createReadStream } from "node:fs";
import { PrebuiltReceiptModel } from "../samples-dev/prebuilt/prebuilt-receipt.js";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const path = "<path to a document>";
const readStream = createReadStream(path);

// The PrebuiltReceiptModel `DocumentModel` instance encodes both the model ID and a stronger return type for the operation
const poller = await client.beginAnalyzeDocument(PrebuiltReceiptModel, readStream, {
  onProgress: ({ status }) => {
    console.log(`status: ${status}`);
  },
});

const {
  documents: [receiptDocument],
} = await poller.pollUntilDone();

// The fields of the document constitute the extracted receipt data.
const receipt = receiptDocument.fields;

if (receipt === undefined) {
  throw new Error("Expected at least one receipt in analysis result.");
}

console.log(`Receipt data (${receiptDocument.docType})`);
console.log("  Merchant Name:", receipt.merchantName?.value);

// The items of the receipt are an example of a `DocumentArrayValue`
if (receipt.items !== undefined) {
  console.log("Items:");
  for (const { properties: item } of receipt.items.values) {
    console.log("- Description:", item.description?.value);
    console.log("  Total Price:", item.totalPrice?.value);
  }
}

console.log("  Total:", receipt.total?.value);
function beginAnalyzeDocument(modelId: string, document: FormRecognizerRequestBody, options?: AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>): Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>

参数

modelId

string

此客户端资源中模型的唯一 ID (名称)

document
FormRecognizerRequestBody

将随请求一起上传的 FormRecognizerRequestBody

options

AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>

分析作和轮询器的可选设置

返回

一个长时间运行的作(轮询器),最终将产生 AnalyzeResult

beginAnalyzeDocument<Result>(DocumentModel<Result>, FormRecognizerRequestBody, AnalyzeDocumentOptions<Result>)

使用具有已知强类型文档架构的模型(DocumentModel)从输入中提取数据。

AnalyzeResult 中生成的字段取决于用于分析的模型。 在 TypeScript 中,此方法重载的结果类型从输入 DocumentModel的类型推断。

例子

此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS ReadableStream对象、浏览器 BlobArrayBuffer。 正文的内容将上传到服务进行分析。

如果提供的输入是字符串,它将被视为要分析的文档位置的 URL。 有关详细信息,请参阅 beginAnalyzeDocumentFromUrl 方法。 使用 URL 时,首选使用此方法,并且仅在此方法中提供 URL 支持以实现向后兼容性。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { createReadStream } from "node:fs";
import { PrebuiltReceiptModel } from "../samples-dev/prebuilt/prebuilt-receipt.js";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const path = "<path to a document>";
const readStream = createReadStream(path);

// The PrebuiltReceiptModel `DocumentModel` instance encodes both the model ID and a stronger return type for the operation
const poller = await client.beginAnalyzeDocument(PrebuiltReceiptModel, readStream, {
  onProgress: ({ status }) => {
    console.log(`status: ${status}`);
  },
});

const {
  documents: [receiptDocument],
} = await poller.pollUntilDone();

// The fields of the document constitute the extracted receipt data.
const receipt = receiptDocument.fields;

if (receipt === undefined) {
  throw new Error("Expected at least one receipt in analysis result.");
}

console.log(`Receipt data (${receiptDocument.docType})`);
console.log("  Merchant Name:", receipt.merchantName?.value);

// The items of the receipt are an example of a `DocumentArrayValue`
if (receipt.items !== undefined) {
  console.log("Items:");
  for (const { properties: item } of receipt.items.values) {
    console.log("- Description:", item.description?.value);
    console.log("  Total Price:", item.totalPrice?.value);
  }
}

console.log("  Total:", receipt.total?.value);
function beginAnalyzeDocument<Result>(model: DocumentModel<Result>, document: FormRecognizerRequestBody, options?: AnalyzeDocumentOptions<Result>): Promise<AnalysisPoller<Result>>

参数

model

DocumentModel<Result>

DocumentModel 表示要用于分析和预期输出类型的模型

document
FormRecognizerRequestBody

将随请求一起上传的 FormRecognizerRequestBody

options

AnalyzeDocumentOptions<Result>

分析作和轮询器的可选设置

返回

Promise<AnalysisPoller<Result>>

一个长时间运行的作(轮询器),最终将生成一个具有与输入模型关联的结果类型的文档的 AnalyzeResult

beginAnalyzeDocumentFromUrl(string, string, AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>)

使用模型的唯一 ID 从输入中提取数据。

此作支持自定义模型和预生成模型。 例如,若要使用预生成的发票模型,请提供模型 ID“prebuilt-invoice”,或使用更简单的预生成布局模型,提供模型 ID“prebuilt-layout”。

AnalyzeResult 中生成的字段取决于用于分析的模型,并且任何提取的文档字段中的值取决于模型中的文档类型(如果有)及其相应的字段架构。

例子

此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。

import { DefaultAzureCredential } from "@azure/identity";
import {
  DocumentAnalysisClient,
  DocumentStringField,
  DocumentArrayField,
  DocumentObjectField,
} from "@azure/ai-form-recognizer";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const poller = await client.beginAnalyzeDocumentFromUrl(
  "prebuilt-receipt",
  // The Document Intelligence service will access the following URL to a receipt image and extract data from it
  "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/receipt/contoso-receipt.png",
);
poller.onProgress((state) => console.log("Operation:", state.modelId, state.status));

const { documents } = await poller.pollUntilDone();

const result = documents && documents[0];
if (result) {
  const receipt = result.fields;
  console.log("=== Receipt Information ===");
  console.log("Type:", result.docType);
  console.log("Merchant:", (receipt["MerchantName"] as DocumentStringField).value);

  console.log("Items:");
  for (const { properties: item } of ((receipt["Items"] as DocumentArrayField).values ||
    []) as DocumentObjectField[]) {
    console.log("- Description:", (item["Description"] as DocumentStringField).value);
    console.log("  Total Price:", (item["TotalPrice"] as DocumentStringField).value);
  }
} else {
  throw new Error("Expected at least one receipt in the result.");
}
function beginAnalyzeDocumentFromUrl(modelId: string, documentUrl: string, options?: AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>): Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>

参数

modelId

string

此客户端资源中模型的唯一 ID (名称)

documentUrl

string

可从公共 Internet 访问的输入文档的 URL(字符串)

options

AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>

分析作和轮询器的可选设置

返回

一个长时间运行的作(轮询器),最终将产生 AnalyzeResult

beginAnalyzeDocumentFromUrl<Result>(DocumentModel<Result>, string, AnalyzeDocumentOptions<Result>)

使用具有已知强类型文档架构的模型(DocumentModel)从输入中提取数据。

AnalyzeResult 中生成的字段取决于用于分析的模型。 在 TypeScript 中,此方法重载的结果类型从输入 DocumentModel的类型推断。

例子

此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { PrebuiltReceiptModel } from "../samples-dev/prebuilt/prebuilt-receipt.js";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const poller = await client.beginAnalyzeDocumentFromUrl(
  PrebuiltReceiptModel,
  // The Document Intelligence service will access the following URL to a receipt image and extract data from it
  "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/receipt/contoso-receipt.png",
);

const {
  documents: [document],
} = await poller.pollUntilDone();

// Use of PrebuiltModels.Receipt above (rather than the raw model ID), as it adds strong typing of the model's output
if (document) {
  const { merchantName, items, total } = document.fields;

  console.log("=== Receipt Information ===");
  console.log("Type:", document.docType);
  console.log("Merchant:", merchantName && merchantName.value);

  console.log("Items:");
  for (const item of (items && items.values) || []) {
    const { description, totalPrice } = item.properties;

    console.log("- Description:", description && description.value);
    console.log("  Total Price:", totalPrice && totalPrice.value);
  }

  console.log("Total:", total && total.value);
} else {
  throw new Error("Expected at least one receipt in the result.");
}
function beginAnalyzeDocumentFromUrl<Result>(model: DocumentModel<Result>, documentUrl: string, options?: AnalyzeDocumentOptions<Result>): Promise<AnalysisPoller<Result>>

参数

model

DocumentModel<Result>

DocumentModel 表示要用于分析和预期输出类型的模型

documentUrl

string

可从公共 Internet 访问的输入文档的 URL(字符串)

options

AnalyzeDocumentOptions<Result>

分析作和轮询器的可选设置

返回

Promise<AnalysisPoller<Result>>

一个长时间运行的作(轮询器),最终将产生 AnalyzeResult

beginClassifyDocument(string, FormRecognizerRequestBody, ClassifyDocumentOptions)

使用其 ID 提供的自定义分类器对文档进行分类。

此方法生成一个长时间运行的作(轮询器),最终将生成 AnalyzeResult。 此类型与 beginAnalyzeDocumentbeginAnalyzeDocumentFromUrl相同,但结果将仅包含其字段的一小部分。 仅填充 documents 字段和 pages 字段,只返回最少的页面信息。 documents 字段将包含有关所有已标识文档及其分类为 docType 的信息。

此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS ReadableStream对象、浏览器 BlobArrayBuffer。 正文的内容将上传到服务进行分析。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { createReadStream } from "node:fs";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const path = "<path to a document>";
const readStream = createReadStream(path);

const poller = await client.beginClassifyDocument("<classifier id>", readStream);

const result = await poller.pollUntilDone();

if (result?.documents?.length === 0) {
  throw new Error("Failed to extract any documents.");
}

for (const document of result.documents) {
  console.log(
    `Extracted a document with type '${document.docType}' on page ${document.boundingRegions?.[0].pageNumber} (confidence: ${document.confidence})`,
  );
}
function beginClassifyDocument(classifierId: string, document: FormRecognizerRequestBody, options?: ClassifyDocumentOptions): Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>

参数

classifierId

string

要用于分析的自定义分类器的 ID

document
FormRecognizerRequestBody

要分类的文档

options
ClassifyDocumentOptions

分类作的选项

返回

一个长时间运行的作(轮询器),最终将产生 AnalyzeResult

beginClassifyDocumentFromUrl(string, string, ClassifyDocumentOptions)

使用 ID 提供的自定义分类器从 URL 对文档进行分类。

此方法生成一个长时间运行的作(轮询器),最终将生成 AnalyzeResult。 此类型与 beginAnalyzeDocumentbeginAnalyzeDocumentFromUrl相同,但结果将仅包含其字段的一小部分。 仅填充 documents 字段和 pages 字段,只返回最少的页面信息。 documents 字段将包含有关所有已标识文档及其分类为 docType 的信息。

此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。

import { DefaultAzureCredential } from "@azure/identity";
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";

const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(
  "https://<resource name>.cognitiveservices.azure.com",
  credential,
);

const documentUrl =
  "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/invoice/Invoice_1.pdf";

const poller = await client.beginClassifyDocumentFromUrl("<classifier id>", documentUrl);

const result = await poller.pollUntilDone();

if (result?.documents?.length === 0) {
  throw new Error("Failed to extract any documents.");
}

for (const document of result.documents) {
  console.log(
    `Extracted a document with type '${document.docType}' on page ${document.boundingRegions?.[0].pageNumber} (confidence: ${document.confidence})`,
  );
}
function beginClassifyDocumentFromUrl(classifierId: string, documentUrl: string, options?: ClassifyDocumentOptions): Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>

参数

classifierId

string

要用于分析的自定义分类器的 ID

documentUrl

string

要分类的文档的 URL

返回