处理文档

本快速入门介绍如何使用 Document AI API 的批处理功能处理来自源存储桶的文档(账单),以及如何将已处理的文档(JSON 文件)存储在目标存储桶中。

准备工作

在运行本快速入门之前,请确保您或您的管理员已满足以下前提条件:

  • 确保已在 Google Cloud 项目中启用 Document AI API。

    转到 API 库

  • 在 Document AI Workbench 中,创建类型为 INVOICE_PROCESSOR 的处理器。如需了解详情,请参阅创建和管理处理器

  • 在 Cloud Storage 中,创建源存储桶以存储要处理的账单,并将账单放入此存储桶。如需了解详情,请参阅创建存储桶

  • 在 Cloud Storage 中,创建一个目标存储桶以存储处理的文件。

创建程序来处理文档

  1. 在 SAP 系统中,使用事务 SE38 在自定义命名空间中创建可执行程序(例如 Z 或 Y)。

    1. 在 SAP GUI 中,输入事务代码 SE38

    2. 程序字段中,输入程序名称,例如 ZDEMO_DOCUMENT_AI

    3. 点击创建

    4. 指定程序属性:

      1. 标题字段中,输入程序标题,例如 Process invoices

      2. 类型字段中,选择 Executable Program

      3. 点击保存

    5. 将程序保存为本地对象

    6. ABAP 编辑器中,添加以下代码:

      **********************************************************************
      *  Copyright 2023 Google LLC                                         *
      *                                                                    *
      *  Licensed under the Apache License, Version 2.0 (the "License");   *
      *  you may not use this file except in compliance with the License.  *
      *  You may obtain a copy of the License at                           *
      *      https://www.apache.org/licenses/LICENSE-2.0                   *
      *  Unless required by applicable law or agreed to in writing,        *
      *  software distributed under the License is distributed on an       *
      *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,      *
      *  either express or implied.                                        *
      *  See the License for the specific language governing permissions   *
      *  and limitations under the License.                                *
      **********************************************************************
      
      REPORT zr_qs_process_documents.
      
      * data declarations
      DATA:
        lv_p_projects_id   TYPE string,
        lv_p_locations_id  TYPE string,
        lv_p_processors_id TYPE string,
        ls_input           TYPE /goog/cl_documentai_v1=>ty_017.
      
      TRY.
      
      * open http connection
          DATA(lo_client) = NEW /goog/cl_documentai_v1( iv_key_name = 'DEMO_DOC_PROCESSING' ).
      
      * populate relevant parameters
          lv_p_projects_id  = 'PROJECT_ID'.
          lv_p_locations_id = 'LOCATION_ID'.
          lv_p_processors_id = 'PROCESSOR_ID'.
          ls_input-input_documents-gcs_prefix-gcs_uri_prefix = 'SOURCE_BUCKET_URI'.
          ls_input-document_output_config-gcs_output_config-gcs_uri = 'TARGET_BUCKET_URI'.
      
      * call api method
          CALL METHOD lo_client->batch_process_processors
            EXPORTING
              iv_p_projects_id   = lv_p_projects_id
              iv_p_locations_id  = lv_p_locations_id
              iv_p_processors_id = lv_p_processors_id
              is_input           = ls_input
            IMPORTING
              es_output          = DATA(ls_output)
              ev_ret_code        = DATA(lv_ret_code)
              ev_err_text        = DATA(lv_err_text)
              es_err_resp        = DATA(ls_err_resp).
      
          IF lo_client->is_success( lv_ret_code ).
            MESSAGE 'Success' TYPE 'S'.
          ELSE.
            MESSAGE lv_err_text TYPE 'E'.
          ENDIF.
      
      * close http connection
          lo_client->close( ).
      
        CATCH /goog/cx_sdk INTO DATA(lo_exception).
          MESSAGE lo_exception->get_text( ) TYPE 'E'.
      ENDTRY.
      

      请替换以下内容:

      • DEMO_DOC_PROCESSING:客户端密钥名称。
      • PROJECT_ID:Google Cloud 项目的 ID。
      • LOCATION_ID:处理器的位置。
      • PROCESSOR_ID:处理器的 ID。
      • SOURCE_BUCKET_URI:保留源文档以进行处理的 Cloud Storage 存储桶文件夹的 URI。
      • TARGET_BUCKET_URI:用于存储已处理文档(JSON 文件)的 Cloud Storage 存储桶的 URI。
  2. SE38 中运行您的应用。

  3. 如需验证结果,请按以下步骤操作:

    1. 在 Google Cloud 控制台中,进入 Cloud Storage 存储桶页面。

    2. 打开目标存储桶。处理后的文档以 JSON 文件的形式存储。

后续步骤