Skip to content

Commit 105166f

Browse files
authored
Merge branch 'main' into batch-create-custom-network
2 parents db54bfb + d7e0845 commit 105166f

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

ai-platform/snippets/predict-text-embeddings-preview.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@
1616

1717
'use strict';
1818

19-
// [START generativeaionvertexai_sdk_embedding]
20-
async function main(
21-
project,
22-
model = 'text-embedding-preview-0409',
23-
texts = 'banana bread?;banana muffins?',
24-
task = 'QUESTION_ANSWERING',
25-
dimensionality = 256,
26-
apiEndpoint = 'us-central1-aiplatform.googleapis.com'
27-
) {
19+
async function main() {
20+
// [START generativeaionvertexai_sdk_embedding]
21+
22+
// TODO(developer): Update the following for your own use case.
23+
const project = 'long-door-651';
24+
const model = 'text-embedding-preview-0815';
25+
const location = 'us-central1';
26+
// Calculate the embedding for code blocks. Using 'RETRIEVAL_DOCUMENT' for corpus.
27+
// Specify the task type as 'CODE_RETRIEVAL_QUERY' for query, e.g. 'Retrieve a function that adds two numbers'.
28+
const texts =
29+
'def func(a, b): return a + b;def func(a, b): return a - b;def func(a, b): return (a ** 2 + b ** 2) ** 0.5';
30+
const task = 'RETRIEVAL_DOCUMENT';
31+
const dimensionality = 3;
32+
const apiEndpoint = 'us-central1-aiplatform.googleapis.com';
33+
2834
const aiplatform = require('@google-cloud/aiplatform');
2935
const {PredictionServiceClient} = aiplatform.v1;
3036
const {helpers} = aiplatform; // helps construct protobuf.Value objects.
3137
const clientOptions = {apiEndpoint: apiEndpoint};
32-
const location = 'us-central1';
3338
const endpoint = `projects/${project}/locations/${location}/publishers/google/models/${model}`;
3439
const parameters = helpers.toValue({
3540
outputDimensionality: parseInt(dimensionality),
@@ -50,14 +55,11 @@ async function main(
5055
});
5156
console.log('Got embeddings: \n' + JSON.stringify(embeddings));
5257
}
53-
54-
callPredict();
58+
await callPredict();
59+
// [END generativeaionvertexai_sdk_embedding]
5560
}
56-
// [END generativeaionvertexai_sdk_embedding]
5761

58-
process.on('unhandledRejection', err => {
59-
console.error(err.message);
62+
main().catch(err => {
63+
console.error(err);
6064
process.exitCode = 1;
6165
});
62-
63-
main(...process.argv.slice(2));

ai-platform/snippets/test/predict-text-embeddings.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ describe('predict text embeddings', () => {
4747
}
4848
});
4949
it('should get text embeddings using the preview model', async () => {
50-
const stdout = execSync(
51-
`node ./predict-text-embeddings-preview.js ${project} text-embedding-preview-0409 '${texts.join(';')}' QUESTION_ANSWERING ${dimensionality}`,
52-
{cwd}
53-
);
50+
const stdout = execSync('node ./predict-text-embeddings-preview.js', {cwd});
5451
const embeddings = JSON.parse(stdout.trimEnd().split('\n').at(-1));
55-
assert.equal(texts.length, embeddings.length);
52+
assert.equal(3, embeddings.length);
5653
for (const embedding of embeddings) {
5754
assert.equal(dimensionality, embedding.length);
5855
}

0 commit comments

Comments
 (0)