-
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
Open
Guiners
wants to merge
18
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/batch-prediction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+455
−3
Open
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
00280b5
adding samples, test, lints
e06efd1
adding samples, test, lints
79fcf8e
adding samples, test, lints
6f885a4
fixing functions names
d07c0db
Merge branch 'main' into sample/batch-prediction
Guiners 8e72ad8
Update genai/batch-prediction/batchpredict-with-bq.js
Guiners 9e26797
Update genai/batch-prediction/batchpredict-with-bq.js
Guiners 563fd36
code review fixes
6491f2d
code review fixes
b63c683
Merge branch 'main' into sample/batch-prediction
Guiners cd1810c
adding new samples
f4a129d
adding new samples
177ff9a
fixing samples
725b465
sample update
e9c45a1
Merge branch 'main' into sample/batch-prediction
Guiners 5c6651b
sample update
f56f29f
Merge remote-tracking branch 'origin/sample/batch-prediction' into sa…
074b211
sample update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
genai/batch-prediction/batchpredict-embeddings-with-gcs.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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)) { | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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; | ||
} | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// 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, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_gcs] | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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)) { | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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] | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
module.exports = { | ||
runBatchPredictionJob, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.