From 30c843b2f92427898eb5cdfbc6789eed85c43c04 Mon Sep 17 00:00:00 2001 From: Alon Gadot Date: Sun, 14 Jan 2024 12:09:25 +0200 Subject: [PATCH] raise from exceptions rather than new exceptions to preserve stack trace --- semantic_router/encoders/cohere.py | 6 ++++-- semantic_router/encoders/fastembed.py | 2 +- semantic_router/encoders/openai.py | 6 ++++-- semantic_router/encoders/zure.py | 6 ++++-- semantic_router/llms/cohere.py | 6 ++++-- semantic_router/llms/openai.py | 6 ++++-- semantic_router/llms/openrouter.py | 6 ++++-- semantic_router/utils/llm.py | 4 ++-- 8 files changed, 27 insertions(+), 15 deletions(-) diff --git a/semantic_router/encoders/cohere.py b/semantic_router/encoders/cohere.py index 4f108095..f534fa51 100644 --- a/semantic_router/encoders/cohere.py +++ b/semantic_router/encoders/cohere.py @@ -25,7 +25,9 @@ def __init__( try: self.client = cohere.Client(cohere_api_key) except Exception as e: - raise ValueError(f"Cohere API client failed to initialize. Error: {e}") + raise ValueError( + f"Cohere API client failed to initialize. Error: {e}" + ) from e def __call__(self, docs: List[str]) -> List[List[float]]: if self.client is None: @@ -34,4 +36,4 @@ def __call__(self, docs: List[str]) -> List[List[float]]: embeds = self.client.embed(docs, input_type="search_query", model=self.name) return embeds.embeddings except Exception as e: - raise ValueError(f"Cohere API call failed. Error: {e}") + raise ValueError(f"Cohere API call failed. Error: {e}") from e diff --git a/semantic_router/encoders/fastembed.py b/semantic_router/encoders/fastembed.py index 018eeee1..2b908369 100644 --- a/semantic_router/encoders/fastembed.py +++ b/semantic_router/encoders/fastembed.py @@ -48,4 +48,4 @@ def __call__(self, docs: List[str]) -> List[List[float]]: embeddings: List[List[float]] = [e.tolist() for e in embeds] return embeddings except Exception as e: - raise ValueError(f"FastEmbed embed failed. Error: {e}") + raise ValueError(f"FastEmbed embed failed. Error: {e}") from e diff --git a/semantic_router/encoders/openai.py b/semantic_router/encoders/openai.py index 761f4931..ce11251b 100644 --- a/semantic_router/encoders/openai.py +++ b/semantic_router/encoders/openai.py @@ -29,7 +29,9 @@ def __init__( try: self.client = openai.Client(api_key=api_key) except Exception as e: - raise ValueError(f"OpenAI API client failed to initialize. Error: {e}") + raise ValueError( + f"OpenAI API client failed to initialize. Error: {e}" + ) from e def __call__(self, docs: List[str]) -> List[List[float]]: if self.client is None: @@ -49,7 +51,7 @@ def __call__(self, docs: List[str]) -> List[List[float]]: logger.warning(f"Retrying in {2**j} seconds...") except Exception as e: logger.error(f"OpenAI API call failed. Error: {error_message}") - raise ValueError(f"OpenAI API call failed. Error: {e}") + raise ValueError(f"OpenAI API call failed. Error: {e}") from e if ( not embeds diff --git a/semantic_router/encoders/zure.py b/semantic_router/encoders/zure.py index 8a558d0d..b53fb662 100644 --- a/semantic_router/encoders/zure.py +++ b/semantic_router/encoders/zure.py @@ -74,7 +74,9 @@ def __init__( # _strict_response_validation=True, ) except Exception as e: - raise ValueError(f"OpenAI API client failed to initialize. Error: {e}") + raise ValueError( + f"OpenAI API client failed to initialize. Error: {e}" + ) from e def __call__(self, docs: List[str]) -> List[List[float]]: if self.client is None: @@ -100,7 +102,7 @@ def __call__(self, docs: List[str]) -> List[List[float]]: logger.warning(f"Retrying in {2**j} seconds...") except Exception as e: logger.error(f"Azure OpenAI API call failed. Error: {error_message}") - raise ValueError(f"Azure OpenAI API call failed. Error: {e}") + raise ValueError(f"Azure OpenAI API call failed. Error: {e}") from e if ( not embeds diff --git a/semantic_router/llms/cohere.py b/semantic_router/llms/cohere.py index 1a3c8e3c..37eb4338 100644 --- a/semantic_router/llms/cohere.py +++ b/semantic_router/llms/cohere.py @@ -24,7 +24,9 @@ def __init__( try: self.client = cohere.Client(cohere_api_key) except Exception as e: - raise ValueError(f"Cohere API client failed to initialize. Error: {e}") + raise ValueError( + f"Cohere API client failed to initialize. Error: {e}" + ) from e def __call__(self, messages: List[Message]) -> str: if self.client is None: @@ -43,4 +45,4 @@ def __call__(self, messages: List[Message]) -> str: return output except Exception as e: - raise ValueError(f"Cohere API call failed. Error: {e}") + raise ValueError(f"Cohere API call failed. Error: {e}") from e diff --git a/semantic_router/llms/openai.py b/semantic_router/llms/openai.py index b0c7d0e6..a93ce0e7 100644 --- a/semantic_router/llms/openai.py +++ b/semantic_router/llms/openai.py @@ -29,7 +29,9 @@ def __init__( try: self.client = openai.OpenAI(api_key=api_key) except Exception as e: - raise ValueError(f"OpenAI API client failed to initialize. Error: {e}") + raise ValueError( + f"OpenAI API client failed to initialize. Error: {e}" + ) from e self.temperature = temperature self.max_tokens = max_tokens @@ -51,4 +53,4 @@ def __call__(self, messages: List[Message]) -> str: return output except Exception as e: logger.error(f"LLM error: {e}") - raise Exception(f"LLM error: {e}") + raise Exception(f"LLM error: {e}") from e diff --git a/semantic_router/llms/openrouter.py b/semantic_router/llms/openrouter.py index 4e687207..b00d68a4 100644 --- a/semantic_router/llms/openrouter.py +++ b/semantic_router/llms/openrouter.py @@ -34,7 +34,9 @@ def __init__( try: self.client = openai.OpenAI(api_key=api_key, base_url=self.base_url) except Exception as e: - raise ValueError(f"OpenRouter API client failed to initialize. Error: {e}") + raise ValueError( + f"OpenRouter API client failed to initialize. Error: {e}" + ) from e self.temperature = temperature self.max_tokens = max_tokens @@ -56,4 +58,4 @@ def __call__(self, messages: List[Message]) -> str: return output except Exception as e: logger.error(f"LLM error: {e}") - raise Exception(f"LLM error: {e}") + raise Exception(f"LLM error: {e}") from e diff --git a/semantic_router/utils/llm.py b/semantic_router/utils/llm.py index 4f89566f..5402e47f 100644 --- a/semantic_router/utils/llm.py +++ b/semantic_router/utils/llm.py @@ -32,7 +32,7 @@ def llm(prompt: str) -> Optional[str]: return output except Exception as e: logger.error(f"LLM error: {e}") - raise Exception(f"LLM error: {e}") + raise Exception(f"LLM error: {e}") from e # TODO integrate async LLM function @@ -62,4 +62,4 @@ def llm(prompt: str) -> Optional[str]: # return output # except Exception as e: # logger.error(f"LLM error: {e}") -# raise Exception(f"LLM error: {e}") +# raise Exception(f"LLM error: {e}") from e