Azure AI 文档智能
Azure AI Document Intelligence (formerly known as
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"来返回纯文本单页或者将文档按页面分割。
前置条件
一个位于以下三个预览区域之一的 Azure AI 文档智能资源:East US、West US2、West Europe - 如果您尚未拥有,请遵循此文档进行创建。您将把 <endpoint> 和 <key> 作为参数传递给加载器。
%pip install --upgrade --quiet langchain langchain-community azure-ai-documentintelligence
Example 1
The first example uses a local file which will be sent to Azure AI Document Intelligence.
使用初始化的文档分析客户端,我们可以创建一个DocumentIntelligenceLoader 实例:<br/>
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()
The default output contains one LangChain 文档,内容格式为 markdown 格式:
documents
Example 2
The input file can also be a public URL path. E.g., 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()
The output will be each page stored as a separate document in the list:
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