Skip to content

Commit f22e9ce

Browse files
authored
feat: Update the model name to gemini-embedding-001 (#4098)
* feat: Update the model name to gemini-embedding-001 * update test * process one input at a time
1 parent a2a6bb3 commit f22e9ce

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// [START generativeaionvertexai_sdk_embedding]
2121
async function main(
2222
project,
23-
model = 'text-embedding-005',
23+
model = 'gemini-embedding-001',
2424
texts = 'banana bread?;banana muffins?',
2525
task = 'QUESTION_ANSWERING',
2626
dimensionality = 0,
@@ -37,19 +37,29 @@ async function main(
3737
const instances = texts
3838
.split(';')
3939
.map(e => helpers.toValue({content: e, task_type: task}));
40+
41+
const client = new PredictionServiceClient(clientOptions);
4042
const parameters = helpers.toValue(
4143
dimensionality > 0 ? {outputDimensionality: parseInt(dimensionality)} : {}
4244
);
43-
const request = {endpoint, instances, parameters};
44-
const client = new PredictionServiceClient(clientOptions);
45-
const [response] = await client.predict(request);
46-
const predictions = response.predictions;
47-
const embeddings = predictions.map(p => {
48-
const embeddingsProto = p.structValue.fields.embeddings;
49-
const valuesProto = embeddingsProto.structValue.fields.values;
50-
return valuesProto.listValue.values.map(v => v.numberValue);
51-
});
52-
console.log('Got embeddings: \n' + JSON.stringify(embeddings));
45+
const allEmbeddings = []
46+
// gemini-embedding-001 takes one input at a time.
47+
for (const instance of instances) {
48+
const request = {endpoint, instances: [instance], parameters};
49+
const [response] = await client.predict(request);
50+
const predictions = response.predictions;
51+
52+
const embeddings = predictions.map(p => {
53+
const embeddingsProto = p.structValue.fields.embeddings;
54+
const valuesProto = embeddingsProto.structValue.fields.values;
55+
return valuesProto.listValue.values.map(v => v.numberValue);
56+
});
57+
58+
allEmbeddings.push(embeddings[0])
59+
}
60+
61+
62+
console.log('Got embeddings: \n' + JSON.stringify(allEmbeddings));
5363
}
5464

5565
callPredict();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const texts = [
3737
describe('predict text embeddings', () => {
3838
it('should get text embeddings using the latest model', async () => {
3939
const stdout = execSync(
40-
`node ./predict-text-embeddings.js ${project} text-embedding-004 '${texts.join(';')}' QUESTION_ANSWERING ${dimensionality}`,
40+
`node ./predict-text-embeddings.js ${project} gemini-embedding-001 '${texts.join(';')}' QUESTION_ANSWERING ${dimensionality}`,
4141
{cwd}
4242
);
4343
const embeddings = JSON.parse(stdout.trimEnd().split('\n').at(-1));

0 commit comments

Comments
 (0)