Skip to content

Commit

Permalink
Feature/model server logging (#3579)
Browse files Browse the repository at this point in the history
* improve model server logging

* improve exception logging with provider/model names

* get everything into one log line

---------

Co-authored-by: Richard Kuo <[email protected]>
  • Loading branch information
rkuo-danswer and LostVector authored Jan 3, 2025
1 parent d1ec72b commit 03acb65
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions backend/model_server/encoders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import json
import time
from types import TracebackType
from typing import cast
from typing import Optional
Expand Down Expand Up @@ -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.")
Expand All @@ -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")
Expand Down Expand Up @@ -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(
Expand All @@ -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


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 03acb65

Please sign in to comment.