|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +// [START googlegenaisdk_batchpredict_with_gcs] |
| 18 | +const {GoogleGenAI} = require('@google/genai'); |
| 19 | + |
| 20 | +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; |
| 21 | +const GOOGLE_CLOUD_LOCATION = |
| 22 | + process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; |
| 23 | +const OUTPUT_URI = 'gs://your-bucket/your-prefix'; |
| 24 | + |
| 25 | +async function runBatchPredictionJob( |
| 26 | + outputUri = OUTPUT_URI, |
| 27 | + projectId = GOOGLE_CLOUD_PROJECT, |
| 28 | + location = GOOGLE_CLOUD_LOCATION |
| 29 | +) { |
| 30 | + const client = new GoogleGenAI({ |
| 31 | + vertexai: true, |
| 32 | + project: projectId, |
| 33 | + location: location, |
| 34 | + httpOptions: { |
| 35 | + apiVersion: 'v1', |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + // See the documentation: https://googleapis.github.io/js-genai/release_docs/classes/batches.Batches.html |
| 40 | + let job = await client.batches.create({ |
| 41 | + // To use a tuned model, set the model param to your tuned model using the following format: |
| 42 | + // model="projects/{PROJECT_ID}/locations/{LOCATION}/models/{MODEL_ID}" |
| 43 | + model: 'gemini-2.5-flash', |
| 44 | + // Source link: https://storage.cloud.google.com/cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl |
| 45 | + src: 'gs://cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl', |
| 46 | + config: { |
| 47 | + dest: outputUri, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + console.log(`Job name: ${job.name}`); |
| 52 | + console.log(`Job state: ${job.state}`); |
| 53 | + |
| 54 | + // Example response: |
| 55 | + // Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000 |
| 56 | + // Job state: JOB_STATE_PENDING |
| 57 | + |
| 58 | + const completedStates = new Set([ |
| 59 | + 'JOB_STATE_SUCCEEDED', |
| 60 | + 'JOB_STATE_FAILED', |
| 61 | + 'JOB_STATE_CANCELLED', |
| 62 | + 'JOB_STATE_PAUSED', |
| 63 | + ]); |
| 64 | + |
| 65 | + while (!completedStates.has(job.state)) { |
| 66 | + await new Promise(resolve => setTimeout(resolve, 30000)); |
| 67 | + job = await client.batches.get({name: job.name}); |
| 68 | + console.log(`Job state: ${job.state}`); |
| 69 | + } |
| 70 | + |
| 71 | + // Example response: |
| 72 | + // Job state: JOB_STATE_PENDING |
| 73 | + // Job state: JOB_STATE_RUNNING |
| 74 | + // Job state: JOB_STATE_RUNNING |
| 75 | + // ... |
| 76 | + // Job state: JOB_STATE_SUCCEEDED |
| 77 | + |
| 78 | + return job.state; |
| 79 | +} |
| 80 | +// [END googlegenaisdk_batchpredict_with_gcs] |
| 81 | + |
| 82 | +module.exports = { |
| 83 | + runBatchPredictionJob, |
| 84 | +}; |
0 commit comments