-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): controlled generation #4148
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
9
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/controlled-generation
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 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
98a2d45
bumping version and fixing tests
99f5214
bumping version and fixing tests
77e4dca
bumping version and fixing tests
554afd0
bumping version and fixing tests
aa02890
adding tests
6cbd2c9
adding tests
5dd5c77
fixing filenames
f7f1b54
fixing code from code review and adding linters
446f1e7
Merge branch 'main' into sample/controlled-generation
Guiners 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
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,60 @@ | ||
// 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_ctrlgen_with_class_schema] | ||
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 generateContent( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const ai = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
class Recipe { | ||
/** | ||
* @param {string} recipe_name | ||
* @param {string[]} ingredients | ||
*/ | ||
constructor(recipe_name, ingredients) { | ||
this.recipe_name = recipe_name; | ||
this.ingredients = ingredients; | ||
} | ||
} | ||
|
||
const response = await ai.models.generateContent({ | ||
model: 'gemini-2.5-flash', | ||
contents: 'List a few popular cookie recipes?', | ||
config: { | ||
responseMimeType: 'application/json', | ||
responseSchema: Recipe, | ||
}, | ||
}); | ||
|
||
console.log(response.text); | ||
return response.text; | ||
} | ||
// [END googlegenaisdk_ctrlgen_with_class_schema] | ||
|
||
module.exports = { | ||
generateContent, | ||
}; |
73 changes: 73 additions & 0 deletions
73
genai/controlled-generation/ctrlgen-with-enum-class-schema.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,73 @@ | ||
// 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_ctrlgen_with_enum_class_schema] | ||
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 generateContent( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const ai = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
class InstrumentClass { | ||
static values() { | ||
return [ | ||
InstrumentClass.PERCUSSION, | ||
InstrumentClass.STRING, | ||
InstrumentClass.WOODWIND, | ||
InstrumentClass.BRASS, | ||
InstrumentClass.KEYBOARD, | ||
]; | ||
} | ||
} | ||
|
||
InstrumentClass.PERCUSSION = 'Percussion'; | ||
InstrumentClass.STRING = 'String'; | ||
InstrumentClass.WOODWIND = 'Woodwind'; | ||
InstrumentClass.BRASS = 'Brass'; | ||
InstrumentClass.KEYBOARD = 'Keyboard'; | ||
|
||
const responseSchema = { | ||
type: 'string', | ||
enum: InstrumentClass.values(), | ||
}; | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const response = await ai.models.generateContent({ | ||
model: 'gemini-2.5-flash', | ||
contents: 'What type of instrument is a guitar?', | ||
config: { | ||
responseMimeType: 'text/x.enum', | ||
responseSchema: responseSchema, | ||
}, | ||
}); | ||
|
||
console.log(response.text); | ||
|
||
return response.text; | ||
} | ||
// [END googlegenaisdk_ctrlgen_with_enum_class_schema] | ||
|
||
module.exports = { | ||
generateContent, | ||
}; |
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
74 changes: 74 additions & 0 deletions
74
genai/controlled-generation/ctrlgen-with-nested-class-schema.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,74 @@ | ||
// 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_ctrlgen_with_nested_class_schema] | ||
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 generateContent( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const ai = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
const Grade = Object.freeze({ | ||
A_PLUS: 'a+', | ||
A: 'a', | ||
B: 'b', | ||
C: 'c', | ||
D: 'd', | ||
F: 'f', | ||
}); | ||
|
||
class Recipe { | ||
/** | ||
* @param {string} recipe_name | ||
* @param {string} rating - Must be one of Grade enum values | ||
*/ | ||
constructor(recipe_name, rating) { | ||
if (!Object.values(Grade).includes(rating)) { | ||
throw new Error(`Invalid rating: ${rating}`); | ||
} | ||
this.recipe_name = recipe_name; | ||
this.rating = rating; | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
const response = await ai.models.generateContent({ | ||
model: 'gemini-2.5-flash', | ||
contents: | ||
'List about 10 home-baked cookies and give them grades based on tastiness.', | ||
config: { | ||
responseMimeType: 'application/json', | ||
responseSchema: Recipe, | ||
}, | ||
}); | ||
|
||
console.log(response.text); | ||
|
||
return response.text; | ||
} | ||
// [END googlegenaisdk_ctrlgen_with_nested_class_schema] | ||
|
||
module.exports = { | ||
generateContent, | ||
}; |
80 changes: 80 additions & 0 deletions
80
genai/controlled-generation/ctrlgen-with-nullable-schema.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,80 @@ | ||
// 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_ctrlgen_with_nullable_schema] | ||
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 generateContent( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const ai = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
const prompt = ` | ||
The week ahead brings a mix of weather conditions. | ||
Sunday is expected to be sunny with a temperature of 77°F and a humidity level of 50%. Winds will be light at around 10 km/h. | ||
Monday will see partly cloudy skies with a slightly cooler temperature of 72°F and the winds will pick up slightly to around 15 km/h. | ||
Tuesday brings rain showers, with temperatures dropping to 64°F and humidity rising to 70%. | ||
Wednesday may see thunderstorms, with a temperature of 68°F. | ||
Thursday will be cloudy with a temperature of 66°F and moderate humidity at 60%. | ||
Friday returns to partly cloudy conditions, with a temperature of 73°F and the Winds will be light at 12 km/h. | ||
Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F, and a humidity level of 40%. Winds will be gentle at 8 km/h. | ||
`; | ||
|
||
const responseSchema = { | ||
type: 'object', | ||
properties: { | ||
forecast: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
Day: {type: 'string', nullable: true}, | ||
Forecast: {type: 'string', nullable: true}, | ||
Temperature: {type: 'integer', nullable: true}, | ||
Humidity: {type: 'string', nullable: true}, | ||
'Wind Speed': {type: 'integer', nullable: true}, | ||
}, | ||
required: ['Day', 'Temperature', 'Forecast', 'Wind Speed'], | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const response = await ai.models.generateContent({ | ||
model: 'gemini-2.5-flash', | ||
contents: prompt, | ||
config: { | ||
responseMimeType: 'application/json', | ||
responseSchema: responseSchema, | ||
}, | ||
}); | ||
console.log(response.text); | ||
|
||
return response.text; | ||
} | ||
// [END googlegenaisdk_ctrlgen_with_nullable_schema] | ||
|
||
module.exports = { | ||
generateContent, | ||
}; |
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'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// [START googlegenaisdk_ctrlgen_with_resp_schema] | ||||||||||||||||||||||||||||||||||||||||||||||||||
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 generateContent( | ||||||||||||||||||||||||||||||||||||||||||||||||||
projectId = GOOGLE_CLOUD_PROJECT, | ||||||||||||||||||||||||||||||||||||||||||||||||||
location = GOOGLE_CLOUD_LOCATION | ||||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const ai = new GoogleGenAI({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
vertexai: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||
project: projectId, | ||||||||||||||||||||||||||||||||||||||||||||||||||
location: location, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const prompt = 'List a few popular cookie recipes.'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const responseSchema = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
type: 'ARRAY', | ||||||||||||||||||||||||||||||||||||||||||||||||||
items: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
type: 'OBJECT', | ||||||||||||||||||||||||||||||||||||||||||||||||||
properties: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
recipe_name: {type: 'STRING'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
ingredients: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
type: 'ARRAY', | ||||||||||||||||||||||||||||||||||||||||||||||||||
items: {type: 'STRING'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
required: ['recipe_name', 'ingredients'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
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. Improve schema definition consistency and adherence to standards:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const response = await ai.models.generateContent({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
model: 'gemini-2.5-flash', | ||||||||||||||||||||||||||||||||||||||||||||||||||
contents: prompt, | ||||||||||||||||||||||||||||||||||||||||||||||||||
config: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
responseMimeType: 'application/json', | ||||||||||||||||||||||||||||||||||||||||||||||||||
responseSchema: responseSchema, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
console.log(response.text); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return response.text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
// [END googlegenaisdk_ctrlgen_with_resp_schema] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
module.exports = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
generateContent, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; |
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,29 @@ | ||
// 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('../controlled-generation/ctrlgen-with-class-schema.js'); | ||
|
||
describe('ctrlgen-with-class-schema', () => { | ||
it('should generate text content in Json', async function () { | ||
this.timeout(10000); | ||
const output = await sample.generateContent(projectId); | ||
assert(output.length > 0 && output.includes('Cookies')); | ||
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.