mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-09-27 12:29:41 +02:00
Feature/model server logging (#3579)
* improve model server logging * improve exception logging with provider/model names * get everything into one log line --------- Co-authored-by: Richard Kuo <rkuo@rkuo.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
from types import TracebackType
|
from types import TracebackType
|
||||||
from typing import cast
|
from typing import cast
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
@@ -320,8 +321,6 @@ async def embed_text(
|
|||||||
api_url: str | None,
|
api_url: str | None,
|
||||||
api_version: str | None,
|
api_version: str | None,
|
||||||
) -> list[Embedding]:
|
) -> list[Embedding]:
|
||||||
logger.info(f"Embedding {len(texts)} texts with provider: {provider_type}")
|
|
||||||
|
|
||||||
if not all(texts):
|
if not all(texts):
|
||||||
logger.error("Empty strings provided for embedding")
|
logger.error("Empty strings provided for embedding")
|
||||||
raise ValueError("Empty strings are not allowed 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")
|
logger.error("No texts provided for embedding")
|
||||||
raise ValueError("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:
|
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:
|
if api_key is None:
|
||||||
logger.error("API key not provided for cloud model")
|
logger.error("API key not provided for cloud model")
|
||||||
raise RuntimeError("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)
|
logger.error(error_message)
|
||||||
raise ValueError(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:
|
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
|
prefixed_texts = [f"{prefix}{text}" for text in texts] if prefix else texts
|
||||||
|
|
||||||
local_model = get_embedding_model(
|
local_model = get_embedding_model(
|
||||||
@@ -382,13 +398,17 @@ async def embed_text(
|
|||||||
for embedding in embeddings_vectors
|
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:
|
else:
|
||||||
logger.error("Neither model name nor provider specified for embedding")
|
logger.error("Neither model name nor provider specified for embedding")
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Either model name or provider must be provided to run embeddings."
|
"Either model name or provider must be provided to run embeddings."
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f"Successfully embedded {len(texts)} texts")
|
|
||||||
return embeddings
|
return embeddings
|
||||||
|
|
||||||
|
|
||||||
@@ -440,7 +460,8 @@ async def process_embed_request(
|
|||||||
) -> EmbedResponse:
|
) -> EmbedResponse:
|
||||||
if not embed_request.texts:
|
if not embed_request.texts:
|
||||||
raise HTTPException(status_code=400, detail="No texts to be embedded")
|
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.")
|
raise ValueError("Empty strings are not allowed for embedding.")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -471,9 +492,12 @@ async def process_embed_request(
|
|||||||
detail=str(e),
|
detail=str(e),
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
exception_detail = f"Error during embedding process:\n{str(e)}"
|
logger.exception(
|
||||||
logger.exception(exception_detail)
|
f"Error during embedding process: provider={embed_request.provider_type} model={embed_request.model_name}"
|
||||||
raise HTTPException(status_code=500, detail=exception_detail)
|
)
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=500, detail=f"Error during embedding process: {e}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/cross-encoder-scores")
|
@router.post("/cross-encoder-scores")
|
||||||
|
Reference in New Issue
Block a user