Skip to content

Commit 6b70ba1

Browse files
lee1premium-2024iennae
authored andcommitted
fix: Validate embedding output dimensionality.
1 parent 6422536 commit 6b70ba1

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

ai-platform/snippets/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"*.js"
1111
],
1212
"scripts": {
13-
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.js"
13+
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*embeddings*.js"
1414
},
1515
"dependencies": {
1616
"@google-cloud/aiplatform": "^3.0.0",

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ async function main(
4343
const client = new PredictionServiceClient(clientOptions);
4444
const [response] = await client.predict(request);
4545
const predictions = response.predictions;
46-
const embeddings = predictions.map(p =>
47-
p.structValue.fields.embeddings.structValue.fields.values.listValue.values.map(
48-
v => v.numberValue
49-
)
50-
);
46+
const embeddings = predictions.map(p => {
47+
const embeddingsProto = p.structValue.fields.embeddings;
48+
const valuesProto = embeddingsProto.structValue.fields.values;
49+
return valuesProto.listValue.values.map(v => v.numberValue);
50+
});
5151
console.log('Got embeddings: \n' + JSON.stringify(embeddings));
5252
}
5353

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ async function main(
4242
const request = {endpoint, instances, parameters};
4343
const client = new PredictionServiceClient(clientOptions);
4444
const [response] = await client.predict(request);
45-
console.log('Got predict response');
4645
const predictions = response.predictions;
47-
const embeddings = predictions.map(p =>
48-
p.structValue.fields.embeddings.structValue.fields.values.listValue.values.map(
49-
v => v.numberValue
50-
)
51-
);
46+
const embeddings = predictions.map(p => {
47+
const embeddingsProto = p.structValue.fields.embeddings;
48+
const valuesProto = embeddingsProto.structValue.fields.values;
49+
return valuesProto.listValue.values.map(v => v.numberValue);
50+
});
5251
console.log('Got embeddings: \n' + JSON.stringify(embeddings));
5352
}
5453

0 commit comments

Comments
 (0)