diff --git a/backend/model_server/encoders.py b/backend/model_server/encoders.py index 94686f44027..3ed3857f6f0 100644 --- a/backend/model_server/encoders.py +++ b/backend/model_server/encoders.py @@ -1,5 +1,6 @@ import asyncio import json +import time from types import TracebackType from typing import cast from typing import Optional @@ -320,8 +321,6 @@ async def embed_text( api_url: str | None, api_version: str | None, ) -> list[Embedding]: - logger.info(f"Embedding {len(texts)} texts with provider: {provider_type}") - if not all(texts): logger.error("Empty strings provided for embedding") raise ValueError("Empty strings are not allowed for embedding.") @@ -330,8 +329,17 @@ async def embed_text( logger.error("No texts provided for embedding") raise ValueError("No texts provided for embedding.") + start = time.monotonic() + + total_chars = 0 + for text in texts: + total_chars += len(text) + if provider_type is not None: - logger.debug(f"Using cloud provider {provider_type} for embedding") + logger.info( + f"Embedding {len(texts)} texts with {total_chars} total characters with provider: {provider_type}" + ) + if api_key is None: logger.error("API key not provided for cloud model") raise RuntimeError("API key not provided for cloud model") @@ -363,8 +371,16 @@ async def embed_text( logger.error(error_message) raise ValueError(error_message) + elapsed = time.monotonic() - start + logger.info( + f"Successfully embedded {len(texts)} texts with {total_chars} total characters " + f"with provider {provider_type} in {elapsed:.2f}" + ) elif model_name is not None: - logger.debug(f"Using local model {model_name} for embedding") + logger.info( + f"Embedding {len(texts)} texts with {total_chars} total characters with local model: {model_name}" + ) + prefixed_texts = [f"{prefix}{text}" for text in texts] if prefix else texts local_model = get_embedding_model( @@ -382,13 +398,17 @@ async def embed_text( for embedding in embeddings_vectors ] + elapsed = time.monotonic() - start + logger.info( + f"Successfully embedded {len(texts)} texts with {total_chars} total characters " + f"with local model {model_name} in {elapsed:.2f}" + ) else: logger.error("Neither model name nor provider specified for embedding") raise ValueError( "Either model name or provider must be provided to run embeddings." ) - logger.info(f"Successfully embedded {len(texts)} texts") return embeddings @@ -440,7 +460,8 @@ async def process_embed_request( ) -> EmbedResponse: if not embed_request.texts: raise HTTPException(status_code=400, detail="No texts to be embedded") - elif not all(embed_request.texts): + + if not all(embed_request.texts): raise ValueError("Empty strings are not allowed for embedding.") try: @@ -471,9 +492,12 @@ async def process_embed_request( detail=str(e), ) except Exception as e: - exception_detail = f"Error during embedding process:\n{str(e)}" - logger.exception(exception_detail) - raise HTTPException(status_code=500, detail=exception_detail) + logger.exception( + f"Error during embedding process: provider={embed_request.provider_type} model={embed_request.model_name}" + ) + raise HTTPException( + status_code=500, detail=f"Error during embedding process: {e}" + ) @router.post("/cross-encoder-scores")