|
| 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 | +async function main() { |
| 20 | + // [START generativeaionvertexai_imagen_get_short_form_image_responses] |
| 21 | + /** |
| 22 | + * TODO(developer): Update these variables before running the sample. |
| 23 | + */ |
| 24 | + const projectId = process.env.CAIP_PROJECT_ID; |
| 25 | + const location = 'us-central1'; |
| 26 | + const inputFile = 'resources/cat.png'; |
| 27 | + // The question about the contents of the image. |
| 28 | + const prompt = 'What breed of cat is this a picture of?'; |
| 29 | + |
| 30 | + const aiplatform = require('@google-cloud/aiplatform'); |
| 31 | + |
| 32 | + // Imports the Google Cloud Prediction Service Client library |
| 33 | + const {PredictionServiceClient} = aiplatform.v1; |
| 34 | + |
| 35 | + // Import the helper module for converting arbitrary protobuf.Value objects |
| 36 | + const {helpers} = aiplatform; |
| 37 | + |
| 38 | + // Specifies the location of the api endpoint |
| 39 | + const clientOptions = { |
| 40 | + apiEndpoint: `${location}-aiplatform.googleapis.com`, |
| 41 | + }; |
| 42 | + |
| 43 | + // Instantiates a client |
| 44 | + const predictionServiceClient = new PredictionServiceClient(clientOptions); |
| 45 | + |
| 46 | + async function getShortFormImageResponses() { |
| 47 | + const fs = require('fs'); |
| 48 | + // Configure the parent resource |
| 49 | + const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagetext@001`; |
| 50 | + |
| 51 | + const imageFile = fs.readFileSync(inputFile); |
| 52 | + // Convert the image data to a Buffer and base64 encode it. |
| 53 | + const encodedImage = Buffer.from(imageFile).toString('base64'); |
| 54 | + |
| 55 | + const instance = { |
| 56 | + prompt: prompt, |
| 57 | + image: { |
| 58 | + bytesBase64Encoded: encodedImage, |
| 59 | + }, |
| 60 | + }; |
| 61 | + const instanceValue = helpers.toValue(instance); |
| 62 | + const instances = [instanceValue]; |
| 63 | + |
| 64 | + const parameter = { |
| 65 | + // Optional parameters |
| 66 | + sampleCount: 2, |
| 67 | + }; |
| 68 | + const parameters = helpers.toValue(parameter); |
| 69 | + |
| 70 | + const request = { |
| 71 | + endpoint, |
| 72 | + instances, |
| 73 | + parameters, |
| 74 | + }; |
| 75 | + |
| 76 | + // Predict request |
| 77 | + const [response] = await predictionServiceClient.predict(request); |
| 78 | + const predictions = response.predictions; |
| 79 | + if (predictions.length === 0) { |
| 80 | + console.log( |
| 81 | + 'No responses were generated. Check the request parameters and image.' |
| 82 | + ); |
| 83 | + } else { |
| 84 | + predictions.forEach(prediction => { |
| 85 | + console.log(prediction.stringValue); |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + await getShortFormImageResponses(); |
| 90 | + // [END generativeaionvertexai_imagen_get_short_form_image_responses] |
| 91 | +} |
| 92 | + |
| 93 | +main().catch(err => { |
| 94 | + console.error(err); |
| 95 | + process.exitcode = 1; |
| 96 | +}); |
0 commit comments