Box AIを使用した構造化データの抽出

Yuko Taniguchi
Box Developer Japan Blog
10 min readJul 1, 2024

この記事では、Box AI APIを使用してドキュメントから構造化データを抽出する方法を紹介します。

この新しいエンドポイントを使用すると、アプリは非構造化ドキュメントを照会し、アドホックな構造に基づいてデータを取得できます。

このエンドポイントはまだベータ版のため、ご利用のBoxアカウントレベルでは使用できない場合があります。

前回の記事では、Box AIを使用して非構造化ドキュメントから構造化メタデータを抽出する方法について説明しました。

Box AIによるメタデータ抽出を使用する場合は、メタデータテンプレートから取得した、あらかじめ設定された構造に従ってデータを抽出するようBox AI APIに指示します。

例を挙げておさらいしましょう。次の請求書のサンプルを考えてみます。

請求書のサンプル

メタデータテンプレートはこちらです。

メタデータテンプレート

メタデータの候補を要求します。

curl --location 'https://api.box.com/2.0/metadata_instances/suggestions?item=file_1443721424754&scope=enterprise_1133807781&template_key=rbInvoicePO&confidence=experimental' \
--header 'Authorization: Bearer Qj...RF'

その後、以下の結果が表示されます。

{
"$scope": "enterprise_1133807781",
"$templateKey": "rbInvoicePO",
"suggestions": {
"documentType": "Invoice",
"total": "1,050",
"vendor": "Quasar Innovations",
"invoiceNumber": "Q2468",
"purchaseOrderNumber": "003"
}
}

つまり、Box AIは、データ構造を取得して、非構造化ドキュメントを読み取り、構造化データに入力するのに最適な候補を返すことができます。

構造化データの抽出

Boxでは、メタデータテンプレートなしでも同じことができるようになりました。Box AI APIにドキュメントとアドホックな構造を指定すると、同様の処理が行われます。以下に例を示します。

curl --location 'https://api.box.com/2.0/ai/extract' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer Qj...RF'
--data '{
"prompt": "{\"fields\":[{\"key\":\"vendor\",\"displayName\":\"Vendor\",\"type\":\"string\",\"description\":\"Vendorname\"},{\"key\":\"documentType\",\"displayName\":\"Type\",\"type\":\"string\",\"description\":\"\"}]}",
"items": [
{
"type": "file",
"id": "1443721424754"
}
]
}'

結果は次のとおりです。

{
"answer": "{\"vendor\": \"Quasar Innovations\", \"documentType\": \"Invoice\"}",
"created_at": "2024-05-31T10:15:38.17-07:00",
"completion_reason": "done"
}

ここでのプロンプトが、JSON文字列で表されたメタデータテンプレートの定義のように見えることに気付いたかもしれません。以下に、実際のテンプレートの定義の概要を示します。

{
"id": "8105a3ed-dca3-495f-9e89-9bdf316bb832",
"type": "metadata_template",
"templateKey": "rbInvoicePO",
"scope": "enterprise_1133807781",
"displayName": "RB: Invoice & POs",
"fields": [
{
"id": "2af17183-d0c7-4510-8344-0cb5facb2120",
"key": "documentType",
"displayName": "Document Type",
"description": "Is this an invoice or a purchase order?",
},
{
"id": "546e67d0-307e-46b8-ab19-e8d2ae05d0ed",
"type": "string",
"key": "vendor",
"displayName": "Vendor",
}
]
}

最初にこれを見たとき、私は「適切なJSONオブジェクトを使用したほうがよくないだろうか。これは面倒だ」と思いました。

このとき、エンジニアリングチームから、Boxメタデータの定義を使用する必要はなく、Box AIが構造化データを探すのに役立つものなら何でも渡すことができると説明されました。以下に例を示します。

curl --location 'https://api.box.com/2.0/ai/extract' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer Qj...RF' \
--data '{
"prompt": "{\"vendor\",\"total\",\"doctype\",\"date\",\"PO\"}",
"items": [
{
"type": "file",
"id": "1443721424754"
}
]
}'

結果は次のとおりです。

{
"answer": "{\"vendor\": \"Quasar Innovations\", \"total\": \"$1,050\", \"doctype\": \"Invoice\", \"PO\": \"003\"}",
"created_at": "2024-05-31T10:28:51.906-07:00",
"completion_reason": "done"
}

平易な英語を使うこともできます。

curl --location 'https://api.box.com/2.0/ai/extract' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer Qj...RF' \
--data '{
"prompt": "find the document type (invoide or po), vendor, total, and po number",
"items": [
{
"type": "file",
"id": "1443721424754"
}
]
}'

結果は次のとおりです。

{
"answer": "{\"Document Type\": \"Invoice\", \"Vendor\": \"Quasar Innovations\", \"Total\": \"$1,050\", \"PO Number\": \"003\"}",
"created_at": "2024-05-31T10:30:51.223-07:00",
"completion_reason": "done"
}

ドキュメントによっては、次のようなシンプルなプロンプトで結果を取得することもできます。

curl --location 'https://api.box.com/2.0/ai/extract' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer Qj...RF' \
--data '{
"prompt": "summarize document",
"items": [
{
"type": "file",
"id": "1443721424754"
}
]
}'

結果は次のとおりです。

{
"answer": "{\"Vendor\": \"Quasar Innovations\", \"Invoice Number\": \"Q2468\", \"Purchase Order Number\": \"003\", \"Total\": \"$1,050\"}",
"created_at": "2024-05-31T10:47:59.295-07:00",
"completion_reason": "done"
}

私は少し立ち止まって、この意味を考えました。

これは素晴らしいことです。つまり、任意のシステムの形式的な構造があれば、それがJSON、XML、YAML、あるいは独自の言語であっても、それをBox AIに読み取らせて照会できるということです。

もちろん、人によっては工夫が必要な場合もあります。Box AIが必要な情報を見つけられるようにプロンプトを微調整する必要があるかもしれません。複雑すぎると、レスポンスで空の結果が返される場合もあります。

たとえば、次のドキュメントのような例です。

非構造化ドキュメントのサンプル
curl --location 'https://api.box.com/2.0/ai/extract' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer Qj...RF' \
--data '{
"prompt": "summarize document",
"items": [
{
"type": "file",
"id": "1530265998769"
}
]
}'

結果は次のとおりです。

{
"answer": "{}",
"created_at": "2024-05-31T10:41:03.228-07:00",
"completion_reason": "done"
}

Boxのエンジニアリングチームは、今後も興味深い技術的ユースケースやアプリケーションを紹介していきます。皆さまがご自身のアプリケーションでこれらの概念をどのように応用するかを楽しみにしています。

アイデア、コメント、フィードバックがある場合は、コミュニティフォーラム (英語のみ) にコメントをお送りください。

--

--