Google Cloud Document AI
Document AI 是 Google Cloud 的文件理解平台,可將文件中的非結構化資料轉換為結構化資料,讓理解、分析和使用更輕鬆。
了解更多
此模組包含基於 Google Cloud 的 DocAI 的 PDF
剖析器。
您需要安裝兩個程式庫才能使用此剖析器
%pip install --upgrade --quiet langchain-google-community[docai]
首先,您需要設定 Google Cloud Storage (GCS) 儲存bucket,並建立您自己的光學字元辨識 (OCR) 處理器,如此處所述:https://cloud.google.com/document-ai/docs/create-processor
GCS_OUTPUT_PATH
應為 GCS 上資料夾的路徑(以 gs://
開頭),而 PROCESSOR_NAME
應類似 projects/PROJECT_NUMBER/locations/LOCATION/processors/PROCESSOR_ID
或 projects/PROJECT_NUMBER/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/PROCESSOR_VERSION_ID
。您可以透過程式設計方式取得,或從 Google Cloud Console 中「處理器詳細資料」標籤的「預測端點」區段複製。
GCS_OUTPUT_PATH = "gs://BUCKET_NAME/FOLDER_PATH"
PROCESSOR_NAME = "projects/PROJECT_NUMBER/locations/LOCATION/processors/PROCESSOR_ID"
from langchain_core.document_loaders.blob_loaders import Blob
from langchain_google_community import DocAIParser
現在,建立 DocAIParser
。
parser = DocAIParser(
location="us", processor_name=PROCESSOR_NAME, gcs_output_path=GCS_OUTPUT_PATH
)
對於此範例,您可以使用已上傳到公用 GCS bucket 的 Alphabet 收益報告。
2022Q1_alphabet_earnings_release.pdf
將文件傳遞至 lazy_parse()
方法以
blob = Blob(
path="gs://cloud-samples-data/gen-app-builder/search/alphabet-investor-pdfs/2022Q1_alphabet_earnings_release.pdf"
)
我們將取得每頁一個文件,總共 11 個
docs = list(parser.lazy_parse(blob))
print(len(docs))
11
您可以逐一執行 blob 的端對端剖析。如果您有許多文件,將它們批次處理在一起,甚至將剖析與處理剖析結果分離,可能會是更好的方法。
operations = parser.docai_parse([blob])
print([op.operation.name for op in operations])
['projects/543079149601/locations/us/operations/16447136779727347991']
您可以檢查操作是否完成
parser.is_running(operations)
True
當它們完成時,您可以剖析結果
parser.is_running(operations)
False
results = parser.get_results(operations)
print(results[0])
DocAIParsingResults(source_path='gs://vertex-pgt/examples/goog-exhibit-99-1-q1-2023-19.pdf', parsed_path='gs://vertex-pgt/test/run1/16447136779727347991/0')
現在我們終於可以從剖析結果產生文件
docs = list(parser.parse_from_results(results))
print(len(docs))
11