使用 Workflows 构建文档处理流水线

如果您使用 Document AI 构建涉及多个文档处理步骤的工作流,则可能需要使用无服务器编排来管理工作流。

Workflows 会按您定义的顺序将一系列任务关联起来。它无需管理基础架构,还可按需无缝扩缩,包括缩减至零。由于采用的是按用量付费价格模式,您只需为执行时间付费。

Workflows 为 Document AI API 提供了一个内置连接器。您只需使用配置文件为连接器定义所需的步骤,无需实现额外的代码。连接器会处理请求的格式设置,隐藏 API 的详细信息。连接器还提供了用于进行身份验证、处理重试和执行长时间运行的操作的内置功能。

如需了解详情,请参阅了解连接器

本指南使用账单解析器处理器处理一组示例账单,采用批处理


如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:

操作演示


准备工作

  1. 登录您的 Google Cloud 账号。如果您是新手 Google Cloud, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Document AI, Cloud Storage, Workflows APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  8. Verify that billing is enabled for your Google Cloud project.

  9. Enable the Document AI, Cloud Storage, Workflows APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

所需的角色

如需获得创建自定义敏感类别所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。

创建账单解析器

使用 Google Cloud 控制台创建新的账单解析器。如需了解详情,请参阅创建和管理处理器页面。

  1. 在 Google Cloud 控制台导航菜单中,点击 Document AI 并 选择 处理器库

    处理器主题库

  2. 处理器库中, 搜索 账单解析器,然后选择创建

    创建账单处理器

  3. 输入处理器名称,例如workflows-invoice-processor

  4. 选择用于托管处理器的区域(在 Document AI API 中为 location)。

  5. 点击创建。系统随即会显示处理器详情标签页。

  6. 复制您的处理器 ID。您稍后在代码中会用到该 ID。

    处理器 ID

在 Cloud Storage 中配置输出存储桶

使用批处理(异步)时,输入和输出文件必须存储在 Cloud Storage 中。以下存储桶包含用作本指南输入的内容的示例文件:

gs://cloud-samples-data/documentai/workflows

配置存储桶以将输出文件存储在项目中。

  1. 按照 Cloud Storage 文档中的创建存储桶指南创建一个存储桶,或使用现有的存储桶。

  2. 在存储桶中创建一个文件夹(例如 docai-workflows-output)。

创建工作流

  1. 在 Google Cloud 控制台中打开 Workflows 信息中心

    前往 Workflows 信息中心

  2. 点击创建 ,以创建您的第一个工作流。系统会显示创建工作流 页面。

    工作流创建

  3. 将工作流重命名docai-workflows-quickstart

  4. 添加说明,如果此项目有服务账号,请选择服务账号。

    1. 如果您没有服务帐号,请选择创建新的服务账号
      • 为您的服务帐号指定一个名称,然后选择创建并继续CreateServiceAccount
      • 为其授予以下角色:
      • Document AI API User
      • Workflows Invoker
      • Logs Writer
      • Service Usage Consumer
      • ServiceAccountRoles
  5. 创建工作流 页面上,点击下一步

定义工作流

  1. Workflows 编辑器 中,清除默认的 YAML 文件内容并粘贴以下代码:

    main:
      params: [input]
      steps:
        - start:
            call: sys.log
            args:
              text: ${input}
        - vars:
            assign:
              - input_gcs_bucket: ${input.inputBucket}
              - output_gcs_bucket: ${input.outputBucket}
              - processor_id: ${input.processorId}
              - location: ${input.location}
              - project_id: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
        - batch_process:
            call: googleapis.documentai.v1.projects.locations.processors.batchProcess
            args:
              name: ${"projects/" + project_id + "/locations/" + location + "/processors/" + processor_id}
              location: ${location}
              body:
                inputDocuments:
                  gcsPrefix:
                    gcsUriPrefix: ${input_gcs_bucket}
                documentOutputConfig:
                  gcsOutputConfig:
                    gcsUri: ${output_gcs_bucket}
                skipHumanReview: true
            result: batch_process_resp
        - return:
            return: ${batch_process_resp}
    
WorkflowsEditor
  1. 选择部署

测试工作流

现在,工作流已部署,当前页面应该是工作流详情页面。

  1. 点击页面顶部的执行以进入执行工作流页面。通过此页面,您可以在控制台中测试工作流。

  2. 替换以下输入 JSON 中的占位符变量:

    • OUTPUT_BUCKET_PATH:之前创建的输出存储桶和文件夹的完整路径。
    • PROCESSOR_ID:之前创建的处理器的 ID。
    • LOCATION:之前创建的处理器的区域。
    {
      "inputBucket": "gs://cloud-samples-data/documentai/workflows",
      "outputBucket": "OUTPUT_BUCKET_PATH",
      "processorId": "PROCESSOR_ID",
      "location": "LOCATION"
    }
    
  3. 将 JSON 复制/粘贴到输入文本框中。

  4. 点击工作流底部的执行以测试您的输入,然后等待该过程完成。

  5. 工作流完成后,输出 JSON 应如下所示。

    {
      "done": true,
      "metadata": {
        "@type": "type.googleapis.com/google.cloud.documentai.v1.BatchProcessMetadata",
        "createTime": "2023-02-24T18:31:18.521764Z",
        "individualProcessStatuses": [
          {
            "humanReviewStatus": {
              "state": "SKIPPED"
            },
            "inputGcsSource": "gs://cloud-samples-data/documentai/workflows/baking_technologies_invoice.pdf",
            "outputGcsDestination": "OUTPUT_BUCKET_PATH/11119195367705871803/0",
            "status": {}
          },
          {
            "humanReviewStatus": {
              "state": "SKIPPED"
            },
            "inputGcsSource": "gs://cloud-samples-data/documentai/workflows/generic_invoice.pdf",
            "outputGcsDestination": "OUTPUT_BUCKET_PATH/11119195367705871803/1",
            "status": {}
          },
          {
            "humanReviewStatus": {
              "state": "SKIPPED"
            },
            "inputGcsSource": "gs://cloud-samples-data/documentai/workflows/google_invoice.pdf",
            "outputGcsDestination": "OUTPUT_BUCKET_PATH/11119195367705871803/2",
            "status": {}
          }
        ],
        "state": "SUCCEEDED",
        "updateTime": "2023-02-24T18:31:52.543163Z"
      },
      "name": "projects/PROJECT_ID/locations/LOCATION/operations/11119195367705871803",
      "response": {
        "@type": "type.googleapis.com/google.cloud.documentai.v1.BatchProcessResponse"
      }
    }
    
  6. 打开输出存储桶,您会看到在已按操作 ID 命名的文件夹中处理的每份账单的输出内容。 BatchGCSOutput

  7. 此文件夹中将有子文件夹,每个子文件夹对应于从包含 Document JSON 文件的输入批次处理的每个文档。

  8. 您可以检查每个 JSON 文件,也可以编写代码为您的用例使用相关字段。如需了解详情,请参阅操作处理响应指南

  9. 您现在可以在工作流中关联其他步骤,以进一步处理输出,例如将提取的实体插入数据库进行分析。

  10. 如需详细了解如何触发工作流,请参阅 Workflows 文档

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

为避免产生不必要的 Google Cloud 费用,请使用 Google Cloud console 删除您不需要的处理器、存储桶、工作流和项目(如果不需要 )。

后续步骤