Skip to content

Commit df392da

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

File tree

2 files changed

+108
-13
lines changed

2 files changed

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

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

Lines changed: 33 additions & 13 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,26 +33,45 @@ 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

35-
describe('Gemma2 predictions', async () => {
36-
const predictionServiceClientMock = {
37-
predict: sinon.stub().resolves([
38-
{
39-
predictions: [
40-
{
41-
stringValue: text,
42-
},
43-
],
44-
},
45-
]),
46-
};
36+
const tpuResponse =
37+
'The sky appears blue due to a phenomenon called **Rayleigh scattering**.';
4738

39+
describe('Gemma2 predictions', async () => {
4840
afterEach(() => {
4941
sinon.restore();
5042
});
5143

5244
it('should run interference with GPU', async () => {
45+
const predictionServiceClientMock = {
46+
predict: sinon.stub().resolves([
47+
{
48+
predictions: [
49+
{
50+
stringValue: gpuResponse,
51+
},
52+
],
53+
},
54+
]),
55+
};
5356
const output = await gemma2PredictGpu(predictionServiceClientMock);
5457

5558
expect(output).include('Rayleigh scattering');
5659
});
60+
61+
it('should run interference with TPU', async () => {
62+
const predictionServiceClientMock = {
63+
predict: sinon.stub().resolves([
64+
{
65+
predictions: [
66+
{
67+
stringValue: tpuResponse,
68+
},
69+
],
70+
},
71+
]),
72+
};
73+
const output = await gemma2PredictTpu(predictionServiceClientMock);
74+
75+
expect(output).include('Rayleigh scattering');
76+
});
5777
});

0 commit comments

Comments
 (0)