Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
00280b5
adding samples, test, lints
Aug 22, 2025
e06efd1
adding samples, test, lints
Aug 22, 2025
79fcf8e
adding samples, test, lints
Sep 4, 2025
6f885a4
fixing functions names
Sep 10, 2025
d07c0db
Merge branch 'main' into sample/batch-prediction
Guiners Sep 12, 2025
8e72ad8
Update genai/batch-prediction/batchpredict-with-bq.js
Guiners Sep 12, 2025
9e26797
Update genai/batch-prediction/batchpredict-with-bq.js
Guiners Sep 12, 2025
563fd36
code review fixes
Sep 22, 2025
6491f2d
code review fixes
Sep 22, 2025
b63c683
Merge branch 'main' into sample/batch-prediction
Guiners Sep 22, 2025
cd1810c
adding new samples
Sep 22, 2025
f4a129d
adding new samples
Sep 22, 2025
177ff9a
fixing samples
Sep 23, 2025
725b465
sample update
Sep 30, 2025
e9c45a1
Merge branch 'main' into sample/batch-prediction
Guiners Oct 2, 2025
5c6651b
sample update
Oct 2, 2025
f56f29f
Merge remote-tracking branch 'origin/sample/batch-prediction' into sa…
Oct 2, 2025
074b211
sample update
Oct 3, 2025
e897843
Merge branch 'main' into sample/batch-prediction
Guiners Oct 13, 2025
7cf04b5
Merge branch 'main' into sample/batch-prediction
Guiners Oct 27, 2025
fa16ac7
fixing varname
Oct 27, 2025
4d88ee1
Merge branch 'main' into sample/batch-prediction
Guiners Oct 28, 2025
a613cea
Merge remote-tracking branch 'origin/sample/batch-prediction' into sa…
Nov 24, 2025
e61e044
adding mocking for batch prediction
Nov 24, 2025
390c0aa
adding mocking for batch prediction
Nov 25, 2025
d881b21
Merge branch 'main' into sample/batch-prediction
Guiners Nov 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions genai/batch-prediction/batchpredict-embeddings-with-gcs.js
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_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 || 'us-central1';
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/js-genai/release_docs/classes/batches.Batches.html
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}`);
}

// 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,
};
83 changes: 83 additions & 0 deletions genai/batch-prediction/batchpredict-with-bq.js
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_bq]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION =
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
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/js-genai/release_docs/classes/batches.Batches.html
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)) {
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,
};
84 changes: 84 additions & 0 deletions genai/batch-prediction/batchpredict-with-gcs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 || 'us-central1';
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/js-genai/release_docs/classes/batches.Batches.html
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)) {
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,
};
3 changes: 3 additions & 0 deletions genai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js"
},
"dependencies": {
"@google-cloud/bigquery": "^8.1.1",
"@google-cloud/storage": "^7.17.1",

"@google/genai": "1.20.0",
"axios": "^1.6.2",
"google-auth-library": "^10.3.0",
Expand Down
59 changes: 59 additions & 0 deletions genai/test/batchpredict-embeddings-with-gcs.test.js
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 projectId = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';
const {delay} = require('./util');
const proxyquire = require('proxyquire');
const {GoogleGenAI_Mock} = require('./batchprediction-utils');

const sample = proxyquire(
'../batch-prediction/batchpredict-embeddings-with-gcs',
{
'@google/genai': {
GoogleGenAI: GoogleGenAI_Mock,
},
}
);

async function getGcsOutputUri() {
return {
uri: 'gs://mock/output',
async cleanup() {},
};
}

describe('batchpredict-with-gcs', () => {
it('should return the batch job state', async function () {
this.timeout(500000);
this.retries(4);
await delay(this.test);
const gcsOutput = await getGcsOutputUri();
try {
const output = await sample.runBatchPredictionJob(
gcsOutput.uri,
projectId,
location
);
assert.notEqual(output, undefined);
} finally {
await gcsOutput.cleanup();
}
});
});
57 changes: 57 additions & 0 deletions genai/test/batchpredict-with-bq.test.js
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 projectId = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';
const {delay} = require('./util');

const proxyquire = require('proxyquire');
const {GoogleGenAI_Mock} = require('./batchprediction-utils');

const sample = proxyquire('../batch-prediction/batchpredict-with-bq', {
'@google/genai': {
GoogleGenAI: GoogleGenAI_Mock,
},
});

async function getBqOutputUri() {
return {
uri: 'gs://mock/output',
async cleanup() {},
};
}

describe('batchpredict-with-bq', () => {
it('should return the batch job state', async function () {
this.timeout(500000);
this.retries(4);
await delay(this.test);
const bqOutput = await getBqOutputUri();
try {
const output = await sample.runBatchPredictionJob(
bqOutput.uri,
projectId,
location
);
assert.notEqual(output, undefined);
} finally {
await bqOutput.cleanup();
}
});
});
Loading