"\u001b[33m2024-04-28 22:54:07 WARNING semantic_router.utils.logger No LLM provided for dynamic route, will use OpenAI LLM default. Ensure API key is set in OPENAI_API_KEY environment variable.\u001b[0m\n"
"tools\n",
"[{'type': 'function', 'function': {'name': 'get_time', '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']}}}]\n",
[](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)
[](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:
%% Cell type:markdown id: tags:
# Dynamic Routes via OpenAI Function Calling
# Dynamic Routes via OpenAI Function Calling
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
In this Notebook we showcase an alternative to dynamic routes where an `openai_function_schema` is specified or the Route, and in doing so, OpenAIs "Function Calling" API is utilized.
In this Notebook we showcase an alternative to dynamic routes where an `openai_function_schema` is specified or the Route, and in doing so, OpenAIs "Function Calling" API is utilized.
The usual Semantic Router procedure is used to determine which Route is most appropriate for the input utterance. Then, rather than using a regular LLM call with careful prompt engineering to allow the LLM to extract function arguments from the input, OpenAIs Function Calling is leveraged to try and obtain function arguments more reliably.
The usual Semantic Router procedure is used to determine which Route is most appropriate for the input utterance. Then, rather than using a regular LLM call with careful prompt engineering to allow the LLM to extract function arguments from the input, OpenAIs Function Calling is leveraged to try and obtain function arguments more reliably.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Installing the Library
## Installing the Library
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
!pipinstall-qUsemantic-router==0.0.34
!pipinstall-qUsemantic-router==0.0.34
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Initializing Routes and RouteLayer
## Initializing Routes and RouteLayer
%% Cell type:markdown id: tags:
%% 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.
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:
%% Cell type:code id: tags:
``` python
``` python
fromsemantic_routerimportRoute
fromsemantic_routerimportRoute
politics=Route(
politics=Route(
name="politics",
name="politics",
utterances=[
utterances=[
"isn't politics the best thing ever",
"isn't politics the best thing ever",
"why don't you tell me about your political opinions",
"why don't you tell me about your political opinions",
"don't you just love the president""don't you just hate the president",
"don't you just love the president""don't you just hate the president",
"they're going to destroy this country!",
"they're going to destroy this country!",
"they will save the country!",
"they will save the country!",
],
],
)
)
chitchat=Route(
chitchat=Route(
name="chitchat",
name="chitchat",
utterances=[
utterances=[
"how's the weather today?",
"how's the weather today?",
"how are things going?",
"how are things going?",
"lovely weather today",
"lovely weather today",
"the weather is horrendous",
"the weather is horrendous",
"let's go to the chippy",
"let's go to the chippy",
],
],
)
)
routes=[politics,chitchat]
routes=[politics,chitchat]
```
```
%% Output
%% Output
c:\Users\Siraj\Documents\Personal\Work\Aurelio\Virtual Environments\semantic_router\Lib\site-packages\tqdm\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
c:\Users\Siraj\Documents\Personal\Work\Aurelio\Virtual Environments\semantic_router\Lib\site-packages\tqdm\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
from .autonotebook import tqdm as notebook_tqdm
%% Cell type:markdown id: tags:
%% 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`.
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 a `function_schema`. The function schema provides instructions on what a function is, so that an LLM can decide how to use it correctly.
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 a `function_schema`. The 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:
%% Cell type:code id: tags:
``` python
``` python
fromdatetimeimportdatetime
fromdatetimeimportdatetime
fromzoneinfoimportZoneInfo
fromzoneinfoimportZoneInfo
defget_time(timezone:str)->str:
defget_time(timezone:str)->str:
"""Finds the current time in a specific timezone.
"""Finds the current time in a specific timezone.
:param timezone: The timezone to find the current time in, should
:param timezone: The timezone to find the current time in, should
be a valid timezone from the IANA Time Zone Database like
be a valid timezone from the IANA Time Zone Database like
"America/New_York" or "Europe/London". Do NOT put the place
"America/New_York" or "Europe/London". Do NOT put the place
name itself like "rome", or "new york", you must provide
name itself like "rome", or "new york", you must provide
the IANA format.
the IANA format.
:type timezone: str
:type timezone: str
:return: The current time in the specified timezone."""
:return: The current time in the specified timezone."""
now=datetime.now(ZoneInfo(timezone))
now=datetime.now(ZoneInfo(timezone))
returnnow.strftime("%H:%M")
returnnow.strftime("%H:%M")
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
get_time("America/New_York")
get_time("America/New_York")
```
```
%% Output
%% Output
'18:23'
'14:54'
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
To get the function schema we can use the `get_schema` function from the `function_call` module.
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.',
'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.',
'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.',
'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',
'parameters': {'type': 'object',
'properties': {'timezone': {'type': 'string',
'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.'}},
'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']}}}
'required': ['timezone']}}}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We use this to define our dynamic route:
We use this to define our dynamic route:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
time_route=Route(
time_route=Route(
name="get_time",
name="get_time",
utterances=[
utterances=[
"what is the time in new york city?",
"what is the time in new york city?",
"what is the time in london?",
"what is the time in london?",
"I live in Rome, what time is it?",
"I live in Rome, what time is it?",
],
],
# function_schema=schema,
# function_schema=schema,
openai_function_schema=openai_function_schema,
openai_function_schema=openai_function_schema,
)
)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Add the new route to our `layer`:
Add the new route to our `layer`:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
rl.add(time_route)
rl.add(time_route)
```
```
%% Output
%% Output
[32m2024-04-27 02:23:30 INFO semantic_router.utils.logger Adding `get_time` route[0m
[32m2024-04-28 22:54:06 INFO semantic_router.utils.logger Adding `get_time` route[0m
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Now we can ask our layer a time related question to trigger our new dynamic route.
Now we can ask our layer a time related question to trigger our new dynamic route.
[33m2024-04-28 22:54:07 WARNING semantic_router.utils.logger No LLM provided for dynamic route, will use OpenAI LLM default. Ensure API key is set in OPENAI_API_KEY environment variable.[0m
tools
[{'type': 'function', 'function': {'name': 'get_time', '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']}}}]