-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): Sample/tuning #4179
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
13
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/tuning
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.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
88dc11f
adding samples, test, lints
d54cf41
adding samples, test, lints
a99c536
adding samples, test, lints
bc930d1
adding samples, test, lints
b8809cc
adding samples, test, lints
74511a9
Merge branch 'main' into sample/tuning
Guiners a3a3868
Merge branch 'main' into sample/tuning
Guiners 66b43c5
adding samples, test, lints
1a94edd
Merge remote-tracking branch 'origin/sample/tuning' into sample/tuning
1f8ea26
fixing functions names
9c14d50
Merge branch 'main' into sample/tuning
gericdong e815d99
Merge branch 'main' into sample/tuning
msampathkumar 22774f0
adding new samples
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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,56 @@ | ||
// 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 proxyquire = require('proxyquire').noCallThru(); | ||
|
||
const projectId = process.env.CAIP_PROJECT_ID; | ||
|
||
describe('tuning-job-create', () => { | ||
it('should create tuning job and return job name', async function () { | ||
this.timeout(1000000); | ||
const mockTuningJob = { | ||
name: 'test-tuning-job', | ||
experiment: 'test-experiment', | ||
tunedModel: { | ||
model: 'test-model', | ||
endpoint: 'test-endpoint', | ||
}, | ||
}; | ||
|
||
class MockTunings { | ||
async tune() { | ||
return mockTuningJob; | ||
} | ||
async get() {} | ||
} | ||
|
||
class MockGoogleGenAI { | ||
constructor() { | ||
this.tunings = new MockTunings(); | ||
} | ||
} | ||
|
||
const sample = proxyquire('../tuning/tuning-job-create.js', { | ||
'@google/genai': {GoogleGenAI: MockGoogleGenAI}, | ||
}); | ||
|
||
const response = await sample.createTuningJob(projectId); | ||
|
||
assert.strictEqual(response, 'test-tuning-job'); | ||
}); | ||
}); |
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,58 @@ | ||
// 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 proxyquire = require('proxyquire').noCallThru(); | ||
|
||
const projectId = process.env.GOOGLE_CLOUD_PROJECT || 'test-project'; | ||
|
||
describe('tuning-job-get', () => { | ||
it('should get tuning job and return job name', async function () { | ||
this.timeout(1000000); | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const mockTuningJob = { | ||
name: 'test-tuning-job', | ||
experiment: 'test-experiment', | ||
tunedModel: { | ||
model: 'test-model', | ||
endpoint: 'test-endpoint', | ||
}, | ||
}; | ||
|
||
class MockTunings { | ||
async get({name}) { | ||
if (name !== 'TestJobName') | ||
throw new Error('Unexpected tuning job name'); | ||
return mockTuningJob; | ||
} | ||
} | ||
|
||
class MockGoogleGenAI { | ||
constructor() { | ||
this.tunings = new MockTunings(); | ||
} | ||
} | ||
|
||
const sample = proxyquire('../tuning/tuning-job-get.js', { | ||
'@google/genai': {GoogleGenAI: MockGoogleGenAI}, | ||
}); | ||
|
||
const response = await sample.getTuningJob('TestJobName', projectId); | ||
|
||
assert.strictEqual(response, 'test-tuning-job'); | ||
}); | ||
}); |
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,28 @@ | ||
// 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 sample = require('../tuning/tuning-job-list.js'); | ||
|
||
describe('tuning-job-list', () => { | ||
it('should return tuning job list', async () => { | ||
const output = await sample.listTuningJobs(projectId); | ||
assert(output); | ||
}); | ||
}); |
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,67 @@ | ||
// 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 proxyquire = require('proxyquire').noCallThru(); | ||
|
||
const projectId = process.env.CAIP_PROJECT_ID; | ||
|
||
describe('tuning-textgen-with-txt', () => { | ||
it('should fetch tuning job and generate content', async function () { | ||
this.timeout(1000000); | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const mockTuningJob = { | ||
name: 'test-tuning-job', | ||
experiment: 'test-experiment', | ||
tunedModel: { | ||
model: 'test-model', | ||
endpoint: 'test-endpoint', | ||
}, | ||
}; | ||
|
||
const mockGenerateContentResult = { | ||
text: 'Because it is hot and glowing!', | ||
}; | ||
|
||
class MockTunings { | ||
async get() { | ||
return mockTuningJob; | ||
} | ||
} | ||
|
||
class MockModels { | ||
async generateContent() { | ||
return mockGenerateContentResult; | ||
} | ||
} | ||
|
||
class MockGoogleGenAI { | ||
constructor() { | ||
this.tunings = new MockTunings(); | ||
this.models = new MockModels(); | ||
} | ||
} | ||
|
||
const sample = proxyquire('../tuning/tuning-textgen-with-txt.js', { | ||
'@google/genai': {GoogleGenAI: MockGoogleGenAI}, | ||
}); | ||
|
||
const response = await sample.generateContent(projectId); | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert.strictEqual(response, 'Because it is hot and glowing!'); | ||
}); | ||
}); |
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,72 @@ | ||
// 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_tuning_job_create] | ||
const {GoogleGenAI} = require('@google/genai'); | ||
|
||
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
|
||
async function createTuningJob( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const client = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
function sleep(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} | ||
|
||
let tuningJob = await client.tunings.tune({ | ||
baseModel: 'gemini-2.5-flash', | ||
trainingDataset: { | ||
gcsUri: | ||
'gs://cloud-samples-data/ai-platform/generative_ai/gemini/text/sft_train_data.jsonl', | ||
}, | ||
config: { | ||
tunedModelDisplayName: 'Example tuning job', | ||
}, | ||
}); | ||
console.log('Created tuning job:', tuningJob); | ||
|
||
const runningStates = new Set(['JOB_STATE_PENDING', 'JOB_STATE_RUNNING']); | ||
|
||
while (runningStates.has(tuningJob.state)) { | ||
console.log(`Job state: ${tuningJob.state}`); | ||
tuningJob = await client.tunings.get({name: tuningJob.name}); | ||
await sleep(60000); | ||
} | ||
|
||
console.log(tuningJob.tunedModel.model); | ||
console.log(tuningJob.tunedModel.endpoint); | ||
console.log(tuningJob.experiment); | ||
|
||
// Example response: | ||
// projects/123456789012/locations/us-central1/models/1234567890@1 | ||
// projects/123456789012/locations/us-central1/endpoints/123456789012345 | ||
// projects/123456789012/locations/us-central1/metadataStores/default/contexts/tuning-experiment-2025010112345678 | ||
|
||
return tuningJob.name; | ||
} | ||
// [END googlegenaisdk_tuning_job_create] | ||
|
||
module.exports = { | ||
createTuningJob, | ||
}; |
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,52 @@ | ||
// 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_tuning_job_get] | ||
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 TUNING_JOB_NAME = 'TestJobName'; | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
async function getTuningJob( | ||
tuningJobName = TUNING_JOB_NAME, | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const client = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
// Get the tuning job and the tuned model. | ||
const tuningJob = await client.tunings.get({name: tuningJobName}); | ||
|
||
console.log(tuningJob.tunedModel.model); | ||
console.log(tuningJob.tunedModel.endpoint); | ||
console.log(tuningJob.experiment); | ||
|
||
// Example response: | ||
// projects/123456789012/locations/us-central1/models/1234567890@1 | ||
// projects/123456789012/locations/us-central1/endpoints/123456789012345 | ||
// projects/123456789012/locations/us-central1/metadataStores/default/contexts/tuning-experiment-2025010112345678 | ||
|
||
return tuningJob.name; | ||
} | ||
// [END googlegenaisdk_tuning_job_get] | ||
|
||
module.exports = { | ||
getTuningJob, | ||
}; |
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,48 @@ | ||
// 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_tuning_job_list] | ||
const {GoogleGenAI} = require('@google/genai'); | ||
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
|
||
async function listTuningJobs( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const client = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
const responses = await client.tunings.list(); | ||
|
||
for await (const item of responses) { | ||
if (item.name && item.name.includes('/tuningJobs/')) { | ||
console.log(item.name); | ||
// Example response: | ||
// projects/123456789012/locations/us-central1/tuningJobs/123456789012345 | ||
} | ||
} | ||
|
||
return responses; | ||
} | ||
// [END googlegenaisdk_tuning_job_list] | ||
|
||
module.exports = { | ||
listTuningJobs, | ||
}; |
Oops, something went wrong.
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.