Membuat daftar evaluasi (v1)

Mencantumkan evaluasi untuk versi pemroses Document AI.

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"
	documentaipb "cloud.google.com/go/documentai/apiv1beta3/documentaipb"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

// listEvaluations lists evaluations for a Document AI processor version.
func listEvaluations(ctx context.Context, w io.Writer, projectID string, locationID string, processorID string, processorVersionID string) error {
	parent := 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.ListEvaluationsRequest{
		Parent: parent,
	}

	it := client.ListEvaluations(ctx, req)
	var count int
	for {
		evaluation, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("it.Next failed: %w", err)
		}
		fmt.Fprintf(w, "Evaluation: %s\n", evaluation.GetName())
		count++
	}

	if count == 0 {
		fmt.Fprintln(w, "No evaluations found.")
	}

	return nil
}

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();

/**
 * Lists evaluations for a specified processor version.
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id').
 * @param {string} locationId - The Google Cloud location where the processor is hosted (for example, 'us' or 'eu').
 * @param {string} processorId - The server-generated ID of the processor (for example, 'b26e033d4567abcd').
 * @param {string} processorVersionId - The ID of the processor version (for example, 'rc').
 */
async function listEvaluations(projectId, locationId, processorId, processorVersionId) {
  try {
    const parent = `projects/${projectId}/locations/${locationId}/processors/${processorId}/processorVersions/${processorVersionId}`;

    const request = {
      parent,
    };

    const [evaluations] = await client.listEvaluations(request);

    if (evaluations.length === 0) {
      console.log('No evaluations found.');
      return;
    }

    for (const evaluation of evaluations) {
      console.log(`Evaluation name: ${evaluation.name}`);
      console.log(`Create time: ${JSON.stringify(evaluation.createTime)}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource 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(`API error: ${err.message}`);
    }
  }
}

module.exports = { listEvaluations };

Langkah berikutnya

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