Skip to content

Commit

Permalink
fix: Route types
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescalam committed Nov 30, 2024
1 parent cd2ac03 commit a35b0e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
16 changes: 8 additions & 8 deletions semantic_router/encoders/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@


class OpenAIEncoder(DenseEncoder):
client: Optional[openai.Client]
async_client: Optional[openai.AsyncClient]
_client: Optional[openai.Client] = PrivateAttr(default=None)
_async_client: Optional[openai.AsyncClient] = PrivateAttr(default=None)
dimensions: Union[int, NotGiven] = NotGiven()
token_limit: int = 8192 # default value, should be replaced by config
_token_encoder: Any = PrivateAttr()
Expand Down Expand Up @@ -77,10 +77,10 @@ def __init__(
if max_retries is not None:
self.max_retries = max_retries
try:
self.client = openai.Client(
self._client = openai.Client(
base_url=base_url, api_key=api_key, organization=openai_org_id
)
self.async_client = openai.AsyncClient(
self._async_client = openai.AsyncClient(
base_url=base_url, api_key=api_key, organization=openai_org_id
)
except Exception as e:
Expand All @@ -103,7 +103,7 @@ def __call__(self, docs: List[str], truncate: bool = True) -> List[List[float]]:
False and a document exceeds the token limit, an error will be
raised.
:return: List of embeddings for each document."""
if self.client is None:
if self._client is None:
raise ValueError("OpenAI client is not initialized.")
embeds = None

Expand All @@ -114,7 +114,7 @@ def __call__(self, docs: List[str], truncate: bool = True) -> List[List[float]]:
# Exponential backoff
for j in range(self.max_retries + 1):
try:
embeds = self.client.embeddings.create(
embeds = self._client.embeddings.create(
input=docs,
model=self.name,
dimensions=self.dimensions,
Expand Down Expand Up @@ -160,7 +160,7 @@ def _truncate(self, text: str) -> str:
return text

async def acall(self, docs: List[str], truncate: bool = True) -> List[List[float]]:
if self.async_client is None:
if self._async_client is None:
raise ValueError("OpenAI async client is not initialized.")
embeds = None

Expand All @@ -171,7 +171,7 @@ async def acall(self, docs: List[str], truncate: bool = True) -> List[List[float
# Exponential backoff
for j in range(self.max_retries + 1):
try:
embeds = await self.async_client.embeddings.create(
embeds = await self._async_client.embeddings.create(
input=docs,
model=self.name,
dimensions=self.dimensions,
Expand Down
7 changes: 1 addition & 6 deletions semantic_router/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
from semantic_router.utils import function_call
from semantic_router.utils.logger import logger

try:
from PIL.Image import Image
except ImportError:
pass


def is_valid(route_config: str) -> bool:
try:
Expand Down Expand Up @@ -45,7 +40,7 @@ def is_valid(route_config: str) -> bool:

class Route(BaseModel):
name: str
utterances: Union[List[str], List[Union[Any, "Image"]]]
utterances: Union[List[str], List[Any]]
description: Optional[str] = None
function_schemas: Optional[List[Dict[str, Any]]] = None
llm: Optional[BaseLLM] = None
Expand Down

0 comments on commit a35b0e1

Please sign in to comment.