Skip to content

Commit 7e8ee24

Browse files
author
Your Name
committed
Enhance error handling in CloudEmbedding class for embedding generation
- Wrapped the embedding generation logic in a try-except block to catch and log exceptions. - Added detailed error messages to improve debugging and user feedback when embedding generation fails.
1 parent d11efe2 commit 7e8ee24

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

backend/model_server/encoders.py

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -217,40 +217,47 @@ async def _embed_vertex(
217217
if not model:
218218
model = DEFAULT_VERTEX_MODEL
219219

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+
)
230233

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
233236

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)]
236239

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+
)
244248
)
245-
)
246-
return embeddings.embeddings
249+
return embeddings.embeddings
247250

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)
251254

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
254261

255262
async def _embed_litellm_proxy(
256263
self, texts: list[str], model_name: str | None

0 commit comments

Comments
 (0)