Skip to content

Commit

Permalink
Removed _simulate_static_route_selection
Browse files Browse the repository at this point in the history
Simulation of static routes from dynamic routes now handled in __call__ as this avoids a situation where we have subtly different logic in __call__ compared to _simulate_static_route_selection.
  • Loading branch information
Siraj-Aizlewood committed Mar 11, 2024
1 parent 5eca3c0 commit eb56039
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions semantic_router/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def __call__(
self,
text: Optional[str] = None,
vector: Optional[List[float]] = None,
simulate_static: bool = False,
) -> RouteChoice:
# if no vector provided, encode text to get vector
if vector is None:
Expand All @@ -230,7 +231,7 @@ def __call__(
route, top_class_scores = self._retrieve_top_route(vector)
passed = self._check_threshold(top_class_scores, route)

if passed and route is not None:
if passed and route is not None and not simulate_static:
if route.function_schema and text is None:
raise ValueError(
"Route has a function schema, but no text was provided."
Expand All @@ -248,6 +249,13 @@ def __call__(
else:
route.llm = self.llm
return route(text)
elif passed and route is not None and simulate_static:
return RouteChoice(
name=route.name,
function_call=None,
similarity_score=None,
trigger=None,
)
else:
# if no route passes threshold, return empty route choice
return RouteChoice()
Expand Down Expand Up @@ -497,28 +505,13 @@ def _vec_evaluate(self, Xq: Union[List[float], Any], y: List[str]) -> float:
"""
correct = 0
for xq, target_route in zip(Xq, y):
# We can't do route_choice = self(vector=xq) here as it won't work for dynamic routes.
route_choice = self._simulate_static_route_selection(vector=xq)
# We treate dynamic routes as static here, because when evaluating we use only vectors, and dynamic routes expect strings by default.
route_choice = self(vector=xq, simulate_static=True)
if route_choice.name == target_route:
correct += 1
accuracy = correct / len(Xq)
return accuracy

def _simulate_static_route_selection(self, vector: List[float]) -> RouteChoice:
"""
Simulate the route selection process treating all routes as static, including threshold checking.
Dynamic routes require a query string to be passed to the __call__ method, but here we work with vectors to boost performance.
Hence, we simulate the route selection process treating all routes as static.
"""
route, scores = self._retrieve_top_route(vector)
passed = self._check_threshold(scores, route)
if passed and route is not None:
return RouteChoice(
name=route.name, function_call=None, similarity_score=None, trigger=None
)
else:
return RouteChoice()

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

Expand Down

0 comments on commit eb56039

Please sign in to comment.