Azure AI 文档智能
Azure AI Document Intelligence(以前称为
Azure Form Recognizer) 是机器学习 基于从中提取文本(包括手写)、表格、文档结构(例如标题、章节标题等)和键值对的服务 数字或扫描的 PDF、图像、Office 和 HTML 文件。Document Intelligence 支持
JPEG/JPG,PNG,BMP,TIFF,HEIF,DOCX,XLSX,PPTX和HTML.
当前使用Document Intelligence可以逐页合并内容并将其转换为 LangChain 文档。默认输出格式是 markdown,可以很容易地与MarkdownHeaderTextSplitter用于语义文档分块。您还可以使用mode="single"或mode="page"返回单页或按页拆分的文档中的纯文本。
先决条件
以下 3 个预览区域之一的 Azure AI Document Intelligence 资源:美国东部、美国西部 2、西欧 - 如果没有,请按照此文档创建一个。您将通过<endpoint>和<key>作为 loader 的参数。
%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 = "<filepath>"
endpoint = "<endpoint>"
key = "<key>"
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, file_path=file_path, api_model="prebuilt-layout"
)
documents = loader.load()
默认输出包含一个具有 markdown 格式内容的 LangChain 文档:
documents
示例 2
输入文件也可以是公共 URL 路径。例如,https://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"]以启用附加组件功能。有关更多信息,请参阅: https://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