@@ -217,40 +217,47 @@ async def _embed_vertex(
217
217
if not model :
218
218
model = DEFAULT_VERTEX_MODEL
219
219
220
- creds_info = json .loads (self .api_key )
221
- credentials = service_account .Credentials .from_service_account_info (creds_info )
222
- project_id = creds_info ["project_id" ]
223
- client = genai .Client (
224
- vertexai = True ,
225
- project = project_id ,
226
- location = VERTEXAI_EMBEDDING_MODEL_LOCATION ,
227
- credentials = credentials
228
- )
229
-
220
+ try :
221
+ creds_info = json .loads (self .api_key )
222
+ credentials = service_account .Credentials .from_service_account_info (
223
+ creds_info ,
224
+ scopes = ['https://www.googleapis.com/auth/cloud-platform' ]
225
+ )
226
+ project_id = creds_info ["project_id" ]
227
+ client = genai .Client (
228
+ vertexai = True ,
229
+ project = project_id ,
230
+ location = VERTEXAI_EMBEDDING_MODEL_LOCATION ,
231
+ credentials = credentials
232
+ )
230
233
231
- is_gemini = "gemini" in model .lower ()
232
- batch_size = 1 if is_gemini else VERTEXAI_EMBEDDING_LOCAL_BATCH_SIZE # This batch size is now fixed by the function
234
+ is_gemini = "gemini" in model .lower ()
235
+ batch_size = 1 if is_gemini else VERTEXAI_EMBEDDING_LOCAL_BATCH_SIZE
233
236
234
- def make_batches (lst , size ):
235
- return [lst [i :i + size ] for i in range (0 , len (lst ), size )]
237
+ def make_batches (lst , size ):
238
+ return [lst [i :i + size ] for i in range (0 , len (lst ), size )]
236
239
237
- async def embed_batch (batch : list [str ]) -> list [list [float ]]:
238
- embeddings = await client .aio .models .embed_content (
239
- model = model ,
240
- contents = batch ,
241
- config = EmbedContentConfig (
242
- task_type = embedding_type ,
243
- output_dimensionality = VERTEXAI_EMBEDDING_MODEL_DIMENSION
240
+ async def embed_batch (batch : list [str ]) -> list [list [float ]]:
241
+ embeddings = await client .aio .models .embed_content (
242
+ model = model ,
243
+ contents = batch ,
244
+ config = EmbedContentConfig (
245
+ task_type = embedding_type ,
246
+ output_dimensionality = VERTEXAI_EMBEDDING_MODEL_DIMENSION
247
+ )
244
248
)
245
- )
246
- return embeddings .embeddings
249
+ return embeddings .embeddings
247
250
248
- batches = make_batches (texts , batch_size )
249
- tasks = [embed_batch (batch ) for batch in batches ]
250
- results = await asyncio .gather (* tasks )
251
+ batches = make_batches (texts , batch_size )
252
+ tasks = [embed_batch (batch ) for batch in batches ]
253
+ results = await asyncio .gather (* tasks )
251
254
252
- # Flatten list of lists
253
- return [embedding .values for batch in results for embedding in batch ]
255
+ # Flatten list of lists
256
+ return [embedding .values for batch in results for embedding in batch ]
257
+ except Exception as e :
258
+ error_msg = f"Failed to generate embeddings with Vertex AI: { e } "
259
+ logger .error (error_msg )
260
+ raise RuntimeError (error_msg ) from e
254
261
255
262
async def _embed_litellm_proxy (
256
263
self , texts : list [str ], model_name : str | None
0 commit comments