"\u001b[32m2024-01-05 12:02:35 INFO semantic_router.utils.logger Generating dynamic route...\u001b[0m\n",
"/Users/jakit/customers/aurelio/semantic-router/.venv/lib/python3.11/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\n",
"\u001b[32m2024-01-05 12:02:38 INFO semantic_router.utils.logger Generated route config:\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"\u001b[32m2024-01-05 12:44:13 INFO semantic_router.utils.logger Generating dynamic route...\u001b[0m\n",
"\u001b[32m2024-01-05 12:44:16 INFO semantic_router.utils.logger Generated route config:\n",
"{\n",
"{\n",
" \"name\": \"get_time\",\n",
" \"name\": \"get_time\",\n",
" \"utterances\": [\n",
" \"utterances\": [\n",
...
@@ -94,8 +96,8 @@
...
@@ -94,8 +96,8 @@
" \"What's the current time in Berlin?\"\n",
" \"What's the current time in Berlin?\"\n",
" ]\n",
" ]\n",
"}\u001b[0m\n",
"}\u001b[0m\n",
"\u001b[32m2024-01-05 12:02:38 INFO semantic_router.utils.logger Generating dynamic route...\u001b[0m\n",
"\u001b[32m2024-01-05 12:44:16 INFO semantic_router.utils.logger Generating dynamic route...\u001b[0m\n",
"\u001b[32m2024-01-05 12:02:41 INFO semantic_router.utils.logger Generated route config:\n",
"\u001b[32m2024-01-05 12:44:19 INFO semantic_router.utils.logger Generated route config:\n",
"{\n",
"{\n",
" \"name\": \"get_news\",\n",
" \"name\": \"get_news\",\n",
" \"utterances\": [\n",
" \"utterances\": [\n",
...
@@ -123,7 +125,7 @@
...
@@ -123,7 +125,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 32,
"execution_count": 4,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
...
@@ -150,14 +152,21 @@
...
@@ -150,14 +152,21 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 33,
"execution_count": 5,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m2024-01-05 12:44:23 INFO semantic_router.utils.logger Using default openai encoder: None\u001b[0m\n"
]
},
{
{
"data": {
"data": {
"text/plain": [
"text/plain": [
"{'encoder_type': 'openai',\n",
"{'encoder_type': 'openai',\n",
" 'encoder_name': 'text-embedding-ada-002',\n",
" 'encoder_name': None,\n",
" 'routes': [{'name': 'get_time',\n",
" 'routes': [{'name': 'get_time',\n",
" 'utterances': [\"What's the current time in New York?\",\n",
" 'utterances': [\"What's the current time in New York?\",\n",
" 'Can you tell me the time in London?',\n",
" 'Can you tell me the time in London?',\n",
...
@@ -188,7 +197,7 @@
...
@@ -188,7 +197,7 @@
" 'function_schema': None}]}"
" 'function_schema': None}]}"
]
]
},
},
"execution_count": 33,
"execution_count": 5,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -202,7 +211,7 @@
...
@@ -202,7 +211,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 34,
"execution_count": 6,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -211,7 +220,7 @@
...
@@ -211,7 +220,7 @@
"Route(name='get_time', utterances=[\"What's the current time in New York?\", 'Can you tell me the time in London?', \"What's the current time in Tokyo?\", 'Can you give me the time in Sydney?', \"What's the current time in Berlin?\"], description=None, function_schema={'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\".\\n:type timezone: str\\n:return: The current time in the specified timezone.', 'signature': '(timezone: str) -> str', 'output': \"<class 'str'>\"})"
"Route(name='get_time', utterances=[\"What's the current time in New York?\", 'Can you tell me the time in London?', \"What's the current time in Tokyo?\", 'Can you give me the time in Sydney?', \"What's the current time in Berlin?\"], description=None, function_schema={'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\".\\n:type timezone: str\\n:return: The current time in the specified timezone.', 'signature': '(timezone: str) -> str', 'output': \"<class 'str'>\"})"
]
]
},
},
"execution_count": 34,
"execution_count": 6,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -223,21 +232,21 @@
...
@@ -223,21 +232,21 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 35,
"execution_count": 7,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"\u001b[32m2024-01-05 12:02:41 INFO semantic_router.utils.logger Removed route `get_weather`\u001b[0m\n"
"\u001b[32m2024-01-05 12:44:32 INFO semantic_router.utils.logger Removed route `get_weather`\u001b[0m\n"
]
]
},
},
{
{
"data": {
"data": {
"text/plain": [
"text/plain": [
"{'encoder_type': 'openai',\n",
"{'encoder_type': 'openai',\n",
" 'encoder_name': 'text-embedding-ada-002',\n",
" 'encoder_name': None,\n",
" 'routes': [{'name': 'get_time',\n",
" 'routes': [{'name': 'get_time',\n",
" 'utterances': [\"What's the current time in New York?\",\n",
" 'utterances': [\"What's the current time in New York?\",\n",
" 'Can you tell me the time in London?',\n",
" 'Can you tell me the time in London?',\n",
...
@@ -262,7 +271,7 @@
...
@@ -262,7 +271,7 @@
" 'output': \"<class 'str'>\"}}]}"
" 'output': \"<class 'str'>\"}}]}"
]
]
},
},
"execution_count": 35,
"execution_count": 7,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -282,14 +291,14 @@
...
@@ -282,14 +291,14 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 36,
"execution_count": 8,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"\u001b[32m2024-01-05 12:02:41 INFO semantic_router.utils.logger Saving route config to output/layer_config.json\u001b[0m\n"
"\u001b[32m2024-01-05 12:45:12 INFO semantic_router.utils.logger Saving route config to output/layer_config.json\u001b[0m\n"
]
]
}
}
],
],
...
@@ -313,14 +322,15 @@
...
@@ -313,14 +322,15 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 37,
"execution_count": 9,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"\u001b[32m2024-01-05 12:02:41 INFO semantic_router.utils.logger Loading route config from output/layer_config.json\u001b[0m\n"
"\u001b[32m2024-01-05 12:45:15 INFO semantic_router.utils.logger Loading route config from output/layer_config.json\u001b[0m\n",
"\u001b[32m2024-01-05 12:45:15 INFO semantic_router.utils.logger Using default openai encoder: None\u001b[0m\n"
]
]
}
}
],
],
...
@@ -339,14 +349,14 @@
...
@@ -339,14 +349,14 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 38,
"execution_count": 11,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"\u001b[32m2024-01-05 12:02:41 INFO semantic_router.utils.logger Initializing RouteLayer\u001b[0m\n"
"\u001b[32m2024-01-05 12:45:50 INFO semantic_router.utils.logger Initializing RouteLayer\u001b[0m\n"
]
]
}
}
],
],
...
@@ -372,14 +382,14 @@
...
@@ -372,14 +382,14 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 39,
"execution_count": 12,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"\u001b[32m2024-01-05 12:02:42 INFO semantic_router.utils.logger Extracting function input...\u001b[0m\n"
"\u001b[32m2024-01-05 12:45:53 INFO semantic_router.utils.logger Extracting function input...\u001b[0m\n"
: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".
"America/New_York" or "Europe/London".
: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))
print(f"Invoked `get_time` function with timezone: `{timezone}`")
print(f"Invoked `get_time` function with timezone: `{timezone}`")
returnnow.strftime("%H:%M")
returnnow.strftime("%H:%M")
defget_news(category:str,country:str)->str:
defget_news(category:str,country:str)->str:
"""Useful to get the news in a specific country"""
"""Useful to get the news in a specific country"""
print(
print(
f"Invoked: `get_news` function with category: `{category}` "
f"Invoked: `get_news` function with category: `{category}` "
f"and country: `{country}`"
f"and country: `{country}`"
)
)
return"Results from dummy news API"
return"Results from dummy news API"
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
get_time("America/New_York")
get_time("America/New_York")
```
```
%% Output
%% Output
Invoked `get_time` function with timezone: `America/New_York`
Invoked `get_time` function with timezone: `America/New_York`
'05:02'
'05:44'
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Now generate a dynamic routing config for each function
Now generate a dynamic routing config for each function
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
fromsemantic_routerimportRoute
fromsemantic_routerimportRoute
functions=[get_time,get_news]
functions=[get_time,get_news]
routes=[]
routes=[]
forfunctioninfunctions:
forfunctioninfunctions:
route=Route.from_dynamic_route(entity=function)
route=Route.from_dynamic_route(entity=function)
routes.append(route)
routes.append(route)
```
```
%% Output
%% Output
[32m2024-01-05 12:02:35 INFO semantic_router.utils.logger Generating dynamic route...[0m
/Users/jakit/customers/aurelio/semantic-router/.venv/lib/python3.11/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
[32m2024-01-05 12:02:38 INFO semantic_router.utils.logger Generated route config:
from .autonotebook import tqdm as notebook_tqdm
[32m2024-01-05 12:44:13 INFO semantic_router.utils.logger Generating dynamic route...[0m
[32m2024-01-05 12:44:16 INFO semantic_router.utils.logger Generated route config:
{
{
"name": "get_time",
"name": "get_time",
"utterances": [
"utterances": [
"What's the current time in New York?",
"What's the current time in New York?",
"Can you tell me the time in London?",
"Can you tell me the time in London?",
"What's the current time in Tokyo?",
"What's the current time in Tokyo?",
"Can you give me the time in Sydney?",
"Can you give me the time in Sydney?",
"What's the current time in Berlin?"
"What's the current time in Berlin?"
]
]
}[0m
}[0m
[32m2024-01-05 12:02:38 INFO semantic_router.utils.logger Generating dynamic route...[0m
[32m2024-01-05 12:44:16 INFO semantic_router.utils.logger Generating dynamic route...[0m
[32m2024-01-05 12:02:41 INFO semantic_router.utils.logger Generated route config:
[32m2024-01-05 12:44:19 INFO semantic_router.utils.logger Generated route config:
{
{
"name": "get_news",
"name": "get_news",
"utterances": [
"utterances": [
"Tell me the latest news from the United States",
"Tell me the latest news from the United States",
"What's happening in India today?",
"What's happening in India today?",
"Can you give me the top stories from Japan",
"Can you give me the top stories from Japan",
"Get me the breaking news from the UK",
"Get me the breaking news from the UK",
"What's the latest in Germany?"
"What's the latest in Germany?"
]
]
}[0m
}[0m
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
# You can manually add or remove routes
# You can manually add or remove routes
get_weather_route=Route(
get_weather_route=Route(
name="get_weather",
name="get_weather",
utterances=[
utterances=[
"what is the weather in SF",
"what is the weather in SF",
"what is the current temperature in London?",
"what is the current temperature in London?",
"tomorrow's weather in Paris?",
"tomorrow's weather in Paris?",
],
],
function_schema=None,
function_schema=None,
)
)
routes.append(get_weather_route)
routes.append(get_weather_route)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Add routes to the layer config
Add routes to the layer config
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
fromsemantic_router.layerimportLayerConfig
fromsemantic_router.layerimportLayerConfig
layer_config=LayerConfig(routes=routes)
layer_config=LayerConfig(routes=routes)
layer_config.to_dict()
layer_config.to_dict()
```
```
%% Output
%% Output
[32m2024-01-05 12:44:23 INFO semantic_router.utils.logger Using default openai encoder: None[0m
{'encoder_type': 'openai',
{'encoder_type': 'openai',
'encoder_name': 'text-embedding-ada-002',
'encoder_name': None,
'routes': [{'name': 'get_time',
'routes': [{'name': 'get_time',
'utterances': ["What's the current time in New York?",
'utterances': ["What's the current time in New York?",
'Can you tell me the time in London?',
'Can you tell me the time in London?',
"What's the current time in Tokyo?",
"What's the current time in Tokyo?",
'Can you give me the time in Sydney?',
'Can you give me the time in Sydney?',
"What's the current time in Berlin?"],
"What's the current time in Berlin?"],
'description': None,
'description': None,
'function_schema': {'name': 'get_time',
'function_schema': {'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".\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".\n:type timezone: str\n:return: The current time in the specified timezone.',
'signature': '(timezone: str) -> str',
'signature': '(timezone: str) -> str',
'output': "<class 'str'>"}},
'output': "<class 'str'>"}},
{'name': 'get_news',
{'name': 'get_news',
'utterances': ['Tell me the latest news from the United States',
'utterances': ['Tell me the latest news from the United States',
"What's happening in India today?",
"What's happening in India today?",
'Can you give me the top stories from Japan',
'Can you give me the top stories from Japan',
'Get me the breaking news from the UK',
'Get me the breaking news from the UK',
"What's the latest in Germany?"],
"What's the latest in Germany?"],
'description': None,
'description': None,
'function_schema': {'name': 'get_news',
'function_schema': {'name': 'get_news',
'description': 'Useful to get the news in a specific country',
'description': 'Useful to get the news in a specific country',
Route(name='get_time', utterances=["What's the current time in New York?", 'Can you tell me the time in London?', "What's the current time in Tokyo?", 'Can you give me the time in Sydney?', "What's the current time in Berlin?"], description=None, function_schema={'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".\n:type timezone: str\n:return: The current time in the specified timezone.', 'signature': '(timezone: str) -> str', 'output': "<class 'str'>"})
Route(name='get_time', utterances=["What's the current time in New York?", 'Can you tell me the time in London?', "What's the current time in Tokyo?", 'Can you give me the time in Sydney?', "What's the current time in Berlin?"], description=None, function_schema={'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".\n:type timezone: str\n:return: The current time in the specified timezone.', 'signature': '(timezone: str) -> str', 'output': "<class 'str'>"})
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
# Remove a route by name
# Remove a route by name
layer_config.remove("get_weather")
layer_config.remove("get_weather")
layer_config.to_dict()
layer_config.to_dict()
```
```
%% Output
%% Output
[32m2024-01-05 12:02:41 INFO semantic_router.utils.logger Removed route `get_weather`[0m
[32m2024-01-05 12:44:32 INFO semantic_router.utils.logger Removed route `get_weather`[0m
{'encoder_type': 'openai',
{'encoder_type': 'openai',
'encoder_name': 'text-embedding-ada-002',
'encoder_name': None,
'routes': [{'name': 'get_time',
'routes': [{'name': 'get_time',
'utterances': ["What's the current time in New York?",
'utterances': ["What's the current time in New York?",
'Can you tell me the time in London?',
'Can you tell me the time in London?',
"What's the current time in Tokyo?",
"What's the current time in Tokyo?",
'Can you give me the time in Sydney?',
'Can you give me the time in Sydney?',
"What's the current time in Berlin?"],
"What's the current time in Berlin?"],
'description': None,
'description': None,
'function_schema': {'name': 'get_time',
'function_schema': {'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".\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".\n:type timezone: str\n:return: The current time in the specified timezone.',
'signature': '(timezone: str) -> str',
'signature': '(timezone: str) -> str',
'output': "<class 'str'>"}},
'output': "<class 'str'>"}},
{'name': 'get_news',
{'name': 'get_news',
'utterances': ['Tell me the latest news from the United States',
'utterances': ['Tell me the latest news from the United States',
"What's happening in India today?",
"What's happening in India today?",
'Can you give me the top stories from Japan',
'Can you give me the top stories from Japan',
'Get me the breaking news from the UK',
'Get me the breaking news from the UK',
"What's the latest in Germany?"],
"What's the latest in Germany?"],
'description': None,
'description': None,
'function_schema': {'name': 'get_news',
'function_schema': {'name': 'get_news',
'description': 'Useful to get the news in a specific country',
'description': 'Useful to get the news in a specific country',