跳到主要内容
Azure AI Document Intelligence(前身为 Azure Form Recognizer)是一项基于机器学习的服务,可从数字或扫描的 PDF、图像、Office 和 HTML 文件中提取文本(包括手写)、表格、文档结构(例如标题、章节标题等)和键值对。 Document Intelligence 支持 PDFJPEG/JPGPNGBMPTIFFHEIFDOCXXLSXPPTXHTML
目前使用 Document Intelligence 的加载器实现可以按页集成内容,并将其转换为 LangChain 文档。默认输出格式是 Markdown,可以轻松与 MarkdownHeaderTextSplitter 链式连接以进行语义文档分块。您也可以使用 mode="single"mode="page" 以返回单页或按页拆分的纯文本。

先决条件

一个位于以下三个预览区域之一的 Azure AI 文件智能资源:美国东部美国西部2西欧 - 如果您还没有,请按照此文档创建一个。您将把 <endpoint><key> 作为参数传递给加载器。
pip install -qU  langchain langchain-community azure-ai-documentintelligence

示例 1

第一个示例使用一个本地文件,该文件将被发送到 Azure AI 文件智能。 通过初始化的文档分析客户端,我们可以继续创建 DocumentIntelligenceLoader 的实例:
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()
默认输出包含一个 LangChain 文档,其内容采用 Markdown 格式。
documents

示例 2

输入文件也可以是公共 URL 路径。例如:raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/layout.png
url_path = "<url>"
loader = AzureAIDocumentIntelligenceLoader(
    api_endpoint=endpoint, api_key=key, url_path=url_path, api_model="prebuilt-layout"
)

documents = loader.load()
documents

示例 3

您还可以指定 mode="page" 以按页加载文档。
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()
输出将是每页作为单独的文档存储在列表中。
for document in documents:
    print(f"Page Content: {document.page_content}")
    print(f"Metadata: {document.metadata}")

示例 4

您还可以指定 analysis_feature=["ocrHighResolution"] 以启用附加功能。欲了解更多信息,请参阅:aka.ms/azsdk/python/documentintelligence/analysisfeature
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()
输出包含使用高分辨率附加功能识别的 LangChain 文档。
documents

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.