Skip to content

Commit

Permalink
fix: linting issue from py313 change gh-114198
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescalam committed Nov 30, 2024
1 parent a5904f6 commit 5461660
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"

[tool.poetry.dependencies]
python = ">=3.9,<3.14"
pydantic = "^2.5.3"
pydantic = "^2.10.2"
openai = ">=1.10.0,<2.0.0"
cohere = {version = ">=5.9.4,<6.00", optional = true}
mistralai= {version = ">=0.0.12,<0.1.0", optional = true}
Expand Down
4 changes: 2 additions & 2 deletions semantic_router/encoders/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Coroutine, List, Optional

from pydantic.v1 import BaseModel, Field, validator
from pydantic import BaseModel, Field, field_validator
import numpy as np

from semantic_router.schema import SparseEmbedding
Expand All @@ -14,7 +14,7 @@ class DenseEncoder(BaseModel):
class Config:
arbitrary_types_allowed = True

@validator("score_threshold", pre=True, always=True)
@field_validator("score_threshold")
def set_score_threshold(cls, v):
return float(v) if v is not None else None

Expand Down
3 changes: 2 additions & 1 deletion semantic_router/encoders/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class CLIPEncoder(DenseEncoder):
name: str = "openai/clip-vit-base-patch16"
type: str = "huggingface"
score_threshold: float = 0.2
tokenizer_kwargs: Dict = {}
processor_kwargs: Dict = {}
model_kwargs: Dict = {}
Expand All @@ -21,6 +20,8 @@ class CLIPEncoder(DenseEncoder):
_Image: Any = PrivateAttr()

def __init__(self, **data):
if data.get("score_threshold") is None:
data["score_threshold"] = 0.2
super().__init__(**data)
self._tokenizer, self._processor, self._model = self._initialize_hf_model()

Expand Down
6 changes: 4 additions & 2 deletions semantic_router/encoders/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
class HuggingFaceEncoder(DenseEncoder):
name: str = "sentence-transformers/all-MiniLM-L6-v2"
type: str = "huggingface"
score_threshold: float = 0.5
tokenizer_kwargs: Dict = {}
model_kwargs: Dict = {}
device: Optional[str] = None
Expand All @@ -43,6 +42,8 @@ class HuggingFaceEncoder(DenseEncoder):
_torch: Any = PrivateAttr()

def __init__(self, **data):
if data.get("score_threshold") is None:
data["score_threshold"] = 0.5
super().__init__(**data)
self._tokenizer, self._model = self._initialize_hf_model()

Expand Down Expand Up @@ -153,7 +154,6 @@ class HFEndpointEncoder(DenseEncoder):
name: str = "hugging_face_custom_endpoint"
huggingface_url: Optional[str] = None
huggingface_api_key: Optional[str] = None
score_threshold: float = 0.8

def __init__(
self,
Expand All @@ -180,6 +180,8 @@ def __init__(
"""
huggingface_url = huggingface_url or os.getenv("HF_API_URL")
huggingface_api_key = huggingface_api_key or os.getenv("HF_API_KEY")
if score_threshold is None:
score_threshold = 0.8

super().__init__(name=name, score_threshold=score_threshold) # type: ignore

Expand Down
7 changes: 4 additions & 3 deletions semantic_router/encoders/vit.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from typing import Any, List, Optional, Dict
from typing import Any, Dict, List, Optional

from pydantic.v1 import PrivateAttr
from pydantic import PrivateAttr

from semantic_router.encoders import DenseEncoder


class VitEncoder(DenseEncoder):
name: str = "google/vit-base-patch16-224"
type: str = "huggingface"
score_threshold: float = 0.5
processor_kwargs: Dict = {}
model_kwargs: Dict = {}
device: Optional[str] = None
Expand All @@ -19,6 +18,8 @@ class VitEncoder(DenseEncoder):
_Image: Any = PrivateAttr()

def __init__(self, **data):
if data.get("score_threshold") is None:
data["score_threshold"] = 0.5
super().__init__(**data)
self._processor, self._model = self._initialize_hf_model()

Expand Down
5 changes: 3 additions & 2 deletions semantic_router/index/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class PostgresIndex(BaseIndex):
connection_string: Optional[str] = None
index_prefix: str = "semantic_router_"
index_name: str = "index"
dimensions: int = 1536
metric: Metric = Metric.COSINE
namespace: Optional[str] = ""
conn: Optional["psycopg2.extensions.connection"] = None
Expand All @@ -115,9 +114,9 @@ def __init__(
connection_string: Optional[str] = None,
index_prefix: str = "semantic_router_",
index_name: str = "index",
dimensions: int = 1536,
metric: Metric = Metric.COSINE,
namespace: Optional[str] = "",
dimensions: int | None = None,
):
"""
Initializes the Postgres index with the specified parameters.
Expand All @@ -135,6 +134,8 @@ def __init__(
:param namespace: An optional namespace for the index.
:type namespace: Optional[str]
"""
if dimensions is None:
dimensions = 1536
super().__init__()
# try and import psycopg2
try:
Expand Down

0 comments on commit 5461660

Please sign in to comment.