跳至主要內容

Azure AI 文件智慧

Azure AI 文件智慧 (先前稱為 Azure Form Recognizer) 是一種基於機器學習的服務,可從數位或掃描的 PDF、影像、Office 和 HTML 檔案中提取文字 (包括手寫)、表格、文件結構 (例如標題、章節標題等) 和索引鍵值組。

文件智慧支援 PDFJPEG/JPGPNGBMPTIFFHEIFDOCXXLSXPPTXHTML

目前使用 文件智慧 的載入器實作可以逐頁整合內容,並將其轉換為 LangChain 文件。預設輸出格式為 Markdown,可以輕鬆地與 MarkdownHeaderTextSplitter 串連,以進行語意文件分塊。您也可以使用 mode="single"mode="page" 來傳回單一頁面中的純文字,或依頁面分割的文件。

先決條件 (Prerequisite)

在 3 個預覽區域之一中建立 Azure AI 文件智慧資源:美國東部美國西部 2西歐 - 如果您沒有,請按照此文件建立一個。您將傳遞 <endpoint><key> 作為載入器的參數。

%pip install --upgrade --quiet  langchain langchain-community azure-ai-documentintelligence

範例 1 (Example 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()

預設輸出包含一個具有 Markdown 格式內容的 LangChain 文件。

documents

範例 2 (Example 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 (Example 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 (Example 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

此頁面有幫助嗎? (Was this page helpful?)