הצגת רשימת הערכות (גרסה 1)

הצגת רשימת ההערכות של גרסת מעבד Document AI.

דוגמת קוד

Go

מידע נוסף מופיע במאמרי העזרה של Document AI Go API.

כדי לבצע אימות ב-Document AI, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.


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

מידע נוסף מופיע במאמרי העזרה של Document AI Node.js API.

כדי לבצע אימות ב-Document AI, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

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

המאמרים הבאים

כדי לחפש ולסנן דוגמאות קוד למוצרים אחרים של Google Cloud , ראו דפדפן לדוגמה שלGoogle Cloud .