Skip to content

Commit 9f3c79d

Browse files
gericdongpattishin
andauthored
feat: readd GenAI embedding code samples (#3616)
Co-authored-by: Patti Shin <[email protected]>
1 parent f7f4545 commit 9f3c79d

4 files changed

+280
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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', textPrompt) {
20+
// [START aiplatform_sdk_text_embedding]
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 textPrompt = 'YOUR_TEXT_PROMPT';
28+
const aiplatform = require('@google-cloud/aiplatform');
29+
30+
// Imports the Google Cloud Prediction service client
31+
const {PredictionServiceClient} = aiplatform.v1;
32+
33+
// Import the helper module for converting arbitrary protobuf.Value objects.
34+
const {helpers} = aiplatform;
35+
36+
// Specifies the location of the api endpoint
37+
const clientOptions = {
38+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
39+
};
40+
const publisher = 'google';
41+
const model = 'multimodalembedding@001';
42+
43+
// Instantiates a client
44+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
45+
46+
async function predictImageFromText() {
47+
// Configure the parent resource
48+
const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;
49+
50+
const prompt = {
51+
text: textPrompt,
52+
};
53+
const instanceValue = helpers.toValue(prompt);
54+
const instances = [instanceValue];
55+
56+
const parameter = {
57+
sampleCount: 1,
58+
};
59+
const parameters = helpers.toValue(parameter);
60+
61+
const request = {
62+
endpoint,
63+
instances,
64+
parameters,
65+
};
66+
67+
// Predict request
68+
const [response] = await predictionServiceClient.predict(request);
69+
console.log('Get image embedding response');
70+
const predictions = response.predictions;
71+
console.log('\tPredictions :');
72+
for (const prediction of predictions) {
73+
console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
74+
}
75+
}
76+
77+
await predictImageFromText();
78+
// [END aiplatform_sdk_text_embedding]
79+
}
80+
81+
exports.predictImageFromText = main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
const {assert} = require('chai');
20+
const {describe, it} = require('mocha');
21+
const sinon = require('sinon');
22+
23+
const {
24+
predictImageFromImageAndText,
25+
} = require('../predict-image-from-image-and-text');
26+
27+
const project = process.env.CAIP_PROJECT_ID;
28+
const LOCATION = 'us-central1';
29+
const baseImagePath = 'resources/daisy.jpg';
30+
const textPrompt = 'an impressionist painting';
31+
32+
describe('AI platform generates image from image and text', async () => {
33+
const stubConsole = function () {
34+
sinon.stub(console, 'error');
35+
sinon.stub(console, 'log');
36+
};
37+
38+
const restoreConsole = function () {
39+
console.log.restore();
40+
console.error.restore();
41+
};
42+
43+
beforeEach(stubConsole);
44+
afterEach(restoreConsole);
45+
46+
it('should make predictions using a large language model', async () => {
47+
await predictImageFromImageAndText(
48+
project,
49+
LOCATION,
50+
baseImagePath,
51+
textPrompt
52+
);
53+
assert.include(console.log.firstCall.args, 'Get image embedding response');
54+
});
55+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
const {assert} = require('chai');
20+
const {describe, it} = require('mocha');
21+
const sinon = require('sinon');
22+
23+
const {predictImageFromText} = require('../predict-image-from-text');
24+
25+
const project = process.env.CAIP_PROJECT_ID;
26+
const LOCATION = 'us-central1';
27+
const textPrompt =
28+
'small red boat on water in the morning watercolor illustration muted colors';
29+
30+
describe('AI platform generates image from text', async () => {
31+
const stubConsole = function () {
32+
sinon.stub(console, 'error');
33+
sinon.stub(console, 'log');
34+
};
35+
36+
const restoreConsole = function () {
37+
console.log.restore();
38+
console.error.restore();
39+
};
40+
41+
beforeEach(stubConsole);
42+
afterEach(restoreConsole);
43+
44+
it('should make predictions using a large language model', async () => {
45+
await predictImageFromText(project, LOCATION, textPrompt);
46+
assert.include(console.log.firstCall.args, 'Get image embedding response');
47+
});
48+
});

0 commit comments

Comments
 (0)