引言
在数字化信息爆炸的时代,高效地提取和管理文档信息变得至关重要。Azure AI Document Intelligence 提供了一套基于机器学习的服务,能够从多种格式(如 PDF、JPEG、DOCX 等)的数字或扫描文档中提取文本、表格、文档结构和关键值对。本篇文章将带你深入了解如何使用 Azure AI Document Intelligence 处理文档,并转化为 LangChain 文档格式。
主要内容
Azure AI Document Intelligence 简介
Azure AI Document Intelligence(前称 Azure Form Recognizer)能够从数字文档和扫描文件中提取信息。支持的格式包括 PDF、JPEG/JPG、PNG 等,输出格式默认为 markdown,适合语义文档分块处理。
前提条件
- 在 Azure 中创建一个 AI Document Intelligence 资源(支持区域:East US、West US2、West Europe)。
- 通过
pip安装必要的 Python 包:%pip install --upgrade --quiet langchain langchain-community azure-ai-documentintelligence
使用 Azure AI Document Intelligence
本文将通过几个示例展示如何使用 Azure AI Document Intelligence。
示例 1: 处理本地文件
通过初始化文档分析客户端,可以创建 DocumentIntelligenceLoader 实例:
from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader
file_path = "<filepath>"
endpoint = "<endpoint>" # 使用API代理服务提高访问稳定性
key = "<key>"
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, file_path=file_path, api_model="prebuilt-layout"
)
documents = loader.load()
默认输出包含一个 markdown 格式的 LangChain 文档。
示例 2: 处理网络文件
同样可以处理公共 URL 指向的文件:
url_path = "<url>"
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, url_path=url_path, api_model="prebuilt-layout"
)
documents = loader.load()
示例 3: 按页加载文档
可以指定 mode="page" 以按页加载文档:
from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader
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
启用 ocrHighResolution 功能以提高识别质量:
from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader
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()
常见问题和解决方案
网络访问限制
由于部分地区网络限制,可能需要使用 API 代理服务来提升访问的稳定性。
API 参数配置
确保 Azure AI Document Intelligence 的 endpoint 和 key 正确配置,可以通过 Azure 门户获取。
总结和进一步学习资源
本文介绍了如何使用 Azure AI Document Intelligence 提取文档信息的基础知识和实践。对于想要进一步学习的读者,可以参考 Azure 的文档加载器概念指南和文档加载器操作指南。
参考资料
结束语:如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---