/
documentModelAdministrationClient.ts
611 lines (586 loc) · 22.8 KB
/
documentModelAdministrationClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { KeyCredential, TokenCredential } from "@azure/core-auth";
import { PagedAsyncIterableIterator } from "@azure/core-paging";
import {
CopyAuthorization,
GeneratedClient,
GetInfoResponse,
ModelInfo,
ModelSummary,
OperationInfo,
} from "./generated";
import {
parseOperationLocation,
toTrainingPollOperationState,
TrainingOperationDefinition,
TrainingPoller,
TrainingPollOperationState,
} from "./lro/training";
import { lro } from "./lro/util/poller";
import {
BuildModelOptions,
CopyModelOptions,
DeleteModelOptions,
DocumentModelAdministrationClientOptions,
GetCopyAuthorizationOptions,
GetInfoOptions,
GetModelOptions,
GetOperationOptions,
ListModelsOptions,
ListOperationsOptions,
} from "./options";
import { makeServiceClient } from "./util";
/**
* A client for interacting with the Form Recognizer service's model management features, such as creating, reading,
* listing, deleting, and copying models.
*
* ### Examples:
*
* #### Azure Active Directory
*
* ```typescript
* import { DocumentModelAdministrationClient } from "@azure/ai-form-recognizer";
* import { DefaultAzureCredential } from "@azure/identity";
*
* const endpoint = "https://<resource name>.cognitiveservices.azure.com";
* const credential = new DefaultAzureCredential();
*
* const client = new DocumentModelAdministrationClient(endpoint, credential);
* ```
*
* #### API Key (Subscription Key)
*
* ```typescript
* import { DocumentModelAdministrationClient, AzureKeyCredential } from "@azure/ai-form-recognizer";
*
* const endpoint = "https://<resource name>.cognitiveservices.azure.com";
* const credential = new AzureKeyCredential("<api key>");
*
* const client = new DocumentModelAdministrationClient(endpoint, credential);
* ```
*/
export class DocumentModelAdministrationClient {
private _restClient: GeneratedClient;
/**
* Create a DocumentModelAdministrationClient instance from a resource endpoint and a an Azure Identity `TokenCredential`.
*
* See the [`@azure/identity`](https://npmjs.com/package/\@azure/identity) package for more information about
* authenticating with Azure Active Directory.
*
* ### Example:
*
* ```javascript
* import { DocumentModelAdministrationClient } from "@azure/ai-form-recognizer";
* import { DefaultAzureCredential } from "@azure/identity";
*
* const endpoint = "https://<resource name>.cognitiveservices.azure.com";
* const credential = new DefaultAzureCredential();
*
* const client = new DocumentModelAdministrationClient(endpoint, credential);
* ```
*
* @param endpoint - the endpoint URL of an Azure Cognitive Services instance
* @param credential - a TokenCredential instance from the `@azure/identity` package
* @param options - optional settings for configuring all methods in the client
*/
public constructor(
endpoint: string,
credential: TokenCredential,
options?: DocumentModelAdministrationClientOptions
);
/**
* Create a DocumentModelAdministrationClient instance from a resource endpoint and a static API key
* (`KeyCredential`),
*
* ### Example:
*
* ```javascript
* import { DocumentModelAdministrationClient, AzureKeyCredential } from "@azure/ai-form-recognizer";
*
* const endpoint = "https://<resource name>.cognitiveservices.azure.com";
* const credential = new AzureKeyCredential("<api key>");
*
* const client = new DocumentModelAdministrationClient(endpoint, credential);
* ```
*
* @param endpoint - the endpoint URL of an Azure Cognitive Services instance
* @param credential - a KeyCredential containing the Cognitive Services instance subscription key
* @param options - optional settings for configuring all methods in the client
*/
public constructor(
endpoint: string,
credential: KeyCredential,
options?: DocumentModelAdministrationClientOptions
);
/**
* @hidden
*/
public constructor(
endpoint: string,
credential: KeyCredential | TokenCredential,
options?: DocumentModelAdministrationClientOptions
);
public constructor(
endpoint: string,
credential: KeyCredential | TokenCredential,
options: DocumentModelAdministrationClientOptions = {}
) {
this._restClient = makeServiceClient(endpoint, credential, options);
}
// #region Model Creation
/**
* Build a new model with a given ID from a set of input documents and labeled fields.
*
* The Model ID can consist of any text, so long as it does not begin with "prebuilt-" (as these models refer to
* prebuilt Form Recognizer models that are common to all resources), and so long as it does not already exist within
* the resource.
*
* The Form Recognizer service reads the training data set from an Azure Storage container, given as a URL to the
* container with a SAS token that allows the service backend to communicate with the container. At a minimum, the
* "read" and "list" permissions are required. In addition, the data in the given container must be organized
* according to a particular convention, which is documented in [the service's documentation for building custom
* models](https://aka.ms/form-recognizer/custom).
*
* ### Example
*
* ```javascript
* const modelId = "aNewModel";
* const containerUrl = "<training data container SAS URL>";
*
* const poller = await client.beginBuildModel(modelId, containerUrl, {
* // Optionally, a text description may be attached to the model
* description: "This is an example model!"
* });
*
* // Model building, like all other model creation operations, returns a poller that eventually produces a ModelInfo
* // object
* const modelInfo = await poller.pollUntilDone();
*
* const {
* modelId, // identical to the modelId given when creating the model
* description, // identical to the description given when creating the model
* createdDateTime, // the Date (timestamp) that the model was created
* docTypes // information about the document types in the model and their field schemas
* } = modelInfo;
* ```
*
* @param modelId - the unique ID of the model to create
* @param containerUrl - SAS-encoded URL to an Azure Storage container holding the training data set
* @param options - optional settings for the model build operation
* @returns a long-running operation (poller) that will eventually produce the created model information or an error
*/
public async beginBuildModel(
modelId: string,
containerUrl: string,
options: BuildModelOptions = {}
): Promise<TrainingPoller> {
return this.createTrainingPoller({
options,
start: () =>
this._restClient.buildDocumentModel(
{
modelId,
description: options.description,
azureBlobSource: {
containerUrl,
},
},
options
),
});
}
/**
* Creates a single composed model from several pre-existing submodels.
*
* The resulting composed model combines the document types of its component models, and inserts a classification step
* into the extraction pipeline to determine which of its component submodels is most appropriate for the given input.
*
* ### Example
*
* ```javascript
* const modelId = "aNewComposedModel";
* const subModelIds = [
* "documentType1Model",
* "documentType2Model",
* "documentType3Model"
* ];
*
* // The resulting composed model can classify and extract data from documents
* // conforming to any of the above document types
* const poller = await client.beginComposeModel(modelId, subModelIds, {
* description: "This is a composed model that can handle several document types."
* });
*
* // Model composition, like all other model creation operations, returns a poller that eventually produces a
* // ModelInfo object
* const modelInfo = await poller.pollUntilDone();
*
* const {
* modelId, // identical to the modelId given when creating the model
* description, // identical to the description given when creating the model
* createdDateTime, // the Date (timestamp) that the model was created
* docTypes // information about the document types of the composed submodels
* } = modelInfo;
* ```
*
* @param modelId - the unique ID of the model to create
* @param componentModels - an Iterable of strings representing the unique model IDs of the models to compose
* @param options - optional settings for model creation
* @returns a long-running operation (poller) that will eventually produce the created model information or an error
*/
public async beginComposeModel(
modelId: string,
componentModels: Iterable<string>,
options: BuildModelOptions = {}
): Promise<TrainingPoller> {
return this.createTrainingPoller({
options,
start: () =>
this._restClient.composeDocumentModel(
{
modelId,
componentModels: [...componentModels].map((submodelId) => ({
modelId: submodelId,
})),
description: options.description,
},
options
),
});
}
/**
* Creates an authorization to copy a model into the resource, used with the `beginCopyModel` method.
*
* The `CopyAuthorization` grants another cognitive service resource the right to create a model in this client's
* resource with the model ID and optional description that are encoded into the authorization.
*
* ### Example
*
* ```javascript
* // The copyAuthorization data structure stored below grants any cognitive services resource the right to copy a
* // model into the client's resource with the given destination model ID.
* const copyAuthorization = await client.getCopyAuthorization("<destination model ID>");
* ```
*
* @param destinationModelId - the unique ID of the destination model (the ID to copy the model into)
* @param options - optional settings for creating the copy authorization
* @returns a copy authorization that encodes the given modelId and optional description
*/
public async getCopyAuthorization(
destinationModelId: string,
options: GetCopyAuthorizationOptions = {}
): Promise<CopyAuthorization> {
return this._restClient.authorizeCopyDocumentModel(
{
modelId: destinationModelId,
description: options.description,
},
options
);
}
/**
* Copies a model with the given ID into the resource and model ID encoded by a given copy authorization.
*
* See {@link CopyAuthorization} and {@link getCopyAuthorization}.
*
* ### Example
*
* ```javascript
* // We need a client for the source model's resource
* const sourceEndpoint = "https://<source resource name>.cognitiveservices.azure.com";
* const sourceCredential = new AzureKeyCredential("<source api key>");
* const sourceClient = new DocumentModelAdministrationClient(sourceEndpoint, sourceCredential);
*
* // We create the copy authorization using a client authenticated with the destination resource. Note that these two
* // resources can be the same (you can copy a model to a new ID in the same resource).
* const copyAuthorization = await client.getCopyAuthorization("<destination model ID>");
*
* // Finally, use the _source_ client to copy the model and await the copy operation
* const poller = await sourceClient.beginCopyModel("<source model ID>");
*
* // Model copying, like all other model creation operations, returns a poller that eventually produces a ModelInfo
* // object
* const modelInfo = await poller.pollUntilDone();
*
* const {
* modelId, // identical to the modelId given when creating the copy authorization
* description, // identical to the description given when creating the copy authorization
* createdDateTime, // the Date (timestamp) that the model was created
* docTypes // information about the document types of the model (identical to the original, source model)
* } = modelInfo;
* ```
*
* @param sourceModelId - the unique ID of the source model that will be copied
* @param authorization - an authorization to copy the model, created using the {@link getCopyAuthorization}
* @param options - optional settings for
* @returns a long-running operation (poller) that will eventually produce the copied model information or an error
*/
public async beginCopyModel(
sourceModelId: string,
authorization: CopyAuthorization,
options: CopyModelOptions = {}
): Promise<TrainingPoller> {
return this.createTrainingPoller({
options,
start: () => this._restClient.copyDocumentModelTo(sourceModelId, authorization, options),
});
}
/**
* Create an LRO poller that handles training operations.
*
* This is the meat of all training polling operations.
*
* @internal
* @param definition - operation definition (start operation method, request options)
* @returns a training poller that produces a ModelInfo
*/
private async createTrainingPoller(
definition: TrainingOperationDefinition
): Promise<TrainingPoller> {
const { resumeFrom } = definition.options;
const toInit =
resumeFrom === undefined
? async () => {
const { operationLocation } = await definition.start();
const operationId = parseOperationLocation(operationLocation);
return this._restClient.getOperation(operationId, definition.options);
}
: () => {
const { operationId } = JSON.parse(resumeFrom) as { operationId: string };
return this._restClient.getOperation(operationId, definition.options);
};
const poller = await lro<ModelInfo, TrainingPollOperationState>(
{
init: async () => toTrainingPollOperationState(await toInit()),
poll: async ({ operationId }) => {
const res = await this._restClient.getOperation(operationId, definition.options);
return toTrainingPollOperationState(res);
},
serialize: ({ operationId }) => JSON.stringify({ operationId }),
},
definition.options.updateIntervalInMs
);
if (definition.options.onProgress !== undefined) {
poller.onProgress(definition.options.onProgress);
definition.options.onProgress(poller.getOperationState());
}
return poller;
}
// #endregion
// #region Model Management
/**
* Retrieve basic information about this client's resource.
*
* ### Example
*
* ```javascript
* const {
* // Information about the custom models in the current resource
* customDocumentModelInfo: {
* // The number of custom models in the current resource
* count,
* // The maximum number of models that the current resource can support
* limit
* }
* } = await client.getInfo();
* ```
*
* @param options - optional settings for the request
* @returns basic information about this client's resource
*/
public getInfo(options?: GetInfoOptions): Promise<GetInfoResponse> {
return this._restClient.getInfo(options);
}
/**
* Retrieves information about a model ({@link ModelInfo}) by ID.
*
* This method can retrieve information about custom as well as prebuilt models.
*
* ### **Breaking Change**
*
* In previous versions of the Form Recognizer REST API and SDK, the `getModel` method could return any model, even
* one that failed to create due to errors. In the new service versions, `getModel` and `listModels` _only produce
* successfully created models_ (i.e. models that are "ready" for use). Failed models are now retrieved through the
* "operations" APIs, see {@link getOperation} and {@link listOperations}.
*
* ### Example
*
* ```javascript
* // The ID of the prebuilt business card model
* const modelId = "prebuilt-businessCard";
*
* const {
* modelId, // identical to the modelId given when calling `getModel`
* description, // a textual description of the model, if provided during model creation
* createdDateTime, // the Date (timestamp) that the model was created
* // information about the document types in the model and their field schemas
* docTypes: {
* // the document type of the prebuilt business card model
* "prebuilt:businesscard": {
* // an optional, textual description of this document type
* description,
* // the schema of the fields in this document type, see the FieldSchema type
* fieldSchema,
* // the service's confidences in the fields (an object with field names as properties and numeric confidence
* // values)
* fieldConfidence
* }
* }
* } = await client.getModel(modelId);
* ```
*
* @param modelId - the unique ID of the model to query
* @param options - optional settings for the request
* @returns information about the model with the given ID
*/
public getModel(modelId: string, options?: GetModelOptions): Promise<ModelInfo> {
return this._restClient.getModel(modelId, options);
}
/**
* List summaries of models in the resource. Custom as well as prebuilt models will be included. This operation
* supports paging.
*
* The model summary ({@link ModelSummary}) includes only the basic information about the model, and does not include
* information about the document types in the model (such as the field schemas and confidence values).
*
* To access the full information about the model, use {@link getModel}.
*
* ### **Breaking Change**
*
* In previous versions of the Form Recognizer REST API and SDK, the `listModels` method would return all models, even
* those that failed to create due to errors. In the new service versions, `listModels` and `getModels` _only produce
* successfully created models_ (i.e. models that are "ready" for use). Failed models are now retrieved through the
* "operations" APIs, see {@link getOperation} and {@link listOperations}.
*
* ### Examples
*
* #### Async Iteration
*
* ```javascript
* for await (const summary of client.listModels()) {
* const {
* modelId, // The model's unique ID
* description, // a textual description of the model, if provided during model creation
* } = summary;
*
* // You can get the full model info using `getModel`
* const model = await client.getModel(modelId);
* }
* ```
*
* #### By Page
*
* ```javascript
* // The listModels method is paged, and you can iterate by page using the `byPage` method.
* const pages = client.listModels().byPage();
*
* for await (const page of pages) {
* // Each page is an array of models and can be iterated synchronously
* for (const model of page) {
* const {
* modelId, // The model's unique ID
* description, // a textual description of the model, if provided during model creation
* } = summary;
*
* // You can get the full model info using `getModel`
* const model = await client.getModel(modelId);
* }
* }
* ```
*
* @param options - optional settings for the model requests
* @returns an async iterable of model summaries that supports paging
*/
public listModels(options?: ListModelsOptions): PagedAsyncIterableIterator<ModelSummary> {
return this._restClient.listModels(options);
}
/**
* Retrieves information about an operation (`OperationInfo`) by its ID.
*
* Operations represent non-analysis tasks, such as building, composing, or copying a model.
*
* @param operationId - the ID of the operation to query
* @param options - optional settings for the request
* @returns information about the operation with the given ID
*
* ### Example
*
* ```javascript
* // The ID of the operation, which should be a GUID
* const operationId = "<operation GUID>";
*
* const {
* operationId, // identical to the operationId given when calling `getOperation`
* kind, // the operation kind, one of "documentModelBuild", "documentModelCompose", or "documentModelCopyTo"
* status, // the status of the operation, one of "notStarted", "running", "failed", "succeeded", or "canceled"
* percentCompleted, // a number between 0 and 100 representing the progress of the operation
* createdDateTime, // a Date object that reflects the time when the operation was started
* lastUpdatedDateTime, // a Date object that reflects the time when the operation state was last modified
* } = await client.getOperation(operationId);
* ```
*/
public getOperation(operationId: string, options?: GetOperationOptions): Promise<OperationInfo> {
return this._restClient.getOperation(operationId, options);
}
/**
* List model creation operations in the resource. This will produce all operations, including operations that failed
* to create models successfully. This operation supports paging.
*
* ### Examples
*
* #### Async Iteration
*
* ```javascript
* for await (const operation of client.listOperations()) {
* const {
* operationId, // the operation's GUID
* status, // the operation status, one of "notStarted", "running", "succeeded", "failed", or "canceled"
* percentCompleted // the progress of the operation, from 0 to 100
* } = operation;
* }
* ```
*
* #### By Page
*
* ```javascript
* // The listOperations method is paged, and you can iterate by page using the `byPage` method.
* const pages = client.listOperations().byPage();
*
* for await (const page of pages) {
* // Each page is an array of operation info objects and can be iterated synchronously
* for (const operation of page) {
* const {
* operationId, // the operation's GUID
* status, // the operation status, one of "notStarted", "running", "succeeded", "failed", or "canceled"
* percentCompleted // the progress of the operation, from 0 to 100
* } = operation;
* }
* }
* ```
*
* @param options - optional settings for the operation requests
* @returns an async iterable of operation information objects that supports paging
*/
public listOperations(
options: ListOperationsOptions = {}
): PagedAsyncIterableIterator<OperationInfo> {
return this._restClient.listOperations(options);
}
/**
* Deletes a model with the given ID from the client's resource, if it exists. This operation CANNOT be reverted.
*
* ### Example
*
* ```javascript
* await client.deleteModel("<model ID to delete>"));
* ```
*
* @param modelId - the unique ID of the model to delete from the resource
* @param options - optional settings for the request
*/
public deleteModel(modelId: string, options?: DeleteModelOptions): Promise<void> {
return this._restClient.deleteModel(modelId, options);
}
// #endregion
}