Azure AI Document Intelligence 使用指南

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 文档。默认输出格式为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

示例 1: 使用本地文件进行分析

在这个示例中,我们将使用 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文档。

示例 2: 使用公共URL路径

输入文件也可以是公共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格式的文档。

示例 3: 按页面加载文档

您可以指定 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}")

输出结果将以列表形式展示,每页内容作为单独的文档存储。

示例 4: 使用分析功能

您可以指定 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—

你可能感兴趣的:(azure,人工智能,flask,python)