Skip to content

Commit 3013c7a

Browse files
authored
Merge pull request #3247 from GoogleCloudPlatform/b281562687-embedding
feat: text embeddings samples for Vertex LLMs
2 parents f3b0100 + 3354c67 commit 3013c7a

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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_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 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 = 'textembedding-gecko@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 instance = {
51+
content: 'What is life?',
52+
};
53+
const instanceValue = helpers.toValue(instance);
54+
const instances = [instanceValue];
55+
56+
const parameter = {
57+
temperature: 0,
58+
maxOutputTokens: 256,
59+
topP: 0,
60+
topK: 1,
61+
};
62+
const parameters = helpers.toValue(parameter);
63+
64+
const request = {
65+
endpoint,
66+
instances,
67+
parameters,
68+
};
69+
70+
// Predict request
71+
const [response] = await predictionServiceClient.predict(request);
72+
console.log('Get text embeddings response');
73+
const predictions = response.predictions;
74+
console.log('\tPredictions :');
75+
for (const prediction of predictions) {
76+
console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
77+
}
78+
}
79+
80+
callPredict();
81+
// [END aiplatform_sdk_embedding]
82+
}
83+
84+
process.on('unhandledRejection', err => {
85+
console.error(err.message);
86+
process.exitCode = 1;
87+
});
88+
89+
main(...process.argv.slice(2));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 path = require('path');
20+
const {assert} = require('chai');
21+
const {describe, it} = require('mocha');
22+
23+
const cp = require('child_process');
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
const cwd = path.join(__dirname, '..');
26+
27+
const project = process.env.CAIP_PROJECT_ID;
28+
const location = 'us-central1';
29+
30+
describe('AI platform predict text embeddings', () => {
31+
it('should make predictions using a large language model', async () => {
32+
const stdout = execSync(
33+
`node ./predict-text-embeddings.js ${project} ${location}`,
34+
{
35+
cwd,
36+
}
37+
);
38+
assert.match(stdout, /Get text embeddings response/);
39+
});
40+
});

0 commit comments

Comments
 (0)