"Then, if the users input utterance is \"What is 2 to the power of 3?\", the route will be triggered, as the input utterance is semantically similar to the route utterances. Furthermore, the route utilizes an LLM to identify that `base=2` and `expoenent=3`. These values are returned in such a way that they can be used in the above `power` function. That is, the dynamic router automates the process of calling relevant functions from natural language inputs.\n",
"Then, if the users input utterance is \"What is 2 to the power of 3?\", the route will be triggered, as the input utterance is semantically similar to the route utterances. Furthermore, the route utilizes an LLM to identify that `base=2` and `exponent=3`. These values are returned in such a way that they can be used in the above `power` function. That is, the dynamic router automates the process of calling relevant functions from natural language inputs.\n",
"\n",
"***⚠️ Note: We have a fully local version of dynamic routes available at [docs/05-local-execution.ipynb](https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb). The local 05 version tends to outperform the OpenAI version we demo in this notebook, so we'd recommend trying [05](https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb)!***"
]
...
...
%% Cell type:markdown id: tags:
[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/02-dynamic-routes.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/02-dynamic-routes.ipynb)
%% Cell type:markdown id: tags:
# Dynamic Routes
%% Cell type:markdown id: tags:
In semantic-router there are two types of routes that can be chosen. Both routes belong to the `Route` object, the only difference between them is that _static_ routes return a `Route.name` when chosen, whereas _dynamic_ routes use an LLM call to produce parameter input values.
For example, a _static_ route will tell us if a query is talking about mathematics by returning the route name (which could be `"math"` for example). A _dynamic_ route does the same thing, but it also extracts key information from the input utterance to be used in a function associated with that route.
For example we could provide a dynamic route with associated utterances:
```
"what is x to the power of y?"
"what is 9 to the power of 4?"
"calculate the result of base x and exponent y"
"calculate the result of base 10 and exponent 3"
"return x to the power of y"
```
and we could also provide the route with a schema outlining key features of the function:
```
def power(base: float, exponent: float) -> float:
"""Raise base to the power of exponent.
Args:
base (float): The base number.
exponent (float): The exponent to which the base is raised.
Returns:
float: The result of base raised to the power of exponent.
"""
return base ** exponent
```
Then, if the users input utterance is "What is 2 to the power of 3?", the route will be triggered, as the input utterance is semantically similar to the route utterances. Furthermore, the route utilizes an LLM to identify that `base=2` and `expoenent=3`. These values are returned in such a way that they can be used in the above `power` function. That is, the dynamic router automates the process of calling relevant functions from natural language inputs.
Then, if the users input utterance is "What is 2 to the power of 3?", the route will be triggered, as the input utterance is semantically similar to the route utterances. Furthermore, the route utilizes an LLM to identify that `base=2` and `exponent=3`. These values are returned in such a way that they can be used in the above `power` function. That is, the dynamic router automates the process of calling relevant functions from natural language inputs.
***⚠️ Note: We have a fully local version of dynamic routes available at [docs/05-local-execution.ipynb](https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb). The local 05 version tends to outperform the OpenAI version we demo in this notebook, so we'd recommend trying [05](https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb)!***
%% Cell type:markdown id: tags:
## Installing the Library
%% Cell type:code id: tags:
``` python
!pipinstalltzdata
!pipinstall-qU"semantic-router==0.1.0.dev3"
```
%% Output
Requirement already satisfied: tzdata in c:\users\siraj\documents\personal\work\aurelio\virtual environments\semantic_router_3\lib\site-packages (2024.1)
[notice] A new release of pip is available: 23.1.2 -> 24.0
[notice] To update, run: python.exe -m pip install --upgrade pip
%% Cell type:markdown id: tags:
## Initializing Routes and RouteLayer
%% Cell type:markdown id: tags:
Dynamic routes are treated in the same way as static routes, let's begin by initializing a `RouteLayer` consisting of static routes.
%% Cell type:code id: tags:
``` python
fromsemantic_routerimportRoute
politics=Route(
name="politics",
utterances=[
"isn't politics the best thing ever",
"why don't you tell me about your political opinions",
"don't you just love the president""don't you just hate the president",
"they're going to destroy this country!",
"they will save the country!",
],
)
chitchat=Route(
name="chitchat",
utterances=[
"how's the weather today?",
"how are things going?",
"lovely weather today",
"the weather is horrendous",
"let's go to the chippy",
],
)
routes=[politics,chitchat]
```
%% Cell type:markdown id: tags:
We initialize our `RouteLayer` with our `encoder` and `routes`. We can use popular encoder APIs like `CohereEncoder` and `OpenAIEncoder`, or local alternatives like `FastEmbedEncoder`.
As with static routes, we must create a dynamic route before adding it to our route layer. To make a route dynamic, we need to provide the `function_schemas` as a list. Each function schema provides instructions on what a function is, so that an LLM can decide how to use it correctly.
%% Cell type:code id: tags:
``` python
fromdatetimeimportdatetime
fromzoneinfoimportZoneInfo
defget_time(timezone:str)->str:
"""Finds the current time in a specific timezone.
:param timezone: The timezone to find the current time in, should
be a valid timezone from the IANA Time Zone Database like
"America/New_York" or "Europe/London". Do NOT put the place
name itself like "rome", or "new york", you must provide
the IANA format.
:type timezone: str
:return: The current time in the specified timezone."""
now=datetime.now(ZoneInfo(timezone))
returnnow.strftime("%H:%M")
```
%% Cell type:code id: tags:
``` python
get_time("America/New_York")
```
%% Output
'07:09'
%% Cell type:markdown id: tags:
To get the function schema we can use the `get_schema` function from the `function_call` module.
'description': 'Finds the current time in a specific timezone.\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". Do NOT put the place\n name itself like "rome", or "new york", you must provide\n the IANA format.\n:type timezone: str\n:return: The current time in the specified timezone.',
'parameters': {'type': 'object',
'properties': {'timezone': {'type': 'string',
'description': '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". Do NOT put the place\n name itself like "rome", or "new york", you must provide\n the IANA format.'}},
'required': ['timezone']}}}]
%% Cell type:markdown id: tags:
We use this to define our dynamic route:
%% Cell type:code id: tags:
``` python
time_route=Route(
name="get_time",
utterances=[
"what is the time in new york city?",
"what is the time in london?",
"I live in Rome, what time is it?",
],
function_schemas=schemas,
)
```
%% Cell type:code id: tags:
``` python
time_route.llm
```
%% Cell type:markdown id: tags:
Add the new route to our `layer`:
%% Cell type:code id: tags:
``` python
rl.add(time_route)
```
%% Output
2025-01-06 12:09:28 - semantic_router.utils.logger - WARNING - base.py:172 - _read_config() - This method should be implemented by subclasses.
2025-01-06 12:09:28 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
2025-01-06 12:09:29 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.
%% Cell type:code id: tags:
``` python
time_route.llm
```
%% Cell type:markdown id: tags:
Now we can ask our layer a time related question to trigger our new dynamic route.
%% Cell type:code id: tags:
``` python
response=rl("what is the time in new york city?")
response
```
%% Output
2025-01-06 12:09:32 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
2025-01-06 12:09:32 - semantic_router.utils.logger - WARNING - base.py:441 - __call__() - No LLM provided for dynamic route, will use OpenAI LLM default. Ensure API key is set in OPENAI_API_KEY environment variable.
2025-01-06 12:09:34 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
Our dynamic route provides both the route itself _and_ the input parameters required to use the route.
%% Cell type:markdown id: tags:
## Dynamic Routes with Multiple Functions
%% Cell type:markdown id: tags:
---
%% Cell type:markdown id: tags:
Routes can be assigned multiple functions. Then, when that particular Route is selected by the Route Layer, a number of those functions might be invoked due to the users utterance containing relevant information that fits their arguments.
%% Cell type:markdown id: tags:
Let's define a Route that has multiple functions.
%% Cell type:code id: tags:
``` python
fromdatetimeimportdatetime,timedelta
fromzoneinfoimportZoneInfo
# Function with one argument
defget_time(timezone:str)->str:
"""Finds the current time in a specific timezone.
:param timezone: The timezone to find the current time in, should
be a valid timezone from the IANA Time Zone Database like
"America/New_York" or "Europe/London". Do NOT put the place
name itself like "rome", or "new york", you must provide
the IANA format.
:type timezone: str
:return: The current time in the specified timezone."""
'description': 'Finds the current time in a specific timezone.\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". Do NOT put the place\n name itself like "rome", or "new york", you must provide\n the IANA format.\n:type timezone: str\n:return: The current time in the specified timezone.',
'parameters': {'type': 'object',
'properties': {'timezone': {'type': 'string',
'description': '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". Do NOT put the place\n name itself like "rome", or "new york", you must provide\n the IANA format.'}},
'required': ['timezone']}}},
{'type': 'function',
'function': {'name': 'get_time_difference',
'description': 'Calculates the time difference between two timezones.\n:param timezone1: The first timezone, should be a valid timezone from the IANA Time Zone Database like "America/New_York" or "Europe/London".\n:param timezone2: The second timezone, should be a valid timezone from the IANA Time Zone Database like "America/New_York" or "Europe/London".\n:type timezone1: str\n:type timezone2: str\n:return: The time difference in hours between the two timezones.',
'parameters': {'type': 'object',
'properties': {'timezone1': {'type': 'string',
'description': 'The first timezone, should be a valid timezone from the IANA Time Zone Database like "America/New_York" or "Europe/London".'},
'timezone2': {'type': 'string',
'description': 'The second timezone, should be a valid timezone from the IANA Time Zone Database like "America/New_York" or "Europe/London".'}},
'required': ['timezone1', 'timezone2']}}},
{'type': 'function',
'function': {'name': 'convert_time',
'description': 'Converts a specific time from one timezone to another.\n:param time: The time to convert in HH:MM format.\n:param from_timezone: The original timezone of the time, should be a valid IANA timezone.\n:param to_timezone: The target timezone for the time, should be a valid IANA timezone.\n:type time: str\n:type from_timezone: str\n:type to_timezone: str\n:return: The converted time in the target timezone.\n:raises ValueError: If the time format or timezone strings are invalid.\n\nExample:\n convert_time("12:30", "America/New_York", "Asia/Tokyo") -> "03:30"',
'parameters': {'type': 'object',
'properties': {'time': {'type': 'string',
'description': 'The time to convert in HH:MM format.'},
'from_timezone': {'type': 'string',
'description': 'The original timezone of the time, should be a valid IANA timezone.'},
'to_timezone': {'type': 'string',
'description': 'The target timezone for the time, should be a valid IANA timezone.'}},
### Testing the `multi_function_route` - The `get_time` Function
%% Cell type:code id: tags:
``` python
response=rl2("what is the time in New York?")
response
```
%% Output
2025-01-06 12:10:47 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
2025-01-06 12:10:47 - semantic_router.utils.logger - WARNING - base.py:441 - __call__() - No LLM provided for dynamic route, will use OpenAI LLM default. Ensure API key is set in OPENAI_API_KEY environment variable.
2025-01-06 12:10:48 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
The time difference between America/Los_Angeles and Europe/Istanbul is 11.0 hours.
C:\Users\Joshu\AppData\Local\Temp\ipykernel_13320\3683005204.py:28: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
The time difference between Europe/Berlin and Asia/Shanghai is 7.0 hours.
12:53
C:\Users\Joshu\AppData\Local\Temp\ipykernel_13320\3683005204.py:28: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).