Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion src/module/DocumentAI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import { makeFormData } from 'koajax';
import { BaseModel, RESTClient, toggle } from 'mobx-restful';

import { LarkData } from '../../type';
import { TaxiInvoice, TrainInvoice, VatInvoice, VehicleInvoice } from './type';
import {
BankCard,
ContractEntity,
OcrText,
ResumeEntity,
TaxiInvoice,
TrainInvoice,
VatInvoice,
VehicleInvoice
} from './type';

export * from './type';

Expand Down Expand Up @@ -58,4 +67,54 @@ export abstract class DocumentAIModel extends BaseModel {

return body!.data!.vehicle_invoice;
}

/**
* @see {@link https://open.feishu.cn/document/server-docs/ai/optical_char_recognition-v1/basic_recognize}
* @param image Base64-encoded image string
*/
@toggle('uploading')
async recognizeText(image: string) {
const { body } = await this.client.post<LarkData<{ texts: OcrText[] }>>(
'optical_char_recognition/v1/image/basic_recognize',
{ image }
);

return body!.data!.texts;
}

/**
* @see {@link https://open.feishu.cn/document/ai/document_ai-v1/bank_card/recognize}
*/
@toggle('uploading')
async recognizeBankCard(file: File) {
const { body } = await this.client.post<
LarkData<{ bank_cards: { entities: BankCard[] }[] }>
>(`${this.baseURI}/bank_card/recognize`, makeFormData({ file }));

return body!.data!.bank_cards;
}

/**
* @see {@link https://open.feishu.cn/document/ai/document_ai-v1/resume/parse}
*/
@toggle('uploading')
async parseResume(file: File) {
const { body } = await this.client.post<
LarkData<{ resumes: { entities: ResumeEntity[] }[] }>
>(`${this.baseURI}/resume/parse`, makeFormData({ file }));

return body!.data!.resumes;
}

/**
* @see {@link https://open.feishu.cn/document/server-docs/ai/document_ai-v1/contract/field_extraction}
*/
@toggle('uploading')
async extractContractFields(file: File) {
const { body } = await this.client.post<
LarkData<{ contracts: { entities: ContractEntity[] }[] }>
>(`${this.baseURI}/contract/field_extraction`, makeFormData({ file }));

return body!.data!.contracts;
}
}
45 changes: 45 additions & 0 deletions src/module/DocumentAI/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,48 @@ export interface VehicleInvoice {
| `total_price${'' | '_little'}`;
value: string;
}

export interface OcrText {
text: string;
/** Array of corner points (top-left, top-right, bottom-right, bottom-left) for the text bounding box */
positions: { x: number; y: number }[];
}

export interface BankCard {
type: 'bank_card_number' | 'bank_card_date_of_expiry';
value: string;
}

export interface ResumeEntity {
type:
| 'name'
| 'gender'
| 'date_of_birth'
| 'marital_status'
| 'nationality'
| 'job_title'
| 'email'
| 'phone'
| 'address'
| `${'education' | 'work_experience' | 'project_experience' | 'award'}_history`
| 'self_description'
| 'skills'
| 'certificates'
| 'languages';
value: string;
}

export interface ContractEntity {
type:
| 'contract_name'
| 'contract_type'
| 'contract_amount'
| 'contract_period'
| 'sign_date'
| 'effective_date'
| 'expiration_date'
| 'terms_of_payment'
| `party_${'a' | 'b'}`
| `party_${'a' | 'b'}_signatory`;
value: string;
}