跳到主要內容
Open In ColabOpen on GitHub

Azure AI 文件智慧

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

文件智慧支援 PDFJPEG/JPGPNGBMPTIFFHEIFDOCXXLSXPPTXHTML

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

先決條件

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

%pip install --upgrade --quiet  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 路徑。例如,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

此頁面是否對您有幫助?