|
| 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( |
| 20 | + project, |
| 21 | + location = 'us-central1', |
| 22 | + baseImagePath, |
| 23 | + textPrompt |
| 24 | +) { |
| 25 | + // [START aiplatform_sdk_text_image_embedding] |
| 26 | + /** |
| 27 | + * TODO(developer): Uncomment these variables before running the sample.\ |
| 28 | + * (Not necessary if passing values as arguments) |
| 29 | + */ |
| 30 | + // const project = 'YOUR_PROJECT_ID'; |
| 31 | + // const location = 'YOUR_PROJECT_LOCATION'; |
| 32 | + // const bastImagePath = "YOUR_BASED_IMAGE_PATH" |
| 33 | + // const textPrompt = 'YOUR_TEXT_PROMPT'; |
| 34 | + const aiplatform = require('@google-cloud/aiplatform'); |
| 35 | + |
| 36 | + // Imports the Google Cloud Prediction service client |
| 37 | + const {PredictionServiceClient} = aiplatform.v1; |
| 38 | + |
| 39 | + // Import the helper module for converting arbitrary protobuf.Value objects. |
| 40 | + const {helpers} = aiplatform; |
| 41 | + |
| 42 | + // Specifies the location of the api endpoint |
| 43 | + const clientOptions = { |
| 44 | + apiEndpoint: 'us-central1-aiplatform.googleapis.com', |
| 45 | + }; |
| 46 | + const publisher = 'google'; |
| 47 | + const model = 'multimodalembedding@001'; |
| 48 | + |
| 49 | + // Instantiates a client |
| 50 | + const predictionServiceClient = new PredictionServiceClient(clientOptions); |
| 51 | + |
| 52 | + async function predictImageFromImageAndText() { |
| 53 | + // Configure the parent resource |
| 54 | + const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`; |
| 55 | + |
| 56 | + const fs = require('fs'); |
| 57 | + const imageFile = fs.readFileSync(baseImagePath); |
| 58 | + |
| 59 | + // Convert the image data to a Buffer and base64 encode it. |
| 60 | + const encodedImage = Buffer.from(imageFile).toString('base64'); |
| 61 | + |
| 62 | + const prompt = { |
| 63 | + text: textPrompt, |
| 64 | + image: { |
| 65 | + bytesBase64Encoded: encodedImage, |
| 66 | + }, |
| 67 | + }; |
| 68 | + const instanceValue = helpers.toValue(prompt); |
| 69 | + const instances = [instanceValue]; |
| 70 | + |
| 71 | + const parameter = { |
| 72 | + sampleCount: 1, |
| 73 | + }; |
| 74 | + const parameters = helpers.toValue(parameter); |
| 75 | + |
| 76 | + const request = { |
| 77 | + endpoint, |
| 78 | + instances, |
| 79 | + parameters, |
| 80 | + }; |
| 81 | + |
| 82 | + // Predict request |
| 83 | + const [response] = await predictionServiceClient.predict(request); |
| 84 | + console.log('Get image embedding response'); |
| 85 | + const predictions = response.predictions; |
| 86 | + console.log('\tPredictions :'); |
| 87 | + for (const prediction of predictions) { |
| 88 | + console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + await predictImageFromImageAndText(); |
| 93 | + // [END aiplatform_sdk_text_image_embedding] |
| 94 | +} |
| 95 | + |
| 96 | +exports.predictImageFromImageAndText = main; |
0 commit comments