-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): Sample/batch prediction #4181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
00280b5
e06efd1
79fcf8e
6f885a4
d07c0db
8e72ad8
9e26797
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
// [START googlegenaisdk_batchpredict_embeddings_with_gcs] | ||
const {GoogleGenAI} = require('@google/genai'); | ||
|
||
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
const OUTPUT_URI = 'gs://your-bucket/your-prefix'; | ||
|
||
async function runBatchPredictionJob( | ||
outputUri = OUTPUT_URI, | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const client = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
httpOptions: { | ||
apiVersion: 'v1', | ||
}, | ||
}); | ||
|
||
// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create | ||
let job = await client.batches.create({ | ||
model: 'text-embedding-005', | ||
// Source link: https://storage.cloud.google.com/cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl | ||
src: 'gs://cloud-samples-data/generative-ai/embeddings/embeddings_input.jsonl', | ||
config: { | ||
dest: outputUri, | ||
}, | ||
}); | ||
|
||
console.log(`Job name: ${job.name}`); | ||
console.log(`Job state: ${job.state}`); | ||
|
||
// Example response: | ||
// Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000 | ||
// Job state: JOB_STATE_PENDING | ||
|
||
const completedStates = new Set([ | ||
'JOB_STATE_SUCCEEDED', | ||
'JOB_STATE_FAILED', | ||
'JOB_STATE_CANCELLED', | ||
'JOB_STATE_PAUSED', | ||
]); | ||
|
||
while (completedStates.has(job.state)) { | ||
await new Promise(resolve => setTimeout(resolve, 30000)); | ||
job = await client.batches.get({name: job.name}); | ||
console.log(`Job state: ${job.state}`); | ||
if (job.state === 'JOB_STATE_FAILED') { | ||
console.log(`Job state: ${job.state}`); | ||
break; | ||
} | ||
Comment on lines
+66
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
// Example response: | ||
// Job state: JOB_STATE_PENDING | ||
// Job state: JOB_STATE_RUNNING | ||
// Job state: JOB_STATE_RUNNING | ||
// ... | ||
// Job state: JOB_STATE_SUCCEEDED | ||
|
||
return job.state; | ||
} | ||
// [END googlegenaisdk_batchpredict_embeddings_with_gcs] | ||
|
||
module.exports = { | ||
runBatchPredictionJob, | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,82 @@ | ||||||
// Copyright 2025 Google LLC | ||||||
// | ||||||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
// you may not use this file except in compliance with the License. | ||||||
// You may obtain a copy of the License at | ||||||
// | ||||||
// https://www.apache.org/licenses/LICENSE-2.0 | ||||||
// | ||||||
// Unless required by applicable law or agreed to in writing, software | ||||||
// distributed under the License is distributed on an "AS IS" BASIS, | ||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
// See the License for the specific language governing permissions and | ||||||
// limitations under the License. | ||||||
|
||||||
'use strict'; | ||||||
|
||||||
// [START googlegenaisdk_batchpredict_with_bq] | ||||||
const {GoogleGenAI} = require('@google/genai'); | ||||||
|
||||||
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||||||
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||||||
const OUTPUT_URI = 'bq://your-project.your_dataset.your_table'; | ||||||
|
||||||
async function runBatchPredictionJob( | ||||||
outputUri = OUTPUT_URI, | ||||||
projectId = GOOGLE_CLOUD_PROJECT, | ||||||
location = GOOGLE_CLOUD_LOCATION | ||||||
) { | ||||||
const client = new GoogleGenAI({ | ||||||
vertexai: true, | ||||||
project: projectId, | ||||||
location: location, | ||||||
httpOptions: { | ||||||
apiVersion: 'v1', | ||||||
}, | ||||||
}); | ||||||
|
||||||
// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create | ||||||
let job = await client.batches.create({ | ||||||
// To use a tuned model, set the model param to your tuned model using the following format: | ||||||
// model="projects/{PROJECT_ID}/locations/{LOCATION}/models/{MODEL_ID}" | ||||||
model: 'gemini-2.5-flash', | ||||||
src: 'bq://storage-samples.generative_ai.batch_requests_for_multimodal_input', | ||||||
config: { | ||||||
dest: outputUri, | ||||||
}, | ||||||
}); | ||||||
|
||||||
console.log(`Job name: ${job.name}`); | ||||||
console.log(`Job state: ${job.state}`); | ||||||
|
||||||
// Example response: | ||||||
// Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000 | ||||||
// Job state: JOB_STATE_PENDING | ||||||
|
||||||
const completedStates = new Set([ | ||||||
'JOB_STATE_SUCCEEDED', | ||||||
'JOB_STATE_FAILED', | ||||||
'JOB_STATE_CANCELLED', | ||||||
'JOB_STATE_PAUSED', | ||||||
]); | ||||||
|
||||||
while (completedStates.has(job.state)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The polling loop condition is inverted. It currently checks
Suggested change
|
||||||
await new Promise(resolve => setTimeout(resolve, 30000)); | ||||||
job = await client.batches.get({name: job.name}); | ||||||
console.log(`Job state: ${job.state}`); | ||||||
} | ||||||
|
||||||
// Example response: | ||||||
// Job state: JOB_STATE_PENDING | ||||||
// Job state: JOB_STATE_RUNNING | ||||||
// Job state: JOB_STATE_RUNNING | ||||||
// ... | ||||||
// Job state: JOB_STATE_SUCCEEDED | ||||||
|
||||||
return job.state; | ||||||
} | ||||||
// [END googlegenaisdk_batchpredict_with_bq] | ||||||
|
||||||
module.exports = { | ||||||
runBatchPredictionJob, | ||||||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,83 @@ | ||||||
// Copyright 2025 Google LLC | ||||||
// | ||||||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
// you may not use this file except in compliance with the License. | ||||||
// You may obtain a copy of the License at | ||||||
// | ||||||
// https://www.apache.org/licenses/LICENSE-2.0 | ||||||
// | ||||||
// Unless required by applicable law or agreed to in writing, software | ||||||
// distributed under the License is distributed on an "AS IS" BASIS, | ||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
// See the License for the specific language governing permissions and | ||||||
// limitations under the License. | ||||||
|
||||||
'use strict'; | ||||||
|
||||||
// [START googlegenaisdk_batchpredict_with_gcs] | ||||||
const {GoogleGenAI} = require('@google/genai'); | ||||||
|
||||||
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||||||
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||||||
const OUTPUT_URI = 'gs://your-bucket/your-prefix'; | ||||||
|
||||||
async function runBatchPredictionJob( | ||||||
outputUri = OUTPUT_URI, | ||||||
projectId = GOOGLE_CLOUD_PROJECT, | ||||||
location = GOOGLE_CLOUD_LOCATION | ||||||
) { | ||||||
const client = new GoogleGenAI({ | ||||||
vertexai: true, | ||||||
project: projectId, | ||||||
location: location, | ||||||
httpOptions: { | ||||||
apiVersion: 'v1', | ||||||
}, | ||||||
}); | ||||||
|
||||||
// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create | ||||||
let job = await client.batches.create({ | ||||||
// To use a tuned model, set the model param to your tuned model using the following format: | ||||||
// model="projects/{PROJECT_ID}/locations/{LOCATION}/models/{MODEL_ID}" | ||||||
model: 'gemini-2.5-flash', | ||||||
// Source link: https://storage.cloud.google.com/cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl | ||||||
src: 'gs://cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl', | ||||||
config: { | ||||||
dest: outputUri, | ||||||
}, | ||||||
}); | ||||||
|
||||||
console.log(`Job name: ${job.name}`); | ||||||
console.log(`Job state: ${job.state}`); | ||||||
|
||||||
// Example response: | ||||||
// Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000 | ||||||
// Job state: JOB_STATE_PENDING | ||||||
|
||||||
const completedStates = new Set([ | ||||||
'JOB_STATE_SUCCEEDED', | ||||||
'JOB_STATE_FAILED', | ||||||
'JOB_STATE_CANCELLED', | ||||||
'JOB_STATE_PAUSED', | ||||||
]); | ||||||
|
||||||
while (completedStates.has(job.state)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The polling loop condition is inverted. It currently checks
Suggested change
|
||||||
await new Promise(resolve => setTimeout(resolve, 30000)); | ||||||
job = await client.batches.get({name: job.name}); | ||||||
console.log(`Job state: ${job.state}`); | ||||||
} | ||||||
|
||||||
// Example response: | ||||||
// Job state: JOB_STATE_PENDING | ||||||
// Job state: JOB_STATE_RUNNING | ||||||
// Job state: JOB_STATE_RUNNING | ||||||
// ... | ||||||
// Job state: JOB_STATE_SUCCEEDED | ||||||
|
||||||
return job.state; | ||||||
} | ||||||
// [END googlegenaisdk_batchpredict_with_gcs] | ||||||
|
||||||
module.exports = { | ||||||
runBatchPredictionJob, | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
const {assert} = require('chai'); | ||
const {describe, it} = require('mocha'); | ||
const {Storage} = require('@google-cloud/storage'); | ||
|
||
const storage = new Storage(); | ||
|
||
const GCS_OUTPUT_BUCKET = 'nodejs-docs-samples-tests'; | ||
|
||
const projectId = process.env.CAIP_PROJECT_ID; | ||
const location = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
const sample = require('../batch-prediction/batchpredict-embeddings-with-gcs'); | ||
|
||
async function gcs_output_uri() { | ||
const dt = new Date(); | ||
const prefix = `text_output/${dt.toISOString()}`; | ||
const fullUri = `gs://${GCS_OUTPUT_BUCKET}/${prefix}`; | ||
|
||
return { | ||
uri: fullUri, | ||
async cleanup() { | ||
const [files] = await storage.bucket(GCS_OUTPUT_BUCKET).getFiles({ | ||
prefix, | ||
}); | ||
for (const file of files) { | ||
await file.delete(); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
describe('batchpredict-embeddings-with-gcs', () => { | ||
it('should return the batch job state', async function () { | ||
this.timeout(50000); | ||
const gscOutput = gcs_output_uri(); | ||
const gscUri = (await gscOutput).uri; | ||
const output = await sample.runBatchPredictionJob( | ||
gscUri, | ||
projectId, | ||
location | ||
); | ||
assert.notEqual(output, undefined); | ||
}); | ||
Comment on lines
+48
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test has two issues:
Also, there's a minor typo it('should return the batch job state', async function () {
this.timeout(50000);
const gcsOutput = await gcs_output_uri();
try {
const output = await sample.runBatchPredictionJob(
gcsOutput.uri,
projectId,
location
);
assert.strictEqual(output, 'JOB_STATE_SUCCEEDED');
} finally {
await gcsOutput.cleanup();
}
}); |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
const {assert} = require('chai'); | ||
const {describe, it} = require('mocha'); | ||
const {BigQuery} = require('@google-cloud/bigquery'); | ||
|
||
const bigquery = new BigQuery(); | ||
|
||
const BQ_OUTPUT_DATASET = `${process.env.BQ_OUTPUT_DATASET}.gen_ai_batch_prediction`; | ||
|
||
const projectId = process.env.CAIP_PROJECT_ID; | ||
const location = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
const sample = require('../batch-prediction/batchpredict-with-bq'); | ||
|
||
async function bq_output_uri() { | ||
const dt = new Date(); | ||
const tableName = `text_output_${dt.getFullYear()}_${dt.getMonth() + 1}_${dt.getDate()}_T${dt.getHours()}_${dt.getMinutes()}_${dt.getSeconds()}`; | ||
const tableUri = `${BQ_OUTPUT_DATASET}.${tableName}`; | ||
const fullUri = `bq://${tableUri}`; | ||
|
||
return { | ||
uri: fullUri, | ||
async cleanup() { | ||
await bigquery.dataset(BQ_OUTPUT_DATASET).table(tableName).delete({ | ||
ignoreNotFound: true, | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
describe('batchpredict-with-bq', () => { | ||
it('should return the batch job state', async function () { | ||
this.timeout(50000); | ||
const bqOutput = bq_output_uri(); | ||
const bqUri = (await bqOutput).uri; | ||
const output = await sample.runBatchPredictionJob( | ||
bqUri, | ||
projectId, | ||
location | ||
); | ||
assert.notEqual(output, undefined); | ||
}); | ||
Comment on lines
+46
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test has two issues:
it('should return the batch job state', async function () {
this.timeout(50000);
const bqOutput = await bq_output_uri();
try {
const output = await sample.runBatchPredictionJob(
bqOutput.uri,
projectId,
location
);
assert.strictEqual(output, 'JOB_STATE_SUCCEEDED');
} finally {
await bqOutput.cleanup();
}
}); |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The polling loop condition is inverted. It currently checks
while (completedStates.has(job.state))
, which means the loop will only run if the job is already in a completed state. Since the initial state isJOB_STATE_PENDING
, the loop will not execute at all, and the function will return prematurely. The condition should be negated to poll until the job reaches a completed state.