Skip to content

Commit 45e2704

Browse files
committed
process one input at a time
1 parent 42cabf8 commit 45e2704

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

0 commit comments

Comments
 (0)