20
20
// [START generativeaionvertexai_sdk_embedding]
21
21
async function main (
22
22
project ,
23
- model = 'text -embedding-005 ' ,
23
+ model = 'gemini -embedding-001 ' ,
24
24
texts = 'banana bread?;banana muffins?' ,
25
25
task = 'QUESTION_ANSWERING' ,
26
26
dimensionality = 0 ,
@@ -37,19 +37,29 @@ async function main(
37
37
const instances = texts
38
38
. split ( ';' )
39
39
. map ( e => helpers . toValue ( { content : e , task_type : task } ) ) ;
40
+
41
+ const client = new PredictionServiceClient ( clientOptions ) ;
40
42
const parameters = helpers . toValue (
41
43
dimensionality > 0 ? { outputDimensionality : parseInt ( dimensionality ) } : { }
42
44
) ;
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 ) ) ;
53
63
}
54
64
55
65
callPredict ( ) ;
0 commit comments