diff --git a/docs/02_dynamic_routes.ipynb b/docs/02_dynamic_routes.ipynb index 61456a07cd961cd9ae6c08b0a65107f3c2e4e94e..2ffc0ce0920f3d18bcfbf6c6288a45205ded9b93 100644 --- a/docs/02_dynamic_routes.ipynb +++ b/docs/02_dynamic_routes.ipynb @@ -136,9 +136,10 @@ "from datetime import datetime\n", "from zoneinfo import ZoneInfo\n", "\n", + "\n", "def get_time(timezone: str) -> str:\n", " \"\"\"Finds the current time in a specific timezone.\n", - " \n", + "\n", " :param timezone: The timezone to find the current time in, should\n", " be a valid timezone from the IANA Time Zone Database like\n", " \"America/New_York\" or \"Europe/London\".\n", @@ -219,9 +220,9 @@ " utterances=[\n", " \"what is the time in new york city?\",\n", " \"what is the time in london?\",\n", - " \"I live in Rome, what time is it?\"\n", + " \"I live in Rome, what time is it?\",\n", " ],\n", - " function_schema=schema\n", + " function_schema=schema,\n", ")" ] }, diff --git a/semantic_router/layer.py b/semantic_router/layer.py index da25f8289bcd29f81a1e4c6a26205396957d1bd5..bd48f77f1f35eb260543448a9fd0fedfb62aaa13 100644 --- a/semantic_router/layer.py +++ b/semantic_router/layer.py @@ -92,9 +92,7 @@ class LayerConfig: encoder_name = layer["encoder_name"] routes = [Route.from_dict(route) for route in layer["routes"]] return cls( - encoder_type=encoder_type, - encoder_name=encoder_name, - routes=routes + encoder_type=encoder_type, encoder_name=encoder_name, routes=routes ) else: raise Exception("Invalid config JSON or YAML") @@ -103,7 +101,7 @@ class LayerConfig: return { "encoder_type": self.encoder_type, "encoder_name": self.encoder_name, - "routes": [route.to_dict() for route in self.routes] + "routes": [route.to_dict() for route in self.routes], } def to_file(self, path: str): @@ -145,9 +143,7 @@ class RouteLayer: score_threshold: float = 0.82 def __init__( - self, - encoder: BaseEncoder | None = None, - routes: list[Route] | None = None + self, encoder: BaseEncoder | None = None, routes: list[Route] | None = None ): logger.info("Initializing RouteLayer") self.index = None @@ -177,46 +173,30 @@ class RouteLayer: else: # if no route passes threshold, return empty route choice return RouteChoice() - + def __str__(self): - return (f"RouteLayer(encoder={self.encoder}, " - f"score_threshold={self.score_threshold}, " - f"routes={self.routes})") + return ( + f"RouteLayer(encoder={self.encoder}, " + f"score_threshold={self.score_threshold}, " + f"routes={self.routes})" + ) @classmethod def from_json(cls, file_path: str): config = LayerConfig.from_file(file_path) - encoder = Encoder( - type=config.encoder_type, - name=config.encoder_name - ) - return cls( - encoder=encoder, - routes=config.routes - ) + encoder = Encoder(type=config.encoder_type, name=config.encoder_name) + return cls(encoder=encoder, routes=config.routes) @classmethod def from_yaml(cls, file_path: str): config = LayerConfig.from_file(file_path) - encoder = Encoder( - type=config.encoder_type, - name=config.encoder_name - ) - return cls( - encoder=encoder, - routes=config.routes - ) - + encoder = Encoder(type=config.encoder_type, name=config.encoder_name) + return cls(encoder=encoder, routes=config.routes) + @classmethod def from_config(cls, config: LayerConfig): - encoder = Encoder( - type=config.encoder_type, - name=config.encoder_name - ) - return cls( - encoder=encoder, - routes=config.routes - ) + encoder = Encoder(type=config.encoder_type, name=config.encoder_name) + return cls(encoder=encoder, routes=config.routes) def add(self, route: Route): print(f"Adding route `{route.name}`") @@ -311,12 +291,12 @@ class RouteLayer: return max(scores) > threshold else: return False - + def to_config(self) -> LayerConfig: return LayerConfig( encoder_type=self.encoder.type, encoder_name=self.encoder.name, - routes=self.routes + routes=self.routes, ) def to_json(self, file_path: str): diff --git a/semantic_router/route.py b/semantic_router/route.py index af75b21178f77a60a5a450f88928a0baae035663..1fa3291a56d3d44fc6ea98f8944c03bd6a084027 100644 --- a/semantic_router/route.py +++ b/semantic_router/route.py @@ -37,6 +37,7 @@ def is_valid(route_config: str) -> bool: logger.error(e) return False + class Route(BaseModel): name: str utterances: list[str] @@ -53,10 +54,7 @@ class Route(BaseModel): else: # otherwise we just pass None for the call func_call = None - return RouteChoice( - name=self.name, - function_call=func_call - ) + return RouteChoice(name=self.name, function_call=func_call) def to_dict(self): return self.dict() @@ -126,5 +124,3 @@ class Route(BaseModel): if is_valid(route_config): return Route.from_dict(json.loads(route_config)) raise Exception("No config generated") - - diff --git a/tests/unit/test_layer.py b/tests/unit/test_layer.py index 873e488a3b66f3a84ea58ad3658604be669b6e36..386edf6d1a102a53c6c6a577ebba229d4f4051b1 100644 --- a/tests/unit/test_layer.py +++ b/tests/unit/test_layer.py @@ -45,6 +45,7 @@ def layer_json(): ] }""" + def layer_yaml(): return """encoder_name: embed-english-v3.0 encoder_type: cohere @@ -63,6 +64,7 @@ routes: - how are things going? """ + @pytest.fixture def base_encoder(): return BaseEncoder(name="test-encoder") diff --git a/tests/unit/test_route.py b/tests/unit/test_route.py index 4e19db24f7b0023c2f37e5ba20e014d29a1565d1..2843ae4065a2ac75498a4e4f29f056b5be21fb00 100644 --- a/tests/unit/test_route.py +++ b/tests/unit/test_route.py @@ -1,5 +1,4 @@ -import os -from unittest.mock import AsyncMock, mock_open, patch +from unittest.mock import AsyncMock, patch import pytest diff --git a/tests/unit/test_schema.py b/tests/unit/test_schema.py index 46799ee8fc7e8be1f92622fc59b1880a1166b2b6..97b5028e448ea4683c2df09aa93c8946447c8b28 100644 --- a/tests/unit/test_schema.py +++ b/tests/unit/test_schema.py @@ -1,6 +1,5 @@ import pytest -from semantic_router.route import Route from semantic_router.schema import ( CohereEncoder, Encoder,