Azure AI Document Intelligence: 提取文档信息的智能助手
Azure AI Document Intelligence(前称:Azure Form Recognizer)是一种基于机器学习的服务,能够从数字或扫描的PDF、图像、Office文件和HTML文件中提取文本(包括手写)、表格、文档结构(如标题、章节标题等)和键值对。它支持多种文件格式,如PDF、JPEG/JPG、PNG、BMP、TIFF、HEIF、DOCX、XLSX、PPTX和HTML。
在本文中,我们将探讨如何使用Azure AI Document Intelligence将文档内容逐页整合并转换为LangChain文档。同时,我们将讨论如何处理网络访问问题,以及如何最大化利用该服务的抓取能力。
主要内容
1. 前提条件
在开始之前,您需要在Azure上创建一个AI Document Intelligence资源。此服务目前在以下三个预览区域提供:美国东部,美国西部2和西欧。请参考此文档创建资源。如果您还没有,请确保获取<endpoint>
和 <key>
以便加载器使用。
安装所需的Python包:
%pip install --upgrade --quiet langchain langchain-community azure-ai-documentintelligence
2. 使用本地文件进行文档加载
我们可以使用本地文件,通过Azure AI Document Intelligence加载文档。
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()
3. 使用公共URL路径加载文档
也可以通过公共URL输入文件,例如:
url_path = "<url>" # 替换为您的URL路径
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, url_path=url_path, api_model="prebuilt-layout"
)
documents = loader.load()
4. 按页加载文档
为方便处理大型文档,您可以指定mode="page"
以按页加载文档。
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}")
5. 使用高分辨率OCR功能
通过指定analysis_feature=["ocrHighResolution"]
,可以启用额外的功能以提高OCR解析的精度。
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()
常见问题和解决方案
1. 网络访问问题
由于某些地区的网络限制,访问Azure API可能不稳定。建议使用API代理服务以提高访问稳定性。
2. OCR识别不准确
在识别复杂文档或手写文本时,可以考虑启用ocrHighResolution
功能以提高精度。
总结与进一步学习资源
通过Azure AI Document Intelligence,开发者能够高效地从多种格式的文档中提取结构化信息,实现智能文档解析。进一步学习可以参考以下资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
—END—