Cortex PARSE_DOCUMENT

PARSE_DOCUMENT 函数是特定于 Cortex AI 任务的函数,能够从文档中提取文本、数据和布局元素。您可以使用 PARSE_DOCUMENT 从各种文档和表单中提取文本,以实现以下目的:

  • RAG 管道为 Cortex Search 提供支持

  • 使用 Cortex AI 函数进行 LLM 处理,如文档摘要或翻译

  • 使用 Cortex AI 结构化输出进行零样本实体提取

PARSE_DOCUMENT 的工作原理

PARSE_DOCUMENT 函数提供两种处理 PDF 文档的模式:

  • 推荐使用 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

为文档处理创建暂存区

在开始处理文档之前,请创建一个存储相关文档的内部或外部暂存区。创建暂存区时,必须进行 服务器端加密。否则,PARSE_DOCUMENT 将返回错误,提示所提供的文件不是预期格式或已在客户端加密。

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

备注

从各个暂存区处理文件目前与自定义 网络策略 不兼容。

小技巧

如果在外部暂存区遇到“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

每个文档的最大页数

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),文档中的每一页均按页计费。

  • 对于图像文件格式(JPEG、JPG、TIF、TIFF、PNG),每个单独的图像文件均按页计费。

Snowflake 建议在使用较小仓库(不大于 MEDIUM)的情况下执行调用 Cortex PARSE_DOCUMENT 函数的查询。较大的仓库并不会提高性能。

错误条件

Snowflake Cortex PARSE_DOCUMENT 可能会生成以下错误消息:

消息

解释

文档包含不支持的语言。

输入文档包含不支持的语言

不支持提供的文件格式 {file_extension}。支持的格式:[“.docx”、“.pptx”、“.pdf”]。

当文档的格式不受支持时返回。

不支持提供的文件格式 .bin。支持的格式:[“.docx”、“.pptx”、“.pdf”]。确保使用服务器端加密存储文件。

当不支持文件格式且将其理解为二进制文件时返回。确保暂存区使用服务器端加密。

超出了最大页数 300 页。

PDF 包含超过 300 页时返回。

超出了最大文件大小 104857600 字节。

文档大于 100 MB 时返回。

找不到提供的文件。

文件不存在。

无法访问提供的文件。

由于权限不足,无法访问文件。

“解析文档”函数未在允许的时间内响应。

出现超时。

内部错误。

出现系统错误。等待并重试。

语言: 中文