2020// [START generativeaionvertexai_sdk_embedding]
2121async 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 ( ) ;
0 commit comments