メインコンテンツまでスキップ

File から text を抽出する

file から text を抽出して処理し、document ingestion を高速化します​

Squid の text extraction feature を使用すると、迅速な情報処理のために file の内容を簡単に処理できます。compliance documentation、earnings report、scientific study など、確認が必要であり、その内容に基づいて action を実行する必要がある document にはさまざまな種類があります。

この情報を手動で ingestion する作業は、時間がかかり error が発生しやすくなります。Squid を使用すると、以前は数日かかっていた task が数秒で完了します。

注記

Text extraction には admin privilege が必要なため、Squid backend やその他の server environment など、Squid API key に access できる secure environment でのみ実行してください。

Extraction client を作成する​

document に対して text extraction を実行するには、まず extraction() method を使用して extraction client を作成します。

Backend code
const extractionClient = this.squid.extraction();

Text を抽出する​

extraction client の extractDataFromDocumentFile method を使用して、file から text を抽出します。method は File または BlobAndFileName のいずれかの type を受け取ります。extractDataFromDocumentFile method は、pages の array に resolve される promise を返します。指定 page の text には content attribute を使用して access できます。

次の例では、file から text を抽出します。

Backend code
const data = {
blob: dataBlob,
name: 'myDocument.pdf',
};

const extractedResult = await extractionClient.extractDataFromDocumentFile(data);
console.log(extractedResult.pages[0].content); // 'Q4 Development Plan...'
抽出される content

Squid の extraction client は、強力な AI 処理を使用して、さまざまな file content から text を抽出します。text file の読み取りだけでなく、scanned document、table、複数の言語などからも text を抽出します。

OCR provider が処理した page 数を報告する場合、result にはその count が ocrPagesProcessed として含まれます。pages を count するのではなく、これを使用して billable page を追跡してください。この 2 つは異なる可能性があります。blank page は処理・課金されますが、content は返しません。

Backend code
// May exceed extractedResult.pages.length when the document contains blank pages.
console.log(extractedResult.ocrPagesProcessed);

extraction client には、remote URL にある document から text を抽出する追加 method があります。どちらの extraction method も optional な options parameter を受け取り、抽出する document の pageIndexes、使用するextraction method、image extraction の option を指定できます。

Backend code
const extractedResult = await extractionClient.extractDataFromDocumentUrl(
'www.file-url.com',
{ pageIndexes: [0, 1, 2] }
);

Extraction method​

Squid は複数の method のいずれかを通じて document を抽出します。method を選択するには、options parameter に preferredExtractionMethod を渡します。同じ option は、file context を knowledge base に upload する際にも受け入れられます。名前が示すとおり、method は guarantee ではなく preference であり、method を request しても error になることはありません。2 つの Basic method は常に適用されますが、provider-backed method の背後にある provider が environment で構成されていない場合、Squid は使用可能な最初の provider-backed method(Mistral OCR、Amazon Textract、Azure AI Document Intelligence の順)に暗黙的にフォールバックします。利用可能なものがない場合は plain Basic(legacy)にフォールバックします。method を選択しない場合、environment の default method にも同じ availability fallback が適用されます。extraction response では、実際に実行された method は報告されません。

  • mistral_ocr(Mistral OCR)、amazon_textract(Amazon Textract)、azure_document_intelligence(Azure AI Document Intelligence): provider-based OCR。embedded image ではない場合でも page 上の figure/graph を識別できるため、最も高い document extraction accuracy を実現する最も高性能な extraction method です。
  • legacy(Basic): PDF の text layer と embedded image を直接読み取ります。高速ですが、scanned page には text layer がないため text は抽出されません。document が knowledge base に ingestion されるとき、抽出された embedded image は AI により description されます(下記の extractImages を参照)。
  • legacy_with_llm(Basic + page understanding): Basic と同じ text extraction に加えて、各 page の image を AI が description します。すべての figure/graph が embedded image として表示されるわけではないため、これが必要になる場合があります。page ごとに 1 回の AI call が必要となるため、長い document は Basic よりも低速かつ高コストで抽出されます。

太字の名称は、document upload 時に同じ選択肢を利用できる Squid Console の knowledge base Add Knowledge dialog に表示される label です。この dialog で preselected される method は、environment の default です。

Backend code
const extractedResult = await extractionClient.extractDataFromDocumentFile(
data,
{ preferredExtractionMethod: 'legacy_with_llm' }
);

2 つの companion option により、すべての method の image 処理を制御します。extractImages の default は true です。document から抽出された image は、knowledge base に ingestion されると AI により description されます。false に設定すると、Basic + page understanding の page description を含むすべての image を完全に skip します。imageMinSizePixels の default は 100 で、いずれかの dimension がこれより小さい embedded image は skip されます。

次のステップ​

text を抽出した後は、次のことができます。

  • text を parse し、Squid の database connectors を使用して database に書き込む。
  • text の一部を Squid AI Agent への query として渡し、text に基づいて質問に回答したり action を実行したりする。