Skip to content
Snippets Groups Projects
Commit 3b0de2d5 authored by zahid-syed's avatar zahid-syed
Browse files

fixing pytest issues for optional dependency issues

parent 363ae337
No related branches found
No related tags found
No related merge requests found
...@@ -13,8 +13,8 @@ class MistralEncoder(BaseEncoder): ...@@ -13,8 +13,8 @@ class MistralEncoder(BaseEncoder):
"""Class to encode text using MistralAI""" """Class to encode text using MistralAI"""
_client: Any = PrivateAttr() _client: Any = PrivateAttr()
embedding_response: Any = PrivateAttr() _embedding_response: Any = PrivateAttr()
mistral_exception: Any = PrivateAttr() _mistral_exception: Any = PrivateAttr()
type: str = "mistral" type: str = "mistral"
def __init__( def __init__(
...@@ -51,14 +51,14 @@ class MistralEncoder(BaseEncoder): ...@@ -51,14 +51,14 @@ class MistralEncoder(BaseEncoder):
) )
try: try:
self.client = MistralClient(api_key=api_key) self._client = MistralClient(api_key=api_key)
self.embedding_response = EmbeddingResponse self._embedding_response = EmbeddingResponse
self.mistral_exception = MistralException self._mistral_exception = MistralException
except Exception as e: except Exception as e:
raise ValueError(f"Unable to connect to MistralAI {e.args}: {e}") from e raise ValueError(f"Unable to connect to MistralAI {e.args}: {e}") from e
def __call__(self, docs: List[str]) -> List[List[float]]: def __call__(self, docs: List[str]) -> List[List[float]]:
if self.client is None: if self._client is None:
raise ValueError("Mistral client not initialized") raise ValueError("Mistral client not initialized")
embeds = None embeds = None
error_message = "" error_message = ""
...@@ -66,10 +66,10 @@ class MistralEncoder(BaseEncoder): ...@@ -66,10 +66,10 @@ class MistralEncoder(BaseEncoder):
# Exponential backoff # Exponential backoff
for _ in range(3): for _ in range(3):
try: try:
embeds = self.client.embeddings(model=self.name, input=docs) embeds = self._client.embeddings(model=self.name, input=docs)
if embeds.data: if embeds.data:
break break
except self.mistral_exception as e: except self._mistral_exception as e:
sleep(2**_) sleep(2**_)
error_message = str(e) error_message = str(e)
except Exception as e: except Exception as e:
...@@ -77,7 +77,7 @@ class MistralEncoder(BaseEncoder): ...@@ -77,7 +77,7 @@ class MistralEncoder(BaseEncoder):
if ( if (
not embeds not embeds
or not isinstance(embeds, self.embedding_response) or not isinstance(embeds, self._embedding_response)
or not embeds.data or not embeds.data
): ):
raise ValueError(f"No embeddings returned from MistralAI: {error_message}") raise ValueError(f"No embeddings returned from MistralAI: {error_message}")
......
...@@ -11,7 +11,7 @@ from pydantic.v1 import PrivateAttr ...@@ -11,7 +11,7 @@ from pydantic.v1 import PrivateAttr
class MistralAILLM(BaseLLM): class MistralAILLM(BaseLLM):
client: Any = PrivateAttr() _client: Any = PrivateAttr()
temperature: Optional[float] temperature: Optional[float]
max_tokens: Optional[int] max_tokens: Optional[int]
...@@ -42,17 +42,17 @@ class MistralAILLM(BaseLLM): ...@@ -42,17 +42,17 @@ class MistralAILLM(BaseLLM):
"`pip install 'semantic-router[mistralai]'`" "`pip install 'semantic-router[mistralai]'`"
) )
try: try:
self.client = MistralClient(api_key=api_key) self._client = MistralClient(api_key=api_key)
except Exception as e: except Exception as e:
raise ValueError( raise ValueError(
f"MistralAI API client failed to initialize. Error: {e}" f"MistralAI API client failed to initialize. Error: {e}"
) from e ) from e
def __call__(self, messages: List[Message]) -> str: def __call__(self, messages: List[Message]) -> str:
if self.client is None: if self._client is None:
raise ValueError("MistralAI client is not initialized.") raise ValueError("MistralAI client is not initialized.")
try: try:
completion = self.client.chat( completion = self._client.chat(
model=self.name, model=self.name,
messages=[m.to_mistral() for m in messages], messages=[m.to_mistral() for m in messages],
temperature=self.temperature, temperature=self.temperature,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment