יצירת מעבד (v1)

יוצרים מעבד חדש של Document AI.

דוגמת קוד

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

/**
 * Creates a new Document AI processor.
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud region where the service is located (for example, 'us-central1')
 */
async function createProcessor(projectId, locationId) {
  try {
    const parent = client.locationPath(projectId, locationId);
    const processor = {
      displayName: 'Form Processor',
      type: 'FORM_PARSER_PROCESSOR',
    };

    const request = {
      parent,
      processor,
    };

    const [response] = await client.createProcessor(request);
    console.log(`Processor created: ${response.name}`);
    console.log(`Display name: ${response.displayName}`);
    console.log(`Type: ${response.type}`);
  } catch (err) {
    if (err.code === status.ALREADY_EXISTS) {
      console.error('Processor already exists in the specified location.');
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error('Invalid processor configuration provided.');
    } else {
      console.error(`Failed to create processor: ${err.message}`);
    }
  }
}

module.exports = { createProcessor };

Python

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

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

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

client = documentai_v1.DocumentProcessorServiceClient()


def create_processor(project_id: str, location_id: str) -> None:
    """Creates a Document AI processor.

    Args:
        project_id: The Google Cloud project ID (for example, 'example-project-id').
        location_id: The Google Cloud location or region where the processor is created (for example, 'us' or 'eu').
    """
    parent = f"projects/{project_id}/locations/{location_id}"
    processor = documentai_v1.Processor(
        display_name="Example OCR Processor",
        type_="OCR_PROCESSOR",
    )

    try:
        response = client.create_processor(parent=parent, processor=processor)
        print(f"Created processor: {response.name}")
        print(f"Display name: {response.display_name}")
        print(f"Type: {response.type_}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to create processor: {error}")

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

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