semantic_router.layer.RouteLayer#

class semantic_router.layer.RouteLayer(encoder: BaseEncoder | None = None, llm: BaseLLM | None = None, routes: List[Route] | None = None, index: BaseIndex | None = None, top_k: int = 5, aggregation: str = 'sum', auto_sync: str | None = None)#

Bases: object

__init__(encoder: BaseEncoder | None = None, llm: BaseLLM | None = None, routes: List[Route] | None = None, index: BaseIndex | None = None, top_k: int = 5, aggregation: str = 'sum', auto_sync: str | None = None)#

Methods

__init__([encoder, llm, routes, index, ...])

acall([text, vector, simulate_static, ...])

add(route)

Add a route to the local RouteLayer and index.

async_group_scores_by_class(query_results)

check_for_matching_routes(top_class)

delete(route_name)

Deletes a route given a specific route name.

evaluate(X, y[, batch_size])

Evaluate the accuracy of the route selection.

fit(X, y[, batch_size, max_iter, ...])

from_config(config[, index])

from_json(file_path)

from_yaml(file_path)

get(name)

get_thresholds()

get_utterance_diff([include_metadata])

Get the difference between the local and remote utterances.

group_scores_by_class(query_results)

is_synced()

Check if the local and remote route layer instances are synchronized.

list_route_names()

retrieve_multiple_routes([text, vector])

sync(sync_mode[, force])

Runs a sync of the local routes with the remote index.

to_config()

to_json(file_path)

to_yaml(file_path)

update(name[, threshold, utterances])

Updates the route specified in name.

Attributes

score_threshold

encoder

index

add(route: Route)#

Add a route to the local RouteLayer and index.

Parameters:

route (Route) – The route to add.

delete(route_name: str)#

Deletes a route given a specific route name.

Parameters:

route_name – the name of the route to be deleted

evaluate(X: List[str], y: List[str], batch_size: int = 500) float#

Evaluate the accuracy of the route selection.

get_utterance_diff(include_metadata: bool = False) List[str]#

Get the difference between the local and remote utterances. Returns a list of strings showing what is different in the remote when compared to the local. For example:

[” route1: utterance1”,

“ route1: utterance2”, “- route2: utterance3”, “- route2: utterance4”]

Tells us that the remote is missing “route2: utterance3” and “route2: utterance4”, which do exist locally. If we see:

[” route1: utterance1”,

“ route1: utterance2”, “+ route2: utterance3”, “+ route2: utterance4”]

This diff tells us that the remote has “route2: utterance3” and “route2: utterance4”, which do not exist locally.

is_synced() bool#

Check if the local and remote route layer instances are synchronized.

Returns:

True if the local and remote route layers are synchronized, False otherwise.

Return type:

bool

sync(sync_mode: str, force: bool = False) List[str]#

Runs a sync of the local routes with the remote index.

Parameters:
  • sync_mode (str) – The mode to sync the routes with the remote index.

  • force (bool, optional) – Whether to force the sync even if the local and remote hashes already match. Defaults to False.

Returns:

A list of diffs describing the addressed differences between the local and remote route layers.

Return type:

List[str]

update(name: str, threshold: float | None = None, utterances: List[str] | None = None)#

Updates the route specified in name. Allows the update of threshold and/or utterances. If no values are provided via the threshold or utterances parameters, those fields are not updated. If neither field is provided raises a ValueError.

The name must exist within the local RouteLayer, if not a KeyError will be raised.