From 18ec60af0ba735909071fd67568e06151d8751eb Mon Sep 17 00:00:00 2001
From: Alon Gadot <gadotal@amazon.com>
Date: Sun, 14 Jan 2024 12:09:25 +0200
Subject: [PATCH] fix: raise from exceptions

---
 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 @@ class CohereEncoder(BaseEncoder):
         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 @@ class CohereEncoder(BaseEncoder):
             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 @@ class FastEmbedEncoder(BaseEncoder):
             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 @@ class OpenAIEncoder(BaseEncoder):
         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 @@ class OpenAIEncoder(BaseEncoder):
                 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 @@ class AzureOpenAIEncoder(BaseEncoder):
                 # _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 @@ class AzureOpenAIEncoder(BaseEncoder):
                 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 @@ class CohereLLM(BaseLLM):
         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 @@ class CohereLLM(BaseLLM):
             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 @@ class OpenAILLM(BaseLLM):
         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 @@ class OpenAILLM(BaseLLM):
             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 @@ class OpenRouterLLM(BaseLLM):
         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 @@ class OpenRouterLLM(BaseLLM):
             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
-- 
GitLab