Skip to content
Snippets Groups Projects
Commit cb6cb68c authored by jamescalam's avatar jamescalam
Browse files

chore: lint

parent aaae6f2a
No related branches found
No related tags found
No related merge requests found
...@@ -35,12 +35,15 @@ class SparseEncoder(BaseModel): ...@@ -35,12 +35,15 @@ class SparseEncoder(BaseModel):
def __call__(self, docs: List[str]) -> List[SparseEmbedding]: def __call__(self, docs: List[str]) -> List[SparseEmbedding]:
raise NotImplementedError("Subclasses must implement this method") raise NotImplementedError("Subclasses must implement this method")
async def acall(self, docs: List[str]) -> Coroutine[Any, Any, List[SparseEmbedding]]: async def acall(
self, docs: List[str]
) -> Coroutine[Any, Any, List[SparseEmbedding]]:
raise NotImplementedError("Subclasses must implement this method") raise NotImplementedError("Subclasses must implement this method")
def _array_to_sparse_embeddings(self, sparse_arrays: np.ndarray) -> List[SparseEmbedding]: def _array_to_sparse_embeddings(
"""Consumes several sparse vectors containing zero-values and returns a compact array. self, sparse_arrays: np.ndarray
""" ) -> List[SparseEmbedding]:
"""Consumes several sparse vectors containing zero-values and returns a compact array."""
if sparse_arrays.ndim != 2: if sparse_arrays.ndim != 2:
raise ValueError(f"Expected a 2D array, got a {sparse_arrays.ndim}D array.") raise ValueError(f"Expected a 2D array, got a {sparse_arrays.ndim}D array.")
# get coordinates of non-zero values # get coordinates of non-zero values
...@@ -50,4 +53,3 @@ class SparseEncoder(BaseModel): ...@@ -50,4 +53,3 @@ class SparseEncoder(BaseModel):
arr_range = range(compact_array[:, 0].max().astype(int) + 1) arr_range = range(compact_array[:, 0].max().astype(int) + 1)
arrs = [compact_array[compact_array[:, 0] == i, :][:, 1:3] for i in arr_range] arrs = [compact_array[compact_array[:, 0] == i, :][:, 1:3] for i in arr_range]
return [SparseEmbedding.from_compact_array(arr) for arr in arrs] return [SparseEmbedding.from_compact_array(arr) for arr in arrs]
...@@ -60,4 +60,3 @@ class BM25Encoder(TfidfEncoder): ...@@ -60,4 +60,3 @@ class BM25Encoder(TfidfEncoder):
position = self.idx_mapping[idx] position = self.idx_mapping[idx]
embeds[i][position] = val embeds[i][position] = val
return embeds return embeds
...@@ -317,7 +317,7 @@ class BaseRouter(BaseModel): ...@@ -317,7 +317,7 @@ class BaseRouter(BaseModel):
self.llm = llm self.llm = llm
self.routes = routes.copy() if routes else [] self.routes = routes.copy() if routes else []
# initialize index # initialize index
self.index =self._get_index(index=index) self.index = self._get_index(index=index)
# set score threshold using default method # set score threshold using default method
self._set_score_threshold() self._set_score_threshold()
self.top_k = top_k self.top_k = top_k
...@@ -346,7 +346,7 @@ class BaseRouter(BaseModel): ...@@ -346,7 +346,7 @@ class BaseRouter(BaseModel):
else: else:
index = index index = index
return index return index
def _get_encoder(self, encoder: Optional[DenseEncoder]) -> DenseEncoder: def _get_encoder(self, encoder: Optional[DenseEncoder]) -> DenseEncoder:
if encoder is None: if encoder is None:
logger.warning("No encoder provided. Using default OpenAIEncoder.") logger.warning("No encoder provided. Using default OpenAIEncoder.")
...@@ -506,19 +506,20 @@ class BaseRouter(BaseModel): ...@@ -506,19 +506,20 @@ class BaseRouter(BaseModel):
categories_with_scores = self._semantic_classify_multiple_routes(results) categories_with_scores = self._semantic_classify_multiple_routes(results)
print(f"{categories_with_scores=}") print(f"{categories_with_scores=}")
return [ return [
RouteChoice(name=category, similarity_score=score) for category, score in categories_with_scores RouteChoice(name=category, similarity_score=score)
for category, score in categories_with_scores
] ]
#route_choices = [] # route_choices = []
# TODO JB: do we need this check? Maybe we should be returning directly # TODO JB: do we need this check? Maybe we should be returning directly
#for category, score in categories_with_scores: # for category, score in categories_with_scores:
# route = self.check_for_matching_routes(category) # route = self.check_for_matching_routes(category)
# if route: # if route:
# route_choice = RouteChoice(name=route.name, similarity_score=score) # route_choice = RouteChoice(name=route.name, similarity_score=score)
# route_choices.append(route_choice) # route_choices.append(route_choice)
#return route_choices # return route_choices
def _retrieve_top_route( def _retrieve_top_route(
self, vector: List[float], route_filter: Optional[List[str]] = None self, vector: List[float], route_filter: Optional[List[str]] = None
) -> Tuple[Optional[Route], List[float]]: ) -> Tuple[Optional[Route], List[float]]:
......
from typing import Any, Dict, List, Optional, Tuple from typing import Any, List, Optional
import asyncio import asyncio
from pydantic.v1 import Field from pydantic.v1 import Field
......
...@@ -426,11 +426,10 @@ class SparseEmbedding(BaseModel): ...@@ -426,11 +426,10 @@ class SparseEmbedding(BaseModel):
"Column 0 should contain index positions, and column 1 should contain respective values." "Column 0 should contain index positions, and column 1 should contain respective values."
) )
return cls(embedding=array) return cls(embedding=array)
@classmethod @classmethod
def from_vector(cls, vector: np.ndarray): def from_vector(cls, vector: np.ndarray):
"""Consumes an array of sparse vectors containing zero-values. """Consumes an array of sparse vectors containing zero-values."""
"""
if vector.ndim != 1: if vector.ndim != 1:
raise ValueError(f"Expected a 1D array, got a {vector.ndim}D array.") raise ValueError(f"Expected a 1D array, got a {vector.ndim}D array.")
return cls.from_compact_array(np.array([np.arange(len(vector)), vector]).T) return cls.from_compact_array(np.array([np.arange(len(vector)), vector]).T)
......
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