Mendapatkan versi pemroses (v1)

Mengambil detail versi pemroses tertentu.

Contoh kode

Go

Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Go Document AI.

Untuk melakukan autentikasi ke Document AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.


import (
	"context"
	"fmt"
	"io"

	documentai "cloud.google.com/go/documentai/apiv1beta3"
	"cloud.google.com/go/documentai/apiv1beta3/documentaipb"
	"google.golang.org/api/option"
)

// getProcessorVersion retrieves details of a specific processor version.
func getProcessorVersion(ctx context.Context, w io.Writer, projectID string, locationID string, processorID string, processorVersionID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/processors/%s/processorVersions/%s", projectID, locationID, processorID, processorVersionID)

	endpoint := fmt.Sprintf("%s-documentai.googleapis.com:443", locationID)
	client, err := documentai.NewDocumentProcessorClient(ctx, option.WithEndpoint(endpoint))
	if err != nil {
		return fmt.Errorf("NewDocumentProcessorClient failed: %w", err)
	}
	defer client.Close()

	req := &documentaipb.GetProcessorVersionRequest{
		Name: name,
	}

	resp, err := client.GetProcessorVersion(ctx, req)
	if err != nil {
		return fmt.Errorf("GetProcessorVersion failed: %w", err)
	}

	fmt.Fprintf(w, "Processor version name: %s\n", resp.GetName())
	fmt.Fprintf(w, "Display name: %s\n", resp.GetDisplayName())
	fmt.Fprintf(w, "State: %s\n", resp.GetState().String())

	return nil
}

Java

Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Java Document AI.

Untuk melakukan autentikasi ke Document AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

import com.google.cloud.documentai.v1.DocumentProcessorServiceClient;
import com.google.cloud.documentai.v1.GetProcessorVersionRequest;
import com.google.cloud.documentai.v1.ProcessorVersion;
import com.google.cloud.documentai.v1.ProcessorVersionName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class GetProcessorVersion {

  public static void main(String[] args) throws Exception {
    String projectId = "example-project_id";
    String locationId = "example-location_id";
    String processorId = "example-processor_id";
    String processorVersionId = "example-processor_version_id";
    getProcessorVersion(projectId, locationId, processorId, processorVersionId);
  }

  public static void getProcessorVersion(
      String projectId, String locationId, String processorId, String processorVersionId)
      throws Exception {
    try (DocumentProcessorServiceClient client = DocumentProcessorServiceClient.create()) {
      String name =
          ProcessorVersionName.of(projectId, locationId, processorId, processorVersionId).toString();
      GetProcessorVersionRequest request =
          GetProcessorVersionRequest.newBuilder().setName(name).build();

      ProcessorVersion response = client.getProcessorVersion(request);
      System.out.println("Processor version: " + response.getName());
      System.out.println("Display name: " + response.getDisplayName());
      System.out.println("State: " + response.getState());
    }
  }
}

Node.js

Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Node.js Document AI.

Untuk melakukan autentikasi ke Document AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

'use strict';

const {DocumentProcessorServiceClient} = require('@google-cloud/documentai');
const {status} = require('@grpc/grpc-js');

const client = new DocumentProcessorServiceClient();

/**
 * Retrieves details of a specific Document AI processor version.
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The location of the processor (for example, 'us' or 'eu').
 * @param {string} processorId - The server-generated ID of the processor.
 * @param {string} processorVersionId - The ID or alias of the processor version (for example, 'pretrained-foundation-model-v1.0' or 'rc').
 */
async function getProcessorVersion(projectId, locationId, processorId, processorVersionId) {
  try {
    // Construct the processor version resource name.
    const name = `projects/${projectId}/locations/${locationId}/processors/${processorId}/processorVersions/${processorVersionId}`;

    const request = {
      name,
    };

    const [processorVersion] = await client.getProcessorVersion(request);

    console.log(`Processor version name: ${processorVersion.name}`);
    console.log(`Display name: ${processorVersion.displayName}`);
    console.log(`State: ${processorVersion.state}`);

    return processorVersion;
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Processor version not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument: ${err.message}`);
    } else {
      console.error(`RPC failed with error: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { getProcessorVersion };

Python

Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Python Document AI.

Untuk melakukan autentikasi ke Document AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

import google.api_core.exceptions
from google.cloud import documentai_v1

client = documentai_v1.DocumentProcessorServiceClient()


def get_processor_version(
    project_id: str,
    location_id: str,
    processor_id: str,
    processor_version_id: str,
) -> None:
    """Retrieves a processor version from Document AI.

    Args:
        project_id: The Google Cloud project ID.
        location_id: The Google Cloud location where the processor is hosted (for example, 'us' or 'eu').
        processor_id: The server-generated ID of the processor.
        processor_version_id: The ID of the processor version to retrieve.
    """
    name = (
        f"projects/{project_id}/locations/{location_id}/processors/"
        f"{processor_id}/processorVersions/{processor_version_id}"
    )

    request = documentai_v1.GetProcessorVersionRequest(name=name)

    try:
        response = client.get_processor_version(request=request)
        print(f"Processor version name: {response.name}")
        print(f"Display name: {response.display_name}")
        print(f"State: {response.state.name}")
    except google.api_core.exceptions.NotFound as exc:
        print(f"Processor version not found: {exc}")
    except google.api_core.exceptions.GoogleAPICallError as exc:
        print(f"API call failed: {exc}")

Langkah berikutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contohGoogle Cloud .