Skip to content

Commit e5cc8c6

Browse files
author
Joanna Grycz
committed
feat: add generativeaionvertexai_gemma2_predict_tpu
1 parent 7783d5a commit e5cc8c6

File tree

3 files changed

+121
-20
lines changed

3 files changed

+121
-20
lines changed

generative-ai/snippets/gemma2PredictGpu.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// [START generativeaionvertexai_gemma2_predict_gpu]
16-
// Imports the Google Cloud Prediction Service Client library
17-
const {
18-
// TODO(developer): Uncomment PredictionServiceClient before running the sample.
19-
// PredictionServiceClient,
20-
helpers,
21-
} = require('@google-cloud/aiplatform');
15+
'use strict';
2216

2317
async function gemma2PredictGpu(predictionServiceClient) {
18+
// [START generativeaionvertexai_gemma2_predict_gpu]
19+
// Imports the Google Cloud Prediction Service Client library
20+
const {
21+
// TODO(developer): Uncomment PredictionServiceClient before running the sample.
22+
// PredictionServiceClient,
23+
helpers,
24+
} = require('@google-cloud/aiplatform');
2425
/**
2526
* TODO(developer): Update these variables before running the sample.
2627
*/
@@ -67,7 +68,8 @@ async function gemma2PredictGpu(predictionServiceClient) {
6768

6869
module.exports = gemma2PredictGpu;
6970

70-
gemma2PredictGpu(...process.argv.slice(2)).catch(err => {
71-
console.error(err.message);
72-
process.exitCode = 1;
73-
});
71+
// TODO(developer): Uncomment below lines before running the sample.
72+
// gemma2PredictGpu(...process.argv.slice(2)).catch(err => {
73+
// console.error(err.message);
74+
// process.exitCode = 1;
75+
// });
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2024 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+
async function gemma2PredictTpu(predictionServiceClient) {
18+
// [START generativeaionvertexai_gemma2_predict_tpu]
19+
// Imports the Google Cloud Prediction Service Client library
20+
const {
21+
// TODO(developer): Uncomment PredictionServiceClient before running the sample.
22+
// PredictionServiceClient,
23+
helpers,
24+
} = require('@google-cloud/aiplatform');
25+
/**
26+
* TODO(developer): Update these variables before running the sample.
27+
*/
28+
const projectId = 'your-project-id';
29+
const endpointRegion = 'your-vertex-endpoint-region';
30+
const endpointId = 'your-vertex-endpoint-id';
31+
32+
// Prompt used in the prediction
33+
const prompt = 'Why is the sky blue?';
34+
35+
// Encapsulate the prompt in a correct format for TPUs
36+
// Example format: [{prompt: 'Why is the sky blue?', temperature: 0.9}]
37+
const input = {
38+
prompt,
39+
// Parameters for default configuration
40+
maxOutputTokens: 1024,
41+
temperature: 0.9,
42+
topP: 1.0,
43+
topK: 1,
44+
};
45+
46+
// Convert input message to a list of GAPIC instances for model input
47+
const instances = [helpers.toValue(input)];
48+
49+
// TODO(developer): Uncomment apiEndpoint and predictionServiceClient before running the sample.
50+
// const apiEndpoint = `${endpointRegion}-aiplatform.googleapis.com`;
51+
52+
// Create a client
53+
// predictionServiceClient = new PredictionServiceClient({apiEndpoint});
54+
55+
// Call the Gemma2 endpoint
56+
const gemma2Endpoint = `projects/${projectId}/locations/${endpointRegion}/endpoints/${endpointId}`;
57+
58+
const [response] = await predictionServiceClient.predict({
59+
endpoint: gemma2Endpoint,
60+
instances,
61+
});
62+
63+
const predictions = response.predictions;
64+
const text = predictions[0].stringValue;
65+
66+
console.log('Predictions:', text);
67+
// [END generativeaionvertexai_gemma2_predict_tpu]
68+
return text;
69+
}
70+
71+
module.exports = gemma2PredictTpu;
72+
73+
// TODO(developer): Uncomment below lines before running the sample.
74+
// gemma2PredictTpu(...process.argv.slice(2)).catch(err => {
75+
// console.error(err.message);
76+
// process.exitCode = 1;
77+
// });

generative-ai/snippets/test/gemma2Prediction.test.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ const {expect} = require('chai');
2020
const {afterEach, describe, it} = require('mocha');
2121
const sinon = require('sinon');
2222
const gemma2PredictGpu = require('../gemma2PredictGpu.js');
23+
const gemma2PredictTpu = require('../gemma2PredictTpu.js');
2324

24-
const text = `The sky appears blue due to a phenomenon called **Rayleigh scattering**.
25+
const gpuResponse = `The sky appears blue due to a phenomenon called **Rayleigh scattering**.
2526
**Here's how it works:**
2627
1. **Sunlight:** Sunlight is composed of all the colors of the rainbow.
2728
2. **Earth's Atmosphere:** When sunlight enters the Earth's atmosphere, it collides with tiny particles like nitrogen and oxygen molecules.
@@ -32,25 +33,46 @@ const text = `The sky appears blue due to a phenomenon called **Rayleigh scatter
3233
* **Longer wavelengths** like red, orange, and yellow are scattered less and travel more directly through the atmosphere. This is why we see these colors during sunrise and sunset, when sunlight has to travel through more of the atmosphere.
3334
`;
3435

36+
const tpuResponse =
37+
'The sky appears blue due to a phenomenon called **Rayleigh scattering**.';
38+
3539
describe('Gemma2 predictions', async () => {
3640
const predictionServiceClientMock = {
37-
predict: sinon.stub().resolves([
41+
predict: sinon.stub().resolves([]),
42+
};
43+
44+
afterEach(() => {
45+
sinon.restore();
46+
});
47+
48+
it('should run interference with GPU', async () => {
49+
predictionServiceClientMock.predict.resolves([
3850
{
3951
predictions: [
4052
{
41-
stringValue: text,
53+
stringValue: gpuResponse,
4254
},
4355
],
4456
},
45-
]),
46-
};
57+
]);
4758

48-
afterEach(() => {
49-
sinon.restore();
59+
const output = await gemma2PredictGpu(predictionServiceClientMock);
60+
61+
expect(output).include('Rayleigh scattering');
5062
});
5163

52-
it('should run interference with GPU', async () => {
53-
const output = await gemma2PredictGpu(predictionServiceClientMock);
64+
it('should run interference with TPU', async () => {
65+
predictionServiceClientMock.predict.resolves([
66+
{
67+
predictions: [
68+
{
69+
stringValue: tpuResponse,
70+
},
71+
],
72+
},
73+
]);
74+
75+
const output = await gemma2PredictTpu(predictionServiceClientMock);
5476

5577
expect(output).include('Rayleigh scattering');
5678
});

0 commit comments

Comments
 (0)