Skip to content

Commit

Permalink
Add error handling to voyageai embedder
Browse files Browse the repository at this point in the history
  • Loading branch information
rbiseck3 committed Dec 20, 2024
1 parent 3b57b1e commit 1e1308a
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion unstructured_ingest/embed/voyageai.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
from pydantic import Field, SecretStr

from unstructured_ingest.embed.interfaces import BaseEmbeddingEncoder, EmbeddingConfig
from unstructured_ingest.logger import logger
from unstructured_ingest.utils.dep_check import requires_dependencies
from unstructured_ingest.v2.errors import (
ProviderError,
UserAuthError,
UserError,
)
from unstructured_ingest.v2.errors import (
RateLimitError as CustomRateLimitError,
)

if TYPE_CHECKING:
from voyageai import Client as VoyageAIClient
Expand Down Expand Up @@ -38,9 +47,32 @@ def get_client(self) -> "VoyageAIClient":
class VoyageAIEmbeddingEncoder(BaseEmbeddingEncoder):
config: VoyageAIEmbeddingConfig

def wrap_error(self, e: Exception) -> Exception:
# https://docs.voyageai.com/docs/error-codes
from voyageai.error import AuthenticationError, RateLimitError, VoyageError

if not isinstance(e, VoyageError):
logger.error(f"unhandled exception from openai: {e}", exc_info=True)
raise e
http_code = e.http_status
message = e.user_message
if isinstance(e, AuthenticationError):
return UserAuthError(message)
if isinstance(e, RateLimitError):
return CustomRateLimitError(message)
if 400 <= http_code < 500:
return UserError(message)
if http_code >= 500:
return ProviderError(message)
logger.error(f"unhandled exception from openai: {e}", exc_info=True)
return e

def _embed_documents(self, elements: list[str]) -> list[list[float]]:
client: VoyageAIClient = self.config.get_client()
response = client.embed(texts=elements, model=self.config.embedder_model_name)
try:
response = client.embed(texts=elements, model=self.config.embedder_model_name)
except Exception as e:
self.wrap_error(e=e)
return response.embeddings

def embed_documents(self, elements: list[dict]) -> list[dict]:
Expand Down

0 comments on commit 1e1308a

Please sign in to comment.