Skip to content

Commit f24e30c

Browse files
author
Joanna Grycz
committed
feat: gemma2 models samples
1 parent 023f53b commit f24e30c

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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_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');
22+
23+
async function gemma2PredictGpu(predictionServiceClient) {
24+
/**
25+
* TODO(developer): Update/uncomment 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+
// Default configuration
32+
const config = {maxOutputTokens: 1024, temperature: 0.9, topP: 1.0, topK: 1};
33+
// Prompt used in the prediction
34+
const prompt = 'Why is the sky blue?';
35+
36+
// Encapsulate the prompt in a correct format for GPUs
37+
// Example format: [{inputs: 'Why is the sky blue?', parameters: {temperature: 0.9}}]
38+
const input = {
39+
inputs: prompt,
40+
parameters: config,
41+
};
42+
43+
// Convert input message to a list of GAPIC instances for model input
44+
const instances = [helpers.toValue(input)];
45+
46+
// TODO(developer): Uncomment apiEndpoint and predictionServiceClient before running the sample.
47+
// const apiEndpoint = `${endpointRegion}-aiplatform.googleapis.com`;
48+
49+
// Create a client
50+
// predictionServiceClient = new PredictionServiceClient({apiEndpoint});
51+
52+
// Call the Gemma2 endpoint
53+
const gemma2Endpoint = `projects/${projectId}/locations/${endpointRegion}/endpoints/${endpointId}`;
54+
55+
const [response] = await predictionServiceClient.predict({
56+
endpoint: gemma2Endpoint,
57+
instances,
58+
});
59+
60+
const predictions = response.predictions;
61+
const text = predictions[0].stringValue;
62+
63+
console.log('Predictions:', text);
64+
// [END generativeaionvertexai_gemma2_predict_gpu]
65+
return text;
66+
}
67+
68+
module.exports = gemma2PredictGpu;
69+
70+
gemma2PredictGpu(...process.argv.slice(2)).catch(err => {
71+
console.error(err.message);
72+
process.exitCode = 1;
73+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const {expect} = require('chai');
20+
const {afterEach, describe, it} = require('mocha');
21+
const sinon = require('sinon');
22+
const gemma2PredictGpu = require('../gemma2PredictGpu.js');
23+
24+
const text = `The sky appears blue due to a phenomenon called **Rayleigh scattering**.
25+
**Here's how it works:**
26+
1. **Sunlight:** Sunlight is composed of all the colors of the rainbow.
27+
2. **Earth's Atmosphere:** When sunlight enters the Earth's atmosphere, it collides with tiny particles like nitrogen and oxygen molecules.
28+
3. **Scattering:** These particles scatter the sunlight in all directions. However, blue light (which has a shorter wavelength) is scattered more effectively than other colors.
29+
4. **Our Perception:** As a result, we see a blue sky because the scattered blue light reaches our eyes from all directions.
30+
**Why not other colors?**
31+
* **Violet light** has an even shorter wavelength than blue and is scattered even more. However, our eyes are less sensitive to violet light, so we perceive the sky as blue.
32+
* **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.
33+
`;
34+
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+
};
47+
48+
afterEach(() => {
49+
sinon.restore();
50+
});
51+
52+
it('should run interference with GPU', async () => {
53+
const output = await gemma2PredictGpu(predictionServiceClientMock);
54+
55+
expect(output).include('Rayleigh scattering');
56+
});
57+
});

0 commit comments

Comments
 (0)