|
| 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_tuning_job_create] |
| 18 | +const {GoogleGenAI} = require('@google/genai'); |
| 19 | + |
| 20 | +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; |
| 21 | +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; |
| 22 | + |
| 23 | +async function createTuningJob( |
| 24 | + projectId = GOOGLE_CLOUD_PROJECT, |
| 25 | + location = GOOGLE_CLOUD_LOCATION |
| 26 | +) { |
| 27 | + const client = new GoogleGenAI({ |
| 28 | + vertexai: true, |
| 29 | + project: projectId, |
| 30 | + location: location, |
| 31 | + }); |
| 32 | + |
| 33 | + function sleep(ms) { |
| 34 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 35 | + } |
| 36 | + |
| 37 | + let tuningJob = await client.tunings.tune({ |
| 38 | + baseModel: 'gemini-2.5-flash', |
| 39 | + trainingDataset: { |
| 40 | + gcsUri: |
| 41 | + 'gs://cloud-samples-data/ai-platform/generative_ai/gemini/text/sft_train_data.jsonl', |
| 42 | + }, |
| 43 | + config: { |
| 44 | + tunedModelDisplayName: 'Example tuning job', |
| 45 | + }, |
| 46 | + }); |
| 47 | + console.log('Created tuning job:', tuningJob); |
| 48 | + |
| 49 | + const runningStates = new Set(['JOB_STATE_PENDING', 'JOB_STATE_RUNNING']); |
| 50 | + |
| 51 | + while (runningStates.has(tuningJob.state)) { |
| 52 | + console.log(`Job state: ${tuningJob.state}`); |
| 53 | + tuningJob = await client.tunings.get({name: tuningJob.name}); |
| 54 | + await sleep(60000); |
| 55 | + } |
| 56 | + |
| 57 | + console.log(tuningJob.tunedModel.model); |
| 58 | + console.log(tuningJob.tunedModel.endpoint); |
| 59 | + console.log(tuningJob.experiment); |
| 60 | + |
| 61 | + // Example response: |
| 62 | + // projects/123456789012/locations/us-central1/models/1234567890@1 |
| 63 | + // projects/123456789012/locations/us-central1/endpoints/123456789012345 |
| 64 | + // projects/123456789012/locations/us-central1/metadataStores/default/contexts/tuning-experiment-2025010112345678 |
| 65 | + |
| 66 | + return tuningJob.name; |
| 67 | +} |
| 68 | +// [END googlegenaisdk_tuning_job_create] |
| 69 | + |
| 70 | +module.exports = { |
| 71 | + createTuningJob, |
| 72 | +}; |
0 commit comments