Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue #94 raise from exceptions #105

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 51 in semantic_router/encoders/fastembed.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/encoders/fastembed.py#L51

Added line #L51 was not covered by tests
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 @@
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 @@
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

Check warning on line 56 in semantic_router/llms/openai.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/llms/openai.py#L56

Added line #L56 was not covered by tests
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 @@
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 @@
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

Check warning on line 61 in semantic_router/llms/openrouter.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/llms/openrouter.py#L61

Added line #L61 was not covered by tests
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 @@
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

Check warning on line 35 in semantic_router/utils/llm.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/utils/llm.py#L35

Added line #L35 was not covered by tests


# TODO integrate async LLM function
Expand Down Expand Up @@ -62,4 +62,4 @@
# 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
Loading