|
| 1 | +/* |
| 2 | + * Copyright 2023 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 | +async function main(project, location = 'us-central1') { |
| 20 | + // [START aiplatform_sdk_ideation] |
| 21 | + /** |
| 22 | + * TODO(developer): Uncomment these variables before running the sample.\ |
| 23 | + * (Not necessary if passing values as arguments) |
| 24 | + */ |
| 25 | + // const project = 'YOUR_PROJECT_ID'; |
| 26 | + // const location = 'YOUR_PROJECT_LOCATION'; |
| 27 | + const aiplatform = require('@google-cloud/aiplatform'); |
| 28 | + |
| 29 | + // Imports the Google Cloud Prediction service client |
| 30 | + const {PredictionServiceClient} = aiplatform.v1; |
| 31 | + |
| 32 | + // Import the helper module for converting arbitrary protobuf.Value objects. |
| 33 | + const {helpers} = aiplatform; |
| 34 | + |
| 35 | + // Specifies the location of the api endpoint |
| 36 | + const clientOptions = { |
| 37 | + apiEndpoint: 'us-central1-aiplatform.googleapis.com', |
| 38 | + }; |
| 39 | + |
| 40 | + const publisher = 'google'; |
| 41 | + const model = 'text-bison@001'; |
| 42 | + |
| 43 | + // Instantiates a client |
| 44 | + const predictionServiceClient = new PredictionServiceClient(clientOptions); |
| 45 | + |
| 46 | + async function callPredict() { |
| 47 | + // Configure the parent resource |
| 48 | + const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`; |
| 49 | + |
| 50 | + const prompt = { |
| 51 | + prompt: |
| 52 | + 'Give me ten interview questions for the role of program manager.', |
| 53 | + }; |
| 54 | + const instanceValue = helpers.toValue(prompt); |
| 55 | + const instances = [instanceValue]; |
| 56 | + |
| 57 | + const parameter = { |
| 58 | + temperature: 0.2, |
| 59 | + maxOutputTokens: 5, |
| 60 | + topP: 0.95, |
| 61 | + topK: 40, |
| 62 | + }; |
| 63 | + const parameters = helpers.toValue(parameter); |
| 64 | + |
| 65 | + const request = { |
| 66 | + endpoint, |
| 67 | + instances, |
| 68 | + parameters, |
| 69 | + }; |
| 70 | + |
| 71 | + // Predict request |
| 72 | + const response = await predictionServiceClient.predict(request); |
| 73 | + console.log('Get text prompt response'); |
| 74 | + console.log(response); |
| 75 | + } |
| 76 | + |
| 77 | + callPredict(); |
| 78 | + // [END aiplatform_sdk_ideation] |
| 79 | +} |
| 80 | + |
| 81 | +process.on('unhandledRejection', err => { |
| 82 | + console.error(err.message); |
| 83 | + process.exitCode = 1; |
| 84 | +}); |
| 85 | + |
| 86 | +main(...process.argv.slice(2)); |
0 commit comments