-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(generative-ai): Add basic samples for Gemini / VertexAI inference #3670
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cfc3e18
feat(generative-ai): Add sample for basic text generation.
arbrown 3b99e39
chore: fix lint issues
arbrown 7b75847
feat: Add basic multimodal example
arbrown a569ee4
chore: add header
arbrown f5b6c48
feat: Add streaming inference example
arbrown 3a2c53d
feat: Add example for multimodal streaming response
arbrown 234d4b0
fix: Fix some tests that were not running correctly
arbrown b61f968
chore: Clarify test names
arbrown 9cc360b
fix: Adjust cloud storage bucket and argurment order for consistency
arbrown 7e9dc09
chore: fix lint errors
arbrown fe3ca39
chore: Update some tests and prompt text to be clearer
arbrown eb85175
chore: Update function calling stream model to address flaky test.
arbrown 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
62 changes: 62 additions & 0 deletions
62
generative-ai/snippets/inference/nonStreamMultiModalityBasic.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,62 @@ | ||
// Copyright 2024 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. | ||
|
||
// [START generativeaionvertexai_non_stream_multimodality_basic] | ||
const {VertexAI} = require('@google-cloud/vertexai'); | ||
|
||
/** | ||
* TODO(developer): Update these variables before running the sample. | ||
*/ | ||
async function generateContent( | ||
projectId = 'PROJECT_ID', | ||
location = 'us-central1', | ||
model = 'gemini-1.5-pro-preview-0409' | ||
) { | ||
// Initialize Vertex AI | ||
const vertexAI = new VertexAI({project: projectId, location: location}); | ||
const generativeModel = vertexAI.getGenerativeModel({model: model}); | ||
|
||
const request = { | ||
contents: [ | ||
{ | ||
role: 'user', | ||
parts: [ | ||
{text: 'Are following video and image correlated?'}, | ||
{ | ||
file_data: { | ||
file_uri: 'gs://cloud-samples-data/video/animals.mp4', | ||
mime_type: 'video/mp4', | ||
}, | ||
}, | ||
{ | ||
file_data: { | ||
file_uri: 'gs://generativeai-downloads/images/character.jpg', | ||
arbrown marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mime_type: 'image/jpeg', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const result = await generativeModel.generateContent(request); | ||
|
||
console.log(result.response.candidates[0].content.parts[0].text); | ||
} | ||
// [END generativeaionvertexai_non_stream_multimodality_basic] | ||
|
||
generateContent(...process.argv.slice(2)).catch(err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); |
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 2024 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. | ||
|
||
// [START generativeaionvertexai_non_stream_text_basic] | ||
const {VertexAI} = require('@google-cloud/vertexai'); | ||
|
||
/** | ||
* TODO(developer): Update these variables before running the sample. | ||
*/ | ||
async function generateContent( | ||
projectId = 'PROJECT_ID', | ||
location = 'us-central1', | ||
model = 'gemini-1.0-pro' | ||
) { | ||
// Initialize Vertex with your Cloud project and location | ||
const vertexAI = new VertexAI({project: projectId, location: location}); | ||
|
||
// Instantiate the model | ||
const generativeModel = vertexAI.getGenerativeModel({ | ||
model: model, | ||
}); | ||
|
||
const request = { | ||
contents: [ | ||
{ | ||
role: 'user', | ||
parts: [ | ||
{ | ||
text: 'Write a story about a magic backpack.', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
console.log(JSON.stringify(request)); | ||
|
||
const result = await generativeModel.generateContent(request); | ||
|
||
console.log(result.response.candidates[0].content.parts[0].text); | ||
} | ||
// [END generativeaionvertexai_non_stream_text_basic] | ||
|
||
generateContent(...process.argv.slice(2)).catch(err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); |
64 changes: 64 additions & 0 deletions
64
generative-ai/snippets/inference/streamMultiModalityBasic.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,64 @@ | ||
// Copyright 2024 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. | ||
|
||
// [START generativeaionvertexai_stream_multimodality_basic] | ||
const {VertexAI} = require('@google-cloud/vertexai'); | ||
|
||
/** | ||
* TODO(developer): Update these variables before running the sample. | ||
*/ | ||
async function generateContent( | ||
projectId = 'PROJECT_ID', | ||
location = 'us-central1', | ||
model = 'gemini-1.5-pro-preview-0409' | ||
) { | ||
// Initialize Vertex AI | ||
const vertexAI = new VertexAI({project: projectId, location: location}); | ||
const generativeModel = vertexAI.getGenerativeModel({model: model}); | ||
|
||
const request = { | ||
contents: [ | ||
{ | ||
role: 'user', | ||
parts: [ | ||
{text: 'Are following video and image correlated?'}, | ||
arbrown marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
file_data: { | ||
file_uri: 'gs://cloud-samples-data/video/animals.mp4', | ||
mime_type: 'video/mp4', | ||
}, | ||
}, | ||
{ | ||
file_data: { | ||
file_uri: 'gs://generativeai-downloads/images/character.jpg', | ||
arbrown marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mime_type: 'image/jpeg', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const result = await generativeModel.generateContentStream(request); | ||
|
||
for await (const item of result.stream) { | ||
console.log(item.candidates[0].content.parts[0].text); | ||
} | ||
} | ||
// [END generativeaionvertexai_stream_multimodality_basic] | ||
|
||
generateContent(...process.argv.slice(2)).catch(err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); |
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 2024 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. | ||
|
||
// [START generativeaionvertexai_stream_text_basic] | ||
const {VertexAI} = require('@google-cloud/vertexai'); | ||
|
||
/** | ||
* TODO(developer): Update these variables before running the sample. | ||
*/ | ||
async function generateContent( | ||
projectId = 'PROJECT_ID', | ||
location = 'us-central1', | ||
model = 'gemini-1.0-pro' | ||
) { | ||
// Initialize Vertex with your Cloud project and location | ||
const vertexAI = new VertexAI({project: projectId, location: location}); | ||
|
||
// Instantiate the model | ||
const generativeModel = vertexAI.getGenerativeModel({ | ||
model: model, | ||
}); | ||
|
||
const request = { | ||
contents: [ | ||
{ | ||
role: 'user', | ||
parts: [ | ||
{ | ||
text: 'Write a story about a magic backpack.', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
console.log(JSON.stringify(request)); | ||
|
||
const result = await generativeModel.generateContentStream(request); | ||
for await (const item of result.stream) { | ||
console.log(item.candidates[0].content.parts[0].text); | ||
} | ||
} | ||
// [END generativeaionvertexai_stream_text_basic] | ||
|
||
generateContent(...process.argv.slice(2)).catch(err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); |
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
41 changes: 41 additions & 0 deletions
41
generative-ai/snippets/test/inference/nonStreamMultiModalityBasic.test.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,41 @@ | ||
// Copyright 2024 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 cp = require('child_process'); | ||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
const projectId = process.env.CAIP_PROJECT_ID; | ||
const location = process.env.LOCATION; | ||
const model = 'gemini-1.5-pro-preview-0409'; | ||
|
||
describe('Generative AI Multimodal Text Inference', () => { | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample.\ | ||
* (Not necessary if passing values as arguments) | ||
*/ | ||
// const projectId = 'YOUR_PROJECT_ID'; | ||
// const location = 'YOUR_LOCATION'; | ||
// const model = 'gemini-1.0-pro'; | ||
gericdong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it('should generate text based on a prompt containing text, a video, and an image', async () => { | ||
const output = execSync( | ||
`node ./inference/nonStreamMultiModalityBasic.js ${projectId} ${location} ${model}` | ||
); | ||
assert(output.length > 0); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
generative-ai/snippets/test/inference/nonStreamTextBasic.test.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,43 @@ | ||
// Copyright 2024 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 cp = require('child_process'); | ||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
const projectId = process.env.CAIP_PROJECT_ID; | ||
const location = process.env.LOCATION; | ||
const model = 'gemini-1.0-pro'; | ||
|
||
describe('Generative AI Basic Text Inference', () => { | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample.\ | ||
* (Not necessary if passing values as arguments) | ||
*/ | ||
// const projectId = 'YOUR_PROJECT_ID'; | ||
// const location = 'YOUR_LOCATION'; | ||
// const model = 'gemini-1.0-pro'; | ||
|
||
it('should create a generative text model and infer text from a prompt', async () => { | ||
const output = execSync( | ||
`node ./inference/nonStreamTextBasic.js ${projectId} ${location} ${model}` | ||
); | ||
|
||
// Assert that the correct prompt was issued | ||
assert(output.match(/Write a story about a magic backpack/)); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
generative-ai/snippets/test/inference/streamMultiModalityBasic.test.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,41 @@ | ||
// Copyright 2024 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 cp = require('child_process'); | ||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
const projectId = process.env.CAIP_PROJECT_ID; | ||
const location = process.env.LOCATION; | ||
const model = 'gemini-1.5-pro-preview-0409'; | ||
|
||
describe('Generative AI Basic Multimodal Text Inference Streaming', () => { | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample.\ | ||
* (Not necessary if passing values as arguments) | ||
*/ | ||
// const projectId = 'YOUR_PROJECT_ID'; | ||
// const location = 'YOUR_LOCATION'; | ||
// const model = 'gemini-1.5-pro-preview-0409'; | ||
|
||
it('should create a generative text model and infer text from a prompt, streaming the results', async () => { | ||
const output = execSync( | ||
`node ./inference/streamMultiModalityBasic.js ${projectId} ${location} ${model}` | ||
); | ||
assert(output.length > 0); | ||
}); | ||
}); |
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.