|
| 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 | +// }); |
0 commit comments