Skip to content

Commit

Permalink
Merge pull request #105 from alongadot/issue-94/reraise-exceptions-w-…
Browse files Browse the repository at this point in the history
…trace

fix: issue #94 raise from exceptions
  • Loading branch information
jamescalam authored Jan 17, 2024
2 parents 779072b + d8319a6 commit a5cfa03
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 15 deletions.
6 changes: 4 additions & 2 deletions semantic_router/encoders/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
2 changes: 1 addition & 1 deletion semantic_router/encoders/fastembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions semantic_router/encoders/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions semantic_router/encoders/zure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions semantic_router/llms/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
6 changes: 4 additions & 2 deletions semantic_router/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
6 changes: 4 additions & 2 deletions semantic_router/llms/openrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
4 changes: 2 additions & 2 deletions semantic_router/utils/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit a5cfa03

Please sign in to comment.