Skip to content
Snippets Groups Projects
Commit 6572f3fa authored by Simonas's avatar Simonas
Browse files

fix: set default layer encoder name to None

parent 1b999668
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Set up functions and routes ### Set up functions and routes
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from datetime import datetime from datetime import datetime
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
def get_time(timezone: str) -> str: def get_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". "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}`")
return now.strftime("%H:%M") return now.strftime("%H:%M")
def get_news(category: str, country: str) -> str: def get_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
from semantic_router import Route from semantic_router import Route
functions = [get_time, get_news] functions = [get_time, get_news]
routes = [] routes = []
for function in functions: for function in functions:
route = Route.from_dynamic_route(entity=function) route = Route.from_dynamic_route(entity=function)
routes.append(route) routes.append(route)
``` ```
%% Output %% Output
2024-01-05 12:02:35 INFO semantic_router.utils.logger Generating dynamic route... /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
2024-01-05 12:02:38 INFO semantic_router.utils.logger Generated route config: from .autonotebook import tqdm as notebook_tqdm
2024-01-05 12:44:13 INFO semantic_router.utils.logger Generating dynamic route...
2024-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?"
] ]
} }
2024-01-05 12:02:38 INFO semantic_router.utils.logger Generating dynamic route... 2024-01-05 12:44:16 INFO semantic_router.utils.logger Generating dynamic route...
2024-01-05 12:02:41 INFO semantic_router.utils.logger Generated route config: 2024-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?"
] ]
} }
%% 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
from semantic_router.layer import LayerConfig from semantic_router.layer import LayerConfig
layer_config = LayerConfig(routes=routes) layer_config = LayerConfig(routes=routes)
layer_config.to_dict() layer_config.to_dict()
``` ```
%% Output %% Output
2024-01-05 12:44:23 INFO semantic_router.utils.logger Using default openai encoder: None
{'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',
'signature': '(category: str, country: str) -> str', 'signature': '(category: str, country: str) -> str',
'output': "<class 'str'>"}}, 'output': "<class 'str'>"}},
{'name': 'get_weather', {'name': 'get_weather',
'utterances': ['what is the weather in SF', 'utterances': ['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?"],
'description': None, 'description': None,
'function_schema': None}]} 'function_schema': None}]}
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Get a route by name # Get a route by name
layer_config.get("get_time") layer_config.get("get_time")
``` ```
%% Output %% Output
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
2024-01-05 12:02:41 INFO semantic_router.utils.logger Removed route `get_weather` 2024-01-05 12:44:32 INFO semantic_router.utils.logger Removed route `get_weather`
{'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',
'signature': '(category: str, country: str) -> str', 'signature': '(category: str, country: str) -> str',
'output': "<class 'str'>"}}]} 'output': "<class 'str'>"}}]}
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Save config to a file (.json or .yaml) Save config to a file (.json or .yaml)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
layer_config.to_file("output/layer_config.json") layer_config.to_file("output/layer_config.json")
``` ```
%% Output %% Output
2024-01-05 12:02:41 INFO semantic_router.utils.logger Saving route config to output/layer_config.json 2024-01-05 12:45:12 INFO semantic_router.utils.logger Saving route config to output/layer_config.json
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Define routing layer ### Define routing layer
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Load config from local file Load config from local file
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.layer import LayerConfig from semantic_router.layer import LayerConfig
layer_config = LayerConfig.from_file("output/layer_config.json") layer_config = LayerConfig.from_file("output/layer_config.json")
``` ```
%% Output %% Output
2024-01-05 12:02:41 INFO semantic_router.utils.logger Loading route config from output/layer_config.json 2024-01-05 12:45:15 INFO semantic_router.utils.logger Loading route config from output/layer_config.json
2024-01-05 12:45:15 INFO semantic_router.utils.logger Using default openai encoder: None
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Initialize routing layer Initialize routing layer
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import os import os
from getpass import getpass from getpass import getpass
from semantic_router import RouteLayer from semantic_router import RouteLayer
# https://dashboard.cohere.com/ # https://dashboard.cohere.com/
os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass( os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass(
"Enter Cohere API Key: " "Enter Cohere API Key: "
) )
layer = RouteLayer.from_config(config=layer_config) layer = RouteLayer.from_config(config=layer_config)
``` ```
%% Output %% Output
2024-01-05 12:02:41 INFO semantic_router.utils.logger Initializing RouteLayer 2024-01-05 12:45:50 INFO semantic_router.utils.logger Initializing RouteLayer
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Do a function call with functions as tool Do a function call with functions as tool
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
layer("What is the time in Stockholm?") layer("What is the time in Stockholm?")
``` ```
%% Output %% Output
2024-01-05 12:02:42 INFO semantic_router.utils.logger Extracting function input... 2024-01-05 12:45:53 INFO semantic_router.utils.logger Extracting function input...
RouteChoice(name='get_time', function_call={'timezone': 'Europe/Stockholm'}) RouteChoice(name='get_time', function_call={'timezone': 'Europe/Stockholm'})
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Define function execution method Define function execution method
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.schema import RouteChoice from semantic_router.schema import RouteChoice
from semantic_router.utils import llm from semantic_router.utils import llm
def route_and_execute(query, functions, layer): def route_and_execute(query, functions, layer):
route_choice: RouteChoice = layer(query) route_choice: RouteChoice = layer(query)
for function in functions: for function in functions:
if function.__name__ == route_choice.name: if function.__name__ == route_choice.name:
if route_choice.function_call: if route_choice.function_call:
return function(**route_choice.function_call) return function(**route_choice.function_call)
# If no function is found, use the LLM for general queries # If no function is found, use the LLM for general queries
return llm.llm(query) return llm.llm(query)
queries = [ queries = [
"What is the time in Stockholm?", "What is the time in Stockholm?",
"What are the tech news in the US?", "What are the tech news in the US?",
"The capital of France?", "The capital of France?",
] ]
for query in queries: for query in queries:
print(f"Query: {query}") print(f"Query: {query}")
print(route_and_execute(query, functions, layer)) print(route_and_execute(query, functions, layer))
``` ```
%% Output %% Output
Query: What is the time in Stockholm? Query: What is the time in Stockholm?
2024-01-05 12:03:45 INFO semantic_router.utils.logger Extracting function input... 2024-01-05 12:45:58 INFO semantic_router.utils.logger Extracting function input...
Invoked `get_time` function with timezone: `Europe/Stockholm` Invoked `get_time` function with timezone: `Europe/Stockholm`
11:03 11:46
Query: What are the tech news in the US? Query: What are the tech news in the US?
2024-01-05 12:03:47 INFO semantic_router.utils.logger Extracting function input... 2024-01-05 12:46:00 INFO semantic_router.utils.logger Extracting function input...
Invoked: `get_news` function with category: `tech` and country: `US` Invoked: `get_news` function with category: `tech` and country: `US`
Results from dummy news API Results from dummy news API
Query: The capital of France? Query: The capital of France?
The capital of France is Paris. It's a beautiful city known for its art, culture, and cuisine. Have you ever been there? The capital of France is Paris. It's a beautiful city known for its art, culture, and cuisine. Have you ever been there?
%% Cell type:code id: tags:
``` python
```
......
...@@ -55,7 +55,7 @@ class LayerConfig: ...@@ -55,7 +55,7 @@ class LayerConfig:
self, self,
routes: list[Route] = [], routes: list[Route] = [],
encoder_type: str = "openai", encoder_type: str = "openai",
encoder_name: str | None = "text-embedding-ada-002", encoder_name: str | None = None,
): ):
self.encoder_type = encoder_type self.encoder_type = encoder_type
if encoder_name is None: if encoder_name is None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment