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 文档。默认输出格式为markdown,可以轻松与MarkdownHeaderTextSplitter结合进行语义文档分割。您还可以使用 mode="single"
或 mode="page"
以页面或单个文档的方式返回纯文本。
您需要在以下三个预览地区之一创建一个 Azure AI Document Intelligence 资源:East US,West US 2,West Europe。如果没有,请按照 此文档 创建。您将需要传递
和
作为加载器的参数。
%pip install --upgrade --quiet langchain langchain-community azure-ai-documentintelligence
在这个示例中,我们将使用 Azure AI Document Intelligence 上传本地文件。初始化文档分析客户端后,我们可以创建一个 DocumentIntelligenceLoader
实例:
from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader
file_path = "" # 本地文件路径
endpoint = "" # Azure AI 的终点
key = "" # API 密钥
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, file_path=file_path, api_model="prebuilt-layout"
)
documents = loader.load()
# 打印加载的文档信息
print(documents)
此时,默认输出包含一个Markdown格式的LangChain文档。
输入文件也可以是公共URL路径。例如使用以下URL进行加载:
url_path = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/layout.png"
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, url_path=url_path, api_model="prebuilt-layout"
)
documents = loader.load()
print(documents)
此时该文件将通过URL进行分析并返回markdown格式的文档。
您可以指定 mode="page"
按页面加载文档:
from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader
file_path = ""
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}")
输出结果将以列表形式展示,每页内容作为单独的文档存储。
您可以指定 analysis_feature=["ocrHighResolution"]
启用附加功能:
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)
此时的输出将包含识别文档,并应用高分辨率附加功能。
如果遇到问题欢迎在评论区交流。
—END—