Membangun pipeline pemrosesan dokumen dengan Workflows

Jika Anda menggunakan Document AI untuk membangun alur kerja yang melibatkan beberapa langkah untuk memproses dokumen, Anda mungkin ingin menggunakan orkestrasi tanpa server untuk mengelola alur kerja.

Workflows menautkan serangkaian tugas dalam urutan yang Anda tentukan. Layanan ini tidak memerlukan pengelolaan infrastruktur dan dapat diskalakan dengan lancar sesuai permintaan, termasuk penskalaan hingga nol. Dengan model harga bayar sesuai penggunaan, Anda hanya membayar waktu eksekusi.

Workflows menyediakan konektor bawaan untuk Document AI API. Konektor tidak memerlukan kode tambahan selain menentukan langkah-langkah menggunakan file konfigurasi. Konektor menangani pemformatan permintaan, menyembunyikan detail API. Konektor juga menyediakan perilaku bawaan untuk autentikasi, menangani percobaan ulang, dan operasi yang berjalan lama.

Untuk mengetahui informasi selengkapnya, lihat Memahami konektor.

Panduan ini memproses sekumpulan contoh invoice menggunakan pemroses Invoice Parser dengan pemrosesan batch


Untuk mengikuti panduan langkah demi langkah untuk tugas ini langsung di Google Cloud konsol, klik Pandu saya:

Pandu saya


Sebelum memulai

  1. Login keakun Anda. Google Cloud Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  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

Peran yang diperlukan

Untuk mendapatkan izin yang diperlukan untuk membuat pengklasifikasi kustom, minta administrator Anda untuk memberi Anda peran IAM berikut di project Anda:

Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.

Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus atau peran bawaan lainnya.

Membuat Invoice Parser

Gunakankonsol untuk membuat Invoice Parser baru. Google Cloud Lihat halaman membuat dan mengelola pemroses untuk mengetahui informasi selengkapnya.

  1. Di menu navigasi Google Cloud konsol, klik Document AI , lalu pilih Processor Gallery.

    Processor Gallery

  2. Di Processor Gallery, cari Invoice Parser, lalu pilih Create.

    Buat pemroses
invoice

  3. Masukkan Processor name, seperti workflows-invoice-processor.

  4. Pilih region (location di Document AI API) untuk menghosting pemroses.

  5. Klik Create. Tab Processor Details akan muncul.

  6. Salin Processor ID Anda. Anda harus menggunakan ini dalam kode nanti.

    ID Pemroses

Mengonfigurasi bucket output di Cloud Storage

Saat menggunakan pemrosesan batch (asinkron), file input dan output harus disimpan di Cloud Storage. Bucket berikut berisi contoh file yang akan digunakan sebagai input untuk panduan ini:

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

Konfigurasi bucket untuk menyimpan file output di project Anda.

  1. Ikuti panduan Membuat Bucket di dokumentasi Cloud Storage untuk membuat bucket, atau gunakan bucket yang sudah ada.

  2. Buat folder di bucket (misalnya, docai-workflows-output).

Membuat alur kerja

  1. Buka dasbor Workflows di Google Cloud konsol.

    Buka dasbor Workflows

  2. Klik Create untuk membuat alur kerja pertama Anda. Halaman Create Workflow akan muncul.

    Membuat Alur Kerja

  3. Ganti nama alur kerja menjadi docai-workflows-quickstart.

  4. Tambahkan deskripsi, dan pilih akun layanan jika Anda memilikinya untuk project ini.

    1. Jika Anda tidak memiliki akun layanan, pilih Create New Service Account.
      • Beri nama akun layanan Anda, lalu pilih Create and Continue. CreateServiceAccount
      • Berikan peran berikut:
      • Document AI API User
      • Workflows Invoker
      • Logs Writer
      • Service Usage Consumer
      • ServiceAccountRoles
  5. Di halaman Create Workflow, klik Next.

Menentukan alur kerja

  1. Di editor Workflows, hapus YAML default dan tempel kode di bawah:

    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. Pilih Deploy.

Menguji alur kerja

Setelah alur kerja di-deploy, Anda akan berada di halaman Workflow details.

  1. Klik Execute di bagian atas halaman untuk membuka halaman Execute workflow. Halaman ini memungkinkan Anda menguji alur kerja di konsol.

  2. Ganti variabel placeholder dalam JSON input berikut:

    • OUTPUT_BUCKET_PATH: Jalur lengkap ke bucket dan folder output yang dibuat sebelumnya.
    • PROCESSOR_ID: ID pemroses untuk pemroses yang dibuat sebelumnya.
    • LOCATION: Region untuk pemroses yang dibuat sebelumnya.
    {
      "inputBucket": "gs://cloud-samples-data/documentai/workflows",
      "outputBucket": "OUTPUT_BUCKET_PATH",
      "processorId": "PROCESSOR_ID",
      "location": "LOCATION"
    }
    
  3. Salin/Tempel JSON ke kotak teks input.

  4. Klik Execute di bagian bawah alur kerja untuk menguji input Anda dan tunggu beberapa saat hingga proses selesai.

  5. Setelah alur kerja selesai, JSON output akan terlihat seperti ini.

    {
      "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. Buka bucket penyimpanan output Anda, Anda akan melihat output untuk setiap invoice yang diproses dalam folder yang diberi nama sesuai Operation ID. BatchGCSOutput

  7. Dalam folder ini akan ada subfolder, satu untuk setiap dokumen yang diproses dari batch input yang berisi Document file JSON.

  8. Anda dapat memeriksa setiap file JSON atau menulis kode untuk menggunakan kolom yang relevan untuk kasus penggunaan Anda. Lihat panduan menangani respons pemrosesan untuk mengetahui informasi selengkapnya.

  9. Anda kini dapat menautkan langkah-langkah tambahan dalam alur kerja untuk memproses lebih lanjut output, seperti menyisipkan entity yang diekstrak ke dalam database untuk analisis.

  10. Lihat Dokumentasi Workflows untuk mengetahui informasi selengkapnya tentang cara memicu alur kerja.

Pembersihan

Agar akunAnda tidak dikenai biaya untuk resource yang digunakan pada halaman ini, ikuti langkah-langkah berikut. Google Cloud

Untuk menghindari tagihan Google Cloud yang tidak perlu, gunakan Google Cloud console untuk menghapus pemroses, bucket, alur kerja, dan project Anda jika tidak lagi diperlukan.

Langkah berikutnya