List evaluations (v1)

List the evaluations for a Document AI processor version.

Code sample

Go

For more information, see the Document AI Go API reference documentation.

To authenticate to Document AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


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

For more information, see the Document AI Node.js API reference documentation.

To authenticate to Document AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

'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 };

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.