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,
);
建構函式
| Document |
從資源端點和靜態 API 金鑰建立 例:
|
| Document |
從資源端點和 Azure 身分識別 如需使用 Azure Active Directory 進行驗證的詳細資訊,請參閱 例:
|
方法
| begin |
使用其唯一標識碼所提供的模型,從輸入擷取數據。 此作業支援自定義和預先建置的模型。 例如,若要使用預先建置的發票模型,請提供模型標識碼 「prebuilt-invoice」,或使用更簡單的預先建置版面配置模型,提供模型標識符 「prebuilt-layout」。。
例子此方法支援可串流要求主體(FormRecognizerRequestBody),例如Node.JS
|
| begin |
使用具有已知強型別文件架構的模型從輸入擷取數據(DocumentModel)。
例子此方法支援可串流要求主體(FormRecognizerRequestBody),例如Node.JS 如果提供的輸入是字串,則會將它視為要分析之檔位置的URL。 如需詳細資訊,請參閱 beginAnalyzeDocumentFromUrl 方法。 使用 URL 時,最好使用該方法,而且只有在此方法中才提供 URL 支援,以提供回溯相容性。
|
| begin |
使用其唯一標識碼所提供的模型,從輸入擷取數據。 此作業支援自定義和預先建置的模型。 例如,若要使用預先建置的發票模型,請提供模型標識碼 「prebuilt-invoice」,或使用更簡單的預先建置版面配置模型,提供模型標識符 「prebuilt-layout」。。
例子這個方法支援從指定 URL 的檔案擷取數據。 窗體辨識器服務會嘗試使用提交的 URL 下載檔案,因此必須可從公用因特網存取該 URL。 例如,SAS 令牌可用來授與 Azure 記憶體中 Blob 的讀取許可權,而服務會使用 SAS 編碼的 URL 來要求檔案。
|
| begin |
使用具有已知強型別文件架構的模型從輸入擷取數據(DocumentModel)。
例子這個方法支援從指定 URL 的檔案擷取數據。 窗體辨識器服務會嘗試使用提交的 URL 下載檔案,因此必須可從公用因特網存取該 URL。 例如,SAS 令牌可用來授與 Azure 記憶體中 Blob 的讀取許可權,而服務會使用 SAS 編碼的 URL 來要求檔案。
|
| begin |
使用識別碼所指定的自定義分類器來分類檔。 這個方法會產生長時間執行的作業 (poller),最終會產生 例此方法支援可串流要求主體(FormRecognizerRequestBody),例如Node.JS
|
| begin |
使用標識碼所提供的自定義分類器,從 URL 分類檔。 這個方法會產生長時間執行的作業 (poller),最終會產生 例這個方法支援從指定 URL 的檔案擷取數據。 窗體辨識器服務會嘗試使用提交的 URL 下載檔案,因此必須可從公用因特網存取該 URL。 例如,SAS 令牌可用來授與 Azure 記憶體中 Blob 的讀取許可權,而服務會使用 SAS 編碼的 URL 來要求檔案。
|
建構函式詳細資料
DocumentAnalysisClient(string, KeyCredential, DocumentAnalysisClientOptions)
從資源端點和靜態 API 金鑰建立 DocumentAnalysisClient 實例 (KeyCredential),
例:
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 身分識別 DocumentAnalysisClient建立 TokenCredential 實例。
如需使用 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>>)
使用其唯一標識碼所提供的模型,從輸入擷取數據。
此作業支援自定義和預先建置的模型。 例如,若要使用預先建置的發票模型,請提供模型標識碼 「prebuilt-invoice」,或使用更簡單的預先建置版面配置模型,提供模型標識符 「prebuilt-layout」。。
AnalyzeResult 中產生的欄位取決於用於分析的模型,而所擷取檔欄位中的值取決於模型中的檔類型(如果有的話),以及其對應的欄位架構。
例子
此方法支援可串流要求主體(FormRecognizerRequestBody),例如Node.JS ReadableStream对象、瀏覽器 Blob和 ArrayBuffers。 本文的內容將會上傳至服務進行分析。
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
這個客戶端資源內模型的唯一識別元(名稱)
- document
- FormRecognizerRequestBody
將隨要求上傳的 FormRecognizerRequestBody
分析作業和輪詢器選擇性設定
傳回
Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
長時間執行的作業(投票器)最終會產生 AnalyzeResult
beginAnalyzeDocument<Result>(DocumentModel<Result>, FormRecognizerRequestBody, AnalyzeDocumentOptions<Result>)
使用具有已知強型別文件架構的模型從輸入擷取數據(DocumentModel)。
AnalyzeResult 中產生的欄位取決於用於分析的模型。 在 TypeScript 中,這個方法多載的結果類型是從輸入的類型推斷 DocumentModel。
例子
此方法支援可串流要求主體(FormRecognizerRequestBody),例如Node.JS ReadableStream对象、瀏覽器 Blob和 ArrayBuffers。 本文的內容將會上傳至服務進行分析。
如果提供的輸入是字串,則會將它視為要分析之檔位置的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>>)
使用其唯一標識碼所提供的模型,從輸入擷取數據。
此作業支援自定義和預先建置的模型。 例如,若要使用預先建置的發票模型,請提供模型標識碼 「prebuilt-invoice」,或使用更簡單的預先建置版面配置模型,提供模型標識符 「prebuilt-layout」。。
AnalyzeResult 中產生的欄位取決於用於分析的模型,而所擷取檔欄位中的值取決於模型中的檔類型(如果有的話),以及其對應的欄位架構。
例子
這個方法支援從指定 URL 的檔案擷取數據。 窗體辨識器服務會嘗試使用提交的 URL 下載檔案,因此必須可從公用因特網存取該 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
這個客戶端資源內模型的唯一識別元(名稱)
- documentUrl
-
string
可從公用因特網存取之輸入檔案的網址(字串)
分析作業和輪詢器選擇性設定
傳回
Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
長時間執行的作業(投票器)最終會產生 AnalyzeResult
beginAnalyzeDocumentFromUrl<Result>(DocumentModel<Result>, string, AnalyzeDocumentOptions<Result>)
使用具有已知強型別文件架構的模型從輸入擷取數據(DocumentModel)。
AnalyzeResult 中產生的欄位取決於用於分析的模型。 在 TypeScript 中,這個方法多載的結果類型是從輸入的類型推斷 DocumentModel。
例子
這個方法支援從指定 URL 的檔案擷取數據。 窗體辨識器服務會嘗試使用提交的 URL 下載檔案,因此必須可從公用因特網存取該 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
可從公用因特網存取之輸入檔案的網址(字串)
- options
-
AnalyzeDocumentOptions<Result>
分析作業和輪詢器選擇性設定
傳回
Promise<AnalysisPoller<Result>>
長時間執行的作業(投票器)最終會產生 AnalyzeResult
beginClassifyDocument(string, FormRecognizerRequestBody, ClassifyDocumentOptions)
使用識別碼所指定的自定義分類器來分類檔。
這個方法會產生長時間執行的作業 (poller),最終會產生 AnalyzeResult。 這與 beginAnalyzeDocument 和 beginAnalyzeDocumentFromUrl相同,但結果只會包含其字段的小子集。 只會填入 [documents] 字段和 [pages] 字段,而且只會傳回最少的頁面資訊。 [documents] 字段將包含所有已識別檔及其分類為 docType 的相關信息。
例
此方法支援可串流要求主體(FormRecognizerRequestBody),例如Node.JS ReadableStream对象、瀏覽器 Blob和 ArrayBuffers。 本文的內容將會上傳至服務進行分析。
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
要用於分析之自定義分類器的標識碼
- document
- FormRecognizerRequestBody
要分類的檔
- options
- ClassifyDocumentOptions
分類作業的選項
傳回
Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
長時間執行的作業(投票器)最終會產生 AnalyzeResult
beginClassifyDocumentFromUrl(string, string, ClassifyDocumentOptions)
使用標識碼所提供的自定義分類器,從 URL 分類檔。
這個方法會產生長時間執行的作業 (poller),最終會產生 AnalyzeResult。 這與 beginAnalyzeDocument 和 beginAnalyzeDocumentFromUrl相同,但結果只會包含其字段的小子集。 只會填入 [documents] 字段和 [pages] 字段,而且只會傳回最少的頁面資訊。 [documents] 字段將包含所有已識別檔及其分類為 docType 的相關信息。
例
這個方法支援從指定 URL 的檔案擷取數據。 窗體辨識器服務會嘗試使用提交的 URL 下載檔案,因此必須可從公用因特網存取該 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
要用於分析之自定義分類器的標識碼
- documentUrl
-
string
要分類的檔URL
- options
- ClassifyDocumentOptions
傳回
Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>