Cortex PARSE_DOCUMENT

PARSE_DOCUMENT 関数は、 ドキュメントからテキスト、 データ、 レイアウト要素を抽出する機能を提供する Cortex AI AISQL 関数 です。PARSE_DOCUMENT を使って、さまざまなドキュメントやフォームからテキストを抽出し、次のような実装を行うことができます。

  • RAG Cortex Searchを支えるパイプライン

  • LLM Cortex AI 関数を使用したドキュメントの要約や翻訳のような処理。

  • Cortex AI 構造化出力を用いたゼロショット・エンティティ抽出

PARSE_DOCUMENT の仕組み

PARSE_DOCUMENT 関数には、 PDF ドキュメントを処理するための2つのモードがあります。

  • OCR モードは、 マニュアル、 契約書、 製品詳細ページ、 保険ポリシーや請求書、 SharePoint ドキュメント などのドキュメントから、素早く高品質なテキスト抽出を行うために推奨されるオプションです。

  • LAYOUT モードは、テキストやテーブルのようなレイアウト要素の抽出に最適化されています。これは、検索情報システムや大容量言語モデル (LLM) の推論を最適化するために、文書知識ベースのコンテキストを改善するために推奨されるオプションです。

注釈

PARSE_DOCUMENT レイアウトモードは現在プレビュー中です。OCR モードは ほとんどのリージョン で一般的に利用可能です。

PARSE_DOCUMENT の使用

Cortex PARSE_DOCUMENT は SQL 関数です。Snowflakeによって完全にホストされ管理されているため、これらを使用するのにセットアップは必要ありません。ドキュメントからデータを抽出するために、 PARSE_DOCUMENT 関数を、 PDF ドキュメントを含むステージに指定します。以下の例では、 parse_document データベースと demo スキーマの documents ステージのファイル document_1.pdf からテキストとレイアウト情報を抽出しています。

注釈

PARSE_DOCUMENT は現在、カスタム ネットワークポリシー と互換性がありません。

SELECT
  SNOWFLAKE.CORTEX.PARSE_DOCUMENT(
    @parse_document.demo.documents,
    'document_1.pdf',
    {'mode': 'LAYOUT'}
  ) AS layout;
Copy

ドキュメント処理のステージを作成

処理したいドキュメントを保存する内部または外部ステージを作成します。ステージを作成する際、 Server Side Encryption を有効にしてください。それ以外の場合は、 PARSE_DOCUMENT が提供されたファイルが期待された形式でない、またはクライアント側で暗号化されているというエラーを返します。

以下の SQL は、適切な内部ステージを作成します。

CREATE OR REPLACE STAGE input_stage
    DIRECTORY = ( ENABLE = true )
    ENCRYPTION = ( TYPE = 'SNOWFLAKE_SSE' );
Copy

次の SQL は、Amazon S3に外部ステージを作成します。Azure、 GCP の外部ステージにも対応しています。

CREATE OR REPLACE STAGE input_stage
    URL='s3://<s3-path>/'
    CREDENTIALS=(AWS_KEY_ID=<aws_key_id>
    AWS_SECRET_KEY=<aws_secret_key>)
    ENCRYPTION=( TYPE = 'AWS_SSE_S3' );
Copy

注釈

ステージングされたファイルを処理することは、現在、カスタム ネットワークポリシー と互換性がありません。

Tip

外部ステージから "Expiry in seconds for AWS role is invalid "というエラーメッセージが表示された場合は、 URL の有効期限が正確にセットされていることを確認してください。このアカウントパラメーターのデフォルト値は内部ステージングに最適化されていますが、外部ステージングで機能するように調整するオプションもあります。変更するには、Snowflakeサポートに連絡先が必要です。

この例では、 PARSE_DOCUMENT の OCR モードを用いて、 下記のように、気象保険文書の最初のページからテキストを抽出します。

気象保険のドキュメント例

このドキュメントを解析するには、 document_stage というステージにアップロードし、以下のクエリを実行します。

SELECT SNOWFLAKE.CORTEX.PARSE_DOCUMENT(
    @document_stage,
    'weather_policy.pdf'
  ) AS weather_policy_doc
Copy

PARSE_DOCUMENT からの未加工の返答は以下の通りです。

{
  "content": "SOME INSURANCE COMPANY\nWEATHER PROTECTION INSURANCE POLICY\nPolicy Number: WP-2025-789456\nEffective Date: April 1, 2025\nExpiration Date: April 1, 2026\nNAMED INSURED AND PROPERTY:\nJohn and Mary Homeowner\n123 Shelter Lane\nWeatherton, ST 12345\n

SECTION I - DEFINITIONS\nThroughout this policy, \"you\" and \"your\" refer to the Named Insured shown in the Declarations and the spouse if a\nresident of the same household. \"We,\" \"us,\" and \"our\" refer to Evergreen Insurance Company providing this\ninsurance. In addition, certain words and phrases are defined as follows:\n1. Weather Event means a natural atmospheric occurrence including but not limited to: a. Wind (including\nhurricanes, tornadoes, and straight-line winds) b. Hail c. Lightning d. Snow, ice, and freezing rain e.\nExcessive rainfall resulting in flooding f. Extreme temperatures causing damage\n2. Named Storm means any storm or weather disturbance that has been declared and named as a tropical\nstorm or hurricane by the National Weather Service or National Hurricane Center.\n3. Actual Cash Value (ACV) means the cost to repair or replace damaged property with new material of like\nkind and quality, less depreciation due to age, wear and condition.\n4. Replacement Cost means the cost to repair or replace damaged property with new material of like kind\nand quality, without deduction for depreciation.\n5. Dwelling means the building structure at the insured location including attached structures and fixtures.\n6. Other Structures means structures on the residence premises separated from the dwelling by clear\nspace or connected only by a fence, utility line, or similar connection.\n7. Personal Property means movable items owned by you and located at the insured property.\n

SECTION II - COVERAGE\nA. PROPERTY COVERAGE\nWe will pay for direct physical loss to property described in the Declarations caused by a Weather Event unless\nthe loss is excluded in Section III - Exclusions.\n1. Dwelling Protection We will cover: a. Your dwelling, including attached structures b. Materials and\nsupplies located on or adjacent to the residence premises for use in construction, alteration, or repair of\nthe dwelling or other structures c. Foundation, floor slab, and footings supporting the dwelling d.\nWall-to-wall carpeting attached to the dwelling\n2. Other Structures Protection We will cover structures on your property separated from your dwelling by\nclear space, including: a. Detached garages b. Storage sheds c. Fences d. Driveways and walkways e.\nPatios and retaining walls\n3. Personal Property Protection We will cover personal property owned or used by you while it is on the\nresidence premises. Coverage includes but is not limited to: a. Furniture b. Clothing c. Electronic\nequipment d. Appliances e. Sporting goods\n4. Loss of Use Protection If a Weather Event makes your residence uninhabitable, we will cover: a.\nAdditional living expenses incurred to maintain your normal standard of living b. Fair rental value if part of\nyour residence is rented to others c. Necessary expenses required to make the residence habitable or\nmove to temporary housing\nB. ADDITIONAL COVERAGES",
  "metadata": {
    "pageCount": 1
  }
}
Copy

この応答をCortex AI LLM 関数で処理することで、非構造化データ分析を簡単に実行できます。次の例では、簡単な質問応答タスクを示します。

SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-3-5-sonnet',
  CONCAT ('Is clothing covered as part of the weather protection insurance policy?',
    TO_VARCHAR(weather_policy_doc))) FROM ocr_example_docs
Copy

応答:

Yes, clothing is covered under the insurance policy. According to Section II - Coverage, Part A.3 (Personal Property Protection), clothing is specifically listed as one of the covered personal property items while it is on the residence premises. The policy states: "We will cover personal property owned or used by you while it is on the residence premises. Coverage includes but is not limited to: a. Furniture b. Clothing c. Electronic equipment d. Appliances e. Sporting goods"

入力要件

PARSE_DOCUMENT は、デジタル文書とスキャン文書の両方に最適化されています。以下のリストは、入力ドキュメントの制限と要件です。

最大ファイルサイズ

100 MB

1ドキュメントあたりの最大ページ数

300ページ

許可されたファイルタイプ

PDF, PPTX, DOCX, JPEG, JPG, PNG, TIFF, TIF

ステージ暗号化

サーバー側の暗号化

文字サイズ

最善の結果には8ポイント以上

対応ドキュメント機能

機能

説明

ページオリエンテーション

PARSE_DOCUMENT は自動的にページオリエンテーションを検出します。

文字

PARSE_DOCUMENT は以下の文字を検出します。

  • a-z

  • A-Z

  • 0-9

  • À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö Ø Ù Ú Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ Ą ą Ć ć Č č Đ đ Ę ę ı Ł ł Ń ń ō Œ œ Ś ś Š š Ÿ Ź ź Ż ż Ž ž ʒ β δ ε з Ṡ

  • ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~ ¡ ¢ £ ¥ § © ª « ­ ® ¯ ° ± ² ³ ´ µ ¶ · º » ¿ ' † ‡ • ‣ ⁋ ₣ ₤ ₦ ₩ € ₭ ₹ ™ ← ↑ → ↓ ↔ ↕ ↖ ↗ ↘ ↙ ↰ ↱ ↲ ↳ ↴ ↵

注釈

PARSE_DOCUMENT は手書き認識の訓練を受けていません。

サポートされている言語

PARSE_DOCUMENT は意図的に以下の言語のために訓練されています:

OCR モード

LAYOUT モード

  • 英語

  • フランス語

  • ドイツ語

  • イタリア語

  • ノルウェー語

  • ポーランド語

  • ポルトガル語

  • スペイン語

  • スウェーデン語

  • 英語

LAYOUT モードはフランス語、ドイツ語、イタリア語、ノルウェー語、ポーランド語、ポルトガル語、スペイン語、スウェーデン語にも対応していますが、最適化されていません。

リージョンの可用性

この機能のサポートは、以下のSnowflakeリージョンのアカウントで利用可能です。

AWS

Azure

Google Cloud Platform

US 西部2(オレゴン州)

東 US 2(バージニア)

US 中央部1(アイオワ)

US 東部(オハイオ)

西 US 2(ワシントン)

US 東部1(北部バージニア)

ヨーロッパ (オランダ)

ヨーロッパ(アイルランド)

ヨーロッパ中部1(フランクフルト)

アジア太平洋(シドニー)

アジア太平洋(東京)

アクセス制御の要件

さらに、 PARSE_DOCUMENT 関数を使用するには、 ACCOUNTADMIN ロールは、 SNOWFLAKE.CORTEX_USER データベースロールを関数を呼び出せるユーザーに付与する必要があります。詳細については 必要な権限 をご参照ください。

コストの考慮事項

Cortex PARSE_DOCUMENT 関数は、処理されるドキュメントのページ数に応じて計算コストが発生します。

  • ドキュメントファイル形式 (PDF, DOCX) の場合、ドキュメント内の各ページが1ページとして請求されます。

  • 画像ファイル形式 (JPEG, JPG, TIF, TIFF, PNG) の場合、個々の画像ファイルは1ページとして請求されます。

Snowflake は、Cortex PARSE_DOCUMENT 関数を呼び出すクエリを、小規模なウェアハウス(MEDIUM 以下)で実行することを推奨しています。ウェアハウスが大きくなってもパフォーマンスは向上しません。

エラー条件

Snowflake Cortex PARSE_DOCUMENT は以下のエラーメッセージを生成する可能性があります。

メッセージ

説明

ドキュメントにサポートされていない言語が含まれています。

入力ドキュメントにサポートされていない言語が含まれています。

プロバイダーのファイル形式 {file_extension} はサポートされていません。サポートされている形式: .['.docx', '.pptx', '.pdf']。

ドキュメントがサポートされていない形式の場合に返されます。

プロバイダーのファイル形式.binはサポートされていません。対応形式: ['.docx', '.pptx', '.pdf']。ファイルがサーバー側で暗号化されて保存されていることを確認してください。

ファイル形式がサポートされておらず、バイナリファイルとして理解できない場合に返されます。ステージがサーバー側の暗号化を使用していることを確認します。

最大ページ数300ページを超えました。

300ページを超える場合は PDF が返されます。

最大ファイルサイズ104857600バイトを超えました。

ドキュメントが100 MB より大きい場合に返されます。

プロバイダーファイルが見つかりません。

ファイルは存在しません。

プロバイダーのファイルにアクセスできません。

権限不足のため、ファイルにアクセスできません。

Parse Document関数が許容時間内に応答しませんでした。

タイムアウトが発生しました。

内部エラー。

システムエラーが発生しました。待って、もう一度お試しください。