@azure/ai-form-recognizer
5.0.0 • Public • PublishedAzure AI Document Intelligence client library for JavaScript
Azure AIDocument Intelligence is a cloud service that uses machine learning to analyze text and structured data from your documents. It includes the following main features:
- Layout - Extract text, table structures, and selection marks, along with their bounding region coordinates, from documents.
- Document - Analyze entities, key-value pairs, tables, and selection marks from documents using the general prebuilt document model.
- Read - Read information about textual elements, such as page words and lines in addition to text language information.
- Prebuilt - Analyze data from certain types of common documents (such as receipts, invoices, business cards, or identity documents) using prebuilt models.
- Custom - Build custom models to extract text, field values, selection marks, and table data from documents. Custom models are built with your own data, so they're tailored to your documents.
- Classifiers - Build custom classifiers to categorize documents into predefined classes.
Source code |Package (NPM) |API reference documentation |Product documentation |Samples
Note
The Document Intelligence service was formerly known as "Azure Form Recognizer." These services are one and the same, and the@azure/ai-form-recognizer
package for JavaScript is the Azure SDK package for the Azure AI Document Intelligence service. At the time of writing, the renaming of Azure Form Recognizer to Azure AI Document Intelligence is underway, so "Form Recognizer" and "Document Intelligence" may be used interchangeably in some cases.
@azure/ai-form-recognizer
package
Install theInstall the Azure Document Intelligence client library for JavaScript withnpm
:
npm install @azure/ai-form-recognizer
Getting started
const{ DocumentAnalysisClient}=require("@azure/ai-form-recognizer");const{ DefaultAzureCredential}=require("@azure/identity");constfs=require("fs");constcredential=newDefaultAzureCredential();constclient=newDocumentAnalysisClient("https://<resource name>.cognitiveservices.azure.com",credential);// Document Intelligence supports many different types of files.constfile=fs.createReadStream("path/to/file.jpg");constpoller=awaitclient.beginAnalyzeDocument("<model ID>",file);const{ pages, tables, styles, keyValuePairs, entities, documents}=awaitpoller.pollUntilDone();
Currently supported environments
- LTS versions of Node.js
- Latest versions of Safari, Chrome, Edge, and Firefox.
See oursupport policy for more details.
Prerequisites
- AnAzure subscription
- ACognitive Services or Form Recognizer resource. If you need to create the resource, you can use theAzure Portal orAzure CLI.
Create a Form Recognizer resource
Note: At the time of writing, the Azure portal still refers to the resource as a "Form Recognizer" resource. In the future, this may be updated to a "Document Intelligence" resource. For now, the following documentation uses the "Form Recognizer" name.
Document Intelligence supports bothmulti-service and single-service access. Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Form Recognizer access only, create a Form Recognizer resource.
You can create the resource using
Option 1:Azure Portal
Option 2:Azure CLI.
Below is an example of how you can create a Form Recognizer resource using the CLI:
# Create a new resource group to hold the Form Recognizer resource -# if using an existing resource group, skip this stepaz group create --name my-resource-group --location westus2
If you use the Azure CLI, replace<your-resource-group-name>
and<your-resource-name>
with your own unique names:
az cognitiveservices account create --kind FormRecognizer --resource-group<your-resource-group-name> --name<your-resource-name> --sku<your-sku-name> --location<your-location>
Create and authenticate a client
In order to interact with the Document Intelligence service, you'll need to select either aDocumentAnalysisClient
or aDocumentModelAdministrationClient
, and create an instance of this type. In the following examples, we will useDocumentAnalysisClient
. To create a client instance to access the Document Intelligence API, you will need theendpoint
of your Form Recognizer resource and acredential
. The clients can use either anAzureKeyCredential
with an API key of your resource or aTokenCredential
that uses Azure Active Directory RBAC to authorize the client.
You can find the endpoint for your Form Recognizer resource either in theAzure Portal or by using theAzure CLI snippet below:
az cognitiveservices account show --name<your-resource-name> --resource-group<your-resource-group-name> --query"properties.endpoint"
Use an API key
Use theAzure Portal to browse to your Form Recognizer resource and retrieve an API key, or use theAzure 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 it as follows:
const{ DocumentAnalysisClient, AzureKeyCredential}=require("@azure/ai-form-recognizer");constclient=newDocumentAnalysisClient("<endpoint>",newAzureKeyCredential("<API key>"));
Use Azure Active Directory
API key authorization is used in most of the examples, but you can also authenticate the client with Azure Active Directory using theAzure Identity library. To use theDefaultAzureCredential provider shown below or other credential providers provided with the Azure SDK, please install the@azure/identity
package:
npm install @azure/identity
To authenticate using a service principal, you will also need toregister an AAD application and grant access to the service 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
.
const{ DocumentAnalysisClient}=require("@azure/ai-form-recognizer");const{ DefaultAzureCredential}=require("@azure/identity");constclient=newDocumentAnalysisClient("<endpoint>",newDefaultAzureCredential());
Key concepts
DocumentAnalysisClient
DocumentAnalysisClient
provides operations for analyzing input documents using custom and prebuilt models. It has three methods:
beginAnalyzeDocument
, which extracts data from an input document file stream using a custom or prebuilt model given by its model ID. For information about the prebuilt models supported in all resources and their model IDs/outputs, please seethe service's documentation of the models.beginAnalyzeDocumentFromUrl
, which performs the same function asbeginAnalyzeDocument
, but submits a publicly-accessible URL of a file instead of a file stream.
DocumentModelAdministrationClient
DocumentModelAdministrationClient
provides operations for managing (creating, reading, listing, and deleting) models in the resource:
beginBuildDocumentModel
starts an operation to create a new document model from your own training data set. The created model can extract fields according to a custom schema. The training data are expected to be located in an Azure Storage container and organized according to a particular convention. See theservice's documentation on creating a training data set for a more detailed explanation of applying labels to a training data set.beginComposeDocumentModel
starts an operation to compose multiple models into a single model. When used for custom form recognition, the new composed model will first perform a classification of the input documents to determine which of its submodels is most appropriate.beginCopyModelTo
starts an operation to copy a custom model from one resource to another (or even to the same resource). It requires aCopyAuthorization
from the target resource, which can be generated using thegetCopyAuthorization
method.getResourceDetails
retrieves information about the resource's limits, such as the number of custom models and the maximum number of models the resource can support.getDocumentModel
,listDocumentModels
, anddeleteDocumentModel
enable managing models in the resource.getOperation
andlistOperations
enable viewing the status of model creation operations, even those operations that are ongoing or that have failed. Operations are retained for 24 hours.
Please note that models can also be created using the Document Intelligence service's graphical user interface:Document Intelligence Studio.
Sample code snippets that illustrate the use ofDocumentModelAdministrationClient
to build a model can be foundbelow, in the "Build a Model" example section..
Long-running operations
Long-running operations (LROs) are operations which consist of an initial request sent to the service to start an operation, followed by polling for a result at a certain interval to determine if the operation has completed and whether it failed or succeeded. Ultimately, the LRO will either fail with an error or produce a result.
In Azure AI Document Intelligence, operations that create models (including copying and composing models) as well as the analysis/data-extraction operations are LROs. The SDK clients provide asynchronousbegin<operation-name>
methods that returnPromise<PollerLike>
objects. ThePollerLike
object represents the operation, which runs asynchronously on the service's infrastructure, and a program can wait for the operation to complete by calling and awaiting thepollUntilDone
method on the poller returned from thebegin<operation-name>
method. Sample code snippets are provided to illustrate using long-running operations in the next section.
Examples
The following section provides several JavaScript code snippets illustrating common patterns used in the Document Intelligence client libraries.
- Analyze a document with a model ID
- Use prebuilt document models
- Use the "layout" prebuilt
- Use the "document" prebuilt
- Use the "read" prebuilt
- Build a model
- Manage models
Analyze a document with a model ID
ThebeginAnalyzeDocument
method can extract fields and table data from documents. Analysis may use either a custom model, trained with your own data, or a prebuilt model provided by the service (seeUse Prebuilt Models below). A custom model is tailored to your own documents, so it should only be used with documents of the same structure as one of the document types in the model (there may be multiple, such as in a composed model).
const{ DocumentAnalysisClient, AzureKeyCredential}=require("@azure/ai-form-recognizer");constfs=require("fs");asyncfunctionmain(){constendpoint="<cognitive services endpoint>";constapiKey="<api key>";constmodelId="<model id>";constpath="<path to a document>";constreadStream=fs.createReadStream(path);constclient=newDocumentAnalysisClient(endpoint,newAzureKeyCredential(apiKey));constpoller=awaitclient.beginAnalyzeDocument(modelId,readStream,{onProgress:({ status})=>{console.log(`status:${status}`);},});// There are more fields than just these threeconst{ documents, pages, tables}=awaitpoller.pollUntilDone();console.log("Documents:");for(constdocumentofdocuments||[]){console.log(`Type:${document.docType}`);console.log("Fields:");for(const[name,field]ofObject.entries(document.fields)){console.log(`Field${name} has value '${field.value}' with a confidence score of${field.confidence}`);}}console.log("Pages:");for(constpageofpages||[]){console.log(`Page number:${page.pageNumber} (${page.width}x${page.height}${page.unit})`);}console.log("Tables:");for(consttableoftables||[]){console.log(`- Table (${table.columnCount}x${table.rowCount})`);for(constcelloftable.cells){console.log(` - cell (${cell.rowIndex},${cell.columnIndex}) "${cell.content}"`);}}}main().catch((err)=>{console.error("The sample encountered an error:",err);});
Analyze a document from a URL
As an alternative to providing a readable stream, a publicly-accessible URL can be provided instead using thebeginAnalyzeDocumentFromUrl
method. "Publicly-accessible" means that URL sources must be accessible from the service's infrastructure (in other words, a private intranet URL, or URLs that use header- or certificate-based secrets, will not work, as the Document Intelligence service must be able to access the URL). However, the URL itself could encode a secret, such as an Azure Storage blob URL that contains a SAS token in the query parameters.
Use prebuilt document models
ThebeginAnalyzeDocument
method also supports extracting fields from certain types of common documents such as receipts, invoices, business cards, identity documents, and more using prebuilt models provided by the Document Intelligence service. The prebuilt models may be provided either as model ID strings (the same as custom document models—see theother prebuilt models section below) or using aDocumentModel
object. When using aDocumentModel
, the Document Intelligence SDK for JavaScript provides a much stronger TypeScript type for the resulting extracted documents based on the model's schema, and it will be converted to use JavaScript naming conventions.
ExampleDocumentModel
objects for the current service API version (2022-08-31
) can be found intheprebuilt
samples directory. In the following example, we'll use thePrebuiltReceiptModel
from the [prebuilt-receipt.ts
] file in that directory.
Since the main benefit ofDocumentModel
-based analysis is stronger TypeScript type constraints, the following sample is written in TypeScript using ECMAScript module syntax:
import{DocumentAnalysisClient,AzureKeyCredential}from"@azure/ai-form-recognizer";// Copy the file from the above-linked sample directory so that it can be imported in this moduleimport{PrebuiltReceiptModel}from"./prebuilt/prebuilt-receipt";importfsfrom"fs";asyncfunctionmain(){constendpoint="<cognitive services endpoint>";constapiKey="<api key>";constpath="<path to your receipt document>";// pdf/jpeg/png/tiff formatsconstreadStream=fs.createReadStream(path);constclient=newDocumentAnalysisClient(endpoint,newAzureKeyCredential(apiKey));// The PrebuiltReceiptModel `DocumentModel` instance encodes both the model ID and a stronger return type for the operationconstpoller=awaitclient.beginAnalyzeDocument(PrebuiltReceiptModel,readStream,{onProgress:({ status})=>{console.log(`status:${status}`);},});const{documents:[receiptDocument],}=awaitpoller.pollUntilDone();// The fields of the document constitute the extracted receipt data.constreceipt=receiptDocument.fields;if(receipt===undefined){thrownewError("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}ofreceipt.items.values){console.log("- Description:",item.description?.value);console.log(" Total Price:",item.totalPrice?.value);}}console.log(" Total:",receipt.total?.value);}main().catch((err)=>{console.error("The sample encountered an error:",err);});
Alternatively, as mentioned above, instead of usingPrebuiltReceiptModel
, which produces the stronger return type, the prebuilt receipt's model ID ("prebuilt-receipt") can be used, but the document fields will not be strongly typed in TypeScript, and the field names will generally be in "PascalCase" instead of "camelCase".
Other prebuilt models
You are not limited to receipts! There are a few prebuilt models to choose from, with more on the way. Each prebuilt model has its own set of supported fields:
- Receipts, using
PrebuiltReceiptModel
(as above) or the prebuilt receipt model ID"prebuilt-receipt"
. - Business cards, using
PrebuiltBusinessCardModel
or its model ID"prebuilt-businessCard"
. - Invoices, using
PrebuiltInvoiceModel
or its model ID"prebuilt-invoice"
. - Identity Documents (such as driver licenses and passports), using
PrebuiltIdDocumentModel
or its model ID"prebuilt-idDocument"
. - W2 Tax Forms (United States), using
PrebuiltTaxUsW2Model
or its model ID"prebuilt-tax.us.w2"
. - Health Insurance Cards (United States), using [
PrebuiltHealthInsuranceCardUsModel
][samples-prebuilt-healthinsurancecard.us] or its model ID"prebuilt-healthInsuranceCard.us"
.
Each of the above prebuilt models producesdocuments
(extracted instances of the model's field schema). There are also three prebuilt models that do not have field schemas and therefore do not producedocuments
. They are:
- The prebuilt Layout model (seeUse the "layout" prebuilt below), which extracts information about basic layout (OCR) elements such as pages and tables.
- The prebuilt General Document model (seeUse the "document" prebuilt below), which adds key-value pairs (directed associations between page elements, such as labeled elements) to the information produced by the layout model.
- The prebuilt Read model (seeUse the "read" prebuilt below), which extracts only textual elements, such as page words and lines, along with information about the language of the document.
For information about the fields of all of these models, seethe service's documentation of the available prebuilt models.
The fields of all prebuilt models may also be accessed programmatically using thegetDocumentModel
method (by their model IDs) ofDocumentModelAdministrationClient
and inspecting thedocTypes
field in the result.
Use the "layout" prebuilt
The"prebuilt-layout"
model extracts only the basic elements of the document, such as pages, (which consist of text words/lines and selection marks), tables, and visual text styles along with their bounding regions and spans within the text content of the input documents. We provide a strongly-typedDocumentModel
instance namedPrebuiltLayoutModel
that invokes this model, or as always its model ID"prebuilt-layout"
may be used directly.
Since the main benefit ofDocumentModel
-based analysis is stronger TypeScript type constraints, the following sample is written in TypeScript using ECMAScript module syntax:
import{DocumentAnalysisClient,AzureKeyCredential}from"@azure/ai-form-recognizer";// Copy the above-linked `DocumentModel` file so that it may be imported in this module.import{PrebuiltLayoutModel}from"./prebuilt/prebuilt-layout";importfsfrom"fs";asyncfunctionmain(){constendpoint="<cognitive services endpoint>";constapiKey="<api key>";constpath="<path to a document>";// pdf/jpeg/png/tiff formatsconstreadStream=fs.createReadStream(path);constclient=newDocumentAnalysisClient(endpoint,newAzureKeyCredential(apiKey));constpoller=awaitclient.beginAnalyzeDocument(PrebuiltLayoutModel,readStream);const{ pages, tables}=awaitpoller.pollUntilDone();for(constpageofpages||[]){console.log(`- Page${page.pageNumber}: (${page.width}x${page.height}${page.unit})`);}for(consttableoftables||[]){console.log(`- Table (${table.columnCount}x${table.rowCount})`);for(constcelloftable.cells){console.log(` cell [${cell.rowIndex},${cell.columnIndex}] "${cell.content}"`);}}}main().catch((err)=>{console.error("The sample encountered an error:",err);});
Use the "document" prebuilt
The"prebuilt-document"
model extracts information about key-value pairs (directed associations between page elements, such as labeled fields) in addition to the properties produced by the layout extraction method. This prebuilt (general) document model provides similar functionality to the custom models trained without label information in previous iterations of the Document Intelligence service, but it is now provided as a prebuilt model that works with a wide variety of documents. We provide a strongly-typedDocumentModel
instance namedPrebuiltDocumentModel
that invokes this model, or as always its model ID"prebuilt-document"
may be used directly.
Since the main benefit ofDocumentModel
-based analysis is stronger TypeScript type constraints, the following sample is written in TypeScript using ECMAScript module syntax:
import{DocumentAnalysisClient,AzureKeyCredential}from"@azure/ai-form-recognizer";// Copy the above-linked `DocumentModel` file so that it may be imported in this module.import{PrebuiltDocumentModel}from"./prebuilt/prebuilt-document";importfsfrom"fs";asyncfunctionmain(){constendpoint="<cognitive services endpoint>";constapiKey="<api key>";constpath="<path to a document>";// pdf/jpeg/png/tiff formatsconstreadStream=fs.createReadStream(path);constclient=newDocumentAnalysisClient(endpoint,newAzureKeyCredential(apiKey));constpoller=awaitclient.beginAnalyzeDocument(PrebuiltDocumentModel,readStream);// `pages`, `tables` and `styles` are also available as in the "layout" example above, but for the sake of this// example we won't show them here.const{ keyValuePairs}=awaitpoller.pollUntilDone();if(!keyValuePairs||keyValuePairs.length<=0){console.log("No key-value pairs were extracted from the document.");}else{console.log("Key-Value Pairs:");for(const{ key, value, confidence}ofkeyValuePairs){console.log("- Key :",`"${key.content}"`);console.log(" Value:",`"${value?.content??"<undefined>"}" (${confidence})`);}}}main().catch((err)=>{console.error("The sample encountered an error:",err);});
Use the "read" prebuilt
The"prebuilt-read"
model extracts textual information in a document such as words and paragraphs and analyzes the language and writing style (e.g. handwritten vs. typeset) of that text. We provide a strongly-typedDocumentModel
instance namedPrebuiltReadModel
that invokes this model, or as always its model ID"prebuilt-read"
may be used directly.
Since the main benefit ofDocumentModel
-based analysis is stronger TypeScript type constraints, the following sample is written in TypeScript using ECMAScript module syntax:
import{DocumentAnalysisClient,AzureKeyCredential}from"@azure/ai-form-recognizer";// Copy the above-linked `DocumentModel` file so that it may be imported in this module.import{PrebuiltReadModel}from"./prebuilt/prebuilt-read";// See the samples directory for a definition of this helper function.import{getTextOfSpans}from"./utils";importfsfrom"fs";asyncfunctionmain(){constendpoint="<cognitive services endpoint>";constapiKey="<api key>";constpath="<path to a document>";// pdf/jpeg/png/tiff formatsconstreadStream=fs.createReadStream(path);constclient=newDocumentAnalysisClient(endpoint,newAzureKeyCredential(apiKey));constpoller=awaitclient.beginAnalyzeDocument(PrebuiltReadModel,readStream);// The "prebuilt-read" model (`beginReadDocument` method) only extracts information about the textual content of the// document, such as page text elements, text styles, and information about the language of the text.const{ content, pages, languages}=awaitpoller.pollUntilDone();if(!pages||pages.length<=0){console.log("No pages were extracted from the document.");}else{console.log("Pages:");for(constpageofpages){console.log("- Page",page.pageNumber,`(unit:${page.unit})`);console.log(`${page.width}x${page.height}, angle:${page.angle}`);console.log(`${page.lines&&page.lines.length} lines,${page.words&&page.words.length} words`);if(page.lines&&page.lines.length>0){console.log(" Lines:");for(constlineofpage.lines){console.log(` - "${line.content}"`);}}}}if(!languages||languages.length<=0){console.log("No language spans were extracted from the document.");}else{console.log("Languages:");for(constlanguageEntryoflanguages){console.log(`- Found language:${languageEntry.locale} (confidence:${languageEntry.confidence})`);for(consttextofgetTextOfSpans(content,languageEntry.spans)){constescapedText=text.replace(/\r?\n/g,"\\n").replace(/"/g,'\\"');console.log(` - "${escapedText}"`);}}}}main().catch((error)=>{console.error("An error occurred:",error);process.exit(1);});
Classify a document
The Document Intelligence service supports custom document classifiers that can classify documents into a set of predefined categories based on a training data set. Documents can be classified with a custom classifier using thebeginClassifyDocument
method ofDocumentAnalysisClient
. LikebeginAnalyzeDocument
above, this method accepts a file or stream containing the document to be classified, and it has abeginClassifyDocumentFromUrl
counterpart that accepts a publicly-accessible URL to a document instead.
The following sample shows how to classify a document using a custom classifier:
const{ AzureKeyCredential, DocumentAnalysisClient}=require("@azure/ai-form-recognizer");asyncfunctionmain(){constendpoint="<endpoint>";constcredential=newAzureKeyCredential("<api key>");constdocumentUrl="https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/invoice/Invoice_1.pdf";constclient=newDocumentAnalysisClient(endpoint,credential);constpoller=awaitclient.beginClassifyDocumentFromUrl("<classifier id>",documentUrl);constresult=awaitpoller.pollUntilDone();if(result.documents===undefined||result.documents.length===0){thrownewError("Failed to extract any documents.");}for(constdocumentofresult.documents){console.log(`Extracted a document with type '${document.docType}' on page${document.boundingRegions?.[0].pageNumber} (confidence:${document.confidence})`);}}main().catch((error)=>{console.error("An error occurred:",error);process.exit(1);});
For information on training a custom classifier, see thesection on classifier training at the end of the next section.
Build a model
The SDK also supports creating models using theDocumentModelAdministrationClient
class. Building a model from labeled training data creates a new model that is trained on your own documents, and the resulting model will be able to recognize values from the structures of those documents. The model building operation accepts a SAS-encoded URL to an Azure Storage Blob container that holds the training documents. The Document Intelligence service's infrastructure will read the files in the container and create a model based on their contents. For more details on how to create and structure a training data container, see theDocument Intelligence service's documentation for building a model.
While we provide these methods for programmatic model creation, the Document Intelligence service team has created an interactive web application,Document Intelligence Studio, that enables creating and managing models on the web.
For example, the following program builds a custom document model using a SAS-encoded URL to a pre-existing Azure Storage container:
const{ DocumentModelAdministrationClient, AzureKeyCredential,}=require("@azure/ai-form-recognizer");asyncfunctionmain(){constendpoint="<cognitive services endpoint>";constapiKey="<api key>";constcontainerSasUrl="<SAS url to the blob container storing training documents>";constclient=newDocumentModelAdministrationClient(endpoint,newAzureKeyCredential(apiKey));// You must provide the model ID. It can be any text that does not start with "prebuilt-".// For example, you could provide a randomly generated GUID using the "uuid" package.// The second parameter is the SAS-encoded URL to an Azure Storage container with the training documents.// The third parameter is the build mode: one of "template" (the only mode prior to 4.0.0-beta.3) or "neural".// See https://aka.ms/azsdk/formrecognizer/buildmode for more information about build modes.constpoller=awaitclient.beginBuildDocumentModel("<model ID>",containerSasUrl,"template",{// The model description is optional and can be any text.description:"This is my new model!",onProgress:({ status})=>{console.log(`operation status:${status}`);},});constmodel=awaitpoller.pollUntilDone();console.log("Model ID:",model.modelId);console.log("Description:",model.description);console.log("Created:",model.createdOn);// A model may contain several document types, which describe the possible object structures of fields extracted using// this modelconsole.log("Document Types:");for(const[docType,{ description,fieldSchema:schema}]ofObject.entries(model.docTypes??{})){console.log(`- Name: "${docType}"`);console.log(` Description: "${description}"`);// For simplicity, this example will only show top-level field namesconsole.log(" Fields:");for(const[fieldName,fieldSchema]ofObject.entries(schema)){console.log(` - "${fieldName}" (${fieldSchema.type})`);console.log(`${fieldSchema.description??"<no description>"}`);}}}main().catch((err)=>{console.error("The sample encountered an error:",err);});
Custom classifiers are built in a similar way using thebeginBuildDocumentClassifier
method rather thanbeginBuildDocumentModel
. Please see thebuild classifier sample for more information about building a custom classifier, as the input training data are provided in a slightly different format. For information about building a training data set for a custom classifier, seethe Document Intelligence service documentation.
Manage models
DocumentModelAdministrationClient
also provides several methods for accessing and listing models. The following example shows how to iterate through the models in a resource (this will include both custom models in the resource as well as prebuilt models that are common to all resources), get a model by ID, and delete a model.
const{ DocumentModelAdministrationClient, AzureKeyCredential,}=require("@azure/ai-form-recognizer");asyncfunctionmain(){constendpoint="<cognitive services endpoint>";constapiKey="<api key>";constclient=newDocumentModelAdministrationClient(endpoint,newAzureKeyCredential(apiKey));// Produces an async iterable that supports paging (`PagedAsyncIterableIterator`). The `listDocumentModels` method will only// iterate over model summaries, which do not include detailed schema information. Schema information is only returned// from `getDocumentModel` as part of the full model information.constmodels=client.listDocumentModels();leti=1;forawait(constsummaryofmodels){console.log(`Model${i++}:`,summary);}// The iterable is paged, and the application can control the flow of paging if neededi=1;forawait(constpageofclient.listDocumentModels().byPage()){for(constsummaryofpage){console.log(`Model${i++}`,summary);}}// We can also get a full ModelInfo by ID. Here we only show the basic information. See the documentation and the// `getDocumentModel` sample program for information about the `docTypes` field, which contains the model's document type// schemas.constmodel=awaitclient.getDocumentModel("<model ID>");console.log("ID",model.modelId);console.log("Created:",model.createdOn);console.log("Description: ",model.description??"<none>");// A model can also be deleted by its model ID. Once it is deleted, it CANNOT be recovered.constmodelIdToDelete="<model ID that should be deleted forever>";awaitclient.deleteDocumentModel(modelIdToDelete);}main().catch((err)=>{console.error("The sample encountered an error:",err);});
Similar methodslistDocumentClassifiers
andgetDocumentClassifier
are available for listing and getting information about custom classifiers in addition todeleteDocumentClassifier
for deleting custom classifiers.
Troubleshooting
For assistance with troubleshooting, see thetroubleshooting guide.
Logging
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set theAZURE_LOG_LEVEL
environment variable toinfo
. Alternatively, logging can be enabled at runtime by callingsetLogLevel
in the@azure/logger
:
const{ setLogLevel}=require("@azure/logger");setLogLevel("info");
For more detailed instructions on how to enable logs, you can look at the@azure/logger package docs.
Next steps
Please take a look at thesamples directory for detailed code samples that show how to use this library including several features and methods that are not shown in the "Examples" section above, such as copying and composing models, listing model management operations, and deleting models.
Contributing
If you'd like to contribute to this library, please read thecontributing guide to learn more about how to build and test the code.
Package Sidebar
Install
npm i @azure/ai-form-recognizer@5.0.0
Repository
Version
5.0.0
License
MIT
Unpacked Size
1.42 MB
Total Files
96