> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Google cloud document AI 集成

> 使用 LangChain Python 集成 Google 云文档 AI 文档转换器。

文档 AI 是 Google Cloud 提供的文档理解平台，用于将文档中的非结构化数据转换为结构化数据，使其更易于理解、分析和使用。

了解更多：

* [文档 AI 概述](https://cloud.google.com/document-ai/docs/overview)
* [文档 AI 视频和实验室](https://cloud.google.com/document-ai/docs/videos)
* [立即尝试!](https://cloud.google.com/document-ai/docs/drag-and-drop)

该模块包含一个基于 Google Cloud 的 DocAI 的 `PDF` 解析器。

使用此解析器需要安装两个库：

```python theme={null}
pip install -qU  langchain-google-community[docai]
```

首先，您需要设置一个 Google Cloud Storage (GCS) 存储桶，并按照此处所述创建您自己的光学字符识别 (OCR) 处理器：[cloud.google.com/document-ai/docs/create-processor](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 中的 `Processor details` 标签的 `Prediction endpoint` 部分复制。

```python theme={null}
GCS_OUTPUT_PATH = "gs://BUCKET_NAME/FOLDER_PATH"
PROCESSOR_NAME = "projects/PROJECT_NUMBER/locations/LOCATION/processors/PROCESSOR_ID"
```

```python theme={null}
from langchain_core.document_loaders.blob_loaders import Blob
from langchain_google_community import DocAIParser
```

现在，创建一个 `DocAIParser`。

```python theme={null}
parser = DocAIParser(
    location="us", processor_name=PROCESSOR_NAME, gcs_output_path=GCS_OUTPUT_PATH
)
```

在这个示例中，您可以使用上传到公共 GCS 存储桶的 Alphabet 收益报告。

[2022Q1\_alphabet\_earnings\_release.pdf](https://storage.googleapis.com/cloud-samples-data/gen-app-builder/search/alphabet-investor-pdfs/2022Q1_alphabet_earnings_release.pdf)

将文档传递给 `lazy_parse()` 方法来

```python theme={null}
blob = Blob(
    path="gs://cloud-samples-data/gen-app-builder/search/alphabet-investor-pdfs/2022Q1_alphabet_earnings_release.pdf"
)
```

我们将获得每页一个文档，总共 11 个：

```python theme={null}
docs = list(parser.lazy_parse(blob))
print(len(docs))
```

```text theme={null}
11
```

您可以逐个对 blob 进行端到端解析。如果您有很多文档，将它们批量处理可能是一个更好的方法，甚至可以将解析与处理解析结果分离。

```python theme={null}
operations = parser.docai_parse([blob])
print([op.operation.name for op in operations])
```

```python theme={null}
['projects/543079149601/locations/us/operations/16447136779727347991']
```

您可以检查操作是否完成：

```python theme={null}
parser.is_running(operations)
```

```text theme={null}
True
```

当它们完成时，您可以解析结果：

```python theme={null}
parser.is_running(operations)
```

```text theme={null}
False
```

```python theme={null}
results = parser.get_results(operations)
print(results[0])
```

```text theme={null}
DocAIParsingResults(source_path='gs://vertex-pgt/examples/goog-exhibit-99-1-q1-2023-19.pdf', parsed_path='gs://vertex-pgt/test/run1/16447136779727347991/0')
```

现在，我们可以从解析结果中生成文档：

```python theme={null}
docs = list(parser.parse_from_results(results))
```

```python theme={null}
print(len(docs))
```

```text theme={null}
11
```

***

> 官方原文：[src/oss/python/integrations/document\_transformers/google\_docai.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/document_transformers/google_docai.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.