Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescalam committed Feb 13, 2024
1 parent b9a4f2c commit 66a7c79
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
4 changes: 2 additions & 2 deletions semantic_router/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class BaseIndex(BaseModel):
# You can define common attributes here if there are any.
# For example, a placeholder for the index attribute:
index: Optional[Any] = None
routes: Optional[List[str]] = None
utterances: Optional[List[str]] = None
routes: Optional[np.ndarray] = None
utterances: Optional[np.ndarray] = None
dimensions: Union[int, None] = None
type: str = "base"

Expand Down
16 changes: 7 additions & 9 deletions semantic_router/index/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ class LocalIndex(BaseIndex):
def __init__(
self,
index: Optional[np.ndarray] = None,
routes: Optional[List[str]] = None,
utterances: Optional[List[str]] = None,
routes: Optional[np.ndarray] = None,
utterances: Optional[np.ndarray] = None,
):
super().__init__(
index=index, routes=routes, utterances=utterances
)
super().__init__(index=index, routes=routes, utterances=utterances)
self.type = "local"

class Config: # Stop pydantic from complaining about Optional[np.ndarray] type hints.
Expand Down Expand Up @@ -41,9 +39,9 @@ def get_routes(self) -> List[Tuple]:
Returns:
List[Tuple]: A list of (route_name, utterance) objects.
"""
if self.route_names is None or self.utterances is None:
if self.routes is None or self.utterances is None:
raise ValueError("No routes have been added to the index.")
return list(zip(self.route_names, self.utterances))
return list(zip(self.routes, self.utterances))

Check warning on line 44 in semantic_router/index/local.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/local.py#L42-L44

Added lines #L42 - L44 were not covered by tests

def describe(self) -> dict:
return {
Expand All @@ -64,7 +62,7 @@ def query(self, vector: np.ndarray, top_k: int = 5) -> Tuple[np.ndarray, List[st
# get routes from index values
route_names = self.routes[idx].copy()
return scores, route_names

def delete(self, route_name: str):
"""
Delete all records of a specific route from the index.
Expand Down Expand Up @@ -95,7 +93,7 @@ def _get_indices_for_route(self, route_name: str):
raise ValueError("Routes are not populated.")

Check warning on line 93 in semantic_router/index/local.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/local.py#L93

Added line #L93 was not covered by tests
idx = [i for i, route in enumerate(self.routes) if route == route_name]
return idx

def __len__(self):
if self.index is not None:
return self.index.shape[0]
Expand Down
16 changes: 8 additions & 8 deletions semantic_router/index/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
import hashlib
import os
from typing import Any, List, Tuple, Optional, Union
from typing import Any, Dict, List, Tuple, Optional, Union
from semantic_router.index.base import BaseIndex
from semantic_router.utils.logger import logger
import numpy as np
Expand Down Expand Up @@ -128,11 +128,13 @@ def _get_route_ids(self, route_name: str):
clean_route = clean_route_name(route_name)
ids, _ = self._get_all(prefix=f"{clean_route}#")
return ids

Check warning on line 130 in semantic_router/index/pinecone.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/pinecone.py#L128-L130

Added lines #L128 - L130 were not covered by tests

def _get_all(self, prefix: Optional[str] = None, include_metadata: bool = False):
"""
Retrieves all vector IDs from the Pinecone index using pagination.
"""
if self.index is None:
raise ValueError("Index is None, could not retrieve vector IDs.")
all_vector_ids = []
next_page_token = None

Check warning on line 139 in semantic_router/index/pinecone.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/pinecone.py#L136-L139

Added lines #L136 - L139 were not covered by tests

Expand All @@ -143,8 +145,8 @@ def _get_all(self, prefix: Optional[str] = None, include_metadata: bool = False)

# Construct the request URL for listing vectors. Adjust parameters as needed.
list_url = f"https://{self.host}/vectors/list{prefix_str}"
params = {}
headers = {"Api-Key": os.getenv("PINECONE_API_KEY")}
params: Dict = {}
headers = {"Api-Key": os.environ["PINECONE_API_KEY"]}
metadata = []

Check warning on line 150 in semantic_router/index/pinecone.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/pinecone.py#L147-L150

Added lines #L147 - L150 were not covered by tests

while True:
Expand All @@ -164,17 +166,15 @@ def _get_all(self, prefix: Optional[str] = None, include_metadata: bool = False)
if include_metadata:
res_meta = self.index.fetch(ids=vector_ids)

Check warning on line 167 in semantic_router/index/pinecone.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/pinecone.py#L166-L167

Added lines #L166 - L167 were not covered by tests
# extract metadata only
metadata.extend(
[x["metadata"] for x in res_meta["vectors"].values()]
)
metadata.extend([x["metadata"] for x in res_meta["vectors"].values()])

Check warning on line 169 in semantic_router/index/pinecone.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/pinecone.py#L169

Added line #L169 was not covered by tests

# Check if there's a next page token; if not, break the loop
next_page_token = response_data.get("pagination", {}).get("next")
if not next_page_token:
break

Check warning on line 174 in semantic_router/index/pinecone.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/pinecone.py#L172-L174

Added lines #L172 - L174 were not covered by tests

return all_vector_ids, metadata

Check warning on line 176 in semantic_router/index/pinecone.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/pinecone.py#L176

Added line #L176 was not covered by tests

def get_routes(self) -> List[Tuple]:
"""
Gets a list of route and utterance objects currently stored in the index.
Expand Down
6 changes: 2 additions & 4 deletions semantic_router/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ def delete(self, route_name: str):
self.index.delete(route_name=route_name)

def _refresh_routes(self):
"""Pulls out the latest routes from the index.
"""
"""Pulls out the latest routes from the index."""
raise NotImplementedError("This method has not yet been implemented.")
route_mapping = {route.name: route for route in self.routes}
index_routes = self.index.get_routes()
Expand All @@ -320,7 +319,6 @@ def _refresh_routes(self):
route = route_mapping[route_name]
self.routes.append(route)

Check warning on line 320 in semantic_router/layer.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/layer.py#L318-L320

Added lines #L318 - L320 were not covered by tests


def _add_routes(self, routes: List[Route]):
# create embeddings for all routes
all_utterances = [
Expand Down Expand Up @@ -458,7 +456,7 @@ def _vec_evaluate(self, Xq: Union[List[float], Any], y: List[str]) -> float:
correct += 1
accuracy = correct / len(Xq)
return accuracy

def _get_route_names(self) -> List[str]:
return [route.name for route in self.routes]

Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ def test_config(self, openai_encoder, routes):
# now load from config and confirm it's the same
route_layer_from_config = RouteLayer.from_config(layer_config)
assert (route_layer_from_config.index.index == route_layer.index.index).all()
assert route_layer_from_config._get_route_names() == route_layer._get_route_names()
assert (
route_layer_from_config._get_route_names() == route_layer._get_route_names()
)
assert route_layer_from_config.score_threshold == route_layer.score_threshold

def test_get_thresholds(self, openai_encoder, routes):
Expand Down

0 comments on commit 66a7c79

Please sign in to comment.