[go: nahoru, domu]

Skip to content
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

feat(generative-ai): Add basic samples for Gemini / VertexAI inference #3670

Merged
merged 12 commits into from
May 6, 2024
63 changes: 63 additions & 0 deletions generative-ai/snippets/inference/nonStreamMultiModalityBasic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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: [
{
file_data: {
file_uri: 'gs://cloud-samples-data/video/animals.mp4',
mime_type: 'video/mp4',
},
},
{
file_data: {
file_uri:
'gs://cloud-samples-data/generative-ai/image/character.jpg',
mime_type: 'image/jpeg',
},
},
{text: 'Are this video and image correlated?'},
],
},
],
};

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;
});
58 changes: 58 additions & 0 deletions generative-ai/snippets/inference/nonStreamTextBasic.js
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;
});
65 changes: 65 additions & 0 deletions generative-ai/snippets/inference/streamMultiModalityBasic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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: [
{
file_data: {
file_uri: 'gs://cloud-samples-data/video/animals.mp4',
mime_type: 'video/mp4',
},
},
{
file_data: {
file_uri:
'gs://cloud-samples-data/generative-ai/image/character.jpg',
mime_type: 'image/jpeg',
},
},
{text: 'Are this video and image correlated?'},
],
},
],
};

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;
});
59 changes: 59 additions & 0 deletions generative-ai/snippets/inference/streamTextBasic.js
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;
});
2 changes: 1 addition & 1 deletion generative-ai/snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"*.js"
],
"scripts": {
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js"
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js"
},
"dependencies": {
"@google-cloud/aiplatform": "^3.12.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const projectId = process.env.CAIP_PROJECT_ID;

describe('Summarize audio', async () => {
it('should summerize audio', async () => {
it('should summarize audio', async () => {
const output = execSync(
`node ./gemini-audio-summarization.js ${projectId}`
);
Expand Down
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.5-pro-preview-0409';

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);
});
});
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/));
});
});
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);
});
});
Loading
Loading